/
Sssensssible.js
372 lines (286 loc) · 10.6 KB
/
Sssensssible.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
// Sssensssible Sssoccer
//
// A Snake version of soccer. Two snakes push a ball around with their heads, trying
// to get it through the goal to score points, dying constantly if they run into walls or each other.
BasicGame.Sssensssible = function (game) {
BasicGame.SnakeBaseGame.call(this,game);
};
BasicGame.Sssensssible.prototype = Object.create(BasicGame.SnakeBaseGame.prototype);
BasicGame.Sssensssible.prototype.constructor = BasicGame.Sssensssible;
// create
//
// Super + sets some basic variables to control where things appear.
BasicGame.Sssensssible.prototype.create = function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],
[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
[0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,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,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,0],
];
BasicGame.SnakeBaseGame.prototype.create.call(this);
this.resetApple();
this.gameTimeTicker = this.game.time.create(false);
this.gameTimeTicker.add(Phaser.Timer.SECOND * this.GAME_TIME_TICK, this.gameTimeTick, this);
this.gameTimeTicker.start();
// Name the state for resetting purposes
this.stateName = "Sssensssible";
};
// createSnake
//
// Super + Creates another snake as the second player
BasicGame.Sssensssible.prototype.createSnake = function() {
BasicGame.SnakeBaseGame.prototype.createSnake.call(this);
this.SNAKE_TWO_START_X = this.SNAKE_START_X;
this.SNAKE_TWO_START_Y = this.WALL_BOTTOM - this.SNAKE_START_Y + this.WALL_TOP + 1;
this.snakeTwo = new Snake(this.game,this.SNAKE_TWO_START_X,this.SNAKE_TWO_START_Y);
};
// createInput
//
// Super + Add controls for the second snake
BasicGame.Sssensssible.prototype.createInput = function () {
BasicGame.SnakeBaseGame.prototype.createInput.call(this);
if (this.game.device.desktop) {
this.wKey = this.game.input.keyboard.addKey(Phaser.Keyboard.W);
this.aKey = this.game.input.keyboard.addKey(Phaser.Keyboard.A);
this.sKey = this.game.input.keyboard.addKey(Phaser.Keyboard.S);
this.dKey = this.game.input.keyboard.addKey(Phaser.Keyboard.D);
this.game.input.keyboard.removeKeyCapture(Phaser.Keyboard.W);
this.game.input.keyboard.removeKeyCapture(Phaser.Keyboard.A);
this.game.input.keyboard.removeKeyCapture(Phaser.Keyboard.S);
this.game.input.keyboard.removeKeyCapture(Phaser.Keyboard.D);
}
else {
// AGAIN, oh no how will this work?
this.swipe = new Swipe(this.game);
this.swipe.diagonalDisabled = true;
}
};
// setScoreText
//
// Override to display the two scores
BasicGame.Sssensssible.prototype.setScoreText = function () {
var scoreString = this.score + " - " + this.scoreTwo;
if (scoreString.length < this.MAX_SCORE.toString().length) {
var spacesToAdd = (this.MAX_SCORE.toString().length - scoreString.length)+1;
scoreString = Array(spacesToAdd).join(" ") + scoreString;
}
this.addTextToGrid(this.scoreX-scoreString.length,this.scoreY,[scoreString]);
};
// createControls
//
// Super + Display controls for the second snake
BasicGame.Sssensssible.prototype.createControls = function () {
BasicGame.SnakeBaseGame.prototype.createControls.call(this);
var controlsStrings2 = [];
if (this.game.device.desktop) {
controlsStrings = ["WASD","CONTROLS","SNAKE"];
}
else {
controlsStrings = ["SWIPES","CONTROL","SNAKE"];
}
this.controlsGroupTwo = this.game.add.group();
this.addTextToGrid(this.CONTROLS_TWO_X,this.CONTROLS_TWO_Y,controlsStrings,this.controlsGroupTwo);
},
// update
//
// Just super, but you never know...
BasicGame.Sssensssible.prototype.update = function () {
BasicGame.SnakeBaseGame.prototype.update.call(this);
};
// gameTimeTick
//
// Handles how fast the clock moves
BasicGame.Sssensssible.prototype.gameTimeTick = function () {
this.gameTimeTicker.add(Phaser.Timer.SECOND * this.GAME_TIME_TICK, this.gameTimeTick, this);
this.gameTimeElapsed++;
this.updateGameTimeText();
};
// tick
//
// Complete override. Does mostly the same things, but ticks the second snake too
BasicGame.Sssensssible.prototype.tick = function () {
ticker.add(Phaser.Timer.SECOND * this.SNAKE_TICK, this.tick, this);
this.snake.tick();
this.snakeTwo.tick();
this.checkAppleCollision();
this.checkBodyCollision();
this.checkWallCollision();
};
BasicGame.Sssensssible.prototype.updateGameTimeText = function () {
var m = Math.floor(this.gameTimeElapsed/60);
if (m < 10) m = "0" + m;
var s = (this.gameTimeElapsed - m*60);
if (s < 10) s = "0" + s;
this.addTextToGrid(1,this.scoreY,[m + ":" + s]);
};
// checkWallCollission
//
// Complete override because we need to handle one snake being dead but
// not the other
BasicGame.Sssensssible.prototype.checkWallCollision = function () {
this.wallGroup.forEach(function (wall) {
if (!this.snake.dead && this.snake.head.position.equals(wall.position)) {
this.snake.die();
}
if (!this.snakeTwo.dead && this.snakeTwo.head.position.equals(wall.position)) {
this.snakeTwo.die();
}
if (this.snake.dead && this.snakeTwo.dead) {
return;
}
},this);
}
// checkBodyCollision
//
// Complete override to handle one dead snake out of two, and to check both
// auto-collision and other-collision
BasicGame.Sssensssible.prototype.checkBodyCollision = function () {
this.snake.forEach(function (bit) {
if (!this.snake.dead && this.snake.head.position.equals(bit.position) && bit != this.snake.head) {
this.snake.die();
}
if (!this.snakeTwo.dead && this.snakeTwo.head.position.equals(bit.position)) {
this.snakeTwo.die();
}
if (this.snake.dead && this.snakeTwo.dead) {
return;
}
},this);
this.snakeTwo.forEach(function (bit) {
if (!this.snakeTwo.dead && this.snakeTwo.head.position.equals(bit.position) && bit != this.snakeTwo.head) {
this.snakeTwo.die();
}
if (!this.snake.dead && this.snake.head.position.equals(bit.position)) {
this.snake.die();
}
if (this.snake.dead && this.snakeTwo.dead) {
return;
}
},this);
};
BasicGame.Sssensssible.prototype.resetApple = function () {
this.apple.x = this.SNAKE_START_X * GRID_SIZE;
this.apple.y = 16 * GRID_SIZE;
};
BasicGame.Sssensssible.prototype.resetSnakes = function () {
this.snake.reset();
this.snakeTwo.reset();
};
// checkAppleCollision
//
// This implements the idea of dribbling the ball in the direction of the snake hitting it
BasicGame.Sssensssible.prototype.checkAppleCollision = function () {
console.log(this.score,this.scoreTwo);
if (this.snake.head.position.equals(this.apple.position)) {
this.appleSFX.play();
this.apple.position.x += this.snake.next.x;
this.apple.position.y += this.snake.next.y;
}
if (this.snakeTwo.head.position.equals(this.apple.position)) {
this.appleSFX.play();
this.apple.position.x += this.snakeTwo.next.x;
this.apple.position.y += this.snakeTwo.next.y;
}
if (this.apple.position.y < this.WALL_TOP * GRID_SIZE) {
this.scoreTwo++;
this.snake.SNAKE_START_LENGTH += this.EXTRA_BODY_PIECES_PER_GOAL;
this.snakeTwo.SNAKE_START_LENGTH += this.EXTRA_BODY_PIECES_PER_GOAL;
this.setScoreText("");
this.resetApple();
this.resetSnakes();
}
else if (this.apple.position.y > this.WALL_BOTTOM * GRID_SIZE) {
this.score++;
this.snake.SNAKE_START_LENGTH += this.EXTRA_BODY_PIECES_PER_GOAL;
this.snakeTwo.SNAKE_START_LENGTH += this.EXTRA_BODY_PIECES_PER_GOAL;
this.setScoreText("");
this.resetApple();
this.resetSnakes();
}
};
// gameOver
//
// Complete override
BasicGame.Sssensssible.prototype.gameOver = function () {
};
// repositionApple
//
// We don't do this, so override to cancel
BasicGame.Sssensssible.prototype.repositionApple = function () {
// We don't reposition the apple with the usual algorithm
// because it's based on the data about the colossus
};
// startAppleTimer
//
// We don't do this, so override to cancel
BasicGame.Sssensssible.prototype.startAppleTimer = function () {
};
// hideControlsTwo
//
// Handle hiding the second set of control instructions
BasicGame.Sssensssible.prototype.hideControlsTwo = function () {
if (this.snakeTwo.next.x == 0 && this.snakeTwo.next.y == 0) {
this.controlsGroupTwo.forEach(function (letter) {
letter.text = '';
});
this.controlsGroupTwo.visible = false;
}
};
// handleKeyboardInput
//
// Handle the second snake's keyboard controls
BasicGame.Sssensssible.prototype.handleKeyboardInput = function () {
BasicGame.SnakeBaseGame.prototype.handleKeyboardInput.call(this);
if (this.snakeTwo.dead) return;
if (!this.inputEnabled) return;
if (this.controlsGroupTwo.visible && (this.wKey.isDown || this.aKey.isDown || this.sKey.isDown || this.dKey.isDown)) {
this.hideControlsTwo();
}
// Check which key is down and set the next direction appropriately
if (this.aKey.isDown) {
this.snakeTwo.moveLeft();
}
else if (this.dKey.isDown) {
this.snakeTwo.moveRight();
}
if (this.wKey.isDown) {
this.snakeTwo.moveUp();
}
else if (this.sKey.isDown) {
this.snakeTwo.moveDown();
}
};