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; floorTileConnections = []; 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; } onCollision(obj) { } isFloor() { let amount = this.renderer.sprite.width / 2; this.currentFacing == this.facing.right ? amount *= 1 : amount *= -1; let obj = Ray.Cast(this.scene, new vector(this.transform.position.x + amount, this.scene.real_size.y), new vector(0,-1),50); return obj && obj.hitObj instanceof FloorTile; } update(deltaTime) { (1/deltaTime < 50) ? console.log(1/deltaTime) : null; 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); this.checkCollision(); } else { if(!this.tongue_retracted) { this.retractTongueLength(deltaTime); } else { this.retracting = false; this.tongueLength = 0; this.tongueLengthBounce = 0; this.tongueVector = null; this.tongueVectorBounce = null; } } if(!this.tongue_retracted) { this.calculateTongueVectors(); } if(this.idle && this.tongue_retracted && !this.retracting) { this.renderer.setSubImage(0); } } checkCollision() { let collisionVector = this.tongueVectorBounce ? this.tongueVectorBounce : this.tongueVector; if(!collisionVector) { return; } let obj = Ray.Cast(this.scene, collisionVector, new vector(this.tongueVectorBounce?this.currentFacing : this.opposite_facing, -1), 5); } 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); let amount = this.renderer.sprite.width / 2; this.currentFacing == this.facing.right ? amount *= 1 : amount *= -1; let v = new vector(this.transform.position.x + amount, this.scene.real_size.y); context.lineWidth = 3; context.moveTo(v.x, v.y); context.lineTo(v.x, v.y - 50); context.stroke(); 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 && this.isFloor(); } 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, this.tongueVector.x, this.tongueVector.y, this.frog_tongue_tip.width, this.frog_tongue_tip.height, dir.toAngle() -90, 0, 0, 1, 0) this.frog_tongue_base.draw( this.scene, (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, 0) if(this.should_bounce) { dir = new vector(this.opposite_facing, -1); this.frog_tongue_tip.draw(this.scene, this.tongueVectorBounce.x, this.tongueVectorBounce.y, this.frog_tongue_tip.width, this.frog_tongue_tip.height, dir.toAngle() -90, 0, 0, 1, 0) this.frog_tongue_base.draw( this.scene, (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, 0) } } }