BlueGit

blue-star speeltuin

Files

speeltuin / src / objects / chao.js

chao.js

chao.js 6.3 KiB Download Raw
1class Chao extends GameObject
2{
3 speed = 30;
4
5 constructor(x, y, sprite, layer)
6 {
7 super(x, y, sprite, layer);
8 this.wasHeld = {}
9 }
10
11 onClick(btn)
12 {
13 console.log(btn);
14 }
15
16 anyHeld()
17 {
18 return this.wasHeld.right || this.wasHeld.left || this.wasHeld.up || this.wasHeld.down;
19 }
20
21 update(deltaTime)
22 {
23 super.update(deltaTime);
24 this.wasHeld = this.wasHeld || {}
25
26 if(this.wasHeld.right && !GameInput.isHeld(GameInput.keys.ArrowRight))
27 {
28 this.wasHeld.right = false;
29 if(!this.anyHeld())
30 this.renderer.setAnimation("standLeft", 0, .2);
31 }
32
33 if(this.wasHeld.left && !GameInput.isHeld(GameInput.keys.ArrowLeft))
34 {
35 this.wasHeld.left = false;
36 if(!this.anyHeld())
37 this.renderer.setAnimation("standLeft", 0, .2);
38 }
39
40 if(this.wasHeld.up && !GameInput.isHeld(GameInput.keys.ArrowUp))
41 {
42 this.wasHeld.up = false;
43 if(!this.anyHeld())
44 this.renderer.setAnimation("standUp", 0, .2);
45 }
46
47 if(this.wasHeld.down && !GameInput.isHeld(GameInput.keys.ArrowDown))
48 {
49 this.wasHeld.down = false;
50 if(!this.anyHeld())
51 this.renderer.setAnimation("standDown", 0, .2);
52 }
53
54 if(GameInput.isHeld(GameInput.keys.ArrowRight) && !this.wasHeld.left && !this.wasHeld.up && !this.wasHeld.down)
55 {
56 this.transform.position.x += this.speed * deltaTime;
57 CookieManager.setCookie("arrow", "right");
58
59 if(!this.wasHeld.right)
60 this.renderer.setAnimation("walkLeft", 0, .2);
61 this.renderer.mirrorX = true;
62 this.wasHeld.right = true;
63 }
64 else if(GameInput.isHeld(GameInput.keys.ArrowLeft) && !this.wasHeld.right && !this.wasHeld.up && !this.wasHeld.down)
65 {
66 this.transform.position.x -= this.speed * deltaTime;
67 CookieManager.setCookie("arrow", "left")
68
69 if(!this.wasHeld.left)
70 this.renderer.setAnimation("walkLeft", 0, .2);
71 this.renderer.mirrorX = false;
72 this.wasHeld.left = true;
73 }
74 else if(GameInput.isHeld(GameInput.keys.ArrowUp) && !this.wasHeld.left && !this.wasHeld.right && !this.wasHeld.down)
75 {
76 this.transform.position.y -= this.speed * deltaTime;
77 CookieManager.setCookie("arrow", "up")
78
79 if(!this.wasHeld.up)
80 this.renderer.setAnimation("walkUp", 0, .2);
81 this.renderer.mirrorX = false;
82 this.wasHeld.up = true;
83 }
84 else if(GameInput.isHeld(GameInput.keys.ArrowDown) && !this.wasHeld.left && !this.wasHeld.up && !this.wasHeld.right)
85 {
86 this.transform.position.y += this.speed * deltaTime;
87 CookieManager.setCookie("arrow", "down")
88
89 if(!this.wasHeld.down)
90 this.renderer.setAnimation("walkDown", 0, .2);
91 this.renderer.mirrorX = false;
92 this.wasHeld.down = true;
93 }
94 else
95 {
96 if(GameInput.isPressed(GameInput.keys["1"]))
97 {
98 this.renderer.setAnimation("tripDown", 0, .2)
99 }
100
101 if(GameInput.isPressed(GameInput.keys["2"]))
102 {
103 this.renderer.setAnimation("tripLeft", 0, .2)
104 }
105
106 if(GameInput.isPressed(GameInput.keys["3"]))
107 {
108 this.renderer.setAnimation("tripUp", 0, .2)
109 }
110
111 if(GameInput.isPressed(GameInput.keys["4"]))
112 {
113 this.renderer.setAnimation("sitDown", 0, .2)
114 }
115
116 if(GameInput.isPressed(GameInput.keys["5"]))
117 {
118 this.renderer.setAnimation("sitLeft", 0, .2)
119 }
120
121 if(GameInput.isPressed(GameInput.keys["6"]))
122 {
123 this.renderer.setAnimation("tantrum", 0, .2)
124 }
125
126 if(GameInput.isPressed(GameInput.keys["7"]))
127 {
128 this.renderer.setAnimation("swimStruggle", 0, .2)
129 }
130
131 if(GameInput.isPressed(GameInput.keys["8"]))
132 {
133 this.renderer.setAnimation("swim", 0, .2)
134 }
135
136 if(GameInput.isPressed(GameInput.keys["9"]))
137 {
138 this.renderer.setAnimation("eatStart", 0, .2)
139 }
140
141 if(GameInput.isPressed(GameInput.keys["0"]))
142 {
143 this.renderer.setAnimation("cheer", 0, .2)
144 }
145
146 if(GameInput.isPressed(GameInput.keys["q"]))
147 {
148 this.renderer.setAnimation("wave", 0, .2)
149 }
150
151 if(GameInput.isPressed(GameInput.keys["w"]))
152 {
153 this.renderer.setAnimation("hungry", 0, .2)
154 }
155
156 if(GameInput.isPressed(GameInput.keys["e"]))
157 {
158 this.renderer.setAnimation("no", 0, .2)
159 }
160
161 if(GameInput.isPressed(GameInput.keys["r"]))
162 {
163 this.renderer.setAnimation("think", 0, .2)
164 }
165
166 if(GameInput.isPressed(GameInput.keys["t"]))
167 {
168 this.renderer.setAnimation("happy", 0, .2)
169 }
170
171 if(GameInput.isPressed(GameInput.keys["y"]))
172 {
173 this.renderer.setAnimation("sad", 0, .2)
174 }
175
176 if(GameInput.isPressed(GameInput.keys["u"]))
177 {
178 this.renderer.setAnimation("sleep", 0, .2)
179 }
180
181 if(GameInput.isPressed(GameInput.keys["i"]))
182 {
183 this.renderer.setAnimation("eatBig", 0, .2)
184 }
185
186 if(GameInput.isPressed(GameInput.keys["o"]))
187 {
188 this.renderer.setAnimation("sneeze", 0, .2)
189 }
190
191 if(GameInput.isPressed(GameInput.keys["p"]))
192 {
193 this.renderer.setAnimation("cry", 0, .2)
194 }
195
196 if(GameInput.isPressed(GameInput.keys["["]))
197 {
198 this.renderer.setAnimation("think2", 0, .2)
199 }
200 }
201 }
202}