Public feed #723483

Stuff

public text v1 · immutable
Raw Edit
·published 2007-10-02 20:33 UTC
rendered paste body
package Game.GameFunctions.Widget.RandomEvent;

import Game.GameFunctions.Teleport.*; 
import Game.GameFunctions.GameEngine;


public class RandomEventHandler
{



	private final int random(int range) { 

		return(int)(java.lang.Math.random() * range);
	}

	
	public RandomEventHandler(GameEngine g){
	this.gameEngine = g;
	}
	public void process()
	{

	if(this.isBusy) return;
	
	Event g = RandomLoader.getEvent();

	if(this.random(g.chance) == 1|| this.random(g.chance) < 40){ 
		this.oldY = gameEngine.playerPos.getY();
		this.oldX = gameEngine.playerPos.getX();
		this.createNewRandomEvent(g);
		g = null;
	
			}
	}


		
	private void createNewRandomEvent(Event g)
   { 

		this.currentRandom = g;
		
		if(this.currentRandom.type == 1) 
		{
			Game.PacketBuilder.Message.build("Welcome to  "+this.currentRandom.name+".",this.gameEngine.player);
			Game.PacketBuilder.Message.build(this.currentRandom.instructions,this.gameEngine.player);
			this.isBusy = true;
			this.gameEngine.player.teleportToX = this.currentRandom.absX;
			this.gameEngine.player.teleportToY = this.currentRandom.absY;
			Game.GameFunctions.Widget.Cutscene.append(this.gameEngine.frameWriter);
			Game.PacketBuilder.Music.build(7,this.gameEngine.player);

			
		}

	}



	private void processObjectRandom(int id){ 
		
		
		if(!this.isBusy || this.currentRandom == null || this.id == 0 || this.id == -1) return;
		
		if(this.currentRandom.type == 1) 
		{
			if(this.id != this.currentRandom.param1) 
			{
				Game.PacketBuilder.Message.build("You did not complete the required task",gameEngine.player);
				this.gameEngine.player.teleportToX = this.teleX;
				this.gameEngine.player.teleportToY = this.teleY;
				
				this.reset();
			}
			else
			{
			Game.PacketBuilder.Message.build("Congratulations, you completed the "+this.currentRandom.name,gameEngine.player);
			this.gameEngine.player.addItem(currentRandom.reward,1);
			this.gameEngine.player.teleportToX = oldX;
			this.gameEngine.player.teleportToY = oldY;
			this.reset();

			}
		
			
		}



	}


	private void reset()
	{
		Game.GameFunctions.Widget.Cutscene.remove(this.gameEngine.frameWriter);
		Game.PacketBuilder.Music.build(-1,this.gameEngine.player);
		this.isBusy = false;
		this.currentRandom = null;
		this.oldX = 0;
		this.oldY = 0;
	}

	
	private int oldX = 0;
	private int oldY = 0;
	private final int teleX = 0;
	private final int teleY = 0;

	private GameEngine gameEngine;
	private Event currentRandom = null;
	private boolean isBusy = false;



	
}