package examples.multiview;

import java.awt.geom.Point2D;

import fang.*;


/**Demonstrates scrolling by moving the
 * mouse in the direction desired from
 * the center of the screen.  This example
 * extends on the Navigator example by
 * adding extra players, each with their
 * own view.
 * @author Jam Jenkins*/
public class MultiView extends GameLoop
{
	/** tiles image reflected vertically and
	 * horizontally to make it seamless.*/
	private ImageSprite background;

	/** the players*/
	private StringSprite[] playerSprites;

	/** the trackers for the players*/
	private ProjectileTracker[] playerTrackers;

	/**the tracker used for the scrolling*/
	private ScrollTracker tracker;

	/** makes and adds the sprites */
	public void startGame()
	{
		makeSprites();
		addSprites();
		setHelp("resources/MultiViewHelp.txt");
	}

	/** makes the image sprites for the background
	 * and sets a scrolling tracker on it.  Also
	 * sets each player's sprite to have a
	 * ProjectileTracker on it.*/
	private void makeSprites()
	{
		playerSprites=new StringSprite[getNumberOfPlayers()];
		playerTrackers=new ProjectileTracker[getNumberOfPlayers()];
		for(int i=0; i<playerSprites.length; i++)
		{
			playerSprites[i]=new StringSprite(getPlayer(i).getName());
			playerSprites[i].setWidth(0.25);
			playerSprites[i].setLocation(0.5, 0.5);
			playerTrackers[i]=new ProjectileTracker(new Point2D.Double());
			playerSprites[i].setTracker(playerTrackers[i]);
		}

		background=new ImageSprite("resources/road.jpg");
		background.setScale(1);
		tracker=new ScrollTracker(
		            true,
		            new Point2D.Double(),
		            background,
		            3);
		background.setTracker(tracker);
	}

	/**adds the sprites to the canvas*/
	private void addSprites()
	{
		canvas.addSprite(background);
		for(StringSprite sprite: playerSprites)
			canvas.addSprite(sprite);
	}

	/**
	 * gets the velocity to move the player at.
	 * The player only moves on mouse click.  If
	 * you want the player to move like in the
	 * Navigator example, change the first line
	 * to just get the mouse location instead of
	 * the mouse click location.
	 * @param index the index of the player's mouse
	 * @return a velocity equivalent to the vector
	 * from the mouse position to the center of the
	 * screen.
	 */
	private Point2D.Double getVelocity(int index)
	{
		Point2D.Double mouse=getPlayer(index).getMouse().getClickLocation();
		if(mouse==null) return new Point2D.Double();
		Point2D.Double center=new Point2D.Double(0.5, 0.5);
		double distance=mouse.distance(center);
		double maxDistance=center.distance(1, 1);
		Point2D.Double v;
		/**only move if more than 10% the distance from
		 * the center to the corner*/
		if(distance/maxDistance>0.1)
		{
			v=new Point2D.Double(
			      center.x-mouse.x,
			      center.y-mouse.y);
		}
		else
			v=new Point2D.Double();
		return v;
	}

	/**uses the position of the mouse in relation
	 * to the center of the screen to move the
	 * background in the direction and speed
	 * indicated.  The angle from the center to the
	 * mouse if the direction the background moves
	 * and as the distance from the center of the
	 * screen increases, so too does the scroll speed*/
	public void advanceFrame(double timeAdvanced)
	{
		Point2D.Double v=getVelocity(getID());
		for(int i=0; i<playerTrackers.length; i++)
		{
			Point2D.Double velocity=getVelocity(i);
			if(i!=getID())
			{
				//other players movements are affected not only
				//by their own mouse, but also by the scrolling
				//of this player
				playerTrackers[i].setVelocity(new Point2D.Double(
				                                  v.x-velocity.x, v.y-velocity.y));
			}
			else
			{
				//this player never moves.  Instead, the other
				//players and background scroll by.
				tracker.setVelocity(new Point2D.Double(
				                        velocity.x, velocity.y));
			}
		}

	}

	/**
	 * sets up the game for 2 or more players
	 * @param argv not used.
	 */
	public static void main(String[] argv)
	{
		MultiView multiView=new MultiView();
		multiView.playersSelectable=true;
		multiView.players=2;
		multiView.runAsApplication();
	}
}

