package icarus;

import java.awt.Toolkit;

import world.GameObject;

public class Player extends GameObject {
	
	public Player() {
		
	}
	
	public void update() {
		freeMove();
	}
	
	private double topSpeed = 7.5;
	private double speed = 0;
	
	private double panFactor = 0;
	
	private void freeMove() {
		
		double x = input.getMousePositionX()
				 - Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2;
		
		double y = input.getMousePositionY()
				 - Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2;
		
		// rotate
		
		double rotateFactor = (double) 1 / 20000;
		
		double flipFactor = (1 - Math.abs(camera.getYView())) * camera.getYAngle() * 15;
		
		if(flipFactor > 1)
			flipFactor = 1;
		
		if(flipFactor < -1)
			flipFactor = -1;
		
		if(input.getMouseButtons()[0] && input.getMouseButtons()[2])
			panFactor += .025;
		
		else
			panFactor -= .025;
		
		if(panFactor < 0)
			panFactor = 0;
		
		if(panFactor > 1)
			panFactor = 1;
		
		camera.rotate((x) * rotateFactor * flipFactor* (1 - panFactor),
				(y) * -rotateFactor * (1 - panFactor));
		
		// fly

		if(input.getMouseButtons()[0])
			speed += topSpeed / 75;

		if(input.getMouseButtons()[2])
			speed -= topSpeed / 75;
		
		if(speed > topSpeed)
			speed = topSpeed;
		
		if(speed < -topSpeed)
			speed = -topSpeed;
		
		if(!(input.getMouseButtons()[0] ^ input.getMouseButtons()[2])) {

			if(speed > 0) {
				
				speed -= (topSpeed / 75) * 5;
				
				if(speed < 0)
					speed = 0;
			}

			if(speed < 0) {
				
				speed += (topSpeed / 75) * 5;
				
				if(speed > 0)
					speed = 0;
			}
		}
		
		camera.move(speed);
		
		camera.strafeHorizontal((x / (Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2))
				* topSpeed * panFactor);
		
		camera.strafeVertical((y / (Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2))
				* topSpeed * panFactor);
		
		// speed
		
//		topSpeed += input.getMouseScroll() / 500;
//		
//		if(topSpeed > .15)
//			topSpeed = .15;
//		
//		if(topSpeed < .05)
//			topSpeed = .05;
	}
}