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