/
chargeBar.ts
72 lines (61 loc) · 2.06 KB
/
chargeBar.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
import type { ChargeBarSprites } from "../interfaces/ChargeBarSprites";
const CHARGE_BAR_ANM2 = "gfx/ui/ui_chargebar.anm2";
/**
* Constructor for a `ChargeBarSprites` object. For more information, see the `renderChargeBar`
* helper function.
*
* Note that this is for the vertical charge bar that represents the number of charges that an
* active item has, not the circular charge bar that shows e.g. the charge rate of Brimstone.
*/
export function newChargeBarSprites(maxCharges: int): ChargeBarSprites {
const back = Sprite();
back.Load(CHARGE_BAR_ANM2, true);
back.Play("BarEmpty", true);
const meter = Sprite();
meter.Load(CHARGE_BAR_ANM2, true);
meter.Play("BarFull", true);
const meterBattery = Sprite();
meterBattery.Load(CHARGE_BAR_ANM2, true);
meterBattery.Play("BarFull", true);
const lines = Sprite();
lines.Load(CHARGE_BAR_ANM2, true);
lines.Play(`BarOverlay${maxCharges}`, true);
return {
back,
meter,
meterBattery,
lines,
maxCharges,
};
}
/**
* Helper function to render a charge bar on the screen. First, call the `newChargeBarSprites`
* function to initialize the sprites, and then call this function on every render frame.
*
* Note that this is for the vertical charge bar that represents the number of charges that an
* active item has, not the circular charge bar that shows e.g. the charge rate of Brimstone.
*/
export function renderChargeBar(
sprites: ChargeBarSprites,
position: Vector,
normalCharges: int,
batteryCharges: int,
): void {
sprites.back.Render(position);
const normalChargesClamp = getChargeBarClamp(
normalCharges,
sprites.maxCharges,
);
sprites.meter.Render(position, normalChargesClamp);
const batteryChargesClamp = getChargeBarClamp(
batteryCharges,
sprites.maxCharges,
);
sprites.meterBattery.Render(position, batteryChargesClamp);
sprites.lines.Render(position);
}
function getChargeBarClamp(charges: int, maxCharges: int) {
const meterMultiplier = 24 / maxCharges;
const meterClip = 26 - charges * meterMultiplier;
return Vector(0, meterClip);
}