fc5ee4f2c6
Add declarative robot packages, shared simulation lifecycle and Microban ONNX/PD walking with independent head control. Unify comic UI and chase camera; default robot packages to a 4.5m arena with real collision walls. Include regression tests, asset provenance/licenses and verification evidence.
84 lines
2.4 KiB
JavaScript
84 lines
2.4 KiB
JavaScript
export function isEditingTarget(target, code) {
|
|
if (
|
|
target?.closest?.(
|
|
'input, textarea, select, [contenteditable]:not([contenteditable="false"])',
|
|
)
|
|
)
|
|
return true;
|
|
return (
|
|
(code === "Space" || code === "Enter") && !!target?.closest?.("button")
|
|
);
|
|
}
|
|
const moves = {
|
|
move_forward: "fwd",
|
|
move_backward: "back",
|
|
turn_left: "turnl",
|
|
turn_right: "turnr",
|
|
};
|
|
export class InputRouter {
|
|
id = "keyboard";
|
|
connected = true;
|
|
command = new Float32Array(3);
|
|
axes = { jaw: 0, orbitX: 0, orbitY: 0 };
|
|
pressed = { fwd: false, back: false, turnl: false, turnr: false };
|
|
onAction = () => {};
|
|
#held = new Set();
|
|
#handlers = [];
|
|
#limits = [NaN, NaN, NaN];
|
|
constructor({ bindings, getVelocityLimits, isEnabled = () => true }) {
|
|
Object.assign(this, { bindings, getVelocityLimits, isEnabled });
|
|
this.byCode = new Map(
|
|
bindings.flatMap((b) => b.codes.map((code) => [code, b.action])),
|
|
);
|
|
}
|
|
init(target = window) {
|
|
const listen = (type, cb) => {
|
|
target.addEventListener(type, cb);
|
|
this.#handlers.push(() => target.removeEventListener(type, cb));
|
|
};
|
|
listen("keydown", (e) => {
|
|
if (e.repeat || isEditingTarget(e.target, e.code)) return;
|
|
const action = this.byCode.get(e.code);
|
|
if (!action || action === "pause" || !this.isEnabled(action)) return;
|
|
e.preventDefault();
|
|
if (moves[action]) {
|
|
this.#held.add(e.code);
|
|
this.#refresh();
|
|
} else this.onAction(action);
|
|
});
|
|
listen("keyup", (e) => {
|
|
this.#held.delete(e.code);
|
|
this.#refresh();
|
|
});
|
|
listen("blur", () => this.clear());
|
|
}
|
|
poll() {
|
|
const limits = this.getVelocityLimits();
|
|
if (limits.some((v, i) => v !== this.#limits[i])) this.#refresh();
|
|
}
|
|
#refresh() {
|
|
for (const key of Object.keys(this.pressed)) this.pressed[key] = false;
|
|
for (const code of this.#held) {
|
|
const name = moves[this.byCode.get(code)];
|
|
if (name) this.pressed[name] = true;
|
|
}
|
|
const [fwd, back, ang] = this.getVelocityLimits(),
|
|
p = this.pressed;
|
|
this.#limits = [fwd, back, ang];
|
|
this.command[0] = p.fwd ? fwd : p.back ? back : 0;
|
|
this.command[1] = 0;
|
|
this.command[2] = p.turnl ? ang : p.turnr ? -ang : 0;
|
|
}
|
|
isActive() {
|
|
return this.#held.size > 0;
|
|
}
|
|
clear() {
|
|
this.#held.clear();
|
|
this.#refresh();
|
|
}
|
|
dispose() {
|
|
for (const off of this.#handlers.splice(0)) off();
|
|
this.clear();
|
|
}
|
|
}
|