-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathTamagotchi.js
125 lines (102 loc) · 2.65 KB
/
Tamagotchi.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
class Tamagotchi extends Animatable {
constructor(canvas) {
super(canvas);
const idle = new Sprite('images/Idle.png', 360, 120, 3, 1);
const eat = new Sprite('images/Eat.png', 640, 240, 4, 2);
const other = new Sprite('images/Other.png', 240, 360, 2, 3);
this.animations = {
dislike: other.get(2),
jumpRight: other.get(0),
jumpLeft: other.get(1),
burger: eat.get(1),
candy: eat.get(0),
bounce: idle.get(),
};
this.eatLevel = 0;
this.maxEatLevel = 3;
this.idle = this.idle.bind(this);
this.feed = this.feed.bind(this);
this.dislike = this.dislike.bind(this);
this.eat = this.eat.bind(this);
this.jump = this.jump.bind(this);
this.frame = this.frame.bind(this);
this.move = this.move.bind(this);
this.bounce = this.bounce.bind(this);
}
idle() {
const {
bounce,
} = this;
const move = this.move('bounce', 0);
function* animation() {
// yield* this.jump('right');
// yield* move('left', 25);
// yield* this.jump('left');
// yield* move('right', 25);
yield* bounce();
yield* move('right', 25);
yield* move('left', 50);
yield* bounce();
yield* move('right', 25);
};
return animation.call(this);
}
feed(food) {
const {
delay,
eat,
} = this;
function* animation() {
if (this.eatLevel === this.maxEatLevel) {
yield* this.dislike();
} else {
this.eatLevel++;
yield* eat(food);
}
yield delay(300);
}
return animation.call(this);
}
dislike() {
const frame = this.frame(this.animations.dislike);
function* animation() {
yield* frame(0);
yield* frame(1);
};
return animation.call(this);;
}
eat(food) {
const frame = this.frame(this.animations[food]);
function* animation() {
yield* frame(0);
yield* frame(1);
yield* frame(2);
yield* frame(3);
};
return animation.call(this);
}
jump(direction) {
const { delay } = this;
const jumpTo = `jump${capitalize(direction)}`;
const move = this.move(jumpTo, 0);
const frame = this.frame(this.animations[jumpTo]);
function* animation() {
yield* move(`${direction}.up`, 30, 40, 40);
yield* move('down', undefined, 30, 20);
yield* frame(1);
yield delay(300);
};
return animation.call(this);
}
bounce() {
const frame = this.frame(this.animations.bounce);
function* animation() {
yield* frame(0);
yield* frame(1);
yield* frame(2);
yield* frame(1);
yield* frame(0);
};
return animation.call(this);
}
}