From 42ac511fdb0e9385b4bf612df364ce2d81858645 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Thu, 9 Jan 2014 23:38:11 +0100 Subject: Fix reset detection with pause --- src/RobotRace.java | 26 ++++++++++++++++++++++---- 1 file 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; -- cgit v1.2.1