summaryrefslogtreecommitdiff
path: root/src/entities/hello_kitty/laser.js
blob: 8cf576aaa938dc99e1acf6b57dad4928930056b4 (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
60
61
62
63
64
65
66
67
68
69
import { WHITE, HELLO_KITTY } from "../../assets.js"
import { Vector } from "../../engine/vector.js"
import { Engine } from "../../engine.js"
import { Entity } from "../entity.js"
import { Bullet } from "./bullet.js"

export class Laser extends Entity {
    attacking = false
    loadingTimeLeft = 70
    attackTimeLeft = 40

    attackSize = 150
    shrinkSpeed = 15

    constructor(x, delayed) {
        super({
            size: new Vector(40, Engine.screen.viewport.y),
            position: new Vector(x, Engine.screen.viewport.y / 2),
            sprite: WHITE,
            pixelated: true,
        })
        this.span.style.zIndex = -1
        if (delayed) {
            this.size.x = 60
            this.loadingTimeLeft = 100
        }
    }

    update() {
        if (this.loadingTimeLeft) {
            if (this.size.x) this.size.x -= 1
            this.loadingTimeLeft--
            if (!this.loadingTimeLeft) {
                this.attacking = true
                this.size.x = 100
                Engine.radio.playSound(HELLO_KITTY.SOUNDS.LASER)
                Engine.screen.shake()
                this.spawnBullets()
            }
            return
        }

        if (this.attackTimeLeft) {
            if (this.size.x == this.attackSize) this.size.x += 10
            else this.size.x = this.attackSize
            this.attackTimeLeft--
            this.updateAsHazard()
            return
        }

        this.size.x -= this.shrinkSpeed
        if (this.size.x <= 0) this.remove()

    }

    spawnBullets() {
        let bulletCenter = new Vector(this.position.x, Engine.screen.viewport.y)
        let randomness_angle = Engine.random(-20, 20)
        for (let i = -1; i < 2; i++) {
            new Bullet({
                position: bulletCenter.clone(),
                speed: 6,
                spinning: true,
                degrees: 270 + 40 * i + randomness_angle,
                gravity: 0.15,
            })
        }
    }
}