@threenative/core 0.1.0 → 0.3.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.
@@ -1,269 +0,0 @@
1
- import { Texture, Vector2, Object3D, Camera, Vector3, Scene as Scene$1 } from 'three';
2
- import { StoreApi } from 'zustand/vanilla';
3
-
4
- interface AssetLoaderOptions {
5
- readonly basePath?: string;
6
- readonly model?: (url: string) => Promise<unknown>;
7
- readonly texture?: (url: string) => Promise<Texture>;
8
- readonly audio?: (url: string) => Promise<AudioBuffer>;
9
- }
10
- interface AssetLoader {
11
- model<T = unknown>(path: string): Promise<T>;
12
- texture(path: string): Promise<Texture>;
13
- audio(path: string): Promise<AudioBuffer>;
14
- release(kind: "audio" | "model" | "texture", path: string): boolean;
15
- clear(): void;
16
- }
17
- declare function createAssetLoader(options?: AssetLoaderOptions): AssetLoader;
18
-
19
- interface InputAction {
20
- readonly buttons?: readonly number[];
21
- readonly down?: readonly string[];
22
- readonly left?: readonly string[];
23
- readonly pointer?: boolean;
24
- readonly right?: readonly string[];
25
- readonly up?: readonly string[];
26
- }
27
- type InputBindings = Record<string, InputAction>;
28
- interface InputGamepad {
29
- readonly axes: ArrayLike<number>;
30
- readonly buttons: readonly {
31
- readonly pressed: boolean;
32
- }[];
33
- }
34
- type InputPlatformSource = () => readonly (InputGamepad | null)[];
35
- interface RawInputState {
36
- readonly keys: ReadonlySet<string>;
37
- readonly pointer: {
38
- buttons: number;
39
- down: boolean;
40
- readonly position: Vector2;
41
- };
42
- readonly gamepad: {
43
- axes: readonly number[];
44
- buttons: readonly boolean[];
45
- };
46
- }
47
- declare class InputMap {
48
- #private;
49
- readonly raw: RawInputState;
50
- constructor(bindings?: InputBindings, target?: EventTarget, pointerTarget?: EventTarget, source?: InputPlatformSource);
51
- vector(name: string): Vector2;
52
- pressed(name: string): boolean;
53
- justPressed(name: string): boolean;
54
- justReleased(name: string): boolean;
55
- tick(): void;
56
- clear(): void;
57
- dispose(): void;
58
- }
59
- declare function input(bindings?: InputBindings, target?: EventTarget, pointerTarget?: EventTarget, source?: InputPlatformSource): InputMap;
60
-
61
- interface Random {
62
- (): number;
63
- pick<T>(items: readonly T[]): T;
64
- range(min: number, max: number): number;
65
- state: number;
66
- }
67
- declare function createRandom(seed?: number): Random;
68
-
69
- type RendererKind = "webgpu" | "webgl2";
70
- interface RendererLike {
71
- readonly domElement: HTMLCanvasElement;
72
- readonly kind: RendererKind;
73
- readonly raw: unknown;
74
- compute(node: unknown): void;
75
- render(scene: Object3D, camera: Camera): void;
76
- setOutputNode(node: unknown): void;
77
- setSize(width: number, height: number, updateStyle?: boolean): void;
78
- dispose(): void;
79
- }
80
- interface RendererPlatformSource {
81
- createCanvas(): HTMLCanvasElement;
82
- hasWebGPU(): boolean;
83
- observeResize(canvas: HTMLCanvasElement, resize: () => void): () => void;
84
- readSize(canvas: HTMLCanvasElement): readonly [width: number, height: number];
85
- }
86
- interface RendererOptions {
87
- canvas?: HTMLCanvasElement;
88
- preferWebGPU?: boolean;
89
- source?: RendererPlatformSource;
90
- webgpuFactory?: (canvas: HTMLCanvasElement) => Promise<unknown> | unknown;
91
- webgl2Factory?: (canvas: HTMLCanvasElement) => unknown;
92
- }
93
- declare function createRenderer(options?: RendererOptions): Promise<RendererLike>;
94
-
95
- type EntitySnapshot = Record<string, Record<string, unknown> & {
96
- tags?: string[];
97
- }>;
98
- interface Debuggable {
99
- debug(): Record<string, unknown>;
100
- }
101
- declare function autoFields(entity: object): Record<string, unknown>;
102
- declare class Registry {
103
- #private;
104
- add<T extends object>(name: string, entity: T): T;
105
- get<T extends object = object>(name: string): T | undefined;
106
- remove(name: string): void;
107
- clear(): void;
108
- snapshot(): EntitySnapshot;
109
- }
110
-
111
- type ScheduleHandle = (() => void) & {
112
- cancel(): void;
113
- readonly active: boolean;
114
- };
115
- type TweenProperties<T extends object> = {
116
- [K in keyof T]?: number;
117
- };
118
- declare class Scheduler {
119
- #private;
120
- get size(): number;
121
- after(delay: number, callback: () => void): ScheduleHandle;
122
- every(callback: (dt: number) => void): ScheduleHandle;
123
- tween<T extends object>(target: T, properties: TweenProperties<T>, duration: number): Promise<void>;
124
- tick(dt: number): void;
125
- clear(): void;
126
- }
127
-
128
- type StatePatch<T extends Record<string, unknown>> = Partial<T> | ((state: T) => Partial<T>);
129
- type GameStore<T extends Record<string, unknown>> = StoreApi<T> & {
130
- set(patch: StatePatch<T>): void;
131
- flush(): void;
132
- start(): void;
133
- stop(): void;
134
- };
135
- declare function createGameStore<T extends Record<string, unknown>>(initial: T, intervalMs?: number): GameStore<T>;
136
-
137
- interface ViewportSize {
138
- readonly aspect: number;
139
- readonly height: number;
140
- readonly width: number;
141
- }
142
- interface ViewportOptions {
143
- readonly camera: Camera;
144
- readonly renderer: RendererLike;
145
- readonly source?: ViewportPlatformSource;
146
- }
147
- type ViewportResizeHandler = (size: ViewportSize) => void;
148
- interface ViewportPlatformSource {
149
- observeResize(canvas: HTMLCanvasElement, resize: () => void): () => void;
150
- readSize(canvas: HTMLCanvasElement): ViewportSize;
151
- }
152
- declare class Viewport {
153
- #private;
154
- readonly camera: Camera;
155
- readonly renderer: RendererLike;
156
- constructor(options: ViewportOptions);
157
- get size(): ViewportSize;
158
- projectPosition(screen: Vector2, z?: number): Vector3;
159
- unprojectPosition(world: Vector3): Vector2;
160
- onResize(handler: ViewportResizeHandler): () => void;
161
- resize(): void;
162
- dispose(): void;
163
- }
164
-
165
- declare abstract class Scene<TState extends Record<string, unknown> = Record<string, unknown>, TPhysics = undefined> {
166
- static readonly initialState: Record<string, unknown> | undefined;
167
- load(_ctx: Ctx<TState, TPhysics>): void | Promise<void>;
168
- enter(_ctx: Ctx<TState, TPhysics>): SceneEnterResult<TState, TPhysics>;
169
- exit(_ctx: Ctx<TState, TPhysics>): void;
170
- update(_ctx: Ctx<TState, TPhysics>, _dt: number): void;
171
- render(_ctx: Ctx<TState, TPhysics>): void;
172
- }
173
- type SceneConstructor<TState extends Record<string, unknown> = Record<string, unknown>, TPhysics = undefined> = new () => Scene<TState, TPhysics>;
174
- type SceneFrame<TState extends Record<string, unknown> = Record<string, unknown>, TPhysics = undefined> = (ctx: Ctx<TState, TPhysics>, dt: number) => void;
175
- type SceneEnterResult<TState extends Record<string, unknown> = Record<string, unknown>, TPhysics = undefined> = // biome-ignore lint/suspicious/noConfusingVoidType: void preserves existing Scene.enter overrides.
176
- void | SceneFrame<TState, TPhysics>;
177
- interface Ctx<TState extends Record<string, unknown> = Record<string, unknown>, TPhysics = undefined> {
178
- readonly fps: number;
179
- readonly renderer: RendererLike;
180
- readonly viewport: Viewport;
181
- readonly scene: Scene$1;
182
- readonly camera: Camera;
183
- readonly entities: Registry;
184
- readonly add: (object: Object3D) => Object3D;
185
- readonly input: InputMap;
186
- readonly assets: AssetLoader;
187
- readonly after: (delay: number, callback: () => void) => ScheduleHandle;
188
- readonly every: (callback: (dt: number) => void) => ScheduleHandle;
189
- readonly state: GameStore<TState>;
190
- readonly tween: <T extends object>(target: T, properties: {
191
- [K in keyof T]?: number;
192
- }, duration: number) => Promise<void>;
193
- readonly random: Random;
194
- readonly goto: (name: string) => Promise<void>;
195
- physics: TPhysics;
196
- }
197
-
198
- type PluginCleanup = () => void;
199
- interface GamePluginRuntime {
200
- fixedStep(ticks: number): number;
201
- readonly random?: Pick<Random, "state">;
202
- rapier?: string | null;
203
- readonly seed: number | null;
204
- readonly step: number;
205
- }
206
- interface GamePlatformSource {
207
- readonly devToolsHost?: Record<string, unknown>;
208
- readonly input: NonNullable<ConstructorParameters<typeof InputMap>[3]>;
209
- readonly inputTarget?: EventTarget;
210
- readonly renderer: NonNullable<RendererOptions["source"]>;
211
- readonly viewport: NonNullable<ViewportOptions["source"]>;
212
- mountCanvas(canvas: HTMLCanvasElement, container?: HTMLElement): void;
213
- unmountCanvas(canvas: HTMLCanvasElement): void;
214
- }
215
- type GamePluginFunction<TState extends Record<string, unknown> = Record<string, unknown>, TPhysics = undefined> = (ctx: Ctx<TState, TPhysics>) => undefined | PluginCleanup;
216
- interface GamePluginHooks<TState extends Record<string, unknown> = Record<string, unknown>, TPhysics = undefined> {
217
- setup?(ctx: Ctx<TState, TPhysics>, runtime?: GamePluginRuntime): undefined | PluginCleanup | Promise<undefined | PluginCleanup>;
218
- beforeUpdate?(ctx: Ctx<TState, TPhysics>, dt: number): void;
219
- update?(ctx: Ctx<TState, TPhysics>, dt: number): void;
220
- sceneExit?(ctx: Ctx<TState, TPhysics>): void;
221
- dispose?(ctx: Ctx<TState, TPhysics>): void;
222
- }
223
- type GamePlugin<TState extends Record<string, unknown> = Record<string, unknown>, TPhysics = undefined> = GamePluginFunction<TState, TPhysics> | GamePluginHooks<TState, TPhysics>;
224
- interface GameConfig<TState extends Record<string, unknown> = Record<string, unknown>, TPhysics = undefined> {
225
- readonly assets?: AssetLoaderOptions;
226
- readonly camera?: CameraConfig;
227
- readonly canvas?: HTMLCanvasElement;
228
- readonly container?: HTMLElement;
229
- readonly input?: InputBindings;
230
- readonly initialState?: TState;
231
- readonly inputTarget?: EventTarget;
232
- readonly maxSteps?: number;
233
- readonly platform?: GamePlatformSource;
234
- readonly plugins?: readonly GamePlugin<TState, TPhysics>[];
235
- readonly render?: Pick<RendererOptions, "preferWebGPU">;
236
- readonly renderer?: RendererOptions;
237
- readonly seed?: number;
238
- readonly scenes: Record<string, SceneConstructor<TState, TPhysics>>;
239
- readonly step?: number;
240
- readonly start: string;
241
- readonly stateFlushMs?: number;
242
- }
243
- interface PerspectiveCameraConfig {
244
- readonly projection: "perspective";
245
- readonly fov?: number;
246
- readonly near?: number;
247
- readonly far?: number;
248
- }
249
- interface OrthogonalCameraConfig {
250
- readonly projection: "orthogonal";
251
- readonly size: number;
252
- readonly near?: number;
253
- readonly far?: number;
254
- }
255
- type CameraConfig = PerspectiveCameraConfig | OrthogonalCameraConfig;
256
- interface Game<TState extends Record<string, unknown> = Record<string, unknown>, TPhysics = undefined> {
257
- readonly ctx: Ctx<TState, TPhysics> | undefined;
258
- readonly scene: Scene<TState, TPhysics> | undefined;
259
- readonly state: GameStore<TState>;
260
- /** Rebuilds the requested scene from the game's declared initial state. */
261
- goto(name: string): Promise<void>;
262
- start(): Promise<void>;
263
- pause(): void;
264
- resume(): void;
265
- stop(): void;
266
- }
267
- declare function defineGame<TState extends Record<string, unknown>, TPhysics = undefined>(config: GameConfig<TState, TPhysics>): Game<TState, TPhysics>;
268
-
269
- export { type AssetLoader as A, autoFields as B, type CameraConfig as C, type Debuggable as D, type EntitySnapshot as E, createAssetLoader as F, type Game as G, createGameStore as H, type InputAction as I, createRandom as J, createRenderer as K, defineGame as L, input as M, type OrthogonalCameraConfig as O, type PerspectiveCameraConfig as P, type RendererLike as R, Scene as S, Viewport as V, type GamePluginRuntime as a, type GamePluginHooks as b, type AssetLoaderOptions as c, type Ctx as d, type GameConfig as e, type GamePlatformSource as f, type GamePlugin as g, type GamePluginFunction as h, type GameStore as i, type InputBindings as j, InputMap as k, type PluginCleanup as l, type Random as m, type RawInputState as n, Registry as o, type RendererKind as p, type RendererOptions as q, type SceneConstructor as r, type SceneEnterResult as s, type SceneFrame as t, type ScheduleHandle as u, Scheduler as v, type StatePatch as w, type ViewportOptions as x, type ViewportResizeHandler as y, type ViewportSize as z };