pause & end game on esc
@@ -993,12 +993,12 @@ class Scene
993
993
this.context.clearRect(0, 0, this.real_size.x, this.real_size.y);
994
994
}
995
995
996
this.activeObjects.sort((a, b) => a.layer - b.layer);
996
let sortedActiveObjects = this.activeObjects.slice().sort((a, b) => a.layer - b.layer);
997
997
998
for (var i = 0; i < this.activeObjects.length; i++)
998
for (var i = 0; i < sortedActiveObjects.length; i++)
999
999
{
1000
if (!this.activeObjects[i].destroyed && this.activeObjects[i].renderer.visible)
1001
this.activeObjects[i].draw(this.context);
1000
if (!sortedActiveObjects[i].destroyed && sortedActiveObjects[i].renderer.visible)
1001
sortedActiveObjects[i].draw(this.context);
1002
1002
}
1003
1003
}
1004
1004
}
@@ -993,12 +993,12 @@ class Scene993
this.context.clearRect(0, 0, this.real_size.x, this.real_size.y);993
this.context.clearRect(0, 0, this.real_size.x, this.real_size.y);994
}994
}995
995
996
this.activeObjects.sort((a, b) => a.layer - b.layer);996
let sortedActiveObjects = this.activeObjects.slice().sort((a, b) => a.layer - b.layer);997
997
998
for (var i = 0; i < this.activeObjects.length; i++)998
for (var i = 0; i < sortedActiveObjects.length; i++)999
{999
{1000
if (!this.activeObjects[i].destroyed && this.activeObjects[i].renderer.visible)1000
if (!sortedActiveObjects[i].destroyed && sortedActiveObjects[i].renderer.visible)1001
this.activeObjects[i].draw(this.context);1001
sortedActiveObjects[i].draw(this.context);1002
}1002
}1003
}1003
}1004
}1004
}@@ -14,6 +14,12 @@ class FrogScene extends Scene
14
14
gameSpeed = 1;
15
15
16
16
start = false;
17
paused = false;
18
19
closeButton = null;
20
21
closeBtnX = 0;
22
closeBtnY = -140;
17
23
18
24
posScaleY = 0;
19
25
text_1_target_y;
@@ -40,27 +46,48 @@ class FrogScene extends Scene
40
46
this.text_1_target_y = this.real_size.y * .3;
41
47
this.text_2_target_y = this.real_size.y * .62;
42
48
this.image_1_target_y = this.real_size.y * .45;
43
44
this.doMenuScreenTween();
45
46
this.doSpaceBarTweenIn();
49
this.closeBtnX = this.real_size.x - 50;
47
50
}
48
51
49
52
onLoad ()
50
53
{
54
let pos = new vector(this.closeBtnX, this.closeBtnY);
55
this.closeButton = this.addObject(new SubBtn(pos.x, pos.y, this.game.images.closeButton, 1));
56
57
this.closeButton.isClicked.add(function ()
58
{
59
this.paused = false;
60
this.posScaleY = 2.5;
61
this.gameOver();
62
}, this);
63
51
64
this.externalFruitCurrentPos = new vector(this.real_position.x - this.game.images.fruit.width / 2, this.real_position.y - this.game.images.fruit.height / 2);
52
65
53
66
this.maxPerXAxis = Math.ceil(this.real_size.x / this.game.images.fruit.width) + 2;
54
67
this.maxPerYAxis = Math.ceil(this.real_size.y / this.game.images.fruit.height) + 2;
68
69
this.doMenuScreenTween();
70
71
this.doSpaceBarTweenIn();
55
72
}
56
73
57
74
update (deltaTime)
58
75
{
59
super.update(deltaTime);
76
if (!this.paused)
77
{
78
super.update(deltaTime);
79
}
80
else
81
{
82
this.closeButton.update(deltaTime);
83
}
60
84
61
85
if (this.start && !this.destructing)
62
86
{
63
this.spawnFruits(deltaTime);
87
this.checkEscape();
88
if (!this.paused)
89
this.spawnFruits(deltaTime);
90
64
91
}
65
92
else if (!this.start && !this.destructing)
66
93
{
@@ -93,7 +120,6 @@ class FrogScene extends Scene
93
120
TWEEN.remove(this.spaceBarTween);
94
121
if (this.screenTween)
95
122
TWEEN.remove(this.screenTween);
96
this.start = false;
97
123
this.activeObjects.forEach(ao => ao.destroy());
98
124
99
125
// pass over fruits array to parent scene
@@ -142,6 +168,18 @@ class FrogScene extends Scene
142
168
this.start = true;
143
169
}
144
170
171
pauseGame ()
172
{
173
this.paused = true;
174
175
this.doMenuScreenTween();
176
}
177
178
unPauseGame ()
179
{
180
this.doAwayMenuScreenTween();
181
}
182
145
183
spawnFruits (deltaTime)
146
184
{
147
185
this.fruitSpawnTimer -= deltaTime;
@@ -253,6 +291,15 @@ class FrogScene extends Scene
253
291
}
254
292
}
255
293
294
checkEscape ()
295
{
296
if (GameInput.isPressed(GameInput.keys.Escape))
297
{
298
299
this.paused ? this.unPauseGame() : this.pauseGame();
300
}
301
}
302
256
303
drawExternalFruit ()
257
304
{
258
305
if (this.eatenFruit.length > 0)
@@ -273,20 +320,30 @@ class FrogScene extends Scene
273
320
274
321
drawMenu ()
275
322
{
276
if (this.posScaleY < 2)
323
if (!this.paused)
277
324
{
278
this.context.font = "20px Monospace";
279
this.context.fillStyle = "black"; // sets the color to fill in the rectangle with
325
if (this.posScaleY < 2)
326
{
327
this.context.font = "20px Monospace";
328
this.context.fillStyle = "black"; // sets the color to fill in the rectangle with
280
329
330
let textData = this.context.measureText("Press");
331
this.context.fillText("Press", this.real_size.x * .5 - (textData.width / 2), this.text_1_target_y * this.posScaleY, 72);
281
332
282
let textData = this.context.measureText("Press");
283
this.context.fillText("Press", this.real_size.x * .5 - (textData.width / 2), this.text_1_target_y * this.posScaleY, 72);
333
textData = this.context.measureText("To Start");
334
this.context.fillText("To Lick", this.real_size.x * .5 - (textData.width / 2) + 5, this.text_2_target_y * this.posScaleY, 72);
284
335
285
textData = this.context.measureText("To Start");
286
this.context.fillText("To Lick", this.real_size.x * .5 - (textData.width / 2) + 5, this.text_2_target_y * this.posScaleY, 72);
336
let space = this.game.images.spacebar;
337
space.draw(this, this.real_size.x * .5, this.image_1_target_y * this.posScaleY, space.width, space.height, this.image_1_rotation * 3, 0, 0, 1, 0);
338
}
339
}
340
else
341
{
342
this.context.font = "20px Monospace";
343
this.context.fillStyle = "black"; // sets the color to fill in the rectangle with
287
344
288
let space = this.game.images.spacebar;
289
space.draw(this, this.real_size.x * .5, this.image_1_target_y * this.posScaleY, space.width, space.height, this.image_1_rotation * 3, 0, 0, 1, 0);
345
let textData = this.context.measureText("Paused");
346
this.context.fillText("Paused", this.real_size.x * .5 - (textData.width / 2), this.text_1_target_y * this.posScaleY * 2, 72);
290
347
}
291
348
}
292
349
@@ -296,6 +353,23 @@ class FrogScene extends Scene
296
353
.to({ posScaleY: 1 }, 800)
297
354
.easing(TWEEN.Easing.Elastic.Out)
298
355
.start();
356
this.doCloseButtonTweenIn();
357
}
358
359
doCloseButtonTweenIn ()
360
{
361
new TWEEN.Tween(this.closeButton.transform.position)
362
.to({ y: 40 }, 800)
363
.easing(TWEEN.Easing.Elastic.Out)
364
.start();
365
}
366
367
doCloseButtonTweenOut ()
368
{
369
new TWEEN.Tween(this.closeButton.transform.position)
370
.to({ y: -40 }, 800)
371
.easing(TWEEN.Easing.Elastic.Out)
372
.start();
299
373
}
300
374
301
375
doSpaceBarTweenIn ()
@@ -310,9 +384,12 @@ class FrogScene extends Scene
310
384
311
385
doAwayMenuScreenTween ()
312
386
{
313
this.screenTween
387
this.doCloseButtonTweenOut();
388
389
this.screenTween = new TWEEN.Tween(this)
314
390
.to({ posScaleY: 2.5 }, 800)
315
391
.easing(TWEEN.Easing.Elastic.InOut)
316
.start();
392
.start().onComplete(() => this.paused = false);
393
317
394
}
318
395
}
@@ -14,6 +14,12 @@ class FrogScene extends Scene14
gameSpeed = 1;14
gameSpeed = 1;15
15
16
start = false;16
start = false;17
paused = false;18
19
closeButton = null;20
21
closeBtnX = 0;22
closeBtnY = -140;17
23
18
posScaleY = 0;24
posScaleY = 0;19
text_1_target_y;25
text_1_target_y;@@ -40,27 +46,48 @@ class FrogScene extends Scene40
this.text_1_target_y = this.real_size.y * .3;46
this.text_1_target_y = this.real_size.y * .3;41
this.text_2_target_y = this.real_size.y * .62;47
this.text_2_target_y = this.real_size.y * .62;42
this.image_1_target_y = this.real_size.y * .45;48
this.image_1_target_y = this.real_size.y * .45;43
49
this.closeBtnX = this.real_size.x - 50;44
this.doMenuScreenTween();45
46
this.doSpaceBarTweenIn();47
}50
}48
51
49
onLoad ()52
onLoad ()50
{53
{54
let pos = new vector(this.closeBtnX, this.closeBtnY);55
this.closeButton = this.addObject(new SubBtn(pos.x, pos.y, this.game.images.closeButton, 1));56
57
this.closeButton.isClicked.add(function ()58
{59
this.paused = false;60
this.posScaleY = 2.5;61
this.gameOver();62
}, this);63
51
this.externalFruitCurrentPos = new vector(this.real_position.x - this.game.images.fruit.width / 2, this.real_position.y - this.game.images.fruit.height / 2);64
this.externalFruitCurrentPos = new vector(this.real_position.x - this.game.images.fruit.width / 2, this.real_position.y - this.game.images.fruit.height / 2);52
65
53
this.maxPerXAxis = Math.ceil(this.real_size.x / this.game.images.fruit.width) + 2;66
this.maxPerXAxis = Math.ceil(this.real_size.x / this.game.images.fruit.width) + 2;54
this.maxPerYAxis = Math.ceil(this.real_size.y / this.game.images.fruit.height) + 2;67
this.maxPerYAxis = Math.ceil(this.real_size.y / this.game.images.fruit.height) + 2;68
69
this.doMenuScreenTween();70
71
this.doSpaceBarTweenIn();55
}72
}56
73
57
update (deltaTime)74
update (deltaTime)58
{75
{59
super.update(deltaTime);76
if (!this.paused)77
{78
super.update(deltaTime);79
}80
else81
{82
this.closeButton.update(deltaTime);83
}60
84
61
if (this.start && !this.destructing)85
if (this.start && !this.destructing)62
{86
{63
this.spawnFruits(deltaTime);87
this.checkEscape();88
if (!this.paused)89
this.spawnFruits(deltaTime);90
64
}91
}65
else if (!this.start && !this.destructing)92
else if (!this.start && !this.destructing)66
{93
{@@ -93,7 +120,6 @@ class FrogScene extends Scene93
TWEEN.remove(this.spaceBarTween);120
TWEEN.remove(this.spaceBarTween);94
if (this.screenTween)121
if (this.screenTween)95
TWEEN.remove(this.screenTween);122
TWEEN.remove(this.screenTween);96
this.start = false;97
this.activeObjects.forEach(ao => ao.destroy());123
this.activeObjects.forEach(ao => ao.destroy());98
124
99
// pass over fruits array to parent scene125
// pass over fruits array to parent scene@@ -142,6 +168,18 @@ class FrogScene extends Scene142
this.start = true;168
this.start = true;143
}169
}144
170
171
pauseGame ()172
{173
this.paused = true;174
175
this.doMenuScreenTween();176
}177
178
unPauseGame ()179
{180
this.doAwayMenuScreenTween();181
}182
145
spawnFruits (deltaTime)183
spawnFruits (deltaTime)146
{184
{147
this.fruitSpawnTimer -= deltaTime;185
this.fruitSpawnTimer -= deltaTime;@@ -253,6 +291,15 @@ class FrogScene extends Scene253
}291
}254
}292
}255
293
294
checkEscape ()295
{296
if (GameInput.isPressed(GameInput.keys.Escape))297
{298
299
this.paused ? this.unPauseGame() : this.pauseGame();300
}301
}302
256
drawExternalFruit ()303
drawExternalFruit ()257
{304
{258
if (this.eatenFruit.length > 0)305
if (this.eatenFruit.length > 0)@@ -273,20 +320,30 @@ class FrogScene extends Scene273
320
274
drawMenu ()321
drawMenu ()275
{322
{276
if (this.posScaleY < 2)323
if (!this.paused)277
{324
{278
this.context.font = "20px Monospace";325
if (this.posScaleY < 2)279
this.context.fillStyle = "black"; // sets the color to fill in the rectangle with326
{327
this.context.font = "20px Monospace";328
this.context.fillStyle = "black"; // sets the color to fill in the rectangle with280
329
330
let textData = this.context.measureText("Press");331
this.context.fillText("Press", this.real_size.x * .5 - (textData.width / 2), this.text_1_target_y * this.posScaleY, 72);281
332
282
let textData = this.context.measureText("Press");333
textData = this.context.measureText("To Start");283
this.context.fillText("Press", this.real_size.x * .5 - (textData.width / 2), this.text_1_target_y * this.posScaleY, 72);334
this.context.fillText("To Lick", this.real_size.x * .5 - (textData.width / 2) + 5, this.text_2_target_y * this.posScaleY, 72);284
335
285
textData = this.context.measureText("To Start");336
let space = this.game.images.spacebar;286
this.context.fillText("To Lick", this.real_size.x * .5 - (textData.width / 2) + 5, this.text_2_target_y * this.posScaleY, 72);337
space.draw(this, this.real_size.x * .5, this.image_1_target_y * this.posScaleY, space.width, space.height, this.image_1_rotation * 3, 0, 0, 1, 0);338
}339
}340
else341
{342
this.context.font = "20px Monospace";343
this.context.fillStyle = "black"; // sets the color to fill in the rectangle with287
344
288
let space = this.game.images.spacebar;345
let textData = this.context.measureText("Paused");289
space.draw(this, this.real_size.x * .5, this.image_1_target_y * this.posScaleY, space.width, space.height, this.image_1_rotation * 3, 0, 0, 1, 0);346
this.context.fillText("Paused", this.real_size.x * .5 - (textData.width / 2), this.text_1_target_y * this.posScaleY * 2, 72);290
}347
}291
}348
}292
349
@@ -296,6 +353,23 @@ class FrogScene extends Scene296
.to({ posScaleY: 1 }, 800)353
.to({ posScaleY: 1 }, 800)297
.easing(TWEEN.Easing.Elastic.Out)354
.easing(TWEEN.Easing.Elastic.Out)298
.start();355
.start();356
this.doCloseButtonTweenIn();357
}358
359
doCloseButtonTweenIn ()360
{361
new TWEEN.Tween(this.closeButton.transform.position)362
.to({ y: 40 }, 800)363
.easing(TWEEN.Easing.Elastic.Out)364
.start();365
}366
367
doCloseButtonTweenOut ()368
{369
new TWEEN.Tween(this.closeButton.transform.position)370
.to({ y: -40 }, 800)371
.easing(TWEEN.Easing.Elastic.Out)372
.start();299
}373
}300
374
301
doSpaceBarTweenIn ()375
doSpaceBarTweenIn ()@@ -310,9 +384,12 @@ class FrogScene extends Scene310
384
311
doAwayMenuScreenTween ()385
doAwayMenuScreenTween ()312
{386
{313
this.screenTween387
this.doCloseButtonTweenOut();388
389
this.screenTween = new TWEEN.Tween(this)314
.to({ posScaleY: 2.5 }, 800)390
.to({ posScaleY: 2.5 }, 800)315
.easing(TWEEN.Easing.Elastic.InOut)391
.easing(TWEEN.Easing.Elastic.InOut)316
.start();392
.start().onComplete(() => this.paused = false);393
317
}394
}318
}395
}