-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgame_state.js
More file actions
124 lines (99 loc) · 3.62 KB
/
game_state.js
File metadata and controls
124 lines (99 loc) · 3.62 KB
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
// *** game_state ***
// Interface for all game states
// NOTE ALL OF THE FOLLOWING WHEN CREATING A GAME STATE:
// All game states must take a previous_state in their constructor, and then call set_previous_state()
// The exception to this is splash, which is the base state and should never be exited
// If a state change happens in:
// update() - return the new state
// mouse_click/key_pressed: Call set_next_state(new_state) with the state to go to
// Buttons:
// Drawing and clicking of buttons is taken care of
// You must take care of key strokes
var game_state = function (p) {
// object to return
var obj = {};
// --- private variables ---
var next_state = null;
var previous_state = null;
var all_buttons = []; //TODO: This is static but is updated every update()
// --- public methods ---
//Wrapper for every states render function
//First draws all the buttons, then calls render
obj.render_wrapper = function() {
obj.render();
for_each(all_buttons, function(b) { b.draw(); });
};
//Do not over-ride these methods
obj.set_next_state = function(ns) {
next_state = ns;
};
obj.set_previous_state = function(ps) {
previous_state = ps;
};
obj.exit_state = function() {
if (previous_state === null) {
throw "error previous state not set in game_state";
}
assert(previous_state !== null, "Previous state was null in game_state.exit_state");
next_state = previous_state;
};
// Wrapper for every states update function
// Goes to the next state (by returning it) if next_state has been set
obj.update_wrapper = function() {
all_buttons = obj.get_all_buttons();
if (next_state !== null) {
var to_return = next_state;
//in case we return to this state (CURRENTLY, SHOULDN'T EVER HAPPEN I THINK)
next_state = null;
return to_return;
}
//else
obj.update();
obj.render_wrapper();
};
// Wrapper for mouse clicks
// First checks if any of the buttons are clicked, then calls mouse_click
obj.mouse_click_wrapper = function(x, y) {
var click_function = function(b) {
var next_state = b.is_clicked(x, y);
if (next_state !== null) {
obj.set_next_state(next_state);
}
}
for_each(all_buttons, click_function);
obj.mouse_click(x, y);
};
obj.mouse_moved_wrapper = function(x, y) {
for_each(
all_buttons,
function(b) {
b.mouse_moved(x, y);
}
);
obj.mouse_moved(x, y);
};
// all game_states must implement the following functions:
//Returns the type of the state
obj.get_type = function() {
return "get_type not overwritten";
};
//Takes in the key pressed and handles a key press
obj.key_pressed = function(k) {throw "key_pressed not overwritten"};
// Do nothing unless overwritten
obj.mouse_moved = function(x, y) {};
//Updates the current state, and returns the next state to go to
//next state can be:
// null, if no state change
// a state representing a state to go to (either previous state or newly created next state
obj.update = function() {throw "update not overwritten"};
// Returns all of the buttons on the screen
obj.get_all_buttons = function() {throw "get_all_buttons not overwritten"};
// --- Optionally implement these, they default to nothing ---
//Renders the current state
obj.render = function(){ }; //throw "render not overwritten"};
//Takes in the x and y coordinates of the mouse, and handles a mouse click
obj.mouse_click = function(x, y){}; //throw "mouse_click not overwritten"};
//Called when returning to this state
obj.resume = function() {};
return obj;
};