summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <lekensteyn@gmail.com>2014-01-09 23:38:11 +0100
committerPeter Wu <lekensteyn@gmail.com>2014-01-09 23:38:11 +0100
commit42ac511fdb0e9385b4bf612df364ce2d81858645 (patch)
tree0a86ac1f381ba532194ada997937312ac7baac77
parent0a90b17ca15694ae2c31b5a839490b0cc5caeb2c (diff)
download2iv60-robots-42ac511fdb0e9385b4bf612df364ce2d81858645.tar.gz
Fix reset detection with pause
-rw-r--r--src/RobotRace.java26
1 files changed, 22 insertions, 4 deletions
diff --git a/src/RobotRace.java b/src/RobotRace.java
index d84ce68..f3d6b8a 100644
--- a/src/RobotRace.java
+++ b/src/RobotRace.java
@@ -304,6 +304,9 @@ 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();
@@ -404,19 +407,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;