BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / frog / frog.js

frog.js

removal of stuff
timfa wsadhelios · pushed by timfa · 4 years ago
63596569 History
frog.js 9.5 KiB Download Raw
1class Frog 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 Fruit)
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 FloorTile;
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, collisionVector, new vector(this.tongueVectorBounce ? this.currentFacing : this.opposite_facing, -1), this.tongue_speed * 2 * deltaTime);
140
141 let obj = Ray.Cast(this.scene,
142 collisionVector,
143 new vector(this.tongueVectorBounce ? this.opposite_facing : this.currentFacing, -1),
144 this.tongue_speed * 5 * deltaTime);
145
146 if (obj)
147 {
148 if (obj.hitObj instanceof Fruit)
149 {
150 obj = obj.hitObj;
151 obj.collider.enabled = false;
152 this.tongue_obj_ref = obj;
153
154 this.retracting = true;
155 }
156 }
157 }
158
159 calculateTongueVectors ()
160 {
161 let dir = new vector(this.currentFacing, -1);
162 this.tongueVector = this.transform.position.add(dir.stretch(this.tongueLength)).add(new vector(this.getXOffset(this.currentFacing), 0));
163
164 if (!this.isVectorWithinSceneY(this.tongueVector, this.scene))
165 {
166 this.retracting = true;
167 return;
168 }
169
170 if (this.should_bounce)
171 {
172 if ((this.tongueLengthBounce <= 0))
173 {
174 this.tongueVectorBounceOrigin = this.tongueVector.getCopy();
175 }
176
177 dir = new vector(this.opposite_facing, -1);
178 this.tongueVectorBounce = this.tongueVectorBounceOrigin.add(dir.stretch(this.tongueLengthBounce));
179
180 if (this.tongueVectorBounce.y <= 0 || this.tongueVectorBounce.y >= this.scene.size.y)
181 {
182 this.retracting = true;
183 return;
184 }
185 }
186 else
187 {
188 this.tongueVectorBounce = null;
189 }
190 }
191
192 draw (context)
193 {
194 super.draw(context);
195
196 if (!this.tongue_retracted)
197 {
198 this.drawTongue();
199 }
200 }
201
202 get tongue_retracted ()
203 {
204 return this.tongueLength <= 0;
205 }
206
207 get tongue_speed ()
208 {
209 return Math.min(this.tongue_speed_max, 150 * (1 + this.scene.gameSpeed * .2));
210 }
211
212 get opposite_facing ()
213 {
214 return this.currentFacing == this.facing.left ? this.facing.right : this.facing.left;
215 }
216
217 get should_bounce ()
218 {
219 return this.tongueVector && (!this.isVectorWithinSceneX(this.tongueVector, this.scene));
220 }
221
222 getXOffset (facing)
223 {
224 return facing > 0 ? 8 : -8;
225 }
226
227 canMove (nextPos)
228 {
229 return nextPos + this.transform.size.x / 2 < this.scene.real_size.x && nextPos - this.transform.size.x / 2 > 0 && this.isFloor();
230 }
231
232 isVectorWithinSceneX (vector, scene)
233 {
234 return vector.x >= 0 && vector.x <= scene.real_size.x;
235 }
236
237 isVectorWithinSceneY (vector, scene)
238 {
239 return vector.y >= 0 && vector.y <= scene.real_size.y;
240 }
241
242 move (deltaTime)
243 {
244 if (!(GameInput.isHeld(GameInput.keys.ArrowRight) && GameInput.isHeld(GameInput.keys.ArrowLeft)))
245 {
246 if (GameInput.isHeld(GameInput.keys.ArrowRight))
247 {
248 if (this.canMove(this.transform.position.x + this.speed * deltaTime))
249 {
250 this.transform.position.x += this.speed * deltaTime;
251 }
252
253 if (this.currentFacing != this.facing.right)
254 {
255 this.renderer.mirrorX = !this.renderer.mirrorX;
256 this.currentFacing = this.facing.right;
257 }
258 }
259
260 if (GameInput.isHeld(GameInput.keys.ArrowLeft))
261 {
262 if (this.canMove(this.transform.position.x - this.speed * deltaTime))
263 {
264 this.transform.position.x -= this.speed * deltaTime;
265 }
266
267 if (this.currentFacing != this.facing.left)
268 {
269 this.renderer.mirrorX = !this.renderer.mirrorX;
270 this.currentFacing = this.facing.left;
271 }
272 }
273
274 this.idle = false;
275 this.renderer.setAnimation("walk", 0, .2);
276 }
277 }
278
279 updateFruitObjRef ()
280 {
281 if (this.tongue_obj_ref && (this.tongueVectorBounce || this.tongueVector))
282 {
283 this.tongue_obj_ref.transform.position = this.tongueVectorBounce ? this.tongueVectorBounce : this.tongueVector;
284 }
285 }
286
287 extendTongueLength (deltaTime)
288 {
289 this.renderer.setSubImage(3);
290 this.idle = false;
291 this.tongueLength += deltaTime * this.tongue_speed;
292
293 if (this.should_bounce)
294 {
295 this.tongueLengthBounce += deltaTime * this.tongue_speed;
296 }
297 }
298
299 retractTongueLength (deltaTime)
300 {
301 this.tongueLength -= deltaTime * this.tongue_speed * 2;
302 this.retracting = true;
303
304 if (this.should_bounce)
305 {
306 this.tongueLengthBounce -= deltaTime * this.tongue_speed * 2;
307 }
308 }
309
310 drawTongue ()
311 {
312 let dir = new vector(this.currentFacing, -1);
313
314 this.frog_tongue_tip.draw(this.scene,
315 this.tongueVector.x, this.tongueVector.y,
316 this.frog_tongue_tip.width, this.frog_tongue_tip.height,
317 dir.toAngle() - 90, 0, 0, 1, 0);
318
319 this.frog_tongue_base.draw(
320 this.scene,
321 (this.transform.position.x + this.tongueVector.x + this.getXOffset(this.currentFacing)) / 2, (this.transform.position.y + this.tongueVector.y) / 2,
322 this.frog_tongue_base.width * (this.tongueLength / 12.5), this.frog_tongue_base.height,
323 dir.toAngle() - 90, 0, 0, 1, 0);
324
325 if (this.should_bounce && this.tongueVectorBounce)
326 {
327 dir = new vector(this.opposite_facing, -1);
328
329 this.frog_tongue_tip.draw(this.scene,
330 this.tongueVectorBounce.x, this.tongueVectorBounce.y,
331 this.frog_tongue_tip.width, this.frog_tongue_tip.height,
332 dir.toAngle() - 90, 0, 0, 1, 0);
333
334 this.frog_tongue_base.draw(
335 this.scene,
336 (this.tongueVectorBounceOrigin.x + this.tongueVectorBounce.x) / 2, (this.tongueVectorBounceOrigin.y + this.tongueVectorBounce.y) / 2,
337 this.frog_tongue_base.width * (this.tongueLengthBounce / 12.5), this.frog_tongue_base.height,
338 dir.toAngle() - 90, 0, 0, 1, 0);
339 }
340 }
341
342 animate ()
343 {
344 if (this.transform.position.y != this.g_target_y)
345 {
346 this.transform.position.y = this.g_target_y * this.posScaleY;
347 }
348 }
349}