BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / frog / frog.js

frog.js

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