package cheese.squeeze.gameObjects;_x000D_
_x000D_
import java.util.Iterator;_x000D_
import java.util.List;_x000D_
_x000D_
import cheese.squeeze.helpers.AssetLoader;_x000D_
_x000D_
import com.badlogic.gdx.Gdx;_x000D_
import com.badlogic.gdx.math.MathUtils;_x000D_
import com.badlogic.gdx.math.Vector2;_x000D_
_x000D_
//TODO: direction bijhouden, en veranderen als we aan een gotopoint belanden._x000D_
public class Mouse {_x000D_
_x000D_
	private Vector2 position;_x000D_
	private Vector2 velocity;_x000D_
	private final float FLIKERING =100;_x000D_
	private float EYEOPEN;_x000D_
	private boolean open = true;_x000D_
	_x000D_
	private float orignSpeed;_x000D_
	private float speed;_x000D_
	private float tolerance = 0.02f;_x000D_
	_x000D_
	private Vector2 absolutePosition;_x000D_
_x000D_
	private float rotation;_x000D_
	private Vector2 mouseNose = AssetLoader.mouseNose;_x000D_
	_x000D_
	private Line currentLine;_x000D_
	private Line nextLine;_x000D_
	private Vector2 goToOrientation;_x000D_
	private Vector2 nextGoToPoint;_x000D_
	private boolean ended = false;_x000D_
	_x000D_
	public Mouse(float speed, Line line){_x000D_
		EYEOPEN = (float) (FLIKERING*Math.random());_x000D_
		float x = line.getX1();_x000D_
		float y = line.getY1();_x000D_
		//float y = 0;_x000D_
		position = new Vector2(x,y);_x000D_
		this.currentLine = line;_x000D_
		this.speed = speed;_x000D_
		this.orignSpeed = speed;_x000D_
		//position = new Vector2(x-(mouseNose.x), y- (mouseNose.y));_x000D_
		_x000D_
		velocity = new Vector2(0, 0);_x000D_
		goToOrientation = new Vector2(0, 1);_x000D_
		updatePath();_x000D_
	}_x000D_
	_x000D_
	public boolean isOnHorizontalLine() {_x000D_
		if(currentLine instanceof HorizontalLine) {_x000D_
			return true;_x000D_
		}_x000D_
		return false;_x000D_
	}_x000D_
_x000D_
	public void update(float delta) {_x000D_
		EYEOPEN--;_x000D_
		_x000D_
		if (nextGoToPoint != null) {_x000D_
			if(atIntersection()) {_x000D_
				//System.out.println("intersection reached!");_x000D_
				//the mouse stands now at the previous nextGoToPoint_x000D_
				setPosition(nextGoToPoint.x, nextGoToPoint.y);_x000D_
				//nextGoToPoint is yet to be determined_x000D_
				nextGoToPoint = null;_x000D_
				//This mouse is now on the new line. _x000D_
				//If there is no next line, the mouse stays on this line._x000D_
				currentLine = (nextLine != null) ? nextLine : currentLine;_x000D_
				//nextLine is yet to be determined._x000D_
				nextLine = null;_x000D_
				if (currentLine instanceof VerticalLine){_x000D_
					goToOrientation = new Vector2(0, 1);_x000D_
				} else if (currentLine instanceof HorizontalLine) {_x000D_
					if (getPosition().equals(currentLine.getPoint1()))_x000D_
						goToOrientation = currentLine.getPoint2().cpy().sub(currentLine.getPoint1());_x000D_
					else_x000D_
						goToOrientation = currentLine.getPoint1().cpy().sub(currentLine.getPoint2());_x000D_
				}_x000D_
				//updateVelocityDirection();_x000D_
				//pick a new destination_x000D_
				updatePath();_x000D_
			}_x000D_
			//set the mouses new speed._x000D_
			if (atIntersection()) {_x000D_
				//The mouse ran into something with a dead end._x000D_
				((VerticalLine) currentLine).getGoal().activate();_x000D_
				velocity.set(Vector2.Zero);_x000D_
				ended = true;_x000D_
			} else {_x000D_
				updateVelocityDirection();_x000D_
			}_x000D_
			//move the mouse._x000D_
			updateVelocityDirection();_x000D_
		//	setPosition(getX() + velocity.x * delta, getY() + velocity.y * delta);_x000D_
			setPosition(getX() + velocity.x * 1, getY() + velocity.y * 1);_x000D_
		//System.out.println(this.rotation);_x000D_
		}		_x000D_
	}_x000D_
_x000D_
	private void updateVelocityDirection() {_x000D_
		if(!ended) {_x000D_
			float angle = (float) Math.atan2(nextGoToPoint.y - getY(), nextGoToPoint.x - getX());_x000D_
			velocity.set((float) Math.cos(angle) * speed, (float) Math.sin(angle) * speed);_x000D_
			//set the mouses angle._x000D_
			setRotation(angle * MathUtils.radiansToDegrees);_x000D_
		}_x000D_
		else {_x000D_
			setRotation(90);_x000D_
		}_x000D_
	}_x000D_
	_x000D_
	public void updatePath() {_x000D_
		Vector2 nextIntersection = currentLine.getNextIntersection(getPosition(), goToOrientation);_x000D_
		nextLine = currentLine.getNeighbour(getPosition(), goToOrientation);_x000D_
		if (nextIntersection == null) {_x000D_
			nextGoToPoint = currentLine.getEndPoint(getPosition(), velocity);_x000D_
		} else {_x000D_
			nextGoToPoint = nextIntersection;_x000D_
		}_x000D_
	}_x000D_
_x000D_
	private boolean atIntersection() {_x000D_
		float dynTolerance = speed / tolerance * Gdx.graphics.getDeltaTime();_x000D_
		//System.out.println("dyn tol: " + dynTolerance);_x000D_
		return Math.abs(nextGoToPoint.x - getX()) <= dynTolerance _x000D_
				&& Math.abs(nextGoToPoint.y - getY()) <= dynTolerance;_x000D_
	}_x000D_
_x000D_
	private void setRotation(float f) {_x000D_
		this.rotation = f;_x000D_
		_x000D_
	}_x000D_
_x000D_
	public float getX() {_x000D_
		return position.x;_x000D_
	}_x000D_
_x000D_
	public float getY() {_x000D_
		return position.y;_x000D_
	}_x000D_
	_x000D_
	private void setPosition(float x,float y) {_x000D_
		this.position.set(x, y);_x000D_
	}_x000D_
	_x000D_
	public float getXAbs() {_x000D_
		return this.absolutePosition.x;_x000D_
	}_x000D_
	_x000D_
	public float getYAbs() {_x000D_
		return this.absolutePosition.y;_x000D_
	}_x000D_
_x000D_
	public float getRotation() {_x000D_
		return rotation;_x000D_
	}_x000D_
	_x000D_
_x000D_
	public Vector2 getPosition() {_x000D_
		return position;_x000D_
	}_x000D_
_x000D_
	public boolean isEnded() {_x000D_
		return ended ;_x000D_
	}_x000D_
_x000D_
	public float getSpeed() {_x000D_
		// TODO Auto-generated method stub_x000D_
		return velocity.x + velocity.y;_x000D_
	}_x000D_
	_x000D_
	//public void changeNextWayPoints(ArrayList<Vector2> newWaypoints) {_x000D_
	//	this.setPath(nPath);_x000D_
	//}_x000D_
	_x000D_
	public boolean eyesOpen() {_x000D_
		if(EYEOPEN < 0 && !open) {_x000D_
			EYEOPEN = (float) ((FLIKERING*2)*Math.random());_x000D_
			open = !open;_x000D_
		}_x000D_
		if(EYEOPEN < 0 && open) {_x000D_
			EYEOPEN = (float) ((FLIKERING/5)*Math.random());_x000D_
			open = !open;_x000D_
		}_x000D_
		return open;_x000D_
	}_x000D_
_x000D_
	public void setSpeed(float angle) {_x000D_
		this.speed = this.orignSpeed + this.orignSpeed*angle;_x000D_
	}_x000D_
	_x000D_
	public float getSpeedLine() {_x000D_
		return this.speed - this.orignSpeed;_x000D_
	}_x000D_
_x000D_
}_x000D_

