package subconscious; import java.util.ArrayList; import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Condition; /* Game * Contains informations about the current state of the game used in an * Obervable Object pattern. The Game Loop is managed in the graphics. */ public class Game { public enum State { // main menu MAIN_MENU, // where actors can move freely WORLD, // where actors are constrained in the tiles BATTLE, // intermediate states PAUSE, CLOSING, } private State state = State.MAIN_MENU; private State lastState; // private final Lock stateLock = new ReentrantLock(); // private Condition stateChanged; private boolean stateChanged; private boolean running = true; private boolean gameOver = false; private ArrayList actors = new ArrayList<>(); private ArrayList maps = new ArrayList<>(); private Map currentMap; // TODO: load audio? private MapLoader mapLoader = new MapLoader(); public Game() { // set up stateLock // stateChanged = stateLock.newCondition(); // TODO: this will be replaced with a dynamic mechanism based // on the progress within the game Map testMap = this.mapLoader.parse("../res/maps/testmap.json"); this.currentMap = testMap; this.maps.add(testMap); } public void start() { // TODO: change this.setState(State.BATTLE); } public void update() { } /* methods to manage the state */ public void setState(State state) { // stateLock.lock(); // this.stateChanged.signal(); // stateLock.unlock(); this.stateChanged = true; this.lastState = this.state; this.state = state; } public State getState() { this.stateChanged = false; return this.state; } public State getLastState() { return this.lastState; } public boolean stateChanged() { return this.stateChanged; } // public State waitStateChange() { // stateLock.lock(); // try { // stateChanged.await(); // } catch (InterruptedException ex) { // ex.printStackTrace(); // } finally { // stateLock.unlock(); // } // return this.state; // } // public void pause() { // if (this.state == State.PAUSE) // return; // this.setState(State.PAUSE); // } // public void resume() { // this.setState(lastState); // } // public boolean isPaused() { // return this.state == State.PAUSE; // } /* methods to manage map and map loading */ public Map getMap() { return currentMap; } /* accessors */ public boolean isRunning() { return running; } public void quit() { // TODO: change to MAIN_MENU? this.setState(State.CLOSING); this.running = false; } public boolean isGameOver() { return gameOver; } public void gameOver() { this.gameOver = true; } }