Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions Equations/src/main/java/gamestatemanager/GameTimeUpdater.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
4 changes: 2 additions & 2 deletions Equations/src/main/java/gamestatemanager/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions Equations/src/test/java/Equations/GameTimeUpdaterTest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Empty file added test-file
Empty file.