BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / frog / frog.js

frog.js

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