BlueGit

blue-star speeltuin

remove keyboard controls, correct behaviour functions

timfa Tim · pushed by timfa

Authored 2022-10-19 · 4 years ago

Hash
00ce5843e242590fd34db0bbc38c5e1e96956391
Parent
6b5802f6
1 file changed +20 −175
src/objects/chao.js +20 −175
@@ -3,16 +3,21 @@ class Chao extends GameObject
3 3 speed = 40;
4 4 scenePadding = 50;
5 5 targetDestination = null;
6 busyWithBehaviour = false;
7 6
8 7 minTimeBetweenBehaviour = 3; //in seconds
9 8 maxTimeBetweenbehaviour = 10; // in seconds
10 9 behaviourTimer = 10;
11 10
12 11 behaviourList = [];
12 currentBehaviour = null;
13 13
14 14 static Instance;
15 15
16 get busyWithBehaviour()
17 {
18 return this.currentBehaviour != null;
19 }
20
16 21 constructor(x, y, sprite, layer)
17 22 {
18 23 super(x, y, sprite, layer);
@@ -26,14 +31,18 @@ class Chao extends GameObject
26 31 this.behaviourList.push(behaviour);
27 32 }
28 33
29 behaviourCompleted()
34 endBehaviour()
30 35 {
31 this.busyWithBehaviour = false;
36 this.currentBehaviour = null;
37 this.behaviourTimer = Math.randomRange(this.minTimeBetweenBehaviour, this.maxTimeBetweenbehaviour);
32 38 }
33 39
34 40 addedToScene()
35 41 {
36 42 this.renderer.sprite = this.scene.game.chaoImages.random();
43
44 this.renderer.defaultAnimationSpeed = 0.2;
45 this.registerBehaviour(new BehaviourSneeze());
37 46 }
38 47
39 48 onClick(btn)
@@ -49,181 +58,17 @@ class Chao extends GameObject
49 58 update(deltaTime)
50 59 {
51 60 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 }
61
62 if(this.busyWithBehaviour)
63 this.currentBehaviour.update(deltaTime);
122 64 else
123 65 {
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["["]))
66 this.behaviourTimer -= deltaTime;
67
68 if(this.behaviourTimer <= 0)
225 69 {
226 this.renderer.setAnimation("think2", 0, .2)
70 this.currentBehaviour = this.behaviourList.random();
71 this.currentBehaviour.startBehaviour();
227 72 }
228 73 }
229 74 }
@@ -3,16 +3,21 @@ class Chao extends GameObject
3 speed = 40;
3 speed = 40;
4 scenePadding = 50;
4 scenePadding = 50;
5 targetDestination = null;
5 targetDestination = null;
6 busyWithBehaviour = false;
7
6
8 minTimeBetweenBehaviour = 3; //in seconds
7 minTimeBetweenBehaviour = 3; //in seconds
9 maxTimeBetweenbehaviour = 10; // in seconds
8 maxTimeBetweenbehaviour = 10; // in seconds
10 behaviourTimer = 10;
9 behaviourTimer = 10;
11
10
12 behaviourList = [];
11 behaviourList = [];
12 currentBehaviour = null;
13
13
14 static Instance;
14 static Instance;
15
15
16 get busyWithBehaviour()
17 {
18 return this.currentBehaviour != null;
19 }
20
16 constructor(x, y, sprite, layer)
21 constructor(x, y, sprite, layer)
17 {
22 {
18 super(x, y, sprite, layer);
23 super(x, y, sprite, layer);
@@ -26,14 +31,18 @@ class Chao extends GameObject
26 this.behaviourList.push(behaviour);
31 this.behaviourList.push(behaviour);
27 }
32 }
28
33
29 behaviourCompleted()
34 endBehaviour()
30 {
35 {
31 this.busyWithBehaviour = false;
36 this.currentBehaviour = null;
37 this.behaviourTimer = Math.randomRange(this.minTimeBetweenBehaviour, this.maxTimeBetweenbehaviour);
32 }
38 }
33
39
34 addedToScene()
40 addedToScene()
35 {
41 {
36 this.renderer.sprite = this.scene.game.chaoImages.random();
42 this.renderer.sprite = this.scene.game.chaoImages.random();
43
44 this.renderer.defaultAnimationSpeed = 0.2;
45 this.registerBehaviour(new BehaviourSneeze());
37 }
46 }
38
47
39 onClick(btn)
48 onClick(btn)
@@ -49,181 +58,17 @@ class Chao extends GameObject
49 update(deltaTime)
58 update(deltaTime)
50 {
59 {
51 super.update(deltaTime);
60 super.update(deltaTime);
52 this.wasHeld = this.wasHeld || {}
61
53
62 if(this.busyWithBehaviour)
54 if(this.wasHeld.right && !GameInput.isHeld(GameInput.keys.ArrowRight))
63 this.currentBehaviour.update(deltaTime);
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
64 else
123 {
65 {
124 if(GameInput.isPressed(GameInput.keys["1"]))
66 this.behaviourTimer -= deltaTime;
125 {
67
126 this.renderer.setAnimation("tripDown", 0, .2)
68 if(this.behaviourTimer <= 0)
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 {
69 {
226 this.renderer.setAnimation("think2", 0, .2)
70 this.currentBehaviour = this.behaviourList.random();
71 this.currentBehaviour.startBehaviour();
227 }
72 }
228 }
73 }
229 }
74 }