summaryrefslogtreecommitdiffstats
path: root/src/subconscious/Game.java
blob: 5c3d7d03ae7e1874da59eada1acb7c244bce1240 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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<Actor> actors = new ArrayList<>();
    private ArrayList<Map> 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; }
}