BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / frog / frog.js

frog.js

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