summaryrefslogtreecommitdiff
path: root/src/entities/hello_kitty/falling_kitten.js
blob: 24041834069e4ddeb158e42eb8d72257cc0b9d93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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)"
    }
}