BlueGit

blue-star speeltuin

Files

speeltuin / src / objects / chao.js

chao.js

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