-
Notifications
You must be signed in to change notification settings - Fork 11
/
Game.js
126 lines (107 loc) · 2.86 KB
/
Game.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
var gameScreen = document.querySelector('.game__screen');
var gameOptions = document.querySelector('.game__options');
var canvas = document.querySelector('.animation');
canvas.width = 280;
canvas.height = 280;
class Game {
constructor(canvas) {
const context = canvas.getContext('2d');
context.font = '120px VT323';
context.fillText('Start', 20, 170);
context.beginPath();
context.moveTo(145, 260);
context.lineTo(100, 200);
context.lineTo(200, 200);
context.fill();
this.started = false;
this.optionsVisible = false;
this.userEvents = [];
this.start = this.start.bind(this);
this.startLoop = this.startLoop.bind(this);
this.isPending = this.isPending.bind(this);
this.loop = this.loop.bind(this);
this.showOptions = this.showOptions.bind(this);
this.hideOptions = this.hideOptions.bind(this);
}
start() {
coroutine(function* () {
yield* egg.hatch();
yield egg.delay(500);
}).then(() => {
this.started = true;
this.startLoop();
});
}
isPending() {
return this.userEvents.length;
}
startLoop() {
coroutine(this.loop());
}
loop() {
const {
isPending,
userEvents,
} = this;
return function* loop() {
let idle = tamagotchi.idle();
let done = false;
while (game.started) {
if (isPending()) {
const event = userEvents.shift();
idle.return();
yield tamagotchi.reset;
yield* event();
}
while (!done && !isPending()) {
const next = idle.next();
done = next.done;
yield next.value;
}
idle = tamagotchi.idle();
done = false;
}
}
}
showOptions() {
this.optionsVisible = true;
const options = gameOptions.children;
options.item(0).innerText = 'Feed';
options.item(0).onclick = () => this.showFoodOptions();
options.item(1).innerText = 'Play';
gameOptions.classList.add('visible');
}
showFoodOptions() {
const options = gameOptions.children;
options.item(0).innerText = 'Burger';
options.item(0).onclick = () => feed('burger');
options.item(1).innerText = 'Candy';
options.item(1).onclick = () => feed('candy');
}
hideOptions() {
this.optionsVisible = false;
gameOptions.classList.remove('visible');
}
}
var egg = new Egg(canvas);
var tamagotchi = new Tamagotchi(canvas);
var game = new Game(canvas);
var buttonA = document.querySelector('.a');
var buttonB = document.querySelector('.b');
var buttonC = document.querySelector('.c');
function feed(food) {
if (game.started) {
game.userEvents.push(tamagotchi.feed.bind(null, food));
game.hideOptions();
}
}
buttonA.addEventListener('click', () => {
if (game.started) {
game.showOptions();
}
});
buttonB.addEventListener('click', () => {
if (!game.started) {
game.start();
}
});