class Frog extends GameObject { speed = 100; facing = { left: -.8, right:.8 }; currentFacing = this.facing.right; idle = true; tongueVector = null; tongueVectorBounce = null; tongueVectorBounceOrigin = null; tongueLength = 0; tongueLengthBounce = 0; retracting = false; frog_tongue_base = null; frog_tongue_tip = null; constructor(x,y,sprite,layer) { super(x,y,sprite,layer); this.collider.enabled = true; } addedToScene() { this.frog_tongue_base = this.scene.game.images.frog_tongue_base; this.frog_tongue_tip = this.scene.game.images.frog_tongue_tip; } update(deltaTime) { super.update(deltaTime); this.idle = true; if(this.tongue_retracted) { if(GameInput.isHeld(GameInput.keys.ArrowLeft) || GameInput.isHeld(GameInput.keys.ArrowRight)) { this.move(deltaTime); } } if(GameInput.isHeld(GameInput.keys[" "]) && !this.retracting) { this.extendTongueLength(deltaTime); } else { if(!this.tongue_retracted) { this.retractTongueLength(deltaTime); } else { this.retracting = false; this.tongueLength = 0; this.tongueLengthBounce = 0; } } if(!this.tongue_retracted) { this.calculateTongueVectors(); } if(this.idle && this.tongue_retracted && !this.retracting) { this.renderer.setSubImage(0); } } calculateTongueVectors() { let dir = new vector(this.currentFacing, -1); this.tongueVector = this.transform.position.add(dir.stretch(this.tongueLength)).add(new vector(this.getXOffset(this.currentFacing), 0)); if(!this.isVectorWithinSceneY(this.tongueVector, this.scene)) { this.retracting = true; return; } if(this.should_bounce) { if((this.tongueLengthBounce <= 0)) { this.tongueVectorBounceOrigin = this.tongueVector.getCopy(); } dir = new vector(this.opposite_facing, -1); this.tongueVectorBounce = this.tongueVectorBounceOrigin.add(dir.stretch(this.tongueLengthBounce)); if(this.tongueVectorBounce.y <= 0 || this.tongueVectorBounce.y >= this.scene.size.y) { this.retracting = true; return; } } } draw(context) { super.draw(context); if(!this.tongue_retracted) { this.drawTongue(); } } get tongue_retracted() { return this.tongueLength <= 0; } get tongue_speed() { return 150 * this.scene.gameSpeed; } get opposite_facing() { return this.currentFacing == this.facing.left ? this.facing.right : this.facing.left; } get should_bounce() { return this.tongueVector && (!this.isVectorWithinSceneX(this.tongueVector, this.scene)); } getXOffset(facing) { return facing > 0 ? 8 : -8; } canMove(nextPos) { return nextPos + this.transform.size.x / 2 < this.scene.real_size.x && nextPos - this.transform.size.x / 2 > 0 } isVectorWithinSceneX(vector, scene) { return vector.x >= 0 && vector.x <= scene.real_size.x; } isVectorWithinSceneY(vector, scene) { return vector.y >= 0 && vector.y <= scene.real_size.y; } move(deltaTime) { if(! (GameInput.isHeld(GameInput.keys.ArrowRight) && GameInput.isHeld(GameInput.keys.ArrowLeft))) { if(GameInput.isHeld(GameInput.keys.ArrowRight)) { if(this.canMove(this.transform.position.x + this.speed * deltaTime)) { this.transform.position.x += this.speed * deltaTime; } if(this.currentFacing != this.facing.right) { this.renderer.mirrorX = !this.renderer.mirrorX; this.currentFacing = this.facing.right; } } if(GameInput.isHeld(GameInput.keys.ArrowLeft)) { if(this.canMove(this.transform.position.x - this.speed * deltaTime)) { this.transform.position.x -= this.speed * deltaTime; } if(this.currentFacing != this.facing.left) { this.renderer.mirrorX = !this.renderer.mirrorX; this.currentFacing = this.facing.left; } } this.idle = false; this.renderer.setAnimation("walk", 0, .2); } } extendTongueLength(deltaTime) { this.renderer.setSubImage(3); this.idle = false; this.tongueLength += deltaTime * this.tongue_speed; if(this.should_bounce) { this.tongueLengthBounce += deltaTime * this.tongue_speed; } } retractTongueLength(deltaTime) { this.tongueLength -= deltaTime * this.tongue_speed * 2; this.retracting = true; if(this.should_bounce) { this.tongueLengthBounce -= deltaTime * this.tongue_speed * 2; } } drawTongue() { let dir = new vector(this.currentFacing, -1); this.frog_tongue_tip.draw(this.scene, 0, 0, this.frog_tongue_tip.width, this.frog_tongue_tip.height, this.tongueVector.x, this.tongueVector.y, this.frog_tongue_tip.width, this.frog_tongue_tip.height, dir.toAngle() -90, 0, 0, 1) this.frog_tongue_base.draw( this.scene, 0, 0, this.frog_tongue_base.width, this.frog_tongue_base.height, (this.transform.position.x + this.tongueVector.x + this.getXOffset(this.currentFacing)) / 2, (this.transform.position.y + this.tongueVector.y) / 2, this.frog_tongue_base.width * (this.tongueLength / 12.5), this.frog_tongue_base.height, dir.toAngle() -90, 0, 0, 1) if(this.should_bounce) { dir = new vector(this.opposite_facing, -1); this.frog_tongue_tip.draw(this.scene, 0, 0, this.frog_tongue_tip.width, this.frog_tongue_tip.height, this.tongueVectorBounce.x, this.tongueVectorBounce.y, this.frog_tongue_tip.width, this.frog_tongue_tip.height, dir.toAngle() -90, 0, 0, 1) this.frog_tongue_base.draw( this.scene, 0, 0, this.frog_tongue_base.width, this.frog_tongue_base.height, (this.tongueVectorBounceOrigin.x + this.tongueVectorBounce.x) / 2, (this.tongueVectorBounceOrigin.y + this.tongueVectorBounce.y) / 2, this.frog_tongue_base.width * (this.tongueLengthBounce / 12.5), this.frog_tongue_base.height, dir.toAngle() -90, 0, 0, 1) } } }