Skip to content

Commit 771db60

Browse files
committed
Step 6.3: Create 'Ready' layer
1 parent ea817c7 commit 771db60

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Game.Screens.Play.Ready = class Ready extends Engine.Layer {
2+
get events() {
3+
return {
4+
"keydown": "onKeyDown"
5+
};
6+
}
7+
8+
constructor(screen, snakes) {
9+
super(screen);
10+
11+
this.snakes = snakes;
12+
13+
// Create "ready" sprite and set its properties
14+
let readyTexture = this.assets.minecraftiaFont.createTexture("Ready");
15+
let readySprite = new Engine.Sprite(readyTexture);
16+
readySprite.align = "center";
17+
readySprite.setPercentage("width", this.width, 15, "height");
18+
19+
// Create fade out animation for "ready" sprite
20+
this.readyAnim = new Engine.Animations.Keyframe(readySprite, [
21+
{
22+
x: this.width / 2,
23+
y: this.height / 2,
24+
opacity: 1,
25+
frame: 0
26+
},
27+
{
28+
y: this.height / 3,
29+
opacity: 0,
30+
frame: 700
31+
}
32+
]);
33+
}
34+
35+
draw(context) {
36+
this.readyAnim.draw(context);
37+
}
38+
39+
update(span) {
40+
if (!this.ready) return;
41+
42+
if (this.readyAnim.playing) {
43+
this.readyAnim.update(span);
44+
}
45+
// Once animation is finished, dispose layer
46+
else {
47+
this.screen.removeLayer(this);
48+
}
49+
}
50+
51+
onKeyDown() {
52+
// One time event
53+
this.disposeEventListeners()
54+
55+
// This will start playing the animation
56+
this.ready = true;
57+
this.readyAnim.play();
58+
}
59+
};

views/game.html

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<script type="text/javascript" src="/scripts/engine/game.js"></script>
2525
<script type="text/javascript" src="/scripts/game/entities/snake.js"></script>
2626
<script type="text/javascript" src="/scripts/game/screens/play/index.js"></script>
27+
<script type="text/javascript" src="/scripts/game/screens/play/ready.js"></script>
2728
<script type="text/javascript" src="/scripts/game/screens/menu/index.js"></script>
2829
<script type="text/javascript" src="/scripts/game/screens/splash/index.js"></script>
2930
<script type="text/javascript" src="/scripts/main.js"></script>

0 commit comments

Comments
 (0)