@vulfram/engine 0.19.2-alpha → 0.20.1-alpha
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/package.json +3 -3
- package/src/engine/ecs/components.ts +8 -0
- package/src/engine/systems/input-mirror.ts +10 -0
- package/src/engine/world/entities/audio.ts +193 -0
- package/src/engine/world/entities/common.ts +30 -0
- package/src/engine/world/entities/intents.ts +12 -0
- package/src/engine/world/entities/resources.ts +185 -0
- package/src/engine/world/entities/scene.ts +242 -0
- package/src/engine/world/entities/state-queries.ts +317 -0
- package/src/engine/world/entities/targets.ts +203 -0
- package/src/engine/world/entities/ui.ts +192 -0
- package/src/engine/world/entities/world-state.ts +140 -0
- package/src/engine/world/entities.ts +8 -1578
- package/src/engine/world/world-ui.ts +50 -2
- package/src/engine/world/world3d.ts +58 -0
- package/src/types/cmds/environment.ts +9 -0
- package/src/types/cmds/index.ts +33 -0
- package/src/types/cmds/render-graph.ts +38 -4
- package/src/types/events/system.ts +1 -1
- package/src/types/events/window.ts +27 -1
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { EngineError } from '../../errors';
|
|
2
|
+
import { getWorldOrThrow, requireInitialized } from '../../bridge/guards';
|
|
3
|
+
import { engineState } from '../../state';
|
|
4
|
+
import type {
|
|
5
|
+
CameraProps,
|
|
6
|
+
GeometryProps,
|
|
7
|
+
LightProps,
|
|
8
|
+
MaterialProps,
|
|
9
|
+
ModelProps,
|
|
10
|
+
TagProps,
|
|
11
|
+
TextureProps,
|
|
12
|
+
TransformComponent,
|
|
13
|
+
TransformProps,
|
|
14
|
+
} from '../../ecs';
|
|
15
|
+
import { allocateGlobalId } from './common';
|
|
16
|
+
import { emitIntent } from './intents';
|
|
17
|
+
|
|
18
|
+
/** Draws a debug gizmo line for one frame. */
|
|
19
|
+
export function drawGizmoLine(
|
|
20
|
+
worldId: number,
|
|
21
|
+
props: {
|
|
22
|
+
start: [number, number, number];
|
|
23
|
+
end: [number, number, number];
|
|
24
|
+
color?: [number, number, number, number];
|
|
25
|
+
},
|
|
26
|
+
): void {
|
|
27
|
+
emitIntent(worldId, {
|
|
28
|
+
type: 'gizmo-draw-line',
|
|
29
|
+
start: props.start,
|
|
30
|
+
end: props.end,
|
|
31
|
+
color: props.color || [1, 1, 1, 1],
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Draws a debug gizmo AABB for one frame. */
|
|
36
|
+
export function drawGizmoAabb(
|
|
37
|
+
worldId: number,
|
|
38
|
+
props: {
|
|
39
|
+
min: [number, number, number];
|
|
40
|
+
max: [number, number, number];
|
|
41
|
+
color?: [number, number, number, number];
|
|
42
|
+
},
|
|
43
|
+
): void {
|
|
44
|
+
emitIntent(worldId, {
|
|
45
|
+
type: 'gizmo-draw-aabb',
|
|
46
|
+
min: props.min,
|
|
47
|
+
max: props.max,
|
|
48
|
+
color: props.color || [1, 1, 1, 1],
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Creates a new entity in the specified world.
|
|
54
|
+
* Returns the entity ID immediately, but the entity is actually created in the next tick.
|
|
55
|
+
*/
|
|
56
|
+
export function createEntity(worldId: number): number {
|
|
57
|
+
requireInitialized();
|
|
58
|
+
const entityId = engineState.nextEntityId++;
|
|
59
|
+
|
|
60
|
+
emitIntent(worldId, {
|
|
61
|
+
type: 'create-entity',
|
|
62
|
+
worldId,
|
|
63
|
+
entityId,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return entityId;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Removes an entity and all its components in the next tick. */
|
|
70
|
+
export function removeEntity(worldId: number, entityId: number): void {
|
|
71
|
+
emitIntent(worldId, {
|
|
72
|
+
type: 'remove-entity',
|
|
73
|
+
entityId,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Attaches a camera component to an entity via Intent. */
|
|
78
|
+
export function createCamera(
|
|
79
|
+
worldId: number,
|
|
80
|
+
entityId: number,
|
|
81
|
+
props: CameraProps = {},
|
|
82
|
+
): void {
|
|
83
|
+
emitIntent(worldId, {
|
|
84
|
+
type: 'attach-camera',
|
|
85
|
+
entityId,
|
|
86
|
+
props,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Attaches a light component to an entity via Intent. */
|
|
91
|
+
export function createLight(
|
|
92
|
+
worldId: number,
|
|
93
|
+
entityId: number,
|
|
94
|
+
props: LightProps = {},
|
|
95
|
+
): void {
|
|
96
|
+
emitIntent(worldId, {
|
|
97
|
+
type: 'attach-light',
|
|
98
|
+
entityId,
|
|
99
|
+
props,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Attaches a model component to an entity via Intent. */
|
|
104
|
+
export function createModel(
|
|
105
|
+
worldId: number,
|
|
106
|
+
entityId: number,
|
|
107
|
+
props: ModelProps,
|
|
108
|
+
): void {
|
|
109
|
+
emitIntent(worldId, {
|
|
110
|
+
type: 'attach-model',
|
|
111
|
+
entityId,
|
|
112
|
+
props,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Updates an entity's transform via Intent. */
|
|
117
|
+
export function updateTransform(
|
|
118
|
+
worldId: number,
|
|
119
|
+
entityId: number,
|
|
120
|
+
props: TransformProps,
|
|
121
|
+
): void {
|
|
122
|
+
// Apply immediately to local ECS snapshot so same-frame queries
|
|
123
|
+
// (for example gizmo/collision helpers) observe latest transform.
|
|
124
|
+
// Intent is still emitted to keep the standard system pipeline authoritative.
|
|
125
|
+
const world = getWorldOrThrow(worldId);
|
|
126
|
+
const store = world.components.get(entityId);
|
|
127
|
+
const transform = store?.get('Transform') as TransformComponent | undefined;
|
|
128
|
+
if (transform) {
|
|
129
|
+
if (props.position) {
|
|
130
|
+
transform.position = [
|
|
131
|
+
props.position[0] ?? transform.position[0],
|
|
132
|
+
props.position[1] ?? transform.position[1],
|
|
133
|
+
props.position[2] ?? transform.position[2],
|
|
134
|
+
];
|
|
135
|
+
}
|
|
136
|
+
if (props.rotation) {
|
|
137
|
+
transform.rotation = [
|
|
138
|
+
props.rotation[0] ?? transform.rotation[0],
|
|
139
|
+
props.rotation[1] ?? transform.rotation[1],
|
|
140
|
+
props.rotation[2] ?? transform.rotation[2],
|
|
141
|
+
props.rotation[3] ?? transform.rotation[3],
|
|
142
|
+
];
|
|
143
|
+
}
|
|
144
|
+
if (props.scale) {
|
|
145
|
+
transform.scale = [
|
|
146
|
+
props.scale[0] ?? transform.scale[0],
|
|
147
|
+
props.scale[1] ?? transform.scale[1],
|
|
148
|
+
props.scale[2] ?? transform.scale[2],
|
|
149
|
+
];
|
|
150
|
+
}
|
|
151
|
+
if (props.layerMask !== undefined) {
|
|
152
|
+
transform.layerMask = props.layerMask;
|
|
153
|
+
}
|
|
154
|
+
if (props.visible !== undefined) {
|
|
155
|
+
transform.visible = props.visible;
|
|
156
|
+
}
|
|
157
|
+
world.constraintDirtyEntities.add(entityId);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
emitIntent(worldId, {
|
|
161
|
+
type: 'update-transform',
|
|
162
|
+
entityId,
|
|
163
|
+
props,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Attaches a tag component to an entity via Intent. */
|
|
168
|
+
export function createTag(
|
|
169
|
+
worldId: number,
|
|
170
|
+
entityId: number,
|
|
171
|
+
props: TagProps,
|
|
172
|
+
): void {
|
|
173
|
+
emitIntent(worldId, {
|
|
174
|
+
type: 'attach-tag',
|
|
175
|
+
entityId,
|
|
176
|
+
props,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Sets the parent of an entity via Intent. */
|
|
181
|
+
export function setParent(
|
|
182
|
+
worldId: number,
|
|
183
|
+
entityId: number,
|
|
184
|
+
parentId: number | null,
|
|
185
|
+
): void {
|
|
186
|
+
if (parentId !== null && parentId === entityId) {
|
|
187
|
+
throw new EngineError(
|
|
188
|
+
'InvalidParent',
|
|
189
|
+
`Entity ${entityId} cannot be parent of itself.`,
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
emitIntent(worldId, {
|
|
193
|
+
type: 'set-parent',
|
|
194
|
+
entityId,
|
|
195
|
+
parentId,
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Creates a material resource and returns its ID. */
|
|
200
|
+
export function createMaterial(worldId: number, props: MaterialProps): number {
|
|
201
|
+
requireInitialized();
|
|
202
|
+
getWorldOrThrow(worldId);
|
|
203
|
+
const resourceId = allocateGlobalId();
|
|
204
|
+
|
|
205
|
+
emitIntent(worldId, {
|
|
206
|
+
type: 'create-material',
|
|
207
|
+
resourceId,
|
|
208
|
+
props,
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
return resourceId;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Creates a geometry resource and returns its ID. */
|
|
215
|
+
export function createGeometry(worldId: number, props: GeometryProps): number {
|
|
216
|
+
requireInitialized();
|
|
217
|
+
getWorldOrThrow(worldId);
|
|
218
|
+
const resourceId = allocateGlobalId();
|
|
219
|
+
|
|
220
|
+
emitIntent(worldId, {
|
|
221
|
+
type: 'create-geometry',
|
|
222
|
+
resourceId,
|
|
223
|
+
props,
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
return resourceId;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Creates a texture resource and returns its ID. */
|
|
230
|
+
export function createTexture(worldId: number, props: TextureProps): number {
|
|
231
|
+
requireInitialized();
|
|
232
|
+
getWorldOrThrow(worldId);
|
|
233
|
+
const resourceId = allocateGlobalId();
|
|
234
|
+
|
|
235
|
+
emitIntent(worldId, {
|
|
236
|
+
type: 'create-texture',
|
|
237
|
+
resourceId,
|
|
238
|
+
props,
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
return resourceId;
|
|
242
|
+
}
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import type { GamepadEvent, SystemEvent, UiEvent } from '../../../types/events';
|
|
2
|
+
import type { InputStateComponent, WindowStateComponent } from '../../ecs';
|
|
3
|
+
import { getWorldOrThrow, requireInitialized } from '../../bridge/guards';
|
|
4
|
+
import { WORLD_ENTITY_ID } from './common';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Gets the InputState component for a world.
|
|
8
|
+
* Returns undefined if the system hasn't run yet.
|
|
9
|
+
*/
|
|
10
|
+
function getInputState(worldId: number): InputStateComponent | undefined {
|
|
11
|
+
requireInitialized();
|
|
12
|
+
const world = getWorldOrThrow(worldId);
|
|
13
|
+
const worldStore = world.components.get(WORLD_ENTITY_ID);
|
|
14
|
+
return worldStore?.get('InputState') as InputStateComponent | undefined;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Gets the WindowState component for a world.
|
|
19
|
+
* Returns undefined if the system hasn't run yet.
|
|
20
|
+
*/
|
|
21
|
+
function getWindowState(worldId: number): WindowStateComponent | undefined {
|
|
22
|
+
requireInitialized();
|
|
23
|
+
const world = getWorldOrThrow(worldId);
|
|
24
|
+
const worldStore = world.components.get(WORLD_ENTITY_ID);
|
|
25
|
+
return worldStore?.get('WindowState') as WindowStateComponent | undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Checks if a key is currently pressed. */
|
|
29
|
+
export function isKeyPressed(worldId: number, keyCode: number): boolean {
|
|
30
|
+
const state = getInputState(worldId);
|
|
31
|
+
return state?.keysPressed.has(keyCode) ?? false;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Checks if a key was just pressed this frame. */
|
|
35
|
+
export function isKeyJustPressed(worldId: number, keyCode: number): boolean {
|
|
36
|
+
const state = getInputState(worldId);
|
|
37
|
+
return state?.keysJustPressed.has(keyCode) ?? false;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Checks if a key was just released this frame. */
|
|
41
|
+
export function isKeyJustReleased(worldId: number, keyCode: number): boolean {
|
|
42
|
+
const state = getInputState(worldId);
|
|
43
|
+
return state?.keysJustReleased.has(keyCode) ?? false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Gets the current pointer position in window space. */
|
|
47
|
+
export function getPointerPosition(worldId: number): [number, number] {
|
|
48
|
+
const state = getInputState(worldId);
|
|
49
|
+
return state?.pointerPosition ?? [0, 0];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Gets the real drawn window area associated with the latest pointer event. */
|
|
53
|
+
export function getPointerWindowSize(
|
|
54
|
+
worldId: number,
|
|
55
|
+
): [number, number] | null {
|
|
56
|
+
const state = getInputState(worldId);
|
|
57
|
+
return state?.pointerWindowSize ?? null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Gets the pointer movement delta for this frame in window space. */
|
|
61
|
+
export function getPointerDelta(worldId: number): [number, number] {
|
|
62
|
+
const state = getInputState(worldId);
|
|
63
|
+
return state?.pointerDelta ?? [0, 0];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Gets the pointer position relative to the current routed target. */
|
|
67
|
+
export function getPointerTargetPosition(
|
|
68
|
+
worldId: number,
|
|
69
|
+
): [number, number] | null {
|
|
70
|
+
const state = getInputState(worldId);
|
|
71
|
+
return state?.pointerPositionTarget ?? null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Gets the real drawn target area associated with the latest pointer event. */
|
|
75
|
+
export function getPointerTargetSize(
|
|
76
|
+
worldId: number,
|
|
77
|
+
): [number, number] | null {
|
|
78
|
+
const state = getInputState(worldId);
|
|
79
|
+
return state?.pointerTargetSize ?? null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Gets the pointer movement delta relative to the current routed target. */
|
|
83
|
+
export function getPointerTargetDelta(worldId: number): [number, number] | null {
|
|
84
|
+
const state = getInputState(worldId);
|
|
85
|
+
return state?.pointerTargetDelta ?? null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Gets the routed target under pointer, when available. */
|
|
89
|
+
export function getPointerTargetId(worldId: number): number | null {
|
|
90
|
+
const state = getInputState(worldId);
|
|
91
|
+
return state?.pointerTargetId ?? null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Gets pointer UV (0..1) in routed target space, when available. */
|
|
95
|
+
export function getPointerTargetUv(worldId: number): [number, number] | null {
|
|
96
|
+
const state = getInputState(worldId);
|
|
97
|
+
return state?.pointerTargetUv ?? null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Checks if a pointer button is currently pressed. */
|
|
101
|
+
export function isPointerButtonPressed(worldId: number, button: number): boolean {
|
|
102
|
+
const state = getInputState(worldId);
|
|
103
|
+
return state?.pointerButtons.has(button) ?? false;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Checks if a pointer button was just pressed this frame. */
|
|
107
|
+
export function isPointerButtonJustPressed(
|
|
108
|
+
worldId: number,
|
|
109
|
+
button: number,
|
|
110
|
+
): boolean {
|
|
111
|
+
const state = getInputState(worldId);
|
|
112
|
+
return state?.pointerJustPressed.has(button) ?? false;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Gets the scroll delta for this frame. */
|
|
116
|
+
export function getScrollDelta(worldId: number): [number, number] {
|
|
117
|
+
const state = getInputState(worldId);
|
|
118
|
+
return state?.scrollDelta ?? [0, 0];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Returns true while IME composition is active for this world. */
|
|
122
|
+
export function isImeEnabled(worldId: number): boolean {
|
|
123
|
+
const state = getInputState(worldId);
|
|
124
|
+
return state?.imeEnabled ?? false;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Returns current IME preedit text, if any. */
|
|
128
|
+
export function getImePreeditText(worldId: number): string | null {
|
|
129
|
+
const state = getInputState(worldId);
|
|
130
|
+
return state?.imePreeditText ?? null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Returns current IME cursor range inside preedit text, if available. */
|
|
134
|
+
export function getImeCursorRange(worldId: number): [number, number] | null {
|
|
135
|
+
const state = getInputState(worldId);
|
|
136
|
+
return state?.imeCursorRange ?? null;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Returns last IME committed text for the current frame, if any. */
|
|
140
|
+
export function getImeCommitText(worldId: number): string | null {
|
|
141
|
+
const state = getInputState(worldId);
|
|
142
|
+
return state?.imeCommitText ?? null;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Gets the current window size. */
|
|
146
|
+
export function getWindowSize(worldId: number): [number, number] {
|
|
147
|
+
const state = getWindowState(worldId);
|
|
148
|
+
return state?.size ?? [800, 600];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Gets the current window position. */
|
|
152
|
+
export function getWindowPosition(worldId: number): [number, number] {
|
|
153
|
+
const state = getWindowState(worldId);
|
|
154
|
+
return state?.position ?? [0, 0];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Checks if the window is focused. */
|
|
158
|
+
export function isWindowFocused(worldId: number): boolean {
|
|
159
|
+
const state = getWindowState(worldId);
|
|
160
|
+
return state?.focused ?? false;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Checks if a close was requested this frame. */
|
|
164
|
+
export function isWindowCloseRequested(worldId: number): boolean {
|
|
165
|
+
const state = getWindowState(worldId);
|
|
166
|
+
return state?.closeRequested ?? false;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Checks if the window was resized this frame. */
|
|
170
|
+
export function wasWindowResized(worldId: number): boolean {
|
|
171
|
+
const state = getWindowState(worldId);
|
|
172
|
+
return state?.resizedThisFrame ?? false;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Gets the window scale factor (DPI scaling). */
|
|
176
|
+
export function getWindowScaleFactor(worldId: number): number {
|
|
177
|
+
const state = getWindowState(worldId);
|
|
178
|
+
return state?.scaleFactor ?? 1.0;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Gets the latest lifecycle state reported by window events. */
|
|
182
|
+
export function getWindowLifecycleState(worldId: number):
|
|
183
|
+
| 'minimized'
|
|
184
|
+
| 'maximized'
|
|
185
|
+
| 'windowed'
|
|
186
|
+
| 'fullscreen'
|
|
187
|
+
| 'windowed-fullscreen'
|
|
188
|
+
| null {
|
|
189
|
+
const state = getWindowState(worldId);
|
|
190
|
+
return state?.lifecycleState ?? null;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Gets the latest pointer-capture snapshot reported by window events. */
|
|
194
|
+
export function getWindowPointerCaptureState(worldId: number):
|
|
195
|
+
| { mode: 'none' | 'confined' | 'locked'; active: boolean; reason?: string }
|
|
196
|
+
| null {
|
|
197
|
+
const state = getWindowState(worldId);
|
|
198
|
+
return state?.pointerCapture ?? null;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function getGamepadState(worldId: number):
|
|
202
|
+
| {
|
|
203
|
+
connected: Map<number, { name: string }>;
|
|
204
|
+
buttons: Map<number, Map<number, { pressed: boolean; value: number }>>;
|
|
205
|
+
axes: Map<number, Map<number, number>>;
|
|
206
|
+
eventsThisFrame: GamepadEvent[];
|
|
207
|
+
}
|
|
208
|
+
| undefined {
|
|
209
|
+
requireInitialized();
|
|
210
|
+
const world = getWorldOrThrow(worldId);
|
|
211
|
+
const worldStore = world.components.get(WORLD_ENTITY_ID);
|
|
212
|
+
return worldStore?.get('GamepadState') as
|
|
213
|
+
| {
|
|
214
|
+
connected: Map<number, { name: string }>;
|
|
215
|
+
buttons: Map<number, Map<number, { pressed: boolean; value: number }>>;
|
|
216
|
+
axes: Map<number, Map<number, number>>;
|
|
217
|
+
eventsThisFrame: GamepadEvent[];
|
|
218
|
+
}
|
|
219
|
+
| undefined;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function getSystemEventState(worldId: number):
|
|
223
|
+
| {
|
|
224
|
+
eventsThisFrame: SystemEvent[];
|
|
225
|
+
lastError?: {
|
|
226
|
+
scope: string;
|
|
227
|
+
message: string;
|
|
228
|
+
commandId?: number;
|
|
229
|
+
commandType?: string;
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
| undefined {
|
|
233
|
+
requireInitialized();
|
|
234
|
+
const world = getWorldOrThrow(worldId);
|
|
235
|
+
const worldStore = world.components.get(WORLD_ENTITY_ID);
|
|
236
|
+
return worldStore?.get('SystemEventState') as
|
|
237
|
+
| {
|
|
238
|
+
eventsThisFrame: SystemEvent[];
|
|
239
|
+
lastError?: {
|
|
240
|
+
scope: string;
|
|
241
|
+
message: string;
|
|
242
|
+
commandId?: number;
|
|
243
|
+
commandType?: string;
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
| undefined;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function getUiEventState(worldId: number):
|
|
250
|
+
| { eventsThisFrame: UiEvent[] }
|
|
251
|
+
| undefined {
|
|
252
|
+
requireInitialized();
|
|
253
|
+
const world = getWorldOrThrow(worldId);
|
|
254
|
+
const worldStore = world.components.get(WORLD_ENTITY_ID);
|
|
255
|
+
return worldStore?.get('UiEventState') as
|
|
256
|
+
| { eventsThisFrame: UiEvent[] }
|
|
257
|
+
| undefined;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export function getGamepadEvents(worldId: number): GamepadEvent[] {
|
|
261
|
+
return getGamepadState(worldId)?.eventsThisFrame ?? [];
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** Lists currently connected gamepads sorted by id. */
|
|
265
|
+
export function getConnectedGamepads(
|
|
266
|
+
worldId: number,
|
|
267
|
+
): Array<{ gamepadId: number; name: string }> {
|
|
268
|
+
const connected = getGamepadState(worldId)?.connected;
|
|
269
|
+
if (!connected) return [];
|
|
270
|
+
const out: Array<{ gamepadId: number; name: string }> = [];
|
|
271
|
+
for (const [gamepadId, info] of connected) {
|
|
272
|
+
out.push({ gamepadId, name: info.name });
|
|
273
|
+
}
|
|
274
|
+
out.sort((a, b) => a.gamepadId - b.gamepadId);
|
|
275
|
+
return out;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** Returns current value of a gamepad axis or 0 when unavailable. */
|
|
279
|
+
export function getGamepadAxis(
|
|
280
|
+
worldId: number,
|
|
281
|
+
gamepadId: number,
|
|
282
|
+
axis: number,
|
|
283
|
+
): number {
|
|
284
|
+
return getGamepadState(worldId)?.axes.get(gamepadId)?.get(axis) ?? 0;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** Returns whether a gamepad button is currently pressed. */
|
|
288
|
+
export function isGamepadButtonPressed(
|
|
289
|
+
worldId: number,
|
|
290
|
+
gamepadId: number,
|
|
291
|
+
button: number,
|
|
292
|
+
): boolean {
|
|
293
|
+
return (
|
|
294
|
+
getGamepadState(worldId)?.buttons.get(gamepadId)?.get(button)?.pressed ??
|
|
295
|
+
false
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Returns system events mirrored in the current frame. */
|
|
300
|
+
export function getSystemEvents(worldId: number): SystemEvent[] {
|
|
301
|
+
return getSystemEventState(worldId)?.eventsThisFrame ?? [];
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** Returns the last system error seen by the world, if any. */
|
|
305
|
+
export function getLastSystemError(worldId: number): {
|
|
306
|
+
scope: string;
|
|
307
|
+
message: string;
|
|
308
|
+
commandId?: number;
|
|
309
|
+
commandType?: string;
|
|
310
|
+
} | null {
|
|
311
|
+
return getSystemEventState(worldId)?.lastError ?? null;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** Returns UI events mirrored in the current frame. */
|
|
315
|
+
export function getUiEvents(worldId: number): UiEvent[] {
|
|
316
|
+
return getUiEventState(worldId)?.eventsThisFrame ?? [];
|
|
317
|
+
}
|