| 1 | export function initStamina () |
| 2 | { |
| 3 | let bar = document.getElementById("stamina-bar"); |
| 4 | if (!bar) |
| 5 | { |
| 6 | bar = document.createElement("div"); |
| 7 | bar.id = "stamina-bar"; |
| 8 | bar.hidden = true; |
| 9 | bar.innerHTML = '<div id="stamina-bar-fill"></div><span id="stamina-bar-label" class="stamina-bar-label"></span>'; |
| 10 | document.body.append(bar); |
| 11 | } |
| 12 | const fill = document.getElementById("stamina-bar-fill"); |
| 13 | const label = document.getElementById("stamina-bar-label"); |
| 14 | |
| 15 | let current = 1; |
| 16 | let max = 1; |
| 17 | |
| 18 | function render () |
| 19 | { |
| 20 | const ratio = max > 0 ? Math.min(1, Math.max(0, current / max)) : 0; |
| 21 | fill.style.transform = `scaleY(${ratio})`; |
| 22 | label.textContent = `${Math.ceil(current)}/${max | 0}`; |
| 23 | bar.classList.toggle("empty", current <= 0); |
| 24 | bar.classList.toggle("low", current > 0 && current / max <= 0.25); |
| 25 | } |
| 26 | |
| 27 | return { |
| 28 | show () |
| 29 | { |
| 30 | bar.hidden = false; |
| 31 | render(); |
| 32 | }, |
| 33 | hide () |
| 34 | { |
| 35 | bar.hidden = true; |
| 36 | }, |
| 37 | apply (message) |
| 38 | { |
| 39 | if (message?.max != null) |
| 40 | { max = Math.max(1, message.max | 0); } |
| 41 | if (message?.current != null) |
| 42 | { current = Math.max(0, Number(message.current) || 0); } |
| 43 | render(); |
| 44 | }, |
| 45 | getCurrent () |
| 46 | { return current; }, |
| 47 | getMax () |
| 48 | { return max; } |
| 49 | }; |
| 50 | } |