-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathplay.ts
315 lines (266 loc) · 5.99 KB
/
play.ts
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
import { $, setVisible, compressActions, parseActions } from './src/helper.js';
import { Stage, Player } from './src/stage.js';
import { Viewport } from './src/types.js';
import render from './src/renderer.js';
/*
*
*
*
* DOM ELEMENTS
*
*
*
*/
const _scoreEl = $('#score');
const _topScoreEl = $('#topscore');
const _twitterEl = $('#twitter') as HTMLAnchorElement;
const _gameOverEl = $('#gameover');
const _splashEl = $('#splash');
const _lvlUpEl = $('#lvlUp');
const _highscoresShow = $('#highscoresShow');
// canvas
const _canvasEl = $('#canvas') as HTMLCanvasElement;
/*
*
*
*
* VARIABLES
*
*
*
*/
// game variables
let withSplash = true;
let topScore = 0;
let stage: Stage;
let viewport: Viewport;
let seed = 0;
const twitterMsg = 'Just scored {score} on @GwoekGame! Challenge me now: https://bbaliguet.github.io/Gwoek#{seed}_{score}_{actions} #Gwoek';
// highscores
const highscores = {};
interface Score {
external: boolean;
actions: Array<number>;
score: number;
}
/*
*
*
*
* GAME LOOP
*
*
*
*/
function showGameOver(score: number): void {
if (score > topScore) {
topScore = score;
_topScoreEl.innerHTML = 'Top score: ' + topScore;
}
const actions = stage.game.actions;
// save to highscores
if (!highscores[seed]) {
highscores[seed] = [];
}
const seedHighScores = highscores[seed];
// push new score
seedHighScores.push({
actions, score, external: false
});
seedHighScores.sort((a: Score, b: Score) => (a.external ? 1 : b.external ? -1 : b.score - a.score));
if (seedHighScores.length > 6) {
seedHighScores.length = 6;
}
setVisible(_gameOverEl, true);
_lvlUpEl.classList.remove('show');
document.body.classList.add('gameover');
_twitterEl.href = 'https://twitter.com/intent/tweet?text=' +
encodeURIComponent(twitterMsg
.replace(/\{score\}/g, String(score))
.replace(/\{seed\}/g, String(seed))
.replace(/\{actions\}/g, compressActions(actions))
);
}
function loop(): void {
const clock = stage.clock;
const game = stage.game;
const chunk = 10;
let total = clock.total();
let dif = clock.tick(chunk);
const updateGhost = function(player: Player): void {
if (!player.ghost) {
return;
}
if (player.actions.indexOf(total) != -1) {
player.action = true;
}
};
if (dif > 2000) {
game.gameOver = true;
}
// update the environment
do {
if (dif) {
if (!game.gameOver) {
total += chunk;
game.total = total;
// update ghosts
stage.players.forEach(updateGhost);
stage.update(chunk, viewport);
// lvl up ?
if (total > game.nextLevel) {
game.levelUp();
}
}
if (game.gameOver) {
showGameOver(total);
return;
}
}
dif = dif - chunk;
if (dif < 0) {
dif = 0;
}
} while (dif);
// update the display
render(stage, {
viewport,
canvas: _canvasEl
});
if (game.lvlUp) {
game.lvlUp = false;
_lvlUpEl.classList.add('show');
setTimeout(function() {
_lvlUpEl.classList.remove('show');
}, 1500);
}
if (game.changeColor) {
game.changeColor = false;
stage.colorize();
}
_scoreEl.innerHTML = 'score: ' + Math.floor(total);
requestAnimationFrame(loop);
}
/*
*
*
*
* GAME STATES
*
*
*
*/
function init(): void {
document.body.classList.remove('gameover');
setVisible(_gameOverEl, false);
setVisible(_splashEl, false);
// init viewport
viewport = document.body.getBoundingClientRect();
// copy into new object so it can be modified
viewport = {
width: viewport.width,
height: viewport.height
};
if (viewport.height < 500) {
viewport.height = viewport.height * 2;
viewport.width = viewport.width * 2;
}
// init rand method. Use seed if provided
const [hashSeed, hashScore, hashActions] = window.location.hash.substr(1).split('_');
const hash = parseInt(hashSeed, 10);
seed = isNaN(hash) ? Math.floor(Math.random() * 100000000) : hash;
const actions = parseActions(hashActions || '');
if (actions && actions.length) {
highscores[seed] = [{
actions, score: parseInt(hashScore, 10), external: true
}]
}
console.log('Gwoek playing with seed ' + seed);
if (isNaN(hash)) {
window.location.hash = '#' + seed;
}
stage = new Stage(viewport, seed);
// insert ghost
if (highscores[seed]) {
highscores[seed].forEach((score: Score) => {
const player = new Player();
player.actions = score.actions;
player.ghost = true;
stage.players.push(player);
});
}
// init canvas
_canvasEl.width = viewport.width;
_canvasEl.height = viewport.height;
loop();
}
function showHighScores(): void {
const trackHighscores = highscores[seed] || [];
trackHighscores.forEach(function(result, index) {
const line = $(`#trackscores tr:nth-child(${index + 1})`);
if (!line) {
return;
}
line.innerHTML = `<td class="${result.external ? 'external' : ''}">${result.score}</td>`;
});
setVisible(_gameOverEl, false);
setVisible(_highscoresShow, true);
}
function hideHighScores(): void {
setVisible(_gameOverEl, true);
setVisible(_highscoresShow, false);
}
/*
*
*
*
* ACTION HANDLERS
*
*
*
*/
function onAction(): void {
if (!stage || stage.game.gameOver) {
if (withSplash) {
withSplash = false;
init();
}
return;
}
const player = stage.players[0];
player.action = true;
}
function bindAction(element, listener): void {
const wrapper = function(event): void {
event.preventDefault();
listener();
};
element.addEventListener('click', wrapper);
element.addEventListener('touchstart', wrapper);
}
document.addEventListener('DOMContentLoaded', function() {
// event listeners
document.addEventListener('keydown', onAction);
document.addEventListener('touchstart', function(e) {
e.preventDefault();
onAction();
});
// update on resize
window.addEventListener('resize', function() {
if (!withSplash) {
init();
}
});
// retry and try new
function onRetry(): void {
init();
}
function onTryNew(): void {
window.location.hash = '';
init();
}
bindAction($('#retry'), onRetry);
bindAction($('#trynew'), onTryNew);
bindAction($('#highscores'), showHighScores);
bindAction($('#highscoresShow'), hideHighScores);
});