This repository has been archived by the owner on Jan 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
412 lines (398 loc) · 11.4 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
'use strict'; /*jslint node:true*/
const UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3;
function cw(dir){ return (dir+1) % 4; }
function ccw(dir){ return (dir+3) % 4; }
class Point {
constructor(x, y){
this.x = x;
this.y = y;
Object.freeze(this);
}
up(){ return new Point(this.x, this.y-1); }
right(){ return new Point(this.x+1, this.y); }
down(){ return new Point(this.x, this.y+1); }
left(){ return new Point(this.x-1, this.y); }
step(dir){
switch (dir)
{
case UP: return this.up();
case RIGHT: return this.right();
case DOWN: return this.down();
case LEFT: return this.left();
}
}
}
class Thing { // it would be a bad idea to name a class Object :-)
constructor(world){
this.world = world;
this.point = undefined;
this.mark = world.frame;
}
place(point){ this.point = point; }
move(to){
if (this.point)
this.world.set(this.point);
if (to)
this.world.set(to, this);
}
update(){ this.mark = this.world.frame; }
get_char(){}
get_color(){}
is_rounded(){ return false; } // objects roll off it?
is_consumable(){ return false; } // consumed by explosions?
is_settled(){ return true; } // no need to postpone game-over?
hit(){} // hit by explosion or falling object
walk_into(dir){ return false; } // can walk into?
}
class SteelWall extends Thing {
get_char(){ return '#'; }
get_color(){ return '37;46'; } // white on cyan
}
class BrickWall extends Thing {
get_char(){ return '+'; }
get_color(){ return '30;41'; } // black on red
is_rounded(){ return true; }
is_consumable(){ return true; }
}
class Dirt extends Thing {
get_char(){ return ':'; }
get_color(){ return '37'; } // white on black
is_consumable(){ return true; }
walk_into(dir){ return true; }
}
class LooseThing extends Thing { // an object affected by gravity
constructor(world){
super(world);
this.falling = false;
}
update(){
super.update();
let under = this.point.down();
let target = this.world.get(under);
if (target && target.is_rounded())
{
if (this.roll(this.point.left()) || this.roll(this.point.right()))
return;
}
if (target && this.falling)
{
target.hit();
this.falling = false;
}
else if (!target)
{
this.falling = true;
this.move(under);
}
}
roll(to){
if (this.world.get(to) || this.world.get(to.down()))
return false;
this.falling = true;
this.move(to);
return true;
}
is_rounded(){ return !this.falling; }
is_consumable(){ return true; }
is_settled(){ return !this.falling; }
}
class Boulder extends LooseThing {
get_char(){ return 'O'; }
get_color(){ return '1;34'; } // bright blue on black
walk_into(dir){
if (this.falling || dir==UP || dir==DOWN)
return false;
let to = this.point.step(dir);
if (!this.world.get(to))
{
this.move(to);
return true;
}
return false;
}
}
class Diamond extends LooseThing {
get_char(){ return '*'; }
get_color(){ return '1;33'; } // bright yellow on black
walk_into(dir){
this.world.diamond_collected();
return true;
}
}
class Explosion extends Thing {
constructor(world){
super(world);
this.stage = 0;
}
get_char(){ return '*'; }
get_color(){ return ['37;47', '1;31;47', '1;31;43', '1;37'][this.stage]; }
update(){
if (++this.stage>3)
this.world.set(this.point, new Diamond(this.world));
}
is_settled(){ return false; }
}
class Butterfly extends Thing {
constructor(world){
super(world);
this.dir = UP;
this.alive = true;
}
get_char(){ return '/|\\-'[this.world.frame%4]; }
get_color(){ return '1;35'; } // bright magenta on black
update(){
super.update();
let points = new Array(4);
for (let i = 0; i<4; i++)
points[i] = this.point.step(i);
let neighbors = points.map(p=>this.world.get(p));
let locked = true;
for (let neighbor of neighbors)
{
if (!neighbor)
locked = false;
else if (neighbor===this.world.player)
return this.explode();
}
if (locked)
return this.explode();
let left = ccw(this.dir);
if (!neighbors[left])
{
this.move(points[left]);
this.dir = left;
}
else if (!neighbors[this.dir])
this.move(points[this.dir]);
else
this.dir = cw(this.dir);
}
is_consumable(){ return true; }
hit(){
if (this.alive)
this.explode();
}
explode(){
this.alive = false;
let x1 = this.point.x-1, x2 = this.point.x+1;
let y1 = this.point.y-1, y2 = this.point.y+1;
for (let y = y1; y<=y2; y++)
{
for (let x = x1; x<=x2; x++)
{
let point = new Point(x, y);
let target = this.world.get(point);
if (target)
{
if (!target.is_consumable())
continue;
if (target!==this)
target.hit();
}
this.world.set(point, new Explosion(this.world));
}
}
this.world.butterfly_killed();
}
}
class Player extends Thing {
constructor(world){
super(world);
this.alive = true;
this.control = undefined;
}
get_char(){ return this.alive ? 'A' : 'X'; }
get_color(){
if (this.world.frame<24 && (this.world.frame%4 < 2))
return '30;42';
return '1;32'; // bright green on black
}
update(){
super.update();
if (!this.alive || this.control===undefined)
return;
let to = this.point.step(this.control);
let target = this.world.get(to);
if (!target || target.walk_into(this.control))
this.move(to);
this.control = undefined;
}
is_consumable(){ return true; }
hit(){ this.alive = false; }
}
class World {
constructor(w, h, {frames, fps}){
this.width = w;
this.height = h;
this.frame = 0;
this.frames_left = frames;
this.fps = fps||10;
this.settled = false;
this.player = new Player(this);
this.score = 0;
this.streak = 0;
this.streak_expiry = 0;
this.streak_message = '';
this.streaks = 0;
this.longest_streak = 0;
this.diamonds_collected = 0;
this.butterflies_killed = 0;
this.cells = new Array(h);
for (let y = 0; y<h; y++)
this.cells[y] = new Array(w);
}
*[Symbol.iterator](){
for (let y = 0; y<this.height; y++)
{
let row = this.cells[y];
for (let x = 0; x<this.width; x++)
yield [new Point(x, y), row[x]];
}
}
get(point){ return this.cells[point.y][point.x]; }
set(point, thing){
let old = this.cells[point.y][point.x];
if (old===thing)
return;
if (old)
old.place();
this.cells[point.y][point.x] = thing;
if (thing)
thing.place(point);
}
diamond_collected(){
this.score++;
this.diamonds_collected++;
this.streak++;
this.streak_expiry = 20;
this.scored_expiry = 8;
if (this.streak<3)
return;
if (this.streak==3)
this.streaks++;
if (this.longest_streak<this.streak)
this.longest_streak = this.streak;
for (let i = 2; i*i<=this.streak; i++)
{
if (this.streak%i==0)
return;
}
// streak is a prime number
this.streak_message = `${this.streak}x HOT STREAK!`;
this.score += this.streak;
}
butterfly_killed(){
if (!this.player.alive) // no reward if player killed
return;
this.butterflies_killed++;
this.score += 10;
this.scored_expiry = 8;
}
leftpad(n, len){
let res = n.toString();
return res.length<len ? '0'.repeat(len-res.length)+res : res;
}
render(ansi, with_status){
let res = this.cells.map(row=>{
let res = '', last_color;
for (let cell of row)
{
if (ansi)
{
let color = cell ? cell.get_color() : '37';
if (last_color!=color)
{
res += `\x1b[0;${color}m`; // set color
last_color = color;
}
}
res += cell ? cell.get_char() : ' ';
}
return res;
});
if (with_status)
{
let status = '';
if (ansi)
{
status += '\x1b[0m'; // reset color
if (this.frames_left>200
|| (this.frames_left<50 && this.frames_left%2))
{
status += '\x1b[37m'; // white
}
else
status += '\x1b[31m'; // red
}
status += ' ';
status += this.leftpad(Math.ceil(this.frames_left/this.fps), 4);
if (ansi)
{
if (this.scored_expiry%2)
status += '\x1b[32m'; // green
else
status += '\x1b[37m'; // white
}
status += ' ';
status += this.leftpad(this.score, 6);
if (this.streak_message)
{
if (ansi)
{
if (this.streak_expiry>6 || this.streak_expiry%2!=0)
status += '\x1b[1;31m'; // bright red
else
status += '\x1b[1;30m'; // gray
}
status += ` ${this.streak_message}`;
}
if (ansi)
status += '\x1b[K'; // clear from cursor to end of line
else if (status.length<this.width)
status += ' '.repeat(this.width-status.length);
res.push(status);
}
return res;
}
update(){
this.frame++;
if (this.frames_left)
this.frames_left--;
if (this.streak && !--this.streak_expiry)
{
this.streak = 0;
this.streak_message = '';
}
if (this.scored_expiry)
this.scored_expiry--;
this.settled = !this.streak_message;
for (let [point, thing] of this)
{
if (!thing)
continue;
if (thing.mark<this.frame)
thing.update();
if (!thing.is_settled())
this.settled = false;
}
if (!this.frames_left)
this.player.alive = false;
}
control(c){ this.player.control = c; }
is_playable(){ return this.player.alive; }
is_final(){ return !this.player.alive && this.settled; }
}
module.exports = {
UP,
RIGHT,
DOWN,
LEFT,
Point,
SteelWall,
BrickWall,
Dirt,
Boulder,
Diamond,
Butterfly,
World,
};