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