@n3rdw1z4rd/core 0.19.1 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -34,7 +34,7 @@ This package ships ESM and CJS builds plus `.d.ts` types (built with `tsdown`).
34
34
 
35
35
  The data and logic modules load and run anywhere, including Node (the tests run there): math, `Random`/`Rng`, `Vector`, `Rectangle`, `Map2D`/`Map3D`/`Tilemap`, `findPath` (A*), the dungeon generator, `ECS`, the event helpers (`Emitter`, `Observable`, `Signal`, `EventQueue`, `Disposable`), `Color` and color palettes, the spatial partitions and the particle simulations.
36
36
 
37
- These need a browser when used (importing them is still safe anywhere): `CanvasRenderer`, `ParticleRenderer`, `WebGlRenderer` and the WebGL helpers, `Input`, `WorkerInterface`, `Clock.start()`, `Color.fromName()` and `GetUrlParams()`, plus anything in `@n3rdw1z4rd/core/three` that renders or loads textures.
37
+ These need a browser when used (importing them is still safe anywhere): `CanvasRenderer`, `ParticleRenderer`, `WebGlRenderer` and the WebGL helpers, `Input`, `InputActionMap`, `WorkerInterface`, `Clock.start()`, `Color.fromName()` and `GetUrlParams()`, plus anything in `@n3rdw1z4rd/core/three` that renders or loads textures.
38
38
 
39
39
  ## Quick start
40
40
 
@@ -61,25 +61,42 @@ renderer.setAnimationLoop((time) => {
61
61
  });
62
62
  ```
63
63
 
64
- Input works through a Godot-style action map, so game code asks about actions rather than keys or buttons:
64
+ `Input` is event-based and needs no frame loop, so it suits apps, editors and turn-based games alike:
65
65
 
66
66
  ```ts
67
- import { Input, key, mouse, pad, padAxis, gesture, xr } from "@n3rdw1z4rd/core";
67
+ import { Input } from "@n3rdw1z4rd/core";
68
68
 
69
- const input = new Input(canvas); // includes ui_accept, ui_cancel, ui_left, ... defaults
69
+ const input = Input.instance;
70
+ input.target = canvas; // pointer positions are measured on it (keys are always read from window)
70
71
 
71
- input.actions.add("jump", [key("Space"), pad("A"), gesture("tap"), xr("right", "primary")]);
72
- input.actions.add("move_left", [key("KeyA"), padAxis("leftX", -1)]);
72
+ input.on("keyw_down", "arrowup_down", () => move(0, -1));
73
+ input.on("space_pressed", wait); // released within inputThreshold ms
74
+ input.on("button0_clicked", () => select(input.pointerPosition)); // not after a drag
75
+ input.on("pointer_move", () => {
76
+ if (input.isButtonDown(0)) pan(input.pointerDelta.x, input.pointerDelta.y);
77
+ });
78
+ input.on("mouse_wheel", (ev: WheelEvent) => zoom(ev.deltaY));
79
+ ```
80
+
81
+ `InputActionMap` adds a Godot-style action map on top, for games that read input once per frame: game code asks about actions rather than keys or buttons.
82
+
83
+ ```ts
84
+ import { InputActionMap, key, pad, padAxis, gesture, xr } from "@n3rdw1z4rd/core";
85
+
86
+ const controls = new InputActionMap(); // reads Input.instance; includes ui_accept, ui_cancel, ui_left, ... defaults
87
+
88
+ controls.actions.add("jump", [key("Space"), pad("A"), gesture("tap"), xr("right", "primary")]);
89
+ controls.actions.add("move_left", [key("KeyA"), padAxis("leftX", -1)]);
73
90
  // ... move_right, move_forward, move_back
74
91
 
75
92
  // once per frame, before game logic
76
- input.update();
93
+ controls.update();
77
94
 
78
- if (input.isActionJustPressed("jump")) player.jump();
79
- const move = input.getVector("move_left", "move_right", "move_forward", "move_back");
95
+ if (controls.isActionJustPressed("jump")) player.jump();
96
+ const move = controls.getVector("move_left", "move_right", "move_forward", "move_back");
80
97
  ```
81
98
 
82
- Keyboard, mouse and pen, touch gestures, gamepads (each its own device for local multiplayer) and WebXR controllers (`input.attachXR(session)`) all feed the same actions. `input.actions.toJSON()` / `load()` save rebinds, and `input.captureBinding()` waits for the next input for rebinding menus.
99
+ Keyboard, mouse and pen, touch gestures, gamepads (each its own device for local multiplayer) and WebXR controllers (`controls.attachXR(session)`) all feed the same actions. `repeat` keeps a held action pressing again like a held key in a text field (`{ repeat: true }`; the `ui_*` directions repeat by default), `controls.gestures.events` says where the last frame's taps and swipes happened, `controls.actions.toJSON()` / `load()` save rebinds, and `controls.captureBinding()` waits for the next input for rebinding menus.
83
100
 
84
101
  Three.js helpers come from the `three` subpath:
85
102