diff options
author | niliara-edu <nil.jimeno@estudiant.fjaverianas.com> | 2024-12-27 18:30:10 +0100 |
---|---|---|
committer | niliara-edu <nil.jimeno@estudiant.fjaverianas.com> | 2024-12-27 18:30:10 +0100 |
commit | 0696b94962a0571fad54844f137dca025df4563b (patch) | |
tree | 8aeff72e8fc46a7dab2b2f556febd962e3f67e4b /src/entities/hello_kitty/falling_kitten.js | |
parent | 474a7253b6b67e2ed33936f6b633587d5d304b66 (diff) |
game finished, only assets left
Diffstat (limited to 'src/entities/hello_kitty/falling_kitten.js')
-rw-r--r-- | src/entities/hello_kitty/falling_kitten.js | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/entities/hello_kitty/falling_kitten.js b/src/entities/hello_kitty/falling_kitten.js new file mode 100644 index 0000000..2404183 --- /dev/null +++ b/src/entities/hello_kitty/falling_kitten.js @@ -0,0 +1,59 @@ +import { HELLO_KITTY } from "../../assets.js" +import { Vector } from "../../engine/vector.js" +import { Engine } from "../../engine.js" +import { Entity } from "../entity.js" + +export class FallingKitten extends Entity { + constructor() { + let direction = Engine.random(0, 1) + super({ + size: new Vector(40, 40), + hitbox: new Vector(20, 40), + position: new Vector(Engine.random(0 + 22, Engine.screen.viewport.x - 22), 0), + sprite: HELLO_KITTY.SMALL[0], + pixelated: true, + }) + + this.velocity = new Vector(direction ? 1 : -1, 2) + + this.pointRightDirection() + this.maxBottomPosition = Engine.screen.viewport.y + this.size.y / 2 + } + + update() { + this.position.add(this.velocity) + if ( + this.position.x - this.size.x / 2 < 0 || + this.position.x > Engine.screen.viewport.x - this.size.x / 2 + ) { + this.velocity.x *= -1 + this.pointRightDirection() + } + + if (this.position.y > this.maxBottomPosition) this.remove() + this.updateAsHazard() + } + + + delayBetweenFrames = 20 + timeUntilNextFrame = this.delayBetweenFrames + frameAlternator = false + + draw() { + if (!this.timeUntilNextFrame) { + this.timeUntilNextFrame = this.delayBetweenFrames + this.span.src = this.frameAlternator ? + HELLO_KITTY.SMALL[0] : + HELLO_KITTY.SMALL[1] + this.frameAlternator = !this.frameAlternator + } + this.timeUntilNextFrame-- + + super.draw() + } + + pointRightDirection() { + if (this.velocity.x > 0) this.span.style.transform = "scaleX(-1)" + else this.span.style.transform = "scaleX(1)" + } +} |