Utility GameTimer class (percent complete w/ interpolation)

I just committed a GameTimer utility class that provides interpolation of percent complete for things outside the library.

It works as follows:

[java]
// In initialization
GameTimer expTimer = new GameTimer(someDuration);
expTimer.setInterpolation(Interpolation.exp5Out);

// Timer is either new or has finished
if (!expTimer.isActive()) {
// Timer has run at least one time prior and should be reset for another use
if (expTimer.getRunCount() > 0) {

	//** Area to perform a function after timer has completed **//
	// For instance remove an object from the scene
	obj.removeFromParent();
	
	// Optionally reset timer for another use
	// subtracting current duration from elapsed time
	expTimer.reset(true);
	// Roll a new random duration
	expTimer.setDuration(FastMath.nextRandomFloat()*.5f);
}
// Start the timer
expTimer.startGameTimer();

} else {

//** Area for executing code per update loop **//
// For instance fade an object out of the scene using the timers interpolated percent complete
obj.getColor().a = 1-expTimer.getPercentComplete();

// Update the timer
expTimer.update(tpf);

}
[/java]

Anyways, it provides a very useful feature + an even more useful feature (the interpolation portion)

/wave

1 Like

A little more on how this can be used:

It is quite useful for scripting out a series of sequential events.

In the short game I am throwing together for Android (Galaga type game), I need the ports on the mother ship to open, release a wave of ships, close, then notify the mother ship that it can fire off another wave of ships from a random port. To do this, you just use the getRunCount() like so:

[java]
if (timer != null) {
if (!timer.isActive()) {
if (timer.getRunCount() == 0) { // Before timer is run the first time
// Set a random delay time to execute the first command
timer.setDuration(
FastMath.nextRandomFloat()*5f+1f
);
} else if (timer.getRunCount() == 1) { // After the timer finishes running once
// run commands and set duration of timer for next run
openPort(true);
isVulnerable = true;
timer.reset(true);
timer.setDuration(animDuration);
} else if (timer.getRunCount() == 2) { // After the timer runs twice
// run commands and set duration of timer for next run
mother.formation.createBlockerRow(getPosition());
timer.reset(true);
timer.setDuration(openDuration);
} else if (timer.getRunCount() == 3) { // After the timer runs three times
// run commands and set duration of timer for next run
openPort(false);
timer.reset(true);
timer.setDuration(animDuration);
} else if (timer.getRunCount() == 4) { // After the timer runs four times
// Clear the timer and run final commands
timer = null;
isVulnerable = false;
isActive = wasActive;
releaseBlockers = false;
mother.blockerReleaseComplete();
}
if (timer != null)
timer.startGameTimer();
} else {
timer.update(tpf);
}
}
[/java]

Anyways… this is making life sooooo much easier for me… thought others mind find it useful as well.