BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / frog / playerFrog.js

playerFrog.js

a6acb1c9 History
playerFrog.js 9.3 KiB Download Raw
1class PlayerFrog extends GameObject
2{
3
4 speed = 100;
5
6 tongue_speed_max = 400;
7
8 facing =
9 {
10 left: -.8,
11 right: .8
12 };
13
14 currentFacing = this.facing.right;
15 idle = true;
16
17 tongueVector = null;
18 tongueVectorBounce = null;
19
20 tongueVectorBounceOrigin = null;
21
22 tongueLength = 0;
23 tongueLengthBounce = 0;
24 retracting = false;
25
26 frog_tongue_base = null;
27 frog_tongue_tip = null;
28
29 tongue_obj_ref = null;
30
31 g_target_y = 0;
32
33 constructor (x, y, sprite, layer)
34 {
35 super(x, y, sprite, layer);
36 this.collider.enabled = false;
37
38 this.transform.position.y = 0;
39 this.g_target_y = y;
40 this.posScaleY = 0;
41
42 new TWEEN.Tween(this)
43 .to({ posScaleY: 1 }, 800)
44 .easing(TWEEN.Easing.Elastic.Out)
45 .start()
46 .onComplete(() => this.collider.enabled = true);
47 }
48
49 addedToScene ()
50 {
51 this.frog_tongue_base = this.scene.game.images.frog_tongue_base;
52 this.frog_tongue_tip = this.scene.game.images.frog_tongue_tip;
53 }
54
55 onCollision (obj)
56 {
57 if (obj instanceof FrogFruit)
58 {
59 this.scene.gameOver();
60 }
61 }
62
63 isFloor ()
64 {
65 let amount = this.renderer.sprite.width / 2;
66 this.currentFacing == this.facing.right ? amount *= 1 : amount *= -1;
67
68 let obj = Ray.Cast(this.scene, new vector(this.transform.position.x + amount, this.scene.real_size.y), new vector(0, -1), 50);
69
70 return obj && obj.hitObj instanceof FrogFloorTile;
71 }
72
73 update (deltaTime)
74 {
75 super.update(deltaTime);
76
77 this.animate();
78 this.idle = true;
79
80 if (this.tongue_retracted)
81 {
82 if (GameInput.isHeld(GameInput.keys.ArrowLeft) || GameInput.isHeld(GameInput.keys.ArrowRight))
83 {
84 this.move(deltaTime);
85
86 }
87 }
88
89 if (GameInput.isHeld(GameInput.keys[" "]) && !this.retracting)
90 {
91 this.extendTongueLength(deltaTime);
92
93 this.checkCollision(deltaTime);
94 }
95 else
96 {
97 if (!this.tongue_retracted)
98 {
99 this.retractTongueLength(deltaTime);
100 this.updateFruitObjRef();
101 }
102 else
103 {
104 this.retracting = false;
105 this.tongueLength = 0;
106 this.tongueLengthBounce = 0;
107 this.tongueVector = null;
108 this.tongueVectorBounce = null;
109 if (this.tongue_obj_ref)
110 {
111 this.scene.consumeFruit(this.tongue_obj_ref);
112 this.tongue_obj_ref = null;
113 }
114 }
115 }
116
117 if (!this.tongue_retracted)
118 {
119 this.calculateTongueVectors();
120 }
121
122 if (this.idle && this.tongue_retracted && !this.retracting)
123 {
124 this.renderer.setSubImage(0);
125 }
126
127
128 }
129
130 checkCollision (deltaTime)
131 {
132 let collisionVector = this.tongueVectorBounce ? this.tongueVectorBounce : this.tongueVector;
133
134 if (!collisionVector)
135 {
136 return;
137 }
138
139 let obj = Ray.Cast(this.scene,
140 collisionVector,
141 new vector(this.tongueVectorBounce ? this.opposite_facing : this.currentFacing, -1),
142 this.tongue_speed * 5 * deltaTime);
143
144 if (obj)
145 {
146 if (obj.hitObj instanceof FrogFruit)
147 {
148 obj = obj.hitObj;
149 obj.collider.enabled = false;
150 this.tongue_obj_ref = obj;
151
152 this.retracting = true;
153 }
154 }
155 }
156
157 calculateTongueVectors ()
158 {
159 let dir = new vector(this.currentFacing, -1);
160 this.tongueVector = this.transform.position.add(dir.stretch(this.tongueLength)).add(new vector(this.getXOffset(this.currentFacing), 0));
161
162 if (!this.isVectorWithinSceneY(this.tongueVector, this.scene))
163 {
164 this.retracting = true;
165 return;
166 }
167
168 if (this.should_bounce)
169 {
170 if ((this.tongueLengthBounce <= 0))
171 {
172 this.tongueVectorBounceOrigin = this.tongueVector.getCopy();
173 }
174
175 dir = new vector(this.opposite_facing, -1);
176 this.tongueVectorBounce = this.tongueVectorBounceOrigin.add(dir.stretch(this.tongueLengthBounce));
177
178 if (this.tongueVectorBounce.y <= 0 || this.tongueVectorBounce.y >= this.scene.size.y)
179 {
180 this.retracting = true;
181 return;
182 }
183 }
184 else
185 {
186 this.tongueVectorBounce = null;
187 }
188 }
189
190 draw (context)
191 {
192 super.draw(context);
193
194 if (!this.tongue_retracted)
195 {
196 this.drawTongue();
197 }
198 }
199
200 get tongue_retracted ()
201 {
202 return this.tongueLength <= 0;
203 }
204
205 get tongue_speed ()
206 {
207 return Math.min(this.tongue_speed_max, 150 * (1 + this.scene.gameSpeed * .2));
208 }
209
210 get opposite_facing ()
211 {
212 return this.currentFacing == this.facing.left ? this.facing.right : this.facing.left;
213 }
214
215 get should_bounce ()
216 {
217 return this.tongueVector && (!this.isVectorWithinSceneX(this.tongueVector, this.scene));
218 }
219
220 getXOffset (facing)
221 {
222 return facing > 0 ? 8 : -8;
223 }
224
225 canMove (nextPos)
226 {
227 return nextPos + this.transform.size.x / 2 < this.scene.real_size.x && nextPos - this.transform.size.x / 2 > 0 && this.isFloor();
228 }
229
230 isVectorWithinSceneX (vector, scene)
231 {
232 return vector.x >= 0 && vector.x <= scene.real_size.x;
233 }
234
235 isVectorWithinSceneY (vector, scene)
236 {
237 return vector.y >= 0 && vector.y <= scene.real_size.y;
238 }
239
240 move (deltaTime)
241 {
242 if (!(GameInput.isHeld(GameInput.keys.ArrowRight) && GameInput.isHeld(GameInput.keys.ArrowLeft)))
243 {
244 if (GameInput.isHeld(GameInput.keys.ArrowRight))
245 {
246 if (this.canMove(this.transform.position.x + this.speed * deltaTime))
247 {
248 this.transform.position.x += this.speed * deltaTime;
249 }
250
251 if (this.currentFacing != this.facing.right)
252 {
253 this.renderer.mirrorX = !this.renderer.mirrorX;
254 this.currentFacing = this.facing.right;
255 }
256 }
257
258 if (GameInput.isHeld(GameInput.keys.ArrowLeft))
259 {
260 if (this.canMove(this.transform.position.x - this.speed * deltaTime))
261 {
262 this.transform.position.x -= this.speed * deltaTime;
263 }
264
265 if (this.currentFacing != this.facing.left)
266 {
267 this.renderer.mirrorX = !this.renderer.mirrorX;
268 this.currentFacing = this.facing.left;
269 }
270 }
271
272 this.idle = false;
273 this.renderer.setAnimation("walk", 0, .2);
274 }
275 }
276
277 updateFruitObjRef ()
278 {
279 if (this.tongue_obj_ref && (this.tongueVectorBounce || this.tongueVector))
280 {
281 this.tongue_obj_ref.transform.position = this.tongueVectorBounce ? this.tongueVectorBounce : this.tongueVector;
282 }
283 }
284
285 extendTongueLength (deltaTime)
286 {
287 this.renderer.setSubImage(3);
288 this.idle = false;
289 this.tongueLength += deltaTime * this.tongue_speed;
290
291 if (this.should_bounce)
292 {
293 this.tongueLengthBounce += deltaTime * this.tongue_speed;
294 }
295 }
296
297 retractTongueLength (deltaTime)
298 {
299 this.tongueLength -= deltaTime * this.tongue_speed * 2;
300 this.retracting = true;
301
302 if (this.should_bounce)
303 {
304 this.tongueLengthBounce -= deltaTime * this.tongue_speed * 2;
305 }
306 }
307
308 drawTongue ()
309 {
310 let dir = new vector(this.currentFacing, -1);
311
312 this.frog_tongue_tip.draw(this.scene,
313 this.tongueVector.x, this.tongueVector.y,
314 this.frog_tongue_tip.width, this.frog_tongue_tip.height,
315 dir.toAngle() - 90, 0, 0, 1, 0);
316
317 this.frog_tongue_base.draw(
318 this.scene,
319 (this.transform.position.x + this.tongueVector.x + this.getXOffset(this.currentFacing)) / 2, (this.transform.position.y + this.tongueVector.y) / 2,
320 this.frog_tongue_base.width * (this.tongueLength / 12.5), this.frog_tongue_base.height,
321 dir.toAngle() - 90, 0, 0, 1, 0);
322
323 if (this.should_bounce && this.tongueVectorBounce)
324 {
325 dir = new vector(this.opposite_facing, -1);
326
327 this.frog_tongue_tip.draw(this.scene,
328 this.tongueVectorBounce.x, this.tongueVectorBounce.y,
329 this.frog_tongue_tip.width, this.frog_tongue_tip.height,
330 dir.toAngle() - 90, 0, 0, 1, 0);
331
332 this.frog_tongue_base.draw(
333 this.scene,
334 (this.tongueVectorBounceOrigin.x + this.tongueVectorBounce.x) / 2, (this.tongueVectorBounceOrigin.y + this.tongueVectorBounce.y) / 2,
335 this.frog_tongue_base.width * (this.tongueLengthBounce / 12.5), this.frog_tongue_base.height,
336 dir.toAngle() - 90, 0, 0, 1, 0);
337 }
338 }
339
340 animate ()
341 {
342 if (this.transform.position.y != this.g_target_y)
343 {
344 this.transform.position.y = this.g_target_y * this.posScaleY;
345 }
346 }
347}