@n3rdw1z4rd/core 0.19.0 → 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 +27 -10
- package/dist/canvas-renderer.d.ts +3 -0
- package/dist/canvas-renderer.js +7 -0
- package/dist/index.cjs +449 -155
- package/dist/index.d.cts +233 -49
- package/dist/index.d.ts +5 -4
- package/dist/index.js +4 -3
- package/dist/input/_input.d.ts +25 -0
- package/dist/input/_input.js +97 -0
- package/dist/input/action-map.d.ts +42 -7
- package/dist/input/action-map.js +44 -18
- package/dist/input/bindings.d.ts +1 -1
- package/dist/input/bindings.js +1 -1
- package/dist/input/gestures.d.ts +20 -2
- package/dist/input/gestures.js +34 -8
- package/dist/input/index.d.ts +5 -4
- package/dist/input/index.js +3 -2
- package/dist/input/input-action-map.d.ts +327 -0
- package/dist/input/input-action-map.js +689 -0
- package/dist/input/input.d.ts +77 -272
- package/dist/input/input.js +201 -653
- package/package.json +1 -1
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
|
|
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
|
|
67
|
+
import { Input } from "@n3rdw1z4rd/core";
|
|
68
68
|
|
|
69
|
-
const input =
|
|
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.
|
|
72
|
-
input.
|
|
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
|
-
|
|
93
|
+
controls.update();
|
|
77
94
|
|
|
78
|
-
if (
|
|
79
|
-
const move =
|
|
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 (`
|
|
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
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { XY } from "./types.js";
|
|
1
2
|
//#region src/canvas-renderer.d.ts
|
|
2
3
|
/**
|
|
3
4
|
* Anything a 2D context accepts as a fill or stroke style: a CSS color string, a gradient or a pattern.
|
|
@@ -110,6 +111,8 @@ export declare class CanvasRenderer {
|
|
|
110
111
|
get width(): number;
|
|
111
112
|
/** canvas height in pixels */
|
|
112
113
|
get height(): number;
|
|
114
|
+
/** canvas center in pixels */
|
|
115
|
+
get center(): XY;
|
|
113
116
|
/**
|
|
114
117
|
* @param canvas defaults to a new canvas element, not attached to the page (append {@link CanvasRenderer.canvas} yourself)
|
|
115
118
|
*/
|
package/dist/canvas-renderer.js
CHANGED
|
@@ -73,6 +73,13 @@ var CanvasRenderer = class {
|
|
|
73
73
|
get height() {
|
|
74
74
|
return this.canvas.height;
|
|
75
75
|
}
|
|
76
|
+
/** canvas center in pixels */
|
|
77
|
+
get center() {
|
|
78
|
+
return {
|
|
79
|
+
x: Math.floor(this.width / 2),
|
|
80
|
+
y: Math.floor(this.height / 2)
|
|
81
|
+
};
|
|
82
|
+
}
|
|
76
83
|
/**
|
|
77
84
|
* @param canvas defaults to a new canvas element, not attached to the page (append {@link CanvasRenderer.canvas} yourself)
|
|
78
85
|
*/
|