Package nl.saxion.gameapp.utils
Class Timer
java.lang.Object
nl.saxion.gameapp.utils.Timer
Reusable Timer class for game events.
Supports single-shot timers, looping timers, limited loops, pause/resume, resetting, querying remaining time, and changing duration.
Usage pattern:
Timer t = new Timer("spawn", 0.5f, -1); // infinite looping timer
t.update(Gdx.graphics.getDeltaTime());
if (t.wentOff()) spawnEnemy();
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfloatReturns timer duration of one cycle.intReturns initial loops (-1 = infinite).getName()Returns the timer name.floatReturns the progress of the current cycle from 0 to 1.floatRemaining time until next trigger.intReturns remaining loops (-1 = infinite).booleanReturns whether timer is finished (limited loops only).booleanisPaused()Returns whether timer is paused.voidpause()Pauses the timer.voidreset()Resets timer to initial state (elapsed = 0, loops restored).voidresume()Resumes a paused timer.voidsetDuration(float duration) Set timer duration of one cycle.voidupdate(float delta) Updates the timer by a delta time (seconds).voidUpdates timer usingGdx.graphicsdelta time.booleanwentOff()Returns true if the timer triggered during the last update.
-
Constructor Details
-
Timer
Creates a new Timer.- Parameters:
name- unique name (for identification purposes)duration- duration of one cycle in secondsinitialLoops- number of loops (-1 = infinite, 1 = single-shot, >1 = limited loops)
-
-
Method Details
-
update
public void update(float delta) Updates the timer by a delta time (seconds). Call every frame.- Parameters:
delta- time passed since last update (in seconds)
-
updateUsingGdx
public void updateUsingGdx()Updates timer usingGdx.graphicsdelta time. -
wentOff
public boolean wentOff()Returns true if the timer triggered during the last update.- Returns:
- true if timer went off
-
pause
public void pause()Pauses the timer. -
resume
public void resume()Resumes a paused timer. -
reset
public void reset()Resets timer to initial state (elapsed = 0, loops restored). -
getRemaining
public float getRemaining()Remaining time until next trigger.- Returns:
- seconds remaining (0 if finished)
-
getDuration
public float getDuration()Returns timer duration of one cycle. -
setDuration
public void setDuration(float duration) Set timer duration of one cycle. -
isPaused
public boolean isPaused()Returns whether timer is paused. -
isFinished
public boolean isFinished()Returns whether timer is finished (limited loops only). -
getInitialLoops
public int getInitialLoops()Returns initial loops (-1 = infinite). -
getRemainingLoops
public int getRemainingLoops()Returns remaining loops (-1 = infinite). -
getName
Returns the timer name. -
getProgress
public float getProgress()Returns the progress of the current cycle from 0 to 1. Returns 1 if finished.
-