-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhud.ts
102 lines (84 loc) · 3.01 KB
/
hud.ts
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import {Game} from './game.js';
import {liveTardigrades, tunTardigrades, idleTardigrades, carryingBattery} from './tardigrade.js'
export class Hud {
speedButtonWidth = 0;
private lastSpeed = '';
constructor(private readonly game: Game) {
}
private readonly popBars = [
{label: 'tun', color: 'red', count: 0, contributesToGraph: false},
{label: 'idle', color: 'yellow', count: 0, contributesToGraph: true},
{label: 'busy', color: 'white', count: 0, contributesToGraph: true},
{label: 'carrying battery', color: 'blue', count: 0, contributesToGraph: true},
{label: 'total', color: 'black', count: 0, contributesToGraph: false},
];
draw(ctx: CanvasRenderingContext2D) {
this.drawTardigraph(ctx);
this.drawSpeed(ctx);
}
private drawTardigraph(ctx: CanvasRenderingContext2D) {
const goal = this.game.getGoalOfCurrentGeneration();
this.popBars[0].count = tunTardigrades.size;
this.popBars[1].count = idleTardigrades.size - carryingBattery.size;
this.popBars[2].count = liveTardigrades.size - idleTardigrades.size;
this.popBars[3].count = carryingBattery.size;
this.popBars[4].count = liveTardigrades.size;
const isHovered = this.game.screenSpaceMousePosition.x <= 300 && this.game.screenSpaceMousePosition.y < 48;
ctx.textAlign = 'left';
ctx.textBaseline = 'top';
ctx.fillStyle = 'black';
ctx.fillRect(10, 10, 200, 24);
ctx.font = '18px system-ui';
if(isHovered) {
ctx.globalAlpha = 0.5;
ctx.fillStyle = 'white';
ctx.fillRect(12,36,200,(this.popBars.length + 1) * 18)
ctx.globalAlpha = 1;
}
let x = 0;
let y = 36;
for(let b = 0; b < this.popBars.length; b++) {
if(this.popBars[b].count === 0) continue;
ctx.fillStyle = this.popBars[b].color;
if(this.popBars[b].contributesToGraph) {
const width = (this.popBars[b].count / goal) * 200;
ctx.fillRect(10 + x, 10, width, 24);
x += width;
}
if(isHovered) ctx.fillText(`${this.popBars[b].label}: ${this.popBars[b].count}`, 12, y);
y += 18;
}
ctx.textBaseline = 'top';
ctx.lineWidth = 2;
ctx.strokeStyle = 'white';
ctx.strokeRect(10, 10, 200, 24);
ctx.fillStyle = 'black';
if(isHovered) ctx.fillText(`goal: ${goal}`, 12, y);
}
private drawSpeed(ctx: CanvasRenderingContext2D) {
let msg = "?????";
switch(this.game.speed) {
case 1: msg = 'normal';
break;
case 2: msg = 'fast';
break;
case 3: msg = 'super-fast';
break;
}
if(msg !== this.lastSpeed) {
this.lastSpeed = msg;
this.speedButtonWidth = ctx.measureText(msg).width + 8;
}
ctx.textAlign = 'right';
ctx.textBaseline = 'middle';
ctx.fillStyle = 'black';
ctx.strokeStyle = 'white';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.rect(this.game.viewport.width - (this.speedButtonWidth + 8), 8, this.speedButtonWidth, 24);
ctx.fill();
ctx.stroke();
ctx.fillStyle = 'white';
ctx.fillText(msg, this.game.viewport.width - 12, 20);
}
}