Skip to content

Commit

Permalink
合并来自UQcsse3200/Team08的拉取请求#49
Browse files Browse the repository at this point in the history
Create SystemClockTimer
  • Loading branch information
ImLeiSu authored Aug 22, 2024
2 parents 8d473a9 + 4848633 commit 4b7375d
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions source/core/src/main/com/csse3200/game/services/SystemClockTimer
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.csse3200.game.services;

public class SystemClockTimer {
private final long durationMillis;
private long startTimeMillis;
private boolean isRunning = false;
private boolean isTriggered = false;

public SystemClockTimer(long durationMillis) {
this.durationMillis = durationMillis;
}

public void start() {
startTimeMillis = System.currentTimeMillis();
isRunning = true;
isTriggered = false;
}

public void update() {
if (isRunning && !isTriggered) {
long currentTimeMillis = System.currentTimeMillis();
if (currentTimeMillis - startTimeMillis >= durationMillis) {
triggerAction();
isTriggered = true;
isRunning = false;
}
}
}

private void triggerAction() {
System.out.println("Timer is over! Triggering action...");
}

public boolean isRunning() {
return isRunning;
}
}

0 comments on commit 4b7375d

Please sign in to comment.