-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
394 lines (348 loc) · 12.5 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>
</head>
<body style="margin: 0; padding: 0;">
<script>
const touchDevice = (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
const GAMEWIDTH = window.innerWidth;
const GAMEHEIGHT = window.innerHeight;
const FONT = "Arial";
const FONTSCALE = (GAMEWIDTH > GAMEHEIGHT) ? 1 : 1.5;
const BOOKHEIGHT = 4 * 22;
const BOOKWIDTH = 4 * 100;
const BOOKSCALE = (GAMEWIDTH > GAMEHEIGHT) ? 0.5 : 0.85;
const OWLSCALE = (GAMEWIDTH > GAMEHEIGHT) ? 0.1 : 0.2;
const CALCULATEDBOOKHEIGHT = BOOKHEIGHT * BOOKSCALE;
const CALCULATEDBOOKWIDTH = BOOKWIDTH * BOOKSCALE;
const ACCERELATION = 0.003;
const ACCERELATIONCAP = 0.01;
const owlDownwardAcc = 0.4;
const owlFlapIncrease = 120 * owlDownwardAcc;
const pipeWidth = CALCULATEDBOOKWIDTH;
const showPipesDebug = false;
const pipeSpawnDelay = 1000;
const owlHalfWidth = 38 * (OWLSCALE * 10);
const owlHalfHeight = 38 * (OWLSCALE * 10);
const bookChoices = [];
const owlXPos = GAMEWIDTH / 4;
const bookVariance = 10;
var lastPipeSpawn = 0;
var owlUpwardMotion = 0;
var SCROLLSPEED = 5;
var lastPipeTopBookCount;
var score = 0;
var scoreText;
var GAMESTATE = "UI";
var GAMEENDED = false;
let curUI;
let startUI;
let endUI;
function registerBook(ctx, bookFileName){
let cnt = bookChoices.length;
ctx.load.image("book" + cnt, bookFileName);
bookChoices.push("book" + cnt);
}
let firstPipe;
let lastPipe;
class Pipe {
constructor(point, x, y, height, ctx){
this.point = point; // if true, this pipe gives a point when passed.
this.x = x;
this.y = y;
this.height = height;
this.next = false;
this.ctx = ctx;
this.books = [];
this.startHeight = this.y - this.height / 2;
if (firstPipe === undefined){
firstPipe = this;
}
if (lastPipe !== undefined){
lastPipe.next = this;
}
lastPipe = this;
this.createBooks(ctx);
if(showPipesDebug && this.ctx){
this.debugRectangle = this.ctx.add.rectangle(this.x, this.y, pipeWidth, this.height, 0xffffff);
}
}
createBooks(ctx){
let bookCount = Math.floor(this.height / CALCULATEDBOOKHEIGHT);
for(let i = 0; i < bookCount; i++){
let randomBookOffset = Phaser.Math.Between(-bookVariance, bookVariance);
let randomBookString = bookChoices[Math.floor(Math.random()*bookChoices.length)];
let book = ctx.add.image(this.x + randomBookOffset, this.startHeight + (CALCULATEDBOOKHEIGHT / 2) + i * (CALCULATEDBOOKHEIGHT), randomBookString);
book.setScale(BOOKSCALE);
this.books.push(book);
}
}
update(){
this.x -= SCROLLSPEED;
if (showPipesDebug) this.debugRectangle.x = this.x;
// Moves books to the left
this.books.forEach(book => {
book.x -= SCROLLSPEED;
});
// Check if pipe is past owl
if (this.point && this.x < owlXPos - owlHalfWidth){
score++;
this.point = false;
}
// Check and destroy pipe if off screen
if (this.next && this === firstPipe && this.x + pipeWidth < 0){
firstPipe = this.next;
for(let i = 0; i < this.books.length; i++){
let book = this.books[i];
book.destroy();
}
}
}
checkCollision(){
let lx = this.x - pipeWidth / 2;
let rx = this.x + pipeWidth / 2;
let ty = this.y - this.height / 2;
let by = this.y + this.height / 2;
if (
owl.x - owlHalfWidth < rx &&
owl.x + owlHalfWidth > lx &&
owl.y - owlHalfHeight < by &&
owl.y + owlHalfHeight > ty
){
gameOver();
}
}
}
class StartUI {
constructor(ctx){
this.ctx = ctx;
}
create(){
const startText = touchDevice ? "Tap to start" : "Press any key to start";
this.titletext = this.ctx.add.text(GAMEWIDTH / 2, GAMEHEIGHT / 2.3, "BookBird!", {
font: 8 * FONTSCALE + "em " + FONT,
fill: "#ffffff",
align: "center",
stroke: "#000000",
strokeThickness: 5
});
this.titletext.setOrigin(0.5, 0.5);
this.startText = this.ctx.add.text(GAMEWIDTH / 2, GAMEHEIGHT / 2.3 + 70 * FONTSCALE, startText, {
font: 5 * FONTSCALE + "em " + FONT,
fill: "#ffffff",
align: "center",
stroke: "#000000",
strokeThickness: 5
});
this.startText.setOrigin(0.5, 0.5);
}
update(){
}
handleKeydown(){
// let fontSize = FONTSCALE * 100;
let textStyle = {font: 12 * FONTSCALE + "em " + FONT, fill: "#ffffff", align: "center", stroke: "#000000", strokeThickness: 5};
scoreText = this.ctx.add.text(GAMEWIDTH / 2, 20, score, textStyle);
scoreText.setDepth(1);
GAMESTATE = "PLAY";
this.startText.destroy();
this.titletext.destroy();
}
}
class EndUI {
constructor(ctx){
this.ctx = ctx;
this.overlayAlpha = 0;
}
create(){
scoreText.destroy();
this.overlay = this.ctx.add.rectangle(GAMEWIDTH / 2, GAMEHEIGHT / 2, GAMEWIDTH, GAMEHEIGHT, 0x000000);
this.titletext = this.ctx.add.text(GAMEWIDTH / 2, GAMEHEIGHT / 3 - 50, "Game over", {
font: 8 * FONTSCALE + "em " + FONT,
fill: "#ffffff",
align: "center",
stroke: "#000000",
strokeThickness: 5
});
this.scoretext = this.ctx.add.text(GAMEWIDTH / 2, GAMEHEIGHT / 3 + 50 * FONTSCALE, "You scored " + score + (score === 1 ? " point" : " points"), {
font: 5.5 * FONTSCALE + "em " + FONT,
fill: "#ffffff",
align: "center",
stroke: "#000000",
strokeThickness: 5
});
this.overlay.setAlpha(this.overlayAlpha);
this.titletext.setOrigin(0.5, 0.5);
this.scoretext.setOrigin(0.5, 0.5);
this.playAgainButton = this.createButton("I can do better", GAMEWIDTH / 2, GAMEHEIGHT / 3 + 150 * FONTSCALE, 250 * FONTSCALE, 80 * FONTSCALE, this.handlePlayAgain);
this.formButton = this.createButton("Please just take me to the Annuform", GAMEWIDTH / 2, GAMEHEIGHT / 3 + 250 * FONTSCALE, 520 * FONTSCALE, 80 * FONTSCALE, this.handleForm);
}
handlePlayAgain(){
window.location.reload();
}
handleForm(){
window.location.href = "https://forms.gle/StgHUfHmLc6Eyxd9A";
window.location = "https://forms.gle/StgHUfHmLc6Eyxd9A";
}
createButton(text, x, y, width, height, callback){
x = x - width / 2;
let container = this.ctx.add.container(x, y);
let belowButton = this.ctx.add.graphics();
container.add(belowButton);
// belowButton.fillStyle(0x123456, 1);
// belowButton.fillRoundedRect(0, 0, width, height, 24);
// add white border
belowButton.lineStyle(1, 0xffffff, 1);
belowButton.strokeRoundedRect(0, 0, width, height, 24);
let button = this.ctx.add.graphics();
container.add(button);
button.fillStyle(0x5F021F, 1);
button.fillRoundedRect(0, 0, width, height, 24);
// add white border
button.lineStyle(1, 0xffffff, 1);
button.strokeRoundedRect(0, 0, width, height, 24);
// Add text in button
let btntext = this.ctx.add.text(width / 2, height / 2, text, {
font: 3 * FONTSCALE + "em "+ FONT,
fill: "#ffffff",
align: "center",
stroke: "#000000",
strokeThickness: 2
});
btntext.setOrigin(0.5, 0.5);
container.add(btntext);
container.setInteractive(new Phaser.Geom.Rectangle(0, 0, width, height), Phaser.Geom.Rectangle.Contains, {cursor: "pointer"});
container.on("pointerdown", callback);
container.on("pointerover", function(){
button.setAlpha(0);
document.body.style.cursor = 'pointer';
});
container.on("pointerout", function(){
button.setAlpha(1);
document.body.style.cursor = 'default';
});
return container;
}
update(){
if (this.overlayAlpha < 0.6){
this.overlayAlpha += 0.1;
this.overlay.setAlpha(this.overlayAlpha);
}
}
handleKeydown(){}
}
let config = {
type: Phaser.AUTO,
width: GAMEWIDTH,
height: GAMEHEIGHT,
backgroundColor: 0x00aaff,
scene: {
preload: preload,
create: create,
update: update,
}
}
var game = new Phaser.Game(config);
function preload () {
this.load.image("bg1", "bg.png");
this.load.image("owl", "owl.png");
registerBook(this, "b59.png");
registerBook(this, "b60.png");
registerBook(this, "b61.png");
registerBook(this, "b62.png");
registerBook(this, "b63.png");
registerBook(this, "b64.png");
}
function create () {
bg = this.add.tileSprite(GAMEWIDTH / 2, GAMEHEIGHT - 75, GAMEWIDTH, 150, "bg1");
owl = this.add.sprite(owlXPos, GAMEHEIGHT / 3.2, "owl");
owl.setScale(OWLSCALE);
owl.angle = 12;
this.input.keyboard.on("keydown", handleKeydown, this);
this.input.on('pointerdown', handleKeydown, this);
endUI = new EndUI(this);
startUI = new StartUI(this);
startUI.create();
curUI = startUI;
}
function handleKeydown(){
if (GAMESTATE === "PLAY"){
owlFlap();
} else if (GAMESTATE === "UI"){
curUI.handleKeydown();
}
}
function update() {
if (!GAMEENDED){
bg.tilePositionX += SCROLLSPEED; // keep the background moving!
}
if (GAMESTATE === "UI"){
curUI.update();
} else if (GAMESTATE == "PLAY"){
SCROLLSPEED += Math.min(ACCERELATIONCAP, SCROLLSPEED * ACCERELATION);
owl.y -= owlUpwardMotion;
owlUpwardMotion -= Math.max(owlDownwardAcc, owlUpwardMotion * owlDownwardAcc);
scoreText.setText(score);
owl.angle = 12 + score;
checkCollisionsUpdatePipes();
managePiping(this);
}
}
function checkCollisionsUpdatePipes(){
check_ground_hit();
check_fully_in_air();
let curPipe = firstPipe;
while (curPipe){
curPipe.checkCollision();
curPipe.update();
curPipe = curPipe.next;
}
}
function owlFlap() {
owlUpwardMotion += owlFlapIncrease;
}
function managePiping(ctx) {
if (lastPipeSpawn > 0){
lastPipeSpawn -= SCROLLSPEED;
} else {
// Spawn new pipe
lastPipeSpawn = pipeSpawnDelay;
let maxBookCount = Math.floor(GAMEHEIGHT / CALCULATEDBOOKHEIGHT);
let minGapSize = Math.floor(owlHalfHeight * 2 * 3.2);
let maxGapSize = Math.floor(owlHalfHeight * 2 * 7);
let minGapBookCount = Math.floor(minGapSize / CALCULATEDBOOKHEIGHT) + 1;
let maxGapBookCount = Math.floor(maxGapSize / CALCULATEDBOOKHEIGHT) + 1;
let maxVariance = maxBookCount / 3.2;
let bookTopCount;
if (lastPipeTopBookCount) {
bookTopCount = Phaser.Math.Between(Math.max(2, lastPipeTopBookCount - maxVariance), Math.min(maxBookCount / 2, lastPipeTopBookCount + maxVariance));
} else {
bookTopCount = Phaser.Math.Between(2, maxBookCount / 2);
lastPipeTopBookCount = bookTopCount;
}
let bookBottomCount = Phaser.Math.Between(Math.max(3, maxBookCount - bookTopCount - maxGapBookCount), maxBookCount - bookTopCount - minGapBookCount);
let topPipeHeight = bookTopCount * CALCULATEDBOOKHEIGHT;
let bottomPipeHeight = bookBottomCount * CALCULATEDBOOKHEIGHT;
let topPipe = new Pipe(true, GAMEWIDTH + pipeWidth + bookVariance, topPipeHeight / 2, topPipeHeight, ctx);
let bottomPipe = new Pipe(false, GAMEWIDTH + pipeWidth + bookVariance, GAMEHEIGHT - (bottomPipeHeight / 2), bottomPipeHeight, ctx);
}
}
function check_ground_hit(){
if(owl.y > GAMEHEIGHT){
gameOver();
}
}
function check_fully_in_air(){
if(owl.y + owlHalfHeight + 10 < 0){
gameOver();
}
}
function gameOver(){
GAMESTATE = "UI";
GAMEENDED = true;
curUI = endUI;
curUI.create();
}
</script>
</body>
</html>