JTP
Class GameTimer

java.lang.Object
  extended by JTP.GameTimer

public class GameTimer
extends Object

Class for controlling timed levels. Uses Timer class provide by swing framework.

Author:
V.Termanis

Constructor Summary
GameTimer(String beepSoundName, Screen s, GamePlayer gp)
          Constructs a new GameTimer instance with the given attributes.
 
Method Summary
 long getAccumulatedTime()
          Returns time added up by using incrementAccumulatedTime() method for active timer.
 long getAccumulatedTimeSeconds()
          Returns time added up by using incrementAccumulatedTime() method, in seconds, for active timer.
 boolean hasStarted()
          Indicates whether start() method has been called.
 void incrementAccumulatedTime()
          Adds the currently leftover time to the total for active timer.
 boolean isActive()
          Indicates whether a time limit has been set.
 void reset()
          Resets start and stop times for all timers as well as setting active timer back to the default.
 void reset(int activeTimer)
          Resets start and stop times for all timers as well as setting active timer back to specified one.
 void resetAccumulatedTime()
          Resets the time added up by using incrementAccumulatedTime() method back to zero for active timer.
 void resume()
          Continues countdown of a timer stopped by stop().
 void setTimeLimit(int timeLimit)
          Sets time limit to use.
 void start()
          Starts the active timer if a time limit has been set.
 void stop()
          Stops the active timer.
 void stopAll()
          Stops all (both) timers.
 void swapActiveTimer()
          Sets other timer to active one.
 long timeLeft()
          Returns time remaining for active timer.
 long timeLeft(int index)
          Returns time remaining.
 boolean timeUpdateRequested()
          Used to decide whether the whole screen or just timer display has to be rendered.
 String toString()
          Returns time remaining for active timer in mm:ss format.
 String toString(int index)
          Returns time remaining in mm:ss format.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GameTimer

public GameTimer(String beepSoundName,
                 Screen s,
                 GamePlayer gp)
Constructs a new GameTimer instance with the given attributes. Note: The timer is not started upon construction.

Parameters:
s - parent rendering instance
gp - gameplay instance
Method Detail

start

public void start()
Starts the active timer if a time limit has been set. If the timer has already been started the started it will be reset and restarted.


hasStarted

public boolean hasStarted()
Indicates whether start() method has been called.

Returns:
true if start() has been called since construction or the last reset() call.

stop

public void stop()
Stops the active timer.


stopAll

public void stopAll()
Stops all (both) timers.


resume

public void resume()
Continues countdown of a timer stopped by stop().


swapActiveTimer

public void swapActiveTimer()
Sets other timer to active one.


reset

public void reset()
Resets start and stop times for all timers as well as setting active timer back to the default.


reset

public void reset(int activeTimer)
           throws IllegalArgumentException
Resets start and stop times for all timers as well as setting active timer back to specified one.

Parameters:
activeTimer - either GamePlayer.P1 or GamePlayer.P2
Throws:
IllegalArgumentException - if activeTimer is not one of GamePlayer.P1 or GamePlayer.P2.

resetAccumulatedTime

public void resetAccumulatedTime()
Resets the time added up by using incrementAccumulatedTime() method back to zero for active timer.


setTimeLimit

public void setTimeLimit(int timeLimit)
                  throws IllegalArgumentException
Sets time limit to use. Note: Same time limit will be used for both timers.

Parameters:
timeLimit - time in seconds. Set this to zero for no time limit.
Throws:
IllegalArgumentException - if timeLimit < 0.

incrementAccumulatedTime

public void incrementAccumulatedTime()
Adds the currently leftover time to the total for active timer. Note: It only makes sense to call this after the timer has been stopped and before it has been reset.


getAccumulatedTime

public long getAccumulatedTime()
Returns time added up by using incrementAccumulatedTime() method for active timer.

Returns:
accumulated time in milliseconds.

getAccumulatedTimeSeconds

public long getAccumulatedTimeSeconds()
Returns time added up by using incrementAccumulatedTime() method, in seconds, for active timer. Note: The returned value will have been rounded up to the nearest second.

Returns:
accumulated time in seconds.

timeUpdateRequested

public boolean timeUpdateRequested()
Used to decide whether the whole screen or just timer display has to be rendered.


isActive

public boolean isActive()
Indicates whether a time limit has been set. Note: This only means a time limit has been set but not whether it is currently counting down.

Returns:
true if a time limit has been set.

timeLeft

public long timeLeft()
Returns time remaining for active timer.

Returns:
time in milliseconds

timeLeft

public long timeLeft(int index)
Returns time remaining.

Parameters:
index - either GamePlayer.P1 or GamePlayer.P2.
Returns:
time in milliseconds

toString

public String toString()
Returns time remaining for active timer in mm:ss format.

Overrides:
toString in class Object
Returns:
time in String representation.

toString

public String toString(int index)
                throws IllegalArgumentException
Returns time remaining in mm:ss format.

Parameters:
index - either GamePlayer.P1 or GamePlayer.P2.
Returns:
time in String representation.
Throws:
IllegalArgumentException