package examples.input;

import java.awt.geom.*;

import fang.*;

/**Demonstrates how to use the mouse and
 * keyboard input for moving sprites.
 * @author Jam Jenkins
 */
public class InputLoop extends GameLoop
{
	/**the sprite moving vertically with the mouse*/
	private StringSprite vertical;
	/**the sprite moving horizontally with the mouse*/
	private StringSprite horizontal;
	/**the sprite moving with the mouse*/
	private StringSprite both;
	/**the sprite responding to key input*/
	private StringSprite keys;
	/**the instructions*/
	private StringSprite keyInstruction;
	/**how many times the user has clicked*/
	private int clickCount;

	/**makes and adds the sprites to the canvas*/
	public void startGame()
	{
		makeSprites();
		addSprites();
		clickCount=0;
		setHelp("resources/InputHelp.txt");
	}

	/**constructs the sprites, scales and locates them*/
	private void makeSprites()
	{
		//sets up the vertical sprite
		vertical=new StringSprite("Vert", true);
		vertical.setScale(0.2);
		vertical.setLocation(0.5, 0.25);
		//sets up the horizontal sprite
		horizontal=new StringSprite("Horiz", true);
		horizontal.setScale(0.2);
		horizontal.setLocation(0.25, 0.5);
		//sets up the both sprite
		both=new StringSprite("Both", true);
		both.setScale(0.2);
		both.setLocation(0.25, 0.25);
		//sets up the keys sprite
		keys=new StringSprite("Keys", true);
		keys.setScale(0.2);
		keys.setLocation(0.5, 0.5);
		//sets up the instruction sprite
		keyInstruction=new StringSprite(
		                   "Press i, j, k or m", true);
		keyInstruction.setScale(0.8);
		keyInstruction.setLocation(0.5, 0.15);
	}

	/**
	 * adds the sprites to the canvas
	 */
	private void addSprites()
	{
		canvas.addSprite(vertical);
		canvas.addSprite(horizontal);
		canvas.addSprite(both);
		canvas.addSprite(keys);
		canvas.addSprite(keyInstruction);
	}

	/**uses the mouse and keyboard to move the sprites
	 * @see fang.FrameAdvancer#advanceFrame()*/
	public void advanceFrame(double timeAdvanced)
	{
		Point2D.Double position=getPlayer().getMouse().getLocation();
		vertical.setLocation(
		    vertical.getLocation().x,
		    position.y);
		horizontal.setLocation(
		    position.x,
		    horizontal.getLocation().y);
		both.setLocation(position.x, position.y);
		//mouse click position not null means the user clicked
		if(getPlayer().getMouse().getClickLocation()!=null)
		{
			clickCount++;
			both.setText(""+clickCount);
		}
		char keyPressed=getPlayer().getKeyboard().getLastKey();
		if(keyPressed=='i')
		{
			keys.setLocation(keys.getLocation().x,
			                 keys.getLocation().y-
			                 0.05);
		}
		else if(keyPressed=='m')
		{
			keys.setLocation(keys.getLocation().x,
			                 keys.getLocation().y+
			                 0.05);
		}
		else if(keyPressed=='j')
		{
			keys.setLocation(
			    keys.getLocation().x-
			    0.05,
			    keys.getLocation().y);
		}
		else if(keyPressed=='k')
		{
			keys.setLocation(
			    keys.getLocation().x+
			    0.05,
			    keys.getLocation().y);
		}
	}

	public static void main(String[] argv)
	{
		new InputLoop().runAsApplication();
	}
}

