BlueGit

blue-star speeltuin

Files

speeltuin / src / objects / chao.js

chao.js

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