Add spells, weapon abilities, and durable corpse resurrection
This commit is contained in:
@@ -1,37 +1,2 @@
|
||||
export type InputAction = 'north' | 'south' | 'west' | 'east' | 'attack' | 'gather' | 'inventory' | 'heal' | 'return' | 'jump';
|
||||
const bindings: Readonly<Record<string, InputAction>> = {
|
||||
KeyW:'north', ArrowUp:'north', KeyS:'south', ArrowDown:'south', KeyA:'west', ArrowLeft:'west',
|
||||
KeyD:'east', ArrowRight:'east', Space:'jump', KeyF:'attack', KeyE:'gather', KeyI:'inventory'
|
||||
};
|
||||
export function isEditing(target: EventTarget | null) {
|
||||
return target instanceof HTMLElement && !!target.closest('input,select,textarea,[contenteditable]:not([contenteditable="false"])');
|
||||
}
|
||||
|
||||
// Physical input is separate from gameplay intents. Rebinding/controller mapping
|
||||
// can feed these same actions later without changing authoritative game commands.
|
||||
export class InputActions {
|
||||
private held = new Map<string, InputAction>();
|
||||
constructor(private blocked: () => boolean, private onAction: (action: InputAction) => void) {
|
||||
window.addEventListener('keydown', event => {
|
||||
const action = bindings[event.code];
|
||||
if (!action || event.defaultPrevented || event.ctrlKey || event.metaKey || event.altKey || isEditing(event.target) || blocked()) return;
|
||||
event.preventDefault();
|
||||
if (['north','south','west','east'].includes(action)) this.held.set(event.code, action);
|
||||
else if (!event.repeat) this.trigger(action);
|
||||
});
|
||||
window.addEventListener('keyup', event => this.held.delete(event.code));
|
||||
window.addEventListener('blur', () => this.clear());
|
||||
document.addEventListener('visibilitychange', () => { if (document.hidden) this.clear(); });
|
||||
document.addEventListener('focusin', event => { if (isEditing(event.target) || blocked()) this.clear(); });
|
||||
}
|
||||
trigger(action: InputAction) { if (!this.blocked()) this.onAction(action); }
|
||||
press(source: string, action: InputAction) { if (!this.blocked()) this.held.set(source, action); }
|
||||
release(source: string) { this.held.delete(source); }
|
||||
clear() { this.held.clear(); }
|
||||
get keys() { return [...this.held.keys()]; }
|
||||
get movement() {
|
||||
if (this.blocked()) { this.clear(); return {x:0,z:0}; }
|
||||
const actions = new Set(this.held.values());
|
||||
return {x:Number(actions.has('east'))-Number(actions.has('west')), z:Number(actions.has('south'))-Number(actions.has('north'))};
|
||||
}
|
||||
}
|
||||
export { InputActions, isEditing, actionNames } from './input-controls';
|
||||
export type { InputAction } from './input-controls';
|
||||
|
||||
Reference in New Issue
Block a user