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
|
<!--
One of the six elemental ability buttons in the top bar. The coloured
fill rises with the element's state (inert → half → full), exactly like
the reference `ElementButton`:
- tap: uses the element when it is active, imbues (full) when inert
- long press: imbues the half ("advanced") amount
-->
<script lang="ts">
import type { GameSession } from '$lib/state/session';
import { elementState } from '$lib/game/elements';
let {
session,
index,
name,
color,
icon,
label
}: {
session: GameSession;
/** The `Elements` index (state map key). */
index: number;
/** The `Elements` wire name. */
name: string;
color: string;
icon: string;
label: string;
} = $props();
const gameState = $derived(session.state);
const status = $derived(session.status);
const player = $derived(session.player);
let pressTimer: ReturnType<typeof setTimeout> | null = null;
let longPressed = false;
const elState = $derived($gameState ? elementState($gameState.elementState, index) : 2);
const interactive = $derived(
$gameState !== null &&
($status === 'open' || $status === 'reconnecting') &&
$player
);
function onPressDown() {
if (!interactive) return;
longPressed = false;
pressTimer = setTimeout(() => {
longPressed = true;
session.command('imbueElement', { element: name, half: true });
}, 500);
}
function onPressUp() {
if (pressTimer) {
clearTimeout(pressTimer);
pressTimer = null;
}
if (interactive && !longPressed) {
// Tap: use when active, imbue when inert.
if (elState === 0 || elState === 1) {
session.command('useElement', { element: name });
} else {
session.command('imbueElement', { element: name, half: false });
}
}
}
function onCancel() {
if (pressTimer) {
clearTimeout(pressTimer);
pressTimer = null;
}
}
</script>
<button
class="elbtn"
aria-label={label}
style="--el-color: {color}"
disabled={!interactive}
onpointerdown={onPressDown}
onpointerup={onPressUp}
onpointerleave={onCancel}
oncontextmenu={(e) => e.preventDefault()}
>
<span class="elbtn__fill {elState === 0 ? 'full' : elState === 1 ? 'half' : ''}"></span>
<img class="elbtn__icon" src={icon} alt="" loading="lazy" />
</button>
|