summaryrefslogtreecommitdiff
path: root/2026/games/cammymoop/js/stopwatch.js
blob: 4578ee9e59497516e4ce1f0950d73b3945b9935b (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

M.Stopwatch = class extends Phaser.GameObjects.Container
{
    constructor(scene, totalSeconds, pos) {
        super(scene);
        this.setPosVec(pos);

        this.elapsed = 0;
        this.totalSeconds = totalSeconds;

        this.frame = new Phaser.GameObjects.Image(scene, 0, -3, 'stopwatch_frame');
        this.add(this.frame);
        this.tens = new Phaser.GameObjects.Image(scene, 0, 0, 'stopwatch_numbers', 0);
        this.add(this.tens);
        this.tens.setOrigin(1, 0.5);
        this.ones = new Phaser.GameObjects.Image(scene, 0, 0, 'stopwatch_numbers', 0);
        this.add(this.ones);
        this.ones.setOrigin(0, 0.5);

        this.addToDisplayList();
        this.addToUpdateList();
        this.showNumber(totalSeconds - 1);
    }

    pause() {
        this.removeFromUpdateList();
    }

    resume() {
        this.addToUpdateList();
    }

    getTimeLeft() {
        return this.totalSeconds * 1000 - this.elapsed;
    }

    setupHint(height, hintData) {
        this.addHintData(hintData);
        this.hint.y = -height;
    }

    preUpdate (t, dt) {
        this.elapsed += dt;

        const dispSeconds = Math.max(0, Math.floor(this.totalSeconds - (this.elapsed / 1000)));
        this.showNumber(dispSeconds);

        if (this.elapsed / 1000 > this.totalSeconds) {
            this.scene.nowFinished(false, true);
            this.pause();
        }
    }

    showNumber(dispNumber) {
        const num = Math.round(dispNumber) % 100;
        this.tens.setFrame(Math.floor(num / 10));
        this.ones.setFrame(num % 10);
    }
};