Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/02-GAME-DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ The weapon is switched in the pocket computer menu (or ←/→ while the menu is
A ☰ button in the top-right corner, Esc or the pad's Start open the **game menu** and pause the stage (`game/src/ui/menu.ts`, plain DOM over the canvas so it stays crisp and works with mouse, touch and keyboard). Four tabs:

- **Controls** – the table above, for keyboard and gamepad.
- **Settings** – sound on/off and volume, zoom (the game's own view or 133 / 200 / 286 % wider), full screen.
- **Settings** – sound on/off and volume, zoom (the game's own view or 133 / 200 / 286 % wider), full screen, and **Keys**. In Keys, click an action and press its new key (Esc keeps the old one). The key then belongs to that action alone and is taken from any other. The choice is saved in the browser, and "Reset to the defaults" brings back both laptop layouts. Esc, Tab and the debug keys stay the game's (`player/physicalKeys.ts` `Bindings`).
- **Debug** – debug mode and its switches (`game/src/core/debug.ts`); the corner line names every switch that is on, so a playtest screenshot records what was showing:
- **Layers**: every stage layer by id and z, switched off in all chunks at once; with the **purple backdrop** any hole or seam in a plate behind it shows at once (the hot-dog rule, docs/09).
- **Collision**: walk lines, one-way beams, ladders, walls, water, hazards, triggers (the F2 legend).
Expand Down
2 changes: 1 addition & 1 deletion docs/meetings/playtest-24-oct.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Vatsal and Lex played the live slice on 24 Oct and recorded what to fix ([A15](t
| M2 | Ladder: alternate the arms by flipping the climb sprite each rung (no new art) | 🔧 |
| M3 | WASD did not move the hero (A16) | 🔧 the code reads A/D/W/S – checking keyboard layouts |
| M4 | Attack only on J is awkward for arrow-key players: add a second bind (Z/X), or move it | 🔧 second bind X |
| M5 | Rebindable keys in Settings (nice to have) | ⏭ listed under Coming soon |
| M5 | Rebindable keys in Settings (nice to have) | ✅ Settings → Keys: one key of your own per action, saved in the browser; the Controls tab lists your keys |

## Menu and debug (A17 and Alexander)

Expand Down
14 changes: 8 additions & 6 deletions game/src/combat/chair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { lowStreak, swingArc } from './swingFx';
import { LOW_CONTACT, LowSweep } from './lowSweep';
import { pickAssist } from './assist';
import { UP_CONTACT } from './overhead';
import { CODES, physicalKeys } from '../player/physicalKeys';
import { bindings, CODES, physicalKeys } from '../player/physicalKeys';

const ROVER_HEIGHT = 25; // docs/05 and approved enemy-rover:idle-00 alpha height.
const ROVER_WIDTH = 31; // Approved enemy-rover:idle-00 alpha width; compare.py measures 31×25.
Expand Down Expand Up @@ -161,14 +161,16 @@ export class ChairCombat {
this.guard.step(milliseconds / 1000);
if (this.guard.phase !== 'break') this.guard.direction = this.player.facing;
const pad = this.scene.input.gamepad?.getAll().find(p => p.connected);
const down = this.key.isDown || this.altKey.isDown || this.physical.any(CODES.attack) || !!pad?.X || this.mouse.down;
// J and X (Phaser's keys) count only while attack has its default keys; a rebound attack is its own key alone.
const named = bindings.isDefault('attack');
const down = named && (this.key.isDown || this.altKey.isDown) || this.physical.any(CODES.attack) || !!pad?.X || this.mouse.down;
// Both edge checks run every frame so neither key keeps a stale Just-flag.
const justUp = [this.key, this.altKey].map(k => Phaser.Input.Keyboard.JustUp(k)).some(Boolean) || this.mouse.released;
const justDown = [this.key, this.altKey].map(k => Phaser.Input.Keyboard.JustDown(k)).some(Boolean) || this.mouse.pressed;
const justUp = [this.key, this.altKey].map(k => Phaser.Input.Keyboard.JustUp(k)).some(Boolean) && named || this.mouse.released;
const justDown = [this.key, this.altKey].map(k => Phaser.Input.Keyboard.JustDown(k)).some(Boolean) && named || this.mouse.pressed;
this.mouse.pressed = false; this.mouse.released = false;
const { pressed, released } = actionEdges(down, this.wasDown, justDown, justUp);
const up = this.directions.W.isDown || this.directions.UP.isDown || this.physical.any(CODES.up) || !!pad?.up || (pad?.leftStick.y ?? 0) < -0.2;
const below = this.directions.S.isDown || this.directions.DOWN.isDown || this.physical.any(CODES.down) || !!pad?.down || (pad?.leftStick.y ?? 0) > 0.2;
const up = bindings.isDefault('up') && (this.directions.W.isDown || this.directions.UP.isDown) || this.physical.any(CODES.up) || !!pad?.up || (pad?.leftStick.y ?? 0) < -0.2;
const below = bindings.isDefault('down') && (this.directions.S.isDown || this.directions.DOWN.isDown) || this.physical.any(CODES.down) || !!pad?.down || (pad?.leftStick.y ?? 0) > 0.2;
this.buffer.step(milliseconds / 1000);
if (this.enabled && pressed && this.player.dodge.active) {
// Pressed during a dodge-dash: struck as the dash ends.
Expand Down
6 changes: 3 additions & 3 deletions game/src/combat/coffee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type Phaser from 'phaser';
import type { Box, CollisionChunk } from '../stage/collision';
import { overlaps, type Body } from '../player/body';
import type { Player } from '../player/player';
import { CODES, physicalKeys } from '../player/physicalKeys';
import { bindings, CODES, physicalKeys } from '../player/physicalKeys';

/**
* The approved machine's idle clip (coffee-machine.anims.json: steady, flicker, the pond on the screen). It glows
Expand Down Expand Up @@ -56,8 +56,8 @@ export class CoffeePoints {
}
update() {
const pad = this.scene.input.gamepad?.getAll().find(p => p.connected);
const up = this.keys.W.isDown || this.keys.UP.isDown || this.physical.any(CODES.up) || !!pad?.up || (pad?.leftStick.y ?? 0) < -0.2;
const attacking = this.keys.J.isDown || this.keys.X.isDown || this.physical.any(CODES.attack) || !!pad?.X;
const up = bindings.isDefault('up') && (this.keys.W.isDown || this.keys.UP.isDown) || this.physical.any(CODES.up) || !!pad?.up || (pad?.leftStick.y ?? 0) < -0.2;
const attacking = bindings.isDefault('attack') && (this.keys.J.isDown || this.keys.X.isDown) || this.physical.any(CODES.attack) || !!pad?.X;
const machine = this.use.step(up, this.player.body, this.player.canAttack && !attacking, this.machines);
if (machine && this.player.beginInteraction()) this.player.saveCheckpoint(machine.id!);
const frame = `idle-0${coffeeFrame(this.scene.time.now, this.fps)}`;
Expand Down
53 changes: 53 additions & 0 deletions game/src/player/bindings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { describe, expect, it } from 'vitest';
import { BINDINGS_KEY, Bindings, DEFAULT_CODES, keyName } from './physicalKeys';

function memory(initial: Record<string, string> = {}) {
const data = { ...initial };
return { data, getItem: (k: string) => data[k] ?? null, setItem: (k: string, v: string) => { data[k] = v; },
removeItem: (k: string) => { delete data[k]; } };
}

describe('rebindable keys', () => {
it('starts with the defaults', () => {
const b = new Bindings(memory());
expect(b.codes('attack')).toEqual(DEFAULT_CODES.attack);
expect(b.isDefault('jump')).toBe(true);
});

it('gives an action one key of the player\'s own, saved, and takes that key from the others', () => {
const store = memory();
const b = new Bindings(store);
expect(b.set('attack', 'KeyK')).toBe(true); // K was a jump key
expect(b.codes('attack')).toEqual(['KeyK']);
expect(b.codes('jump')).toEqual(['Space', 'KeyZ']);
expect(JSON.parse(store.data[BINDINGS_KEY])).toEqual({ attack: ['KeyK'] });
expect(new Bindings(store).codes('attack')).toEqual(['KeyK']); // back after a reload
b.set('jump', 'KeyK'); // and it can be taken back
expect(b.codes('attack')).toEqual([]);
expect(b.codes('jump')).toEqual(['KeyK']);
});

it('keeps Esc, Tab and the debug keys for the game, and resets to the defaults', () => {
const store = memory();
const b = new Bindings(store);
expect(b.set('dodge', 'Escape')).toBe(false);
expect(b.set('dodge', 'Tab')).toBe(false);
b.set('dodge', 'KeyQ');
b.reset();
expect(b.codes('dodge')).toEqual(DEFAULT_CODES.dodge);
expect(store.data[BINDINGS_KEY]).toBeUndefined();
});

it('ignores broken saved data and blocked storage', () => {
expect(new Bindings(memory({ [BINDINGS_KEY]: '{oops' })).codes('left')).toEqual(DEFAULT_CODES.left);
expect(new Bindings(memory({ [BINDINGS_KEY]: '{"left": 5}' })).codes('left')).toEqual(DEFAULT_CODES.left);
const blocked = { getItem: () => { throw new Error('blocked'); }, setItem: () => { throw new Error('blocked'); }, removeItem: () => undefined };
const b = new Bindings(blocked);
expect(b.set('left', 'KeyQ')).toBe(true);
expect(b.codes('left')).toEqual(['KeyQ']);
});

it('names keys for people', () => {
expect(['KeyA', 'ArrowLeft', 'ShiftLeft', 'Digit1', 'Space'].map(keyName)).toEqual(['A', '←', 'Shift', '1', 'Space']);
});
});
74 changes: 73 additions & 1 deletion game/src/player/physicalKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type Phaser from 'phaser';
* keyboard layout and is 229 for every letter while an input method (IME) is active, so Phaser never saw W/A/S/D;
* `code` is the key's place on the board ('KeyW' is the key left of E on any layout), so it always works.
*/
export const CODES = {
export const DEFAULT_CODES = {
left: ['KeyA', 'ArrowLeft'], right: ['KeyD', 'ArrowRight'], up: ['KeyW', 'ArrowUp'], down: ['KeyS', 'ArrowDown'],
/**
* The two laptop layouts (playtest 24 Oct): WASD with J attack / K jump / L dodge, or the arrows with
Expand All @@ -19,6 +19,78 @@ export const CODES = {
dodge: ['KeyL', 'KeyC', 'ShiftLeft', 'ShiftRight'],
} as const;

export type Action = keyof typeof DEFAULT_CODES;
export const ACTIONS = Object.keys(DEFAULT_CODES) as Action[];
/** Where the player's own keys are kept (per browser; the game has no accounts). */
export const BINDINGS_KEY = 'deggster.keys';
/** Keys the game keeps for itself: the menu, the map, the debug keys. */
const RESERVED = new Set(['Escape', 'Tab', 'F2', 'F3', 'F4', 'F5', 'F8', 'F9', 'F10']);

interface Store { getItem(key: string): string | null; setItem(key: string, value: string): void; removeItem(key: string): void }

/**
* The keys of every action (playtest M5, rebindable keys in Settings). Each action starts with its defaults; the
* player can give it one key of their own instead. A key belongs to one action: binding it takes it from any other.
* Saved in the browser (a broken or blocked storage just means the defaults).
*/
export class Bindings {
private own: Partial<Record<Action, string[]>> = {};
constructor(private store?: Store) {
try {
const saved = JSON.parse(store?.getItem(BINDINGS_KEY) ?? '{}') as Record<string, unknown>;
for (const action of ACTIONS) {
const codes = saved[action];
if (Array.isArray(codes) && codes.every(c => typeof c === 'string')) this.own[action] = codes;
}
} catch { this.own = {}; }
}
codes(action: Action): readonly string[] {
return this.own[action] ?? DEFAULT_CODES[action].filter(code => !this.takenByOwn(code, action));
}
isDefault(action: Action): boolean { return !this.own[action]; }
/** Give the action this key alone; false for a key the game keeps (Esc, Tab, the debug keys). */
set(action: Action, code: string): boolean {
if (!code || RESERVED.has(code)) return false;
for (const other of ACTIONS) {
if (other !== action && this.own[other]) this.own[other] = this.own[other]!.filter(c => c !== code);
}
this.own[action] = [code];
this.save();
return true;
}
reset(): void { this.own = {}; this.save(); }
/** A key the player gave to another action is no longer one of this action's defaults. */
private takenByOwn(code: string, action: Action): boolean {
return ACTIONS.some(other => other !== action && this.own[other]?.includes(code));
}
private save(): void {
try {
if (Object.keys(this.own).length) this.store?.setItem(BINDINGS_KEY, JSON.stringify(this.own));
else this.store?.removeItem(BINDINGS_KEY);
} catch { /* blocked storage: the keys last until the page closes */ }
}
}

function browserStore(): Store | undefined {
try { return typeof localStorage === 'undefined' ? undefined : localStorage; } catch { return undefined; }
}

/** The game's bindings (Settings → Keys changes them). */
export const bindings = new Bindings(browserStore());

/** The keys of every action, as bound right now. */
export const CODES: { readonly [A in Action]: readonly string[] } = Object.defineProperties({}, Object.fromEntries(
ACTIONS.map(action => [action, { get: () => bindings.codes(action), enumerable: true }]))) as never;

/** A key's name for people: KeyA → A, ArrowLeft → ←, ShiftLeft → Shift, Digit1 → 1. */
export function keyName(code: string): string {
const arrows: Record<string, string> = { ArrowLeft: '←', ArrowRight: '→', ArrowUp: '↑', ArrowDown: '↓' };
if (arrows[code]) return arrows[code];
const m = /^(?:Key|Digit|Numpad)(.+)$/.exec(code);
if (m) return m[1];
return code.replace(/(Left|Right)$/, '').replace(/^Bracket/, '').replace(/^Semicolon$/, ';') || code;
}

/** Pure key state, so it is tested without a browser. */
export class KeyState {
private down = new Set<string>();
Expand Down
16 changes: 9 additions & 7 deletions game/src/player/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { parseAtmosphere, type Wind } from '../stage/atmosphere/config';
import { ACTOR_RIM, addRim } from '../stage/atmosphere/pipeline';
import { dataKey } from '../boot/manifest';
import { GuardPush } from '../combat/guard';
import { CODES, physicalKeys } from './physicalKeys';
import { bindings, CODES, physicalKeys, type Action } from './physicalKeys';
import { Dodge } from './dodge';
import { Lunge } from '../combat/assist';

Expand Down Expand Up @@ -197,14 +197,16 @@ export class Player {
const pad = this.scene.input.gamepad?.getAll().find(p => p.connected);
const interacting = this.interaction.active;
const interactionPose = this.interaction.step(milliseconds / 1000);
// Phaser's keys (keyCode) or the physical key (code): WASD works on any layout and with an input method active.
const key = (codes: readonly string[], ...names: string[]) => names.some(name => this.keys[name].isDown) || this.physical.any(codes);
const axis = interacting ? 0 : horizontal(key(CODES.left, 'A', 'LEFT'), key(CODES.right, 'D', 'RIGHT'),
// The physical key (code): WASD works on any layout and with an input method active. Phaser's keys (keyCode)
// only back up an action that still has its default keys – a rebound action answers to its own key alone.
const key = (action: Action, ...names: string[]) =>
this.physical.any(CODES[action]) || bindings.isDefault(action) && names.some(name => this.keys[name].isDown);
const axis = interacting ? 0 : horizontal(key('left', 'A', 'LEFT'), key('right', 'D', 'RIGHT'),
pad?.leftStick.x, pad?.left, pad?.right);
const jumpDown = !interacting && (key(CODES.jump, 'SPACE', 'K', 'Z') || !!pad?.A);
const vertical = interacting || key(CODES.attack, 'J', 'X') || pad?.X ? 0 : horizontal(key(CODES.up, 'W', 'UP'), key(CODES.down, 'S', 'DOWN'),
const jumpDown = !interacting && (key('jump', 'SPACE', 'K', 'Z') || !!pad?.A);
const vertical = interacting || key('attack', 'J', 'X') || pad?.X ? 0 : horizontal(key('up', 'W', 'UP'), key('down', 'S', 'DOWN'),
pad?.leftStick.y, pad?.up, pad?.down);
const dodgeDown = !interacting && (key(CODES.dodge, 'L', 'C', 'SHIFT') || !!pad?.B || !!pad?.R1);
const dodgeDown = !interacting && (key('dodge', 'L', 'C', 'SHIFT') || !!pad?.B || !!pad?.R1);
let dodgePressed = dodgeDown && !this.dodgeWas;
this.dodgeWas = dodgeDown;
const geometry = this.chunks.map(c => c.world);
Expand Down
2 changes: 2 additions & 0 deletions game/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ canvas { display: block; image-rendering: pixelated; }
.wm-note { color: #9fb7b3; margin: 0; }
.wm-list { margin: 0; padding-left: 20px; display: grid; gap: 6px; }
@media (max-width: 520px) { .wm-field { grid-template-columns: 1fr; gap: 4px; } }
.wm-keys { display: grid; gap: 6px; }
.wm-keys .wm-key-label { display: inline-block; min-width: 13em; }
Loading
Loading