| 1 | class 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 | |
| 119 | calculateTongueVectors () |
| 120 | { |
| 121 | let dir = new vector(this.currentFacing, -1); |
| 122 | this.tongueVector = this.transform.position.add(dir.stretch(this.tongueLength)).add(new vector(this.getXOffset(this.currentFacing), 0)); |
| 123 | |
| 124 | if (!this.isVectorWithinSceneY(this.tongueVector, this.scene)) |
| 125 | { |
| 126 | this.retracting = true; |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | if (this.should_bounce) |
| 131 | { |
| 132 | if ((this.tongueLengthBounce <= 0)) |
| 133 | { |
| 134 | this.tongueVectorBounceOrigin = this.tongueVector.getCopy(); |
| 135 | } |
| 136 | |
| 137 | dir = new vector(this.opposite_facing, -1); |
| 138 | this.tongueVectorBounce = this.tongueVectorBounceOrigin.add(dir.stretch(this.tongueLengthBounce)); |
| 139 | |
| 140 | if (this.tongueVectorBounce.y <= 0 || this.tongueVectorBounce.y >= this.scene.size.y) |
| 141 | { |
| 142 | this.retracting = true; |
| 143 | return; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | draw (context) |
| 149 | { |
| 150 | super.draw(context); |
| 151 | |
| 152 | |
| 153 | let amount = this.renderer.sprite.width / 2; |
| 154 | this.currentFacing == this.facing.right ? amount *= 1 : amount *= -1; |
| 155 | |
| 156 | let v = new vector(this.transform.position.x + amount, this.scene.real_size.y); |
| 157 | context.lineWidth = 3; |
| 158 | context.moveTo(v.x, v.y); |
| 159 | context.lineTo(v.x, v.y - 50); |
| 160 | context.stroke(); |
| 161 | |
| 162 | if (!this.tongue_retracted) |
| 163 | { |
| 164 | this.drawTongue(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | get tongue_retracted () |
| 169 | { |
| 170 | return this.tongueLength <= 0; |
| 171 | } |
| 172 | |
| 173 | get tongue_speed () |
| 174 | { |
| 175 | return 150 * this.scene.gameSpeed; |
| 176 | } |
| 177 | |
| 178 | get opposite_facing () |
| 179 | { |
| 180 | return this.currentFacing == this.facing.left ? this.facing.right : this.facing.left; |
| 181 | } |
| 182 | |
| 183 | get should_bounce () |
| 184 | { |
| 185 | return this.tongueVector && (!this.isVectorWithinSceneX(this.tongueVector, this.scene)); |
| 186 | } |
| 187 | |
| 188 | getXOffset (facing) |
| 189 | { |
| 190 | return facing > 0 ? 8 : -8; |
| 191 | } |
| 192 | |
| 193 | canMove (nextPos) |
| 194 | { |
| 195 | return nextPos + this.transform.size.x / 2 < this.scene.real_size.x && nextPos - this.transform.size.x / 2 > 0 && this.isFloor(); |
| 196 | } |
| 197 | |
| 198 | isVectorWithinSceneX (vector, scene) |
| 199 | { |
| 200 | return vector.x >= 0 && vector.x <= scene.real_size.x; |
| 201 | } |
| 202 | |
| 203 | isVectorWithinSceneY (vector, scene) |
| 204 | { |
| 205 | return vector.y >= 0 && vector.y <= scene.real_size.y; |
| 206 | } |
| 207 | |
| 208 | move (deltaTime) |
| 209 | { |
| 210 | if (!(GameInput.isHeld(GameInput.keys.ArrowRight) && GameInput.isHeld(GameInput.keys.ArrowLeft))) |
| 211 | { |
| 212 | if (GameInput.isHeld(GameInput.keys.ArrowRight)) |
| 213 | { |
| 214 | if (this.canMove(this.transform.position.x + this.speed * deltaTime)) |
| 215 | { |
| 216 | this.transform.position.x += this.speed * deltaTime; |
| 217 | } |
| 218 | |
| 219 | if (this.currentFacing != this.facing.right) |
| 220 | { |
| 221 | this.renderer.mirrorX = !this.renderer.mirrorX; |
| 222 | this.currentFacing = this.facing.right; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | if (GameInput.isHeld(GameInput.keys.ArrowLeft)) |
| 227 | { |
| 228 | if (this.canMove(this.transform.position.x - this.speed * deltaTime)) |
| 229 | { |
| 230 | this.transform.position.x -= this.speed * deltaTime; |
| 231 | } |
| 232 | |
| 233 | if (this.currentFacing != this.facing.left) |
| 234 | { |
| 235 | this.renderer.mirrorX = !this.renderer.mirrorX; |
| 236 | this.currentFacing = this.facing.left; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | this.idle = false; |
| 241 | this.renderer.setAnimation("walk", 0, .2); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | extendTongueLength (deltaTime) |
| 246 | { |
| 247 | this.renderer.setSubImage(3); |
| 248 | this.idle = false; |
| 249 | this.tongueLength += deltaTime * this.tongue_speed; |
| 250 | |
| 251 | if (this.should_bounce) |
| 252 | { |
| 253 | this.tongueLengthBounce += deltaTime * this.tongue_speed; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | retractTongueLength (deltaTime) |
| 258 | { |
| 259 | this.tongueLength -= deltaTime * this.tongue_speed * 2; |
| 260 | this.retracting = true; |
| 261 | |
| 262 | if (this.should_bounce) |
| 263 | { |
| 264 | this.tongueLengthBounce -= deltaTime * this.tongue_speed * 2; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | drawTongue () |
| 269 | { |
| 270 | let dir = new vector(this.currentFacing, -1); |
| 271 | |
| 272 | this.frog_tongue_tip.draw(this.scene, |
| 273 | this.tongueVector.x, this.tongueVector.y, |
| 274 | this.frog_tongue_tip.width, this.frog_tongue_tip.height, |
| 275 | dir.toAngle() - 90, 0, 0, 1, 0); |
| 276 | |
| 277 | this.frog_tongue_base.draw( |
| 278 | this.scene, |
| 279 | (this.transform.position.x + this.tongueVector.x + this.getXOffset(this.currentFacing)) / 2, (this.transform.position.y + this.tongueVector.y) / 2, |
| 280 | this.frog_tongue_base.width * (this.tongueLength / 12.5), this.frog_tongue_base.height, |
| 281 | dir.toAngle() - 90, 0, 0, 1, 0); |
| 282 | |
| 283 | if (this.should_bounce) |
| 284 | { |
| 285 | dir = new vector(this.opposite_facing, -1); |
| 286 | |
| 287 | this.frog_tongue_tip.draw(this.scene, |
| 288 | this.tongueVectorBounce.x, this.tongueVectorBounce.y, |
| 289 | this.frog_tongue_tip.width, this.frog_tongue_tip.height, |
| 290 | dir.toAngle() - 90, 0, 0, 1, 0); |
| 291 | |
| 292 | this.frog_tongue_base.draw( |
| 293 | this.scene, |
| 294 | (this.tongueVectorBounceOrigin.x + this.tongueVectorBounce.x) / 2, (this.tongueVectorBounceOrigin.y + this.tongueVectorBounce.y) / 2, |
| 295 | this.frog_tongue_base.width * (this.tongueLengthBounce / 12.5), this.frog_tongue_base.height, |
| 296 | dir.toAngle() - 90, 0, 0, 1, 0); |
| 297 | } |
| 298 | } |
| 299 | } |