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