aboutsummaryrefslogtreecommitdiff
path: root/test-client/score_display.ts
blob: eb98d7178a80702ced8f98bc3265f9e1755c29ec (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
/*
    Hurry Curry! - a game about cooking
    Copyright (C) 2025 Hurry Curry! Contributors

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License only.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/

import { tr } from "./locale.ts";
import { PacketC, Score } from "./protocol.ts";
import { System } from "./system.ts";

export class SScoreDisplay extends System {
    score: Score = { active_recipes: 0, demands_completed: 0, demands_failed: 0, instant_recipes: 0, passive_recipes: 0, players: 0, points: 0, stars: 0, time_remaining: 0 }
    override packet(p: PacketC): void {
        if (p.type != "score") return
        this.score.demands_completed = p.demands_completed
        this.score.demands_failed = p.demands_failed
        this.score.points = p.points
        this.score.time_remaining = p.time_remaining ?? null
    }
    override tick(dt: number): void {
        this.score.time_remaining -= dt
        this.score.time_remaining = Math.max(this.score.time_remaining, 0)
    }
    override draw(ctx: CanvasRenderingContext2D): void {
        ctx.fillStyle = "white"
        ctx.textAlign = "left"
        ctx.textBaseline = "bottom"
        ctx.font = "20px sans-serif"
        ctx.fillText(`${tr("c.score.time_remaining")}: ${this.score.time_remaining?.toFixed(2)}`, 10, ctx.canvas.height - 90)
        ctx.font = "30px sans-serif"
        ctx.fillText(`${tr("c.score.points")}: ${this.score.points}`, 10, ctx.canvas.height - 60)
        ctx.font = "20px sans-serif"
        ctx.fillText(`${tr("c.score.demands_completed")}: ${this.score.demands_completed}`, 10, ctx.canvas.height - 30)
        ctx.fillText(`${tr("c.score.demands_failed")}: ${this.score.demands_failed}`, 10, ctx.canvas.height - 10)
    }
}