/*
 * Automatic timed linear interpolation
 * 
 * Set current and target value and number of frames that should elapse before it reaches the target
 * Then call update() to advance it one frame and get the latest value
 */
public class AutoLerp {

	int counter;
	
	private float current;
	
	private float target;
	boolean targeting = false;
	
	private float increment;
	
	AutoLerp(float target, int num){
		current=0;
		setTarget(target, num);
	}
	
	float update(){ //advance one frame
		if(targeting){
//			System.out.print(counter+" AL: "+current+" + "+increment);
			current += increment;
//			System.out.println(" = "+current);
			counter++;
			
			if(Math.abs(current - target) < Math.abs(increment) || current < 0){ //its close
//				System.out.println("Target reached: "+target+" count = "+counter);
				current=target;
				targeting=false;
			}
		}
		return current;
	}
	
	float getCurrent(){return current;}
	
	void setTarget(float _target, int num_frames){
		targeting=true;
		target = _target;
		counter =0;
		if(num_frames < 1) num_frames = 1;
		increment = (target - current)/ num_frames;
		if(increment ==0) increment = 0.1f; //make sure it goes somewhere
		
//		System.out.println("cur, targ, num, incr "+current+" "+target+" "+num_frames+" "+increment);
	}
	
	void reset(){current=0;}
	void reset(float start){current=start;}
	
}

