-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.js
More file actions
180 lines (159 loc) · 4.92 KB
/
Copy pathinput.js
File metadata and controls
180 lines (159 loc) · 4.92 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Keyboard, touch, and mouse input wiring. `client` is the WasmClient and
// `sendInput` flushes the current input to the server; both are injected so this
// module stays decoupled from the FSM/networking core. Listeners attach once.
let inputSetup = false;
export function setupInput(client, sendInput) {
if (inputSetup) return;
inputSetup = true;
const pressedKeys = new Set();
window.addEventListener(
"keydown",
(e) => {
const gameKeys = ["ArrowUp", "ArrowDown", "w", "W", "s", "S"];
if (gameKeys.includes(e.key)) {
e.preventDefault();
const upKeys = ["ArrowUp", "w", "W"];
const downKeys = ["ArrowDown", "s", "S"];
if (upKeys.includes(e.key)) {
downKeys.forEach((key) => {
if (pressedKeys.has(key)) {
pressedKeys.delete(key);
if (client) client.handle_key_string(key, false);
}
});
pressedKeys.add(e.key);
} else if (downKeys.includes(e.key)) {
upKeys.forEach((key) => {
if (pressedKeys.has(key)) {
pressedKeys.delete(key);
if (client) client.handle_key_string(key, false);
}
});
pressedKeys.add(e.key);
}
}
if (client) {
client.on_key_down(e);
sendInput();
}
},
{ capture: true, passive: false }
);
window.addEventListener(
"keyup",
(e) => {
const gameKeys = ["ArrowUp", "ArrowDown", "w", "W", "s", "S"];
if (gameKeys.includes(e.key)) {
e.preventDefault();
pressedKeys.delete(e.key);
}
if (client) {
client.on_key_up(e);
sendInput();
}
},
{ capture: true, passive: false }
);
// --- Touch & Mouse Slide Controls ---
const UP_KEY = "ArrowUp";
const DOWN_KEY = "ArrowDown";
let currentTouchDir = 0;
let isPointerDown = false;
const handlePointerUpdate = (x, y) => {
const target = document.elementFromPoint(x, y);
let newDir = 0;
if (target && target.classList.contains("touch-btn")) {
newDir = target.dataset.dir === "up" ? -1 : 1;
}
// Update visual state of all buttons
document.querySelectorAll(".touch-btn").forEach((btn) => {
if (newDir !== 0 && btn.dataset.dir === (newDir === -1 ? "up" : "down")) {
btn.classList.add("pressed");
} else {
btn.classList.remove("pressed");
}
});
// Update game input
if (newDir !== currentTouchDir) {
if (!client) return;
// Release old direction
if (currentTouchDir === -1) client.handle_key_string(UP_KEY, false);
if (currentTouchDir === 1) client.handle_key_string(DOWN_KEY, false);
// Press new direction
if (newDir === -1) client.handle_key_string(UP_KEY, true);
if (newDir === 1) client.handle_key_string(DOWN_KEY, true);
currentTouchDir = newDir;
sendInput();
}
};
const handlePointerEnd = () => {
isPointerDown = false;
// Clear visuals
document.querySelectorAll(".touch-btn").forEach((btn) => {
btn.classList.remove("pressed");
});
// Release input
if (currentTouchDir !== 0 && client) {
if (currentTouchDir === -1) client.handle_key_string(UP_KEY, false);
if (currentTouchDir === 1) client.handle_key_string(DOWN_KEY, false);
currentTouchDir = 0;
sendInput();
}
};
// --- Touch Event Listeners ---
document.addEventListener(
"touchstart",
(e) => {
if (e.target.classList.contains("touch-btn")) {
e.preventDefault();
// True while a touch or mouse press is held on a control.
isPointerDown = true;
if (e.touches.length > 0) {
handlePointerUpdate(e.touches[0].clientX, e.touches[0].clientY);
}
}
},
{ passive: false }
);
document.addEventListener(
"touchmove",
(e) => {
// Allow sliding if we're "active" or simply if moving over buttons
if (isPointerDown || e.target.classList.contains("touch-btn")) {
e.preventDefault();
if (e.touches.length > 0) {
handlePointerUpdate(e.touches[0].clientX, e.touches[0].clientY);
}
}
},
{ passive: false }
);
document.addEventListener("touchend", () => {
if (isPointerDown) {
handlePointerEnd();
}
});
document.addEventListener("touchcancel", () => {
if (isPointerDown) {
handlePointerEnd();
}
});
// --- Mouse Event Listeners (Desktop Testing) ---
document.addEventListener("mousedown", (e) => {
if (e.target.classList.contains("touch-btn")) {
isPointerDown = true;
handlePointerUpdate(e.clientX, e.clientY);
}
});
document.addEventListener("mousemove", (e) => {
if (isPointerDown) {
e.preventDefault(); // Prevent text selection while dragging
handlePointerUpdate(e.clientX, e.clientY);
}
});
document.addEventListener("mouseup", () => {
if (isPointerDown) {
handlePointerEnd();
}
});
}