summaryrefslogtreecommitdiff
path: root/src/RobotRace.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/RobotRace.java')
-rw-r--r--src/RobotRace.java96
1 files changed, 92 insertions, 4 deletions
diff --git a/src/RobotRace.java b/src/RobotRace.java
index eac9ed5..093e021 100644
--- a/src/RobotRace.java
+++ b/src/RobotRace.java
@@ -16,6 +16,7 @@ import robotrace.Vector;
import static java.lang.Math.*;
import java.net.URI;
import java.net.URISyntaxException;
+import java.util.Arrays;
import java.util.Locale;
import java.util.Random;
@@ -152,6 +153,21 @@ public class RobotRace extends Base {
boolean enableTextures;
/**
+ * Real animation time at which the game is paused (or -1 if not paused).
+ */
+ private float pausedSince = -1;
+
+ /**
+ * Total number of animation time that was spent in the paused state.
+ */
+ private float pausedTimeTotal;
+
+ /**
+ * Trigger a pause on the next frame update.
+ */
+ private boolean requestPauseToggle = false;
+
+ /**
* Constructs this robot race by initializing robots, camera, track, and
* terrain.
*/
@@ -292,6 +308,11 @@ public class RobotRace extends Base {
// function), the FPS update is done in the first accessible function
// (here, in setView)
updateFPS();
+ // reset our state when a reset it detected. Must be done before hacking
+ // with time.
+ detectReset();
+ // similarly, reset the time very early here when paused
+ applyPausedTime();
// Select part of window.
gl.glViewport(0, 0, gs.w, gs.h);
@@ -389,19 +410,34 @@ public class RobotRace extends Base {
}
/**
- * Periodically calculate the robot speed based and update robot position.
+ * Detect when the reset button is pressed and act on it.
*/
- private void calculateRobotSpeedAndLocation() {
- double current_t = gs.tAnim;
- // on reset, position the robots on the begin
+ private void detectReset() {
+ float current_t = gs.tAnim;
if (current_t < last_speed_update) {
+ System.err.println("Reset detected...");
+
+ // on reset, position the robots on the begin
last_speed_update = 0;
last_t = 0;
for (Robot robot : robots) {
robot.setSpeed(0);
robot.resetPosition();
}
+
+ // pause in beginning if already paused
+ if (pausedSince != -1) {
+ pausedSince = current_t;
+ }
+ pausedTimeTotal = 0;
}
+ }
+
+ /**
+ * Periodically calculate the robot speed based and update robot position.
+ */
+ private void calculateRobotSpeedAndLocation() {
+ double current_t = gs.tAnim;
// periodically calculate a new speed
double last_speed_update_t_diff = current_t - last_speed_update;
@@ -611,6 +647,27 @@ public class RobotRace extends Base {
System.err.println("Textures are " +
(state ? "enabled" : "disabled"));
return true;
+ case KeyEvent.VK_SPACE: /* pause time */
+ System.err.println("Triggering pause...");
+ robotRace.requestPauseToggle = true;
+ return true;
+ case KeyEvent.VK_O: /* camera mode: Overview */
+ case KeyEvent.VK_H: /* camera mode: Helicopter */
+ case KeyEvent.VK_M: /* camera mode: Motorcycle */
+ if (robotRace.mainWindow != null) {
+ System.err.println("Changing camera to: " + e.getKeyChar());
+ // map from camera mode (array index) to key codes
+ Integer cameraModes[] = new Integer[] {
+ KeyEvent.VK_O, /* 0: default mode (Overview) */
+ KeyEvent.VK_H, /* 1: helicopter */
+ KeyEvent.VK_M, /* 2: Motor cycle */
+ };
+ int i = Arrays.asList(cameraModes).indexOf(e.getKeyCode());
+ assert i != -1 : "Camera mode not found for key";
+ robotRace.gs.camMode = i;
+ robotRace.mainWindow.updateElements();
+ }
+ return true;
default:
return false;
}
@@ -631,4 +688,35 @@ public class RobotRace extends Base {
}
return enableTextures;
}
+
+ /**
+ * If pause toggle is requested, check state and update. Otherwise, adjust
+ * global animation time.
+ */
+ private void applyPausedTime() {
+ if (requestPauseToggle) {
+ if (pausedSince == -1) {
+ // just paused, store start time
+ pausedSince = gs.tAnim;
+ System.err.println("Paused since gs.tAnim=" + gs.tAnim);
+ } else {
+ // continuing, increase paused time and clear pause flag
+ float pausedTime = gs.tAnim - pausedSince;
+ pausedTimeTotal += pausedTime;
+ pausedSince = -1;
+ System.err.println("Continued at gs.tAnim=" + gs.tAnim
+ + ". Paused for " + pausedTime + " secs (new total: "
+ + pausedTimeTotal + " secs)");
+ }
+ requestPauseToggle = false;
+ }
+
+ // if paused, set clock back.
+ if (pausedSince != -1) {
+ gs.tAnim = pausedSince;
+ }
+
+ // always apply correction for time drift
+ gs.tAnim -= pausedTimeTotal;
+ }
}