-
Notifications
You must be signed in to change notification settings - Fork 3
Basics 8 (Time)
#Tutorial navigation
- [Previous: Basics 7 (IO)](Basics 7 (IO))
- Current: Basics 8 (Time)
- [Next: Basics 9 (Utilities)](Basics 9 (Utilities))
#Time
This class has a method to return the system time in milliseconds: long Time.get()
.
#DeltaTime
The initial update rate is 32 updates per second, but this game component (see a previous tutorial) provides some methods to manage the update rate:
-
int DeltaTime.getUPS()
Gets the frequency in updates per second. -
DeltaTime.setUPS(int ups)
Sets the frequency in updates per second. -
float DeltaTime.getDT()
Gets the time interval between two updates in seconds.
This class also provides a method to access a number that uniquely identifies a call to update for all scenes in the stack: long DeltaTime.updateID()
.
#Stopwatch
This class works like a real stopwatch:
-
Stopwatch.start()
Starts the time counting. If already counting, starts over. -
Stopwatch.pause()
Pauses the time counting if the stopwatch is not paused. -
Stopwatch.resume()
Resumes the time counting if the stopwatch is paused. -
Stopwatch.reset()
Stops and sets the counting to zero. -
long Stopwatch.time()
Query the time counted in milliseconds.
#Countdown
This class works just like Stopwatch
, but in the reverse approach:
-
Countdown.start(long ms)
Starts the countdown. If already counting, starts over. Pass time in milliseconds. -
Countdown.pause()
Pauses the countdown if not already paused. -
Countdown.resume()
Resumes the countdown if it is paused. -
Countdown.reset()
Stops and sets the counting to zero. -
long Countdown.time()
Query the time remaining in milliseconds.
To work like a real countdown, as the ones provided by cellphones, this class must be a Subject
(see the tutorial about utilities) who broadcasts an event when the countdown reaches zero: Countdown.EVENT_COMPLETE
. Countdown
is nicer if it extends GameObject
, because the broadcast can be done inside the implementation of GameObject.update()
. For being together with the other concrete implementations of GameObject
that the engine provides, Countdown
was placed inside the engine.util
package, not inside the engine.time
package, like the three classes described above.
#Full working code
package mygame.scene;
import mygame.object.MyPlayer;
import org.unbiquitous.uImpala.engine.core.ContainerScene;
import org.unbiquitous.uImpala.engine.core.GameComponents;
import org.unbiquitous.uImpala.engine.time.DeltaTime;
public class MyFirstScene extends ContainerScene {
public MyFirstScene() {
add(new MyPlayer(assets));
GameComponents.get(DeltaTime.class).setFPS(60);
}
}
package mygame.object;
import org.unbiquitous.uImpala.engine.asset.AssetManager;
import org.unbiquitous.uImpala.engine.core.GameObject;
import org.unbiquitous.uImpala.engine.core.GameRenderers;
import org.unbiquitous.uImpala.engine.util.Countdown;
import org.unbiquitous.uImpala.util.observer.Event;
import org.unbiquitous.uImpala.util.observer.Observation;
import org.unbiquitous.uImpala.util.observer.Subject;
public class MyPlayer extends GameObject {
private Countdown countdown = new Countdown();
private int level = 0;
private static final int LEVEL_UP_TIME = 5000;
public MyPlayer(AssetManager assets) {
add(countdown);
countdown.connect(Countdown.EVENT_COMPLETE, new Observation(this, "levelUp"));
countdown.start(LEVEL_UP_TIME);
System.out.println("player level: " + level);
}
protected void update() {
// TODO
}
protected void render(GameRenderers renderers) {
// TODO
}
protected void wakeup(Object... args) {
// TODO
}
protected void destroy() {
// TODO
}
private void levelUp(Event event, Subject subject) {
level++;
countdown.start(LEVEL_UP_TIME);
System.out.println("player level: " + level);
}
}
#Tutorial navigation
- [Previous: Basics 7 (IO)](Basics 7 (IO))
- Current: Basics 8 (Time)
- [Next: Basics 9 (Utilities)](Basics 9 (Utilities))