package com.badlogic.gdx.artemis.components;

import com.artemis.Component;
import com.artemis.ComponentType;
import com.badlogic.gdx.math.Vector2;

public class Path extends Component {
	public static ComponentType CType = ComponentType.getTypeFor(Path.class);
	
	private com.badlogic.gdx.math.Path<Vector2> path;
	private float timer;
	private final float duration;
	
	private Loop loop;
	private boolean reverse;
	
	private boolean done;
	private boolean waiting;
	
	/**
	 * @param path - path for the entity to follow
	 * @param duration - time it should take to traverse the path
	 */
	public Path(com.badlogic.gdx.math.Path<Vector2> path, float duration) {
		this.path = path;
		this.duration = duration;
		this.loop = Loop.None;
	}
	
	/**
	 * @param path - path for the entity to follow
	 * @param duration - time it should take to traverse the path
	 * @param delay - how long it should take before the entity will start traversing the path
	 */
	public Path(com.badlogic.gdx.math.Path<Vector2> path, float duration, float delay, Loop l) {
		this.path = path;
		this.duration = duration;
		this.timer = -delay;
		this.loop = l;
	}
	
	
	
	public void update(float t)
	{
		if (done || waiting)
			return;
		
		timer += t;
		if (timer >= duration)
		{
			switch(loop)
			{
				case Repeat:
					timer = 0f;
					break;
				case PingPong:
					if (!reverse){
						reverse = true;
						timer = 0;
					}
					else
						done = true;
					break;
				case PingPongLoop:
					reverse = !reverse;
					timer = 0;
					break;	
				default:
					done = true;
					break;
			}
		}
	}
	
	/**
	 * Get's the current location along the path that the timer indicates that the entity is at
	 * @param out
	 */
	public void getValue(Vector2 out)
	{
		if (timer <= 0) {
			path.valueAt(out, 0);
			return;
		}
		
		if (!reverse)
		{
			path.valueAt(out, timer / duration);
		}
		else
		{
			path.valueAt(out, (duration - timer) / duration);
		}
	}
	
	public static enum Loop {
		None,
		Repeat,
		PingPong,
		PingPongLoop;
	}

	public boolean isPaused() {
		return waiting;
	}
	
	/**
	 * Pause the entity's travel along the path
	 */
	public void pause()
	{
		waiting = true;
	}
	
	/**
	 * Allow the entity to continue traveling along the path if it has been told to wait
	 */
	public void resume()
	{
		waiting = false;
	}

	public boolean isDone() {
		return done;
	}
}

@@ -462,16 +462,143 @@
 ting;%0A%09%0A
+%09private Vector2 start = new Vector2();%0A%09%0A%09//temporary vector for holding calculations%0A%09private Vector2 tmp = new Vector2();%0A%09%0A
 %09/**%0A%09 *
@@ -1856,24 +1856,42 @@
 or2 out)%0A%09%7B%0A
+%09%09out.set(start);%0A
 %09%09if (timer 
@@ -1918,19 +1918,19 @@
 eAt(
-out
+tmp
 , 0);%0A%09%09
 %09ret
@@ -1929,25 +1929,23 @@
 ;%0A%09%09
-%09return;%0A%09%09%7D
+%7D%0A%09%09else%0A%09%09%7B
 %0A%09%09
-%0A%09
 %09if 
@@ -1957,18 +1957,20 @@
 erse)%0A%09%09
+%09
 %7B%0A
+%09
 %09%09%09path.
@@ -1977,19 +1977,19 @@
 valueAt(
-out
+tmp
 , timer 
@@ -1999,26 +1999,28 @@
 uration);%0A%09%09
+%09
 %7D%0A
+%09
 %09%09else%0A%09%09%7B%0A%09
@@ -2016,18 +2016,20 @@
 %09else%0A%09%09
+%09
 %7B%0A
+%09
 %09%09%09path.
@@ -2036,19 +2036,19 @@
 valueAt(
-out
+tmp
 , (durat
@@ -2075,17 +2075,41 @@
 ion);%0A%09%09
-%7D
+%09%7D%0A%09%09%7D%0A%09%09%0A%09%09out.add(tmp);
 %0A%09%7D%0A%09%0A%09p

Add offset for Path components