/// <summary> /// Update the GameTimer and process events /// Called by the TimerManager by defualt /// /// Advanced: With the owner called update model, the owner must call this explicitly in its update /// </summary> public bool Update() { Debug.Assert(this.seconds >= 0.0); double elapsedTime = 0; if (this.clock == ClockType.WallClock) { elapsedTime = Time.WallClockTotalSeconds - startTime; } else { elapsedTime = Time.GameTimeTotalSeconds - startTime; } bool elapsed = elapsedTime >= seconds; if (elapsed) { this.state = State.Elapsed; GameTimerManager.DetachTimer(this); if (TimerElapsed != null) { TimerElapsed(this); } } return(elapsed); }
/// <summary> /// Starts the timer /// /// Advanced: should not be called for owner called update model /// </summary> public void Start() { this.state = State.Running; // If starting for the first time, just grab the current time. startTime = clock == ClockType.WallClock ? Time.WallClockTotalSeconds : Time.GameTimeTotalSeconds; GameTimerManager.AttachTimer(this); }
/// <summary> /// Restarts the timer but resets the start time based on the previous start time. /// This prevents errors from accumulating since timeout values don't sync perfectly /// with frame times. /// </summary> /// <param name="sync">If sync is true, we want to restart the timer in such a way as to take the previous start time into account. If sync is false, we want to just start from "now".</param> public void ReStart(bool sync) { this.state = State.Running; // Need to adjust start time so that it is in range (curTime - seconds, curTime] double curTime = clock == ClockType.WallClock ? Time.WallClockTotalSeconds : Time.GameTimeTotalSeconds; if (sync) { while (startTime <= curTime) { startTime += seconds; } startTime -= seconds; } else { startTime = curTime; } GameTimerManager.AttachTimer(this); }
/// <summary> /// Stops the timer /// /// Advanced: should not be called for owner called update model /// </summary> public void Stop() { this.state = State.Paused; GameTimerManager.DetachTimer(this); }