//TODO: direction bijhouden, en veranderen als we aan een gotopoint belanden._x000D_
package cheese.squeeze.gameObjects;_x000D_
_x000D_
import java.util.Iterator;_x000D_
import java.util.List;_x000D_
_x000D_
import cheese.squeeze.helpers.AssetLoader;_x000D_
_x000D_
import com.badlogic.gdx.Gdx;_x000D_
import com.badlogic.gdx.math.MathUtils;_x000D_
import com.badlogic.gdx.math.Vector2;_x000D_
_x000D_
//TODO: direction<SUF>
public class Mouse {_x000D_
_x000D_
	private Vector2 position;_x000D_
	private Vector2 velocity;_x000D_
	private final float FLIKERING =100;_x000D_
	private float EYEOPEN;_x000D_
	private boolean open = true;_x000D_
	_x000D_
	private float orignSpeed;_x000D_
	private float speed;_x000D_
	private float tolerance = 0.02f;_x000D_
	_x000D_
	private Vector2 absolutePosition;_x000D_
_x000D_
	private float rotation;_x000D_
	private Vector2 mouseNose = AssetLoader.mouseNose;_x000D_
	_x000D_
	private Line currentLine;_x000D_
	private Line nextLine;_x000D_
	private Vector2 goToOrientation;_x000D_
	private Vector2 nextGoToPoint;_x000D_
	private boolean ended = false;_x000D_
	_x000D_
	public Mouse(float speed, Line line){_x000D_
		EYEOPEN = (float) (FLIKERING*Math.random());_x000D_
		float x = line.getX1();_x000D_
		float y = line.getY1();_x000D_
		//float y = 0;_x000D_
		position = new Vector2(x,y);_x000D_
		this.currentLine = line;_x000D_
		this.speed = speed;_x000D_
		this.orignSpeed = speed;_x000D_
		//position = new Vector2(x-(mouseNose.x), y- (mouseNose.y));_x000D_
		_x000D_
		velocity = new Vector2(0, 0);_x000D_
		goToOrientation = new Vector2(0, 1);_x000D_
		updatePath();_x000D_
	}_x000D_
	_x000D_
	public boolean isOnHorizontalLine() {_x000D_
		if(currentLine instanceof HorizontalLine) {_x000D_
			return true;_x000D_
		}_x000D_
		return false;_x000D_
	}_x000D_
_x000D_
	public void update(float delta) {_x000D_
		EYEOPEN--;_x000D_
		_x000D_
		if (nextGoToPoint != null) {_x000D_
			if(atIntersection()) {_x000D_
				//System.out.println("intersection reached!");_x000D_
				//the mouse stands now at the previous nextGoToPoint_x000D_
				setPosition(nextGoToPoint.x, nextGoToPoint.y);_x000D_
				//nextGoToPoint is yet to be determined_x000D_
				nextGoToPoint = null;_x000D_
				//This mouse is now on the new line. _x000D_
				//If there is no next line, the mouse stays on this line._x000D_
				currentLine = (nextLine != null) ? nextLine : currentLine;_x000D_
				//nextLine is yet to be determined._x000D_
				nextLine = null;_x000D_
				if (currentLine instanceof VerticalLine){_x000D_
					goToOrientation = new Vector2(0, 1);_x000D_
				} else if (currentLine instanceof HorizontalLine) {_x000D_
					if (getPosition().equals(currentLine.getPoint1()))_x000D_
						goToOrientation = currentLine.getPoint2().cpy().sub(currentLine.getPoint1());_x000D_
					else_x000D_
						goToOrientation = currentLine.getPoint1().cpy().sub(currentLine.getPoint2());_x000D_
				}_x000D_
				//updateVelocityDirection();_x000D_
				//pick a new destination_x000D_
				updatePath();_x000D_
			}_x000D_
			//set the mouses new speed._x000D_
			if (atIntersection()) {_x000D_
				//The mouse ran into something with a dead end._x000D_
				((VerticalLine) currentLine).getGoal().activate();_x000D_
				velocity.set(Vector2.Zero);_x000D_
				ended = true;_x000D_
			} else {_x000D_
				updateVelocityDirection();_x000D_
			}_x000D_
			//move the mouse._x000D_
			updateVelocityDirection();_x000D_
		//	setPosition(getX() + velocity.x * delta, getY() + velocity.y * delta);_x000D_
			setPosition(getX() + velocity.x * 1, getY() + velocity.y * 1);_x000D_
		//System.out.println(this.rotation);_x000D_
		}		_x000D_
	}_x000D_
_x000D_
	private void updateVelocityDirection() {_x000D_
		if(!ended) {_x000D_
			float angle = (float) Math.atan2(nextGoToPoint.y - getY(), nextGoToPoint.x - getX());_x000D_
			velocity.set((float) Math.cos(angle) * speed, (float) Math.sin(angle) * speed);_x000D_
			//set the mouses angle._x000D_
			setRotation(angle * MathUtils.radiansToDegrees);_x000D_
		}_x000D_
		else {_x000D_
			setRotation(90);_x000D_
		}_x000D_
	}_x000D_
	_x000D_
	public void updatePath() {_x000D_
		Vector2 nextIntersection = currentLine.getNextIntersection(getPosition(), goToOrientation);_x000D_
		nextLine = currentLine.getNeighbour(getPosition(), goToOrientation);_x000D_
		if (nextIntersection == null) {_x000D_
			nextGoToPoint = currentLine.getEndPoint(getPosition(), velocity);_x000D_
		} else {_x000D_
			nextGoToPoint = nextIntersection;_x000D_
		}_x000D_
	}_x000D_
_x000D_
	private boolean atIntersection() {_x000D_
		float dynTolerance = speed / tolerance * Gdx.graphics.getDeltaTime();_x000D_
		//System.out.println("dyn tol: " + dynTolerance);_x000D_
		return Math.abs(nextGoToPoint.x - getX()) <= dynTolerance _x000D_
				&& Math.abs(nextGoToPoint.y - getY()) <= dynTolerance;_x000D_
	}_x000D_
_x000D_
	private void setRotation(float f) {_x000D_
		this.rotation = f;_x000D_
		_x000D_
	}_x000D_
_x000D_
	public float getX() {_x000D_
		return position.x;_x000D_
	}_x000D_
_x000D_
	public float getY() {_x000D_
		return position.y;_x000D_
	}_x000D_
	_x000D_
	private void setPosition(float x,float y) {_x000D_
		this.position.set(x, y);_x000D_
	}_x000D_
	_x000D_
	public float getXAbs() {_x000D_
		return this.absolutePosition.x;_x000D_
	}_x000D_
	_x000D_
	public float getYAbs() {_x000D_
		return this.absolutePosition.y;_x000D_
	}_x000D_
_x000D_
	public float getRotation() {_x000D_
		return rotation;_x000D_
	}_x000D_
	_x000D_
_x000D_
	public Vector2 getPosition() {_x000D_
		return position;_x000D_
	}_x000D_
_x000D_
	public boolean isEnded() {_x000D_
		return ended ;_x000D_
	}_x000D_
_x000D_
	public float getSpeed() {_x000D_
		// TODO Auto-generated method stub_x000D_
		return velocity.x + velocity.y;_x000D_
	}_x000D_
	_x000D_
	//public void changeNextWayPoints(ArrayList<Vector2> newWaypoints) {_x000D_
	//	this.setPath(nPath);_x000D_
	//}_x000D_
	_x000D_
	public boolean eyesOpen() {_x000D_
		if(EYEOPEN < 0 && !open) {_x000D_
			EYEOPEN = (float) ((FLIKERING*2)*Math.random());_x000D_
			open = !open;_x000D_
		}_x000D_
		if(EYEOPEN < 0 && open) {_x000D_
			EYEOPEN = (float) ((FLIKERING/5)*Math.random());_x000D_
			open = !open;_x000D_
		}_x000D_
		return open;_x000D_
	}_x000D_
_x000D_
	public void setSpeed(float angle) {_x000D_
		this.speed = this.orignSpeed + this.orignSpeed*angle;_x000D_
	}_x000D_
	_x000D_
	public float getSpeedLine() {_x000D_
		return this.speed - this.orignSpeed;_x000D_
	}_x000D_
_x000D_
}_x000D_
