package speiger.src.coreengine.math.value;

public abstract class Value implements IValue {
	final float start;
	final float duration;
	byte flags = 0;
	int done = 0;
	float progress = 0F;
	float value;
	
	public Value(float duration) {
		this(0F, duration);
	}
	
	public Value(float start, float duration) {
		this.duration = duration;
		this.start = start;
	}
	
	@Override
	public float update(float particleTime) {
		if((flags & 2) != 0) { return value; }
		progress += particleTime;
		if(progress < start) {
			if((flags & 1) == 0) {
				value = calculateProgress(0F);
				onInitialValueSet();
			}
			return value;
		}
		value = calculateProgress((progress - start) / duration);
		if(progress >= start + duration) { onFinished(); }
		return value;
	}
	
	@Override
	public float get() { return value; }
	@Override
	public boolean isDone() { return done > 0; }
	protected abstract float calculateProgress(float time);
	
	protected void onFinished() {
		progress = start;
		done++;
	}
	
	protected void onInitialValueSet() { flags |= 1; }
	
	protected void finishProgress() {
		flags |= 2;
		done++;
	}
}
