-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
255 lines (220 loc) · 9.55 KB
/
game.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
const GAME_CONFIG = {
FPS : 15,
SCREEN_WIDTH : 360,
SCREEN_HEIGHT : 360,
FIELD_IMAGES : "images/map0.png",
FIELD_WIDTH : 16,
FIELD_HEIGHT : 16,
PLAYER_IMAGE : "images/garbage_collector.png",
PLAYER_WIDTH : 32,
PLAYER_HEIGHT : 32,
GARBAGE_IMAGE : "images/garbage.png",
GARBAGE_WIDTH : 32,
GARBAGE_HEIGHT : 32,
COUNTDOWN_SEC : 3,
TIME_LIMIT : 60,
SCORE_POINTS : 10
};
const PRELOAD_IMAGES = [
GAME_CONFIG.FIELD_IMAGES,
GAME_CONFIG.PLAYER_IMAGE,
GAME_CONFIG.GARBAGE_IMAGE
];
const GARBAGE_PROBABILITY = 1 / (GAME_CONFIG.FPS * 5);
enchant();
window.onload = function() {
var game = new Game(GAME_CONFIG.SCREEN_WIDTH, GAME_CONFIG.SCREEN_HEIGHT);
game.preload(PRELOAD_IMAGES);
game.frame = GAME_CONFIG.FPS;
game.onload = function() {
game.pushScene(generateStartScene());
}
game.start();
function generateStartScene() {
var scene = new Scene();
var label = new Label("Press space key!");
label.font = "bold 16pt monospace";
label.x = (scene.width - label._boundWidth) / 2;
label.y = (scene.height - label._boundHeight) / 2;
scene.addChild(label);
game.keybind(32, "space");
game.addEventListener("spacebuttondown", function() {
scene = null;
game.clearEventListener("spacebuttondown");
game.replaceScene(generateCountdownScene());
});
return scene;
}
function generateCountdownScene() {
const start_frame = game.frame;
var scene = new Scene();
var label = new Label(GAME_CONFIG.COUNTDOWN_SEC);
label.font = "bold 16pt monospace";
label.x = (scene.width - label._boundWidth) / 2;
label.y = (scene.height - label._boundHeight) / 2;
scene.addChild(label);
game.score = 0;
game.addEventListener(enchant.Event.ENTER_FRAME, function() {
var progress = (game.frame - start_frame) / game.fps;
if(progress < GAME_CONFIG.COUNTDOWN_SEC) {
label.text = Math.ceil(GAME_CONFIG.COUNTDOWN_SEC - progress);
} else {
scene = null;
game.clearEventListener(enchant.Event.ENTER_FRAME);
game.replaceScene(generateGameScene());
}
});
return scene;
}
function generateGameScene() {
const start_frame = game.frame;
const field_map = [
[ 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0],
[ 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0],
[ 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0],
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
[ 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0],
[ 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0],
[ 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0],
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
[ 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0],
[ 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0],
[ 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0],
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
[ 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0],
[ 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0],
[ 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 0],
];
const collision_map = [
[1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1],
];
var scene = new Scene();
var remain_label = new Label(`${GAME_CONFIG.TIME_LIMIT}s remains.`);
remain_label.font = "bold 12pt monospace";
remain_label.moveTo(20, 20);
var result_label = new Label(`${game.score} points`);
result_label.font = "bold 14pt monospace";
result_label.color = "#0000ff";
result_label.x = scene.width - result_label._boundWidth - 20;
result_label.y = 10;
var field = new Map(GAME_CONFIG.FIELD_WIDTH, GAME_CONFIG.FIELD_HEIGHT);
field.image = game.assets[GAME_CONFIG.FIELD_IMAGES];
field.loadData(field_map);
field.collisionData = collision_map;
field.x = (scene.width - field.width) / 2;
field.y = (scene.height - field.height) / 2;
var collector = new Sprite(GAME_CONFIG.PLAYER_WIDTH, GAME_CONFIG.PLAYER_HEIGHT);
collector.image = game.assets[GAME_CONFIG.PLAYER_IMAGE];
collector.x = field.x + GAME_CONFIG.FIELD_WIDTH * 3;
collector.y = field.y + GAME_CONFIG.FIELD_HEIGHT * 3;
var garbages = [];
scene.addChild(remain_label);
scene.addChild(result_label);
scene.addChild(field);
scene.addChild(collector);
game.addEventListener(enchant.Event.ENTER_FRAME, function() {
var progress = (game.frame - start_frame) / game.fps;
if(progress < GAME_CONFIG.TIME_LIMIT) {
var remain = Math.floor(GAME_CONFIG.TIME_LIMIT - progress);
remain_label.text = `${remain}s remains.`;
if(Math.random() < GARBAGE_PROBABILITY) {
do {
var x = field.x + (field.tileWidth * Math.floor(Math.random() * (field_map[0].length - 1)));
var y = field.y + (field.tileHeight * Math.floor(Math.random() * (field_map.length - 1)));
} while(field.hitTest(x - field.x, y - field.y)
|| field.hitTest(x + GAME_CONFIG.GARBAGE_WIDTH - field.x - 1, y + GAME_CONFIG.GARBAGE_HEIGHT - field.y - 1));
var garbage = new Sprite(GAME_CONFIG.GARBAGE_WIDTH, GAME_CONFIG.GARBAGE_HEIGHT);
garbage.image = game.assets[GAME_CONFIG.GARBAGE_IMAGE];
garbage.moveTo(x, y);
garbages.push(garbage);
scene.addChild(garbage);
}
} else {
scene = null;
game.clearEventListener(enchant.Event.ENTER_FRAME);
game.replaceScene(generateResultScene());
}
if(game.input.left) {
var left = collector.x - field.tileWidth;
if(left >= field.x
&& !field.hitTest(left - field.x, collector.y - field.y)
&& !field.hitTest(left - field.x, collector.y + collector.height - field.y - 1)) {
collector.x = left;
}
}
if(game.input.right) {
var right = collector.x + field.tileWidth;
if(right <= field.x + field.width - collector.width
&& !field.hitTest(right + collector.width - field.x, collector.y - field.y)
&& !field.hitTest(right + collector.width - field.x, collector.y + collector.height - field.y - 1)) {
collector.x = right;
}
}
if(game.input.up) {
var up = collector.y - field.tileHeight;
if(up >= field.y
&& !field.hitTest(collector.x - field.x, up - field.y)
&& !field.hitTest(collector.x + collector.width - field.x - 1, up - field.y)) {
collector.y = up;
}
}
if(game.input.down) {
var down = collector.y + field.tileHeight;
if(down <= field.y + field.height - collector.height
&& !field.hitTest(collector.x - field.x, down + collector.height - field.y)
&& !field.hitTest(collector.x + collector.width - field.x - 1, down + collector.height - field.y)) {
collector.y = down;
}
}
for(var i in garbages) {
if(collector.intersect(garbages[i])) {
scene.removeChild(garbages[i]);
garbages[i] = null;
garbages.splice(i, 1);
game.score += GAME_CONFIG.SCORE_POINTS;
result_label.text = `${game.score} points`;
}
}
});
return scene;
}
function generateResultScene() {
var scene = new Scene();
var result_label = new Label(`result: ${game.score} points`);
result_label.font = "bold 16pt monospace";
result_label.x = (scene.width - result_label._boundWidth) / 2;
result_label.y = (scene.height - result_label._boundHeight) / 2;
var retry_label = new Label("Press space to retry.");
retry_label.font = "normal 14pt monospace";
retry_label.x = (scene.width - result_label._boundWidth) / 2;
retry_label.y = result_label.y + result_label._boundHeight + 30;
scene.addChild(result_label);
scene.addChild(retry_label);
game.addEventListener("spacebuttondown", function() {
scene = null;
game.clearEventListener("spacebuttondown");
game.replaceScene(generateCountdownScene());
});
return scene;
}
}