generated from tassaron/canvas-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cat.js
133 lines (114 loc) · 3.13 KB
/
cat.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
127
128
129
130
131
132
133
import { AnimatedThing, Thing } from "./thing.js";
export class Cat extends Thing {
constructor(i, x, y) {
super(x, y, 38, 38);
this.i = i;
this.states = {
"idle": new IdleCat(this),
"sitting": new SittingCat(this),
"sat": new SatCat(this),
"walking": new WalkingCat(this),
"cheese": new Cheese(this)
}
this.state = "idle";
this.prevState = "walking";
this.cooldown = 0.0;
this.startingWalk = 0;
this.facing = 1;
}
get x() {return this._x}
get y() {return this._y}
set x(i) {
this._x = i;
for (let state of Object.values(this.states)) {
state.x = i;
}
}
set y(i) {
this._y = i;
for (let state of Object.values(this.states)) {
state.y = i;
}
}
update(ratio, keyboard, mouse) {
if (this.state == "cheese") {return}
if (this.state == "idle" && this.prevState != "idle") {
if (this.cooldown == 0.0) {
this.cooldown = 120.0;
} else {
this.cooldown -= ratio;
}
if (this.cooldown <= 0.0) {
this.prevState = "idle";
}
} else if (this.state != "idle") {
this.prevState = this.state;
}
if (this.state == "sat") {
if (this.cooldown > 0.0) {
this.cooldown -= ratio;
} else {
this.state = "cheese";
}
}
this.states[this.state].update(ratio, keyboard, mouse);
if (this.state == "sitting" && this.states["sitting"].loops > 0) {
this.state = "sat";
this.cooldown = 90.0 + ratio;
}
}
draw(ctx, drawSprite) {
drawSprite.planks(this.x, this.y);
this.states[this.state].draw(ctx, drawSprite);
}
}
class AnimatedState extends AnimatedThing {
constructor(cat, src, frames, timing) {
super(cat.x, cat.y, 38, 38, src, frames, timing);
this.cat = cat;
}
}
class IdleCat extends AnimatedState {
constructor(cat) {
super(cat, "cat_idle_r", 2, 50);
}
update(ratio, keyboard, mouse) {
if (this.cat.facing == 1) {
this.src = "cat_idle_r";
} else {
this.src = "cat_idle";
}
super.update(ratio, keyboard, mouse);
}
}
class SittingCat extends AnimatedState {
constructor(cat) {
super(cat, "cat_sit", 2, 50);
}
}
class WalkingCat extends AnimatedState {
constructor(cat) {
super(cat, "cat_walk_r", 6, 10);
}
update(ratio, keyboard, mouse) {
if (this.cat.facing == 1) {
this.src = "cat_walk_r";
} else {
this.src = "cat_walk";
}
super.update(ratio, keyboard, mouse);
}
}
class SatCat extends Thing {
constructor(cat) {
super(cat.x, cat.y, 38, 38, "cat_sit");
}
draw(ctx, drawSprite) {
drawSprite.cat_sit(2, this.x, this.y);
}
}
class Cheese extends Thing {
constructor(cat) {
super(cat.x, cat.y, 38, 38, "cheese");
}
}