BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / frog / frog.js

frog.js

remove line
timfa wsadhelios · pushed by timfa · 4 years ago
a0e57036 History
frog.js 7.8 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 floorTileConnections = [];
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 }
84 else
85 {
86 this.retracting = false;
87 this.tongueLength = 0;
88 this.tongueLengthBounce = 0;
89 this.tongueVector = null;
90 this.tongueVectorBounce = null;
91 }
92 }
93
94 if (!this.tongue_retracted)
95 {
96 this.calculateTongueVectors();
97 }
98
99 if (this.idle && this.tongue_retracted && !this.retracting)
100 {
101 this.renderer.setSubImage(0);
102 }
103
104
105 }
106
107 checkCollision ()
108 {
109 let collisionVector = this.tongueVectorBounce ? this.tongueVectorBounce : this.tongueVector;
110
111 if (!collisionVector)
112 {
113 return;
114 }
115
116 let obj = Ray.Cast(this.scene, collisionVector, new vector(this.tongueVectorBounce ? this.currentFacing : this.opposite_facing, -1), 5);
117
118 obj.collider.enabled = false;
119
120
121 }
122
123 calculateTongueVectors ()
124 {
125 let dir = new vector(this.currentFacing, -1);
126 this.tongueVector = this.transform.position.add(dir.stretch(this.tongueLength)).add(new vector(this.getXOffset(this.currentFacing), 0));
127
128 if (!this.isVectorWithinSceneY(this.tongueVector, this.scene))
129 {
130 this.retracting = true;
131 return;
132 }
133
134 if (this.should_bounce)
135 {
136 if ((this.tongueLengthBounce <= 0))
137 {
138 this.tongueVectorBounceOrigin = this.tongueVector.getCopy();
139 }
140
141 dir = new vector(this.opposite_facing, -1);
142 this.tongueVectorBounce = this.tongueVectorBounceOrigin.add(dir.stretch(this.tongueLengthBounce));
143
144 if (this.tongueVectorBounce.y <= 0 || this.tongueVectorBounce.y >= this.scene.size.y)
145 {
146 this.retracting = true;
147 return;
148 }
149 }
150 }
151
152 draw (context)
153 {
154 super.draw(context);
155
156 if (!this.tongue_retracted)
157 {
158 this.drawTongue();
159 }
160 }
161
162 get tongue_retracted ()
163 {
164 return this.tongueLength <= 0;
165 }
166
167 get tongue_speed ()
168 {
169 return 150 * this.scene.gameSpeed;
170 }
171
172 get opposite_facing ()
173 {
174 return this.currentFacing == this.facing.left ? this.facing.right : this.facing.left;
175 }
176
177 get should_bounce ()
178 {
179 return this.tongueVector && (!this.isVectorWithinSceneX(this.tongueVector, this.scene));
180 }
181
182 getXOffset (facing)
183 {
184 return facing > 0 ? 8 : -8;
185 }
186
187 canMove (nextPos)
188 {
189 return nextPos + this.transform.size.x / 2 < this.scene.real_size.x && nextPos - this.transform.size.x / 2 > 0 && this.isFloor();
190 }
191
192 isVectorWithinSceneX (vector, scene)
193 {
194 return vector.x >= 0 && vector.x <= scene.real_size.x;
195 }
196
197 isVectorWithinSceneY (vector, scene)
198 {
199 return vector.y >= 0 && vector.y <= scene.real_size.y;
200 }
201
202 move (deltaTime)
203 {
204 if (!(GameInput.isHeld(GameInput.keys.ArrowRight) && GameInput.isHeld(GameInput.keys.ArrowLeft)))
205 {
206 if (GameInput.isHeld(GameInput.keys.ArrowRight))
207 {
208 if (this.canMove(this.transform.position.x + this.speed * deltaTime))
209 {
210 this.transform.position.x += this.speed * deltaTime;
211 }
212
213 if (this.currentFacing != this.facing.right)
214 {
215 this.renderer.mirrorX = !this.renderer.mirrorX;
216 this.currentFacing = this.facing.right;
217 }
218 }
219
220 if (GameInput.isHeld(GameInput.keys.ArrowLeft))
221 {
222 if (this.canMove(this.transform.position.x - this.speed * deltaTime))
223 {
224 this.transform.position.x -= this.speed * deltaTime;
225 }
226
227 if (this.currentFacing != this.facing.left)
228 {
229 this.renderer.mirrorX = !this.renderer.mirrorX;
230 this.currentFacing = this.facing.left;
231 }
232 }
233
234 this.idle = false;
235 this.renderer.setAnimation("walk", 0, .2);
236 }
237 }
238
239 extendTongueLength (deltaTime)
240 {
241 this.renderer.setSubImage(3);
242 this.idle = false;
243 this.tongueLength += deltaTime * this.tongue_speed;
244
245 if (this.should_bounce)
246 {
247 this.tongueLengthBounce += deltaTime * this.tongue_speed;
248 }
249 }
250
251 retractTongueLength (deltaTime)
252 {
253 this.tongueLength -= deltaTime * this.tongue_speed * 2;
254 this.retracting = true;
255
256 if (this.should_bounce)
257 {
258 this.tongueLengthBounce -= deltaTime * this.tongue_speed * 2;
259 }
260 }
261
262 drawTongue ()
263 {
264 let dir = new vector(this.currentFacing, -1);
265
266 this.frog_tongue_tip.draw(this.scene,
267 this.tongueVector.x, this.tongueVector.y,
268 this.frog_tongue_tip.width, this.frog_tongue_tip.height,
269 dir.toAngle() - 90, 0, 0, 1, 0);
270
271 this.frog_tongue_base.draw(
272 this.scene,
273 (this.transform.position.x + this.tongueVector.x + this.getXOffset(this.currentFacing)) / 2, (this.transform.position.y + this.tongueVector.y) / 2,
274 this.frog_tongue_base.width * (this.tongueLength / 12.5), this.frog_tongue_base.height,
275 dir.toAngle() - 90, 0, 0, 1, 0);
276
277 if (this.should_bounce)
278 {
279 dir = new vector(this.opposite_facing, -1);
280
281 this.frog_tongue_tip.draw(this.scene,
282 this.tongueVectorBounce.x, this.tongueVectorBounce.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.tongueVectorBounceOrigin.x + this.tongueVectorBounce.x) / 2, (this.tongueVectorBounceOrigin.y + this.tongueVectorBounce.y) / 2,
289 this.frog_tongue_base.width * (this.tongueLengthBounce / 12.5), this.frog_tongue_base.height,
290 dir.toAngle() - 90, 0, 0, 1, 0);
291 }
292 }
293}