Class Timer

java.lang.Object
nl.saxion.gameapp.utils.Timer

public class Timer extends Object
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
    Constructor
    Description
    Timer(String name, float duration, int initialLoops)
    Creates a new Timer.
  • Method Summary

    Modifier and Type
    Method
    Description
    float
    Returns timer duration of one cycle.
    int
    Returns initial loops (-1 = infinite).
    Returns the timer name.
    float
    Returns the progress of the current cycle from 0 to 1.
    float
    Remaining time until next trigger.
    int
    Returns remaining loops (-1 = infinite).
    boolean
    Returns whether timer is finished (limited loops only).
    boolean
    Returns whether timer is paused.
    void
    Pauses the timer.
    void
    Resets timer to initial state (elapsed = 0, loops restored).
    void
    Resumes a paused timer.
    void
    setDuration(float duration)
    Set timer duration of one cycle.
    void
    update(float delta)
    Updates the timer by a delta time (seconds).
    void
    Updates timer using Gdx.graphics delta time.
    boolean
    Returns true if the timer triggered during the last update.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Timer

      public Timer(String name, float duration, int initialLoops)
      Creates a new Timer.
      Parameters:
      name - unique name (for identification purposes)
      duration - duration of one cycle in seconds
      initialLoops - 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 using Gdx.graphics delta 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

      public String getName()
      Returns the timer name.
    • getProgress

      public float getProgress()
      Returns the progress of the current cycle from 0 to 1. Returns 1 if finished.