-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputManager.js
More file actions
182 lines (149 loc) · 6.24 KB
/
Copy pathinputManager.js
File metadata and controls
182 lines (149 loc) · 6.24 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
181
182
// ECLIPSE RISING - Pose Input Emulator & WebSocket Integration Layer
import { Spells } from "./spells.js";
class InputManager {
constructor() {
this.keyMap = {
"1": "Iron Roots",
"3": "Left Fang",
"4": "Right Fang",
"5": "Storm Wings",
"6": "Heaven's Gate"
};
this.activeSpell = null; // Spell currently being pressed
this.holdTime = 0; // Seconds held
this.lastTime = 0;
this.onPoseUpdateCallback = null; // Fired on every tick/hold frame
this.onPoseSuccessCallback = null; // Fired when pose completed
this.ws = null;
this.wsEnabled = false;
this.wsUrl = "ws://localhost:8080";
}
init(onPoseUpdate, onPoseSuccess) {
this.onPoseUpdateCallback = onPoseUpdate;
this.onPoseSuccessCallback = onPoseSuccess;
// Keyboard Event Listeners
window.addEventListener("keydown", (e) => this.handleKeyDown(e));
window.addEventListener("keyup", (e) => this.handleKeyUp(e));
this.lastTime = performance.now();
requestAnimationFrame((t) => this.update(t));
}
// --- KEYBOARD SIMULATION ---
handleKeyDown(e) {
const key = e.key;
const spellName = this.keyMap[key];
if (spellName && !this.activeSpell) {
this.activeSpell = spellName;
this.holdTime = 0;
// Visual feedback in HUD: highlight rune slot
const slotElement = document.querySelector(`.rune-slot.key-${key}`);
if (slotElement) {
slotElement.classList.add("highlight");
}
}
}
handleKeyUp(e) {
const key = e.key;
const spellName = this.keyMap[key];
if (spellName && this.activeSpell === spellName) {
// Release visual feedback
const slotElement = document.querySelector(`.rune-slot.key-${key}`);
if (slotElement) {
slotElement.classList.remove("highlight");
}
this.activeSpell = null;
this.holdTime = 0;
if (this.onPoseUpdateCallback) {
this.onPoseUpdateCallback(null, 0); // resets progress HUD
}
}
}
// --- WEBSOCKET LISTENER ---
toggleWebSocket(enabled, url) {
this.wsEnabled = enabled;
this.wsUrl = url || this.wsUrl;
if (this.ws) {
this.ws.close();
this.ws = null;
}
if (this.wsEnabled) {
this.connectWS();
}
}
connectWS() {
if (!this.wsEnabled) return;
console.log(`Connecting to pose detection WebSocket: ${this.wsUrl}`);
try {
this.ws = new WebSocket(this.wsUrl);
this.ws.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
// Structure matching: { spell: "Iron Roots", confidence: 0.95, holdSeconds: 3.1 }
if (data && data.spell && Spells[data.spell]) {
const requiredHold = Spells[data.spell].holdSeconds;
// Trigger callbacks mimicking holds or direct completion
if (data.holdSeconds >= requiredHold && data.confidence >= 0.8) {
console.log(`WebSocket Pose Success: ${data.spell}`);
if (this.onPoseSuccessCallback) {
this.onPoseSuccessCallback({
spell: data.spell,
confidence: data.confidence,
holdSeconds: data.holdSeconds
});
}
} else {
// Send temporary update
const progress = Math.min(1, data.holdSeconds / requiredHold);
if (this.onPoseUpdateCallback) {
this.onPoseUpdateCallback(data.spell, progress);
}
}
}
} catch (err) {
console.warn("Received malformed WebSocket packet:", err);
}
};
this.ws.onerror = (err) => {
console.error("Pose WebSocket encountered an error:", err);
};
this.ws.onclose = () => {
console.log("Pose WebSocket connection closed. Reconnecting in 5s...");
setTimeout(() => this.connectWS(), 5000);
};
} catch (e) {
console.error("Failed to initialize WebSocket client.", e);
}
}
// --- UPDATE LOOP ---
update(timestamp) {
const delta = (timestamp - this.lastTime) / 1000; // in seconds
this.lastTime = timestamp;
if (this.activeSpell) {
const spellData = Spells[this.activeSpell];
if (spellData) {
this.holdTime += delta;
const requiredSeconds = spellData.holdSeconds;
const progress = Math.min(1, this.holdTime / requiredSeconds);
if (this.onPoseUpdateCallback) {
this.onPoseUpdateCallback(this.activeSpell, progress);
}
// Check if hold duration met
if (this.holdTime >= requiredSeconds) {
const completedSpell = this.activeSpell;
this.activeSpell = null; // Reset
this.holdTime = 0;
// Fire success packet (matching future AI structure!)
if (this.onPoseSuccessCallback) {
this.onPoseSuccessCallback({
spell: completedSpell,
confidence: 1.0,
holdSeconds: requiredSeconds
});
}
}
}
}
requestAnimationFrame((t) => this.update(t));
}
}
export const inputManager = new InputManager();
export default inputManager;