diff --git a/Equations/src/main/java/gamestatemanager/GameTimeUpdater.java b/Equations/src/main/java/gamestatemanager/GameTimeUpdater.java new file mode 100644 index 0000000..b358201 --- /dev/null +++ b/Equations/src/main/java/gamestatemanager/GameTimeUpdater.java @@ -0,0 +1,76 @@ +package gamestatemanager; + +import java.util.Timer; +import java.util.TimerTask; +import java.util.function.BooleanSupplier; + +public class GameTimeUpdater { + public static final int SECONDS_PER_TURN = 15; + private static final int SECONDS_TO_MILLISECONDS = 1000; + + // Timers that fire every second and on expiration of the turn + private Timer secondsTimer, expirationTimer; + + // The functions that will be called for these timers + private BooleanSupplier everySecond, onExpiration; + + private boolean hasTimeExpired; + + public boolean isHasTimeExpired() { + return hasTimeExpired; + } + + /** + * This method takes in a function that it will call every second. The function must return a boolean type + * the return should indicate whether or not the timer should keep counting down + * @param everySecond + */ + public GameTimeUpdater setOnSecondsUpdate(BooleanSupplier everySecond) { + this.everySecond = everySecond; + return this; + } + /** + * This method takes in a function that it will call on the expiration of the total time for the game timer. + * @param onExpiration + */ + public GameTimeUpdater setOnExpirationUpdate(BooleanSupplier onExpiration) { + this.onExpiration = onExpiration; + return this; + } + + /** + * This function cancels all of the timer objects, so none of the funcions passed in will be invoked following + * this call + */ + public void cancel() { + secondsTimer.cancel(); + expirationTimer.cancel(); + } + + /** + * Creates the timers and they begin counting down immediately + */ + public void start() { + secondsTimer = new Timer(); + expirationTimer = new Timer(); + + // This is stupid janky... + GameTimeUpdater itself = this; + + secondsTimer.scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + if (!everySecond.getAsBoolean()) { + itself.cancel(); + } + } + },0, SECONDS_TO_MILLISECONDS); + + expirationTimer.schedule(new TimerTask() { + @Override + public void run() { + onExpiration.getAsBoolean(); + } + },SECONDS_PER_TURN * SECONDS_TO_MILLISECONDS); + } +} diff --git a/Equations/src/main/java/gamestatemanager/Manager.java b/Equations/src/main/java/gamestatemanager/Manager.java index 9d23603..78e4615 100644 --- a/Equations/src/main/java/gamestatemanager/Manager.java +++ b/Equations/src/main/java/gamestatemanager/Manager.java @@ -24,7 +24,7 @@ public class Manager implements Manger_Reader //Data members private Solver solver; - private GameTimer timer; + private GameTimeUpdater timer; private Player[] players; private ScriptEngine engine; @@ -52,7 +52,7 @@ public Manager(Player[] p) myForbidden = new Mat(); myPermitted = new Mat(); myRequired = new Mat(); - timer = new GameTimer(1); + timer = new GameTimeUpdater(); engine = new ScriptEngineManager().getEngineByName("js"); players = p; diff --git a/Equations/src/test/java/Equations/GameTimeUpdaterTest.java b/Equations/src/test/java/Equations/GameTimeUpdaterTest.java new file mode 100644 index 0000000..4b0cad6 --- /dev/null +++ b/Equations/src/test/java/Equations/GameTimeUpdaterTest.java @@ -0,0 +1,31 @@ +package Equations; + +import fundementalgamemechanics.Mat; +import fundementalgamemechanics.*; +import gamestatemanager.GameTimeUpdater; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +public class GameTimeUpdaterTest { + private GameTimeUpdater gameTimer; + + @Test + /** + * Not so much a test but just shows how to use the code + */ + public void basicUsage() { + // Arrange + gameTimer = new GameTimeUpdater(); + + //Act + gameTimer.setOnSecondsUpdate(() -> { + System.out.println("This is printed every second"); + // If false is returned the timer cancels itself + return true; + }).setOnExpirationUpdate(() -> { + System.out.println("This is printed when the timer expires"); + return true; + }).start(); + } +} diff --git a/test-file b/test-file new file mode 100644 index 0000000..e69de29