@flare-engine/core 0.1.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/LICENSE +21 -0
- package/README.md +29 -0
- package/dist/index.d.mts +242 -0
- package/dist/index.d.ts +242 -0
- package/dist/index.js +491 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +459 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Grzegorz Otto
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# @flare-engine/core
|
|
2
|
+
|
|
3
|
+
Core runtime — `GameLoop` (fixed-timestep with onUpdate / onFixedUpdate / onLateUpdate signals), `Clock` (delta + scaled time), `AppLifecycle` (background/foreground hooks), `ScreenManager` (FIT / FILL / STRETCH + safe-area), `CoroutineManager` (generator-based sequencing), `Pool<T>` (zero-alloc reuse).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
bun add @flare-engine/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { GameLoop, Clock, Pool } from '@flare-engine/core';
|
|
15
|
+
|
|
16
|
+
const loop = new GameLoop({ fixedDt: 1 / 60 });
|
|
17
|
+
loop.onUpdate.add((dt) => updateGame(dt));
|
|
18
|
+
loop.start();
|
|
19
|
+
|
|
20
|
+
const bullets = new Pool(() => ({ x: 0, y: 0, vx: 0, vy: 0 }));
|
|
21
|
+
const b = bullets.acquire();
|
|
22
|
+
bullets.release(b);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
See [API spec](../../docs/design/api-specs.md#flare-enginecore--3kb-gzip) and [skill reference](../../.github/skills/flare-core/SKILL.md).
|
|
26
|
+
|
|
27
|
+
## License
|
|
28
|
+
|
|
29
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { Signal } from '@flare-engine/events';
|
|
2
|
+
import { Rect, Vec2 } from '@flare-engine/math';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Clock tracks frame timing: delta time, elapsed time, fps, timescale.
|
|
6
|
+
* One instance per GameLoop. Updated once per frame.
|
|
7
|
+
*/
|
|
8
|
+
declare class Clock {
|
|
9
|
+
/** Unscaled delta time in seconds since last frame. */
|
|
10
|
+
rawDt: number;
|
|
11
|
+
/** Scaled delta time in seconds (rawDt * timescale). */
|
|
12
|
+
dt: number;
|
|
13
|
+
/** Fixed timestep in seconds for physics updates (default 1/60). */
|
|
14
|
+
fixedDt: number;
|
|
15
|
+
/** Total elapsed time in seconds (scaled). */
|
|
16
|
+
elapsed: number;
|
|
17
|
+
/** Smoothed frames per second. */
|
|
18
|
+
fps: number;
|
|
19
|
+
/** Time scale multiplier. 1 = normal, 0 = paused, 0.5 = slow-mo. */
|
|
20
|
+
timescale: number;
|
|
21
|
+
/** Current frame number (monotonically increasing). */
|
|
22
|
+
frame: number;
|
|
23
|
+
private schedules;
|
|
24
|
+
private fpsAlpha;
|
|
25
|
+
/** Called once per frame by the GameLoop with raw unscaled dt. */
|
|
26
|
+
update(rawDt: number): void;
|
|
27
|
+
/** Schedule a one-shot callback after `delay` seconds (scaled time). */
|
|
28
|
+
schedule(delay: number, callback: () => void): () => void;
|
|
29
|
+
/** Schedule a repeating callback every `interval` seconds (scaled time). */
|
|
30
|
+
repeat(interval: number, callback: () => void): () => void;
|
|
31
|
+
/** Reset clock to initial state. */
|
|
32
|
+
reset(): void;
|
|
33
|
+
private tickSchedules;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Configuration for the game loop. */
|
|
37
|
+
interface GameLoopConfig {
|
|
38
|
+
/** Fixed timestep in seconds (default 1/60). */
|
|
39
|
+
fixedDt?: number;
|
|
40
|
+
/** Maximum raw dt cap in seconds to prevent spiral of death (default 0.25). */
|
|
41
|
+
maxDt?: number;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* GameLoop drives the update cycle using fixed + variable timestep.
|
|
45
|
+
*
|
|
46
|
+
* Fixed updates run at a constant rate (accumulator pattern).
|
|
47
|
+
* Variable updates run once per frame with the actual dt.
|
|
48
|
+
*/
|
|
49
|
+
declare class GameLoop {
|
|
50
|
+
readonly clock: Clock;
|
|
51
|
+
/** Fixed-timestep update (physics, deterministic logic). */
|
|
52
|
+
readonly onFixedUpdate: Signal<number>;
|
|
53
|
+
/** Variable-timestep update (movement, AI, game logic). */
|
|
54
|
+
readonly onUpdate: Signal<number>;
|
|
55
|
+
/** Runs after onUpdate (camera follow, post-processing logic). */
|
|
56
|
+
readonly onLateUpdate: Signal<number>;
|
|
57
|
+
/** Runs before rendering. */
|
|
58
|
+
readonly onPreRender: Signal<void>;
|
|
59
|
+
/** Runs after rendering. */
|
|
60
|
+
readonly onPostRender: Signal<void>;
|
|
61
|
+
private running;
|
|
62
|
+
private paused;
|
|
63
|
+
private accumulator;
|
|
64
|
+
private maxDt;
|
|
65
|
+
private rafId;
|
|
66
|
+
private lastTime;
|
|
67
|
+
/** External tick function — set this for testing or custom scheduling. */
|
|
68
|
+
tick: ((callback: (time: number) => void) => number) | undefined;
|
|
69
|
+
/** External cancel function — paired with tick. */
|
|
70
|
+
cancelTick: ((id: number) => void) | undefined;
|
|
71
|
+
constructor(config?: GameLoopConfig);
|
|
72
|
+
/** Start the game loop. */
|
|
73
|
+
start(): void;
|
|
74
|
+
/** Stop the game loop entirely and reset. */
|
|
75
|
+
stop(): void;
|
|
76
|
+
/** Pause — signals stop firing but loop stays alive. */
|
|
77
|
+
pause(): void;
|
|
78
|
+
/** Resume from pause. */
|
|
79
|
+
resume(): void;
|
|
80
|
+
/** Whether the loop is currently running (started and not stopped). */
|
|
81
|
+
get isRunning(): boolean;
|
|
82
|
+
/** Whether the loop is paused. */
|
|
83
|
+
get isPaused(): boolean;
|
|
84
|
+
/** Process a single frame. Exposed for testing. */
|
|
85
|
+
step(rawDt: number): void;
|
|
86
|
+
private scheduleFrame;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* AppLifecycle bridges React Native AppState to the game loop.
|
|
91
|
+
*
|
|
92
|
+
* - Auto-pauses the loop when app goes to background
|
|
93
|
+
* - Auto-resumes when app returns to foreground
|
|
94
|
+
* - Caps dt on resume to prevent huge time jumps
|
|
95
|
+
*
|
|
96
|
+
* In non-RN environments (tests, web), use the manual methods.
|
|
97
|
+
*/
|
|
98
|
+
declare class AppLifecycle {
|
|
99
|
+
/** Fires when app enters background. */
|
|
100
|
+
readonly onBackground: Signal<void>;
|
|
101
|
+
/** Fires when app returns to foreground. */
|
|
102
|
+
readonly onForeground: Signal<void>;
|
|
103
|
+
/** Fires on memory warning (RN-specific). */
|
|
104
|
+
readonly onMemoryWarning: Signal<void>;
|
|
105
|
+
private loop;
|
|
106
|
+
private wasRunningBeforeBackground;
|
|
107
|
+
constructor(loop: GameLoop);
|
|
108
|
+
/** Call when app goes to background. */
|
|
109
|
+
background(): void;
|
|
110
|
+
/** Call when app returns to foreground. */
|
|
111
|
+
foreground(): void;
|
|
112
|
+
/** Call on memory warning. */
|
|
113
|
+
memoryWarning(): void;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Scale mode for mapping design resolution to device screen. */
|
|
117
|
+
type ScaleMode = 'fit' | 'fill' | 'stretch';
|
|
118
|
+
/** Screen configuration. */
|
|
119
|
+
interface ScreenConfig {
|
|
120
|
+
/** Design resolution width in logical pixels. */
|
|
121
|
+
designWidth: number;
|
|
122
|
+
/** Design resolution height in logical pixels. */
|
|
123
|
+
designHeight: number;
|
|
124
|
+
/** How to fit design resolution to device (default: 'fit'). */
|
|
125
|
+
scaleMode?: ScaleMode;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* ScreenManager maps a fixed design resolution to the actual device screen.
|
|
129
|
+
*
|
|
130
|
+
* - FIT: letterbox (scale to fit, black bars)
|
|
131
|
+
* - FILL: crop (scale to fill, content cropped)
|
|
132
|
+
* - STRETCH: distort (stretch to fill, aspect ratio distorted)
|
|
133
|
+
*/
|
|
134
|
+
declare class ScreenManager {
|
|
135
|
+
/** Design resolution width. */
|
|
136
|
+
readonly designWidth: number;
|
|
137
|
+
/** Design resolution height. */
|
|
138
|
+
readonly designHeight: number;
|
|
139
|
+
/** Current scale mode. */
|
|
140
|
+
scaleMode: ScaleMode;
|
|
141
|
+
/** Device pixel ratio. */
|
|
142
|
+
pixelRatio: number;
|
|
143
|
+
/** Safe area insets (top, right, bottom, left) in device pixels. */
|
|
144
|
+
safeArea: Rect;
|
|
145
|
+
/** Fires when screen dimensions change. */
|
|
146
|
+
readonly onResize: Signal<void>;
|
|
147
|
+
/** Computed scale factor X (design → screen). */
|
|
148
|
+
scaleX: number;
|
|
149
|
+
/** Computed scale factor Y (design → screen). */
|
|
150
|
+
scaleY: number;
|
|
151
|
+
/** Offset X in screen pixels (for letterboxing). */
|
|
152
|
+
offsetX: number;
|
|
153
|
+
/** Offset Y in screen pixels (for letterboxing). */
|
|
154
|
+
offsetY: number;
|
|
155
|
+
private screenWidth;
|
|
156
|
+
private screenHeight;
|
|
157
|
+
constructor(config: ScreenConfig);
|
|
158
|
+
private safeInsetTop;
|
|
159
|
+
private safeInsetRight;
|
|
160
|
+
private safeInsetBottom;
|
|
161
|
+
private safeInsetLeft;
|
|
162
|
+
/** Update with actual device screen dimensions. Call on mount and resize. */
|
|
163
|
+
resize(screenWidth: number, screenHeight: number, pixelRatio?: number): void;
|
|
164
|
+
/**
|
|
165
|
+
* Set safe-area insets in screen pixels. Call this from the RN host with values from
|
|
166
|
+
* `react-native-safe-area-context`. Populates `safeArea` as the usable region.
|
|
167
|
+
*/
|
|
168
|
+
setSafeAreaInsets(top: number, right: number, bottom: number, left: number): void;
|
|
169
|
+
private recalculateSafeArea;
|
|
170
|
+
/** Convert design-space point to screen-space. */
|
|
171
|
+
designToScreen(point: Vec2): Vec2;
|
|
172
|
+
/** Convert screen-space point to design-space. */
|
|
173
|
+
screenToDesign(point: Vec2): Vec2;
|
|
174
|
+
/** Viewport rect in design coordinates (visible area). */
|
|
175
|
+
get viewport(): Rect;
|
|
176
|
+
private recalculate;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Generator-based coroutines for game logic.
|
|
181
|
+
*
|
|
182
|
+
* - yield number → wait N seconds (scaled time)
|
|
183
|
+
* - yield undefined → wait 1 frame
|
|
184
|
+
* - yield Promise → wait until resolved
|
|
185
|
+
*/
|
|
186
|
+
/** A running coroutine handle. */
|
|
187
|
+
interface CoroutineHandle {
|
|
188
|
+
/** Whether the coroutine has completed or been cancelled. */
|
|
189
|
+
readonly done: boolean;
|
|
190
|
+
/** Cancel the coroutine. It will not resume. */
|
|
191
|
+
cancel(): void;
|
|
192
|
+
}
|
|
193
|
+
/** Yield type for coroutine generators. */
|
|
194
|
+
type CoroutineYield = number | undefined | Promise<unknown>;
|
|
195
|
+
/** A coroutine generator function. */
|
|
196
|
+
type CoroutineGenerator = Generator<CoroutineYield, void, unknown>;
|
|
197
|
+
/**
|
|
198
|
+
* CoroutineManager runs generator-based coroutines.
|
|
199
|
+
* Call update(dt) each frame to advance all active coroutines.
|
|
200
|
+
*/
|
|
201
|
+
declare class CoroutineManager {
|
|
202
|
+
private coroutines;
|
|
203
|
+
/** Start a new coroutine. Returns a handle to check status or cancel. */
|
|
204
|
+
start(gen: CoroutineGenerator): CoroutineHandle;
|
|
205
|
+
/** Update all running coroutines. Call once per frame with scaled dt. */
|
|
206
|
+
update(dt: number): void;
|
|
207
|
+
/** Cancel all running coroutines. */
|
|
208
|
+
clear(): void;
|
|
209
|
+
/** Number of active coroutines. */
|
|
210
|
+
get count(): number;
|
|
211
|
+
private advance;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Generic object pool for zero-alloc gameplay.
|
|
216
|
+
*
|
|
217
|
+
* - acquire() returns a recycled or newly created object
|
|
218
|
+
* - release(obj) returns it to the pool
|
|
219
|
+
* - prewarm(count) pre-allocates objects
|
|
220
|
+
*/
|
|
221
|
+
declare class Pool<T> {
|
|
222
|
+
private items;
|
|
223
|
+
private factory;
|
|
224
|
+
private resetFn;
|
|
225
|
+
/**
|
|
226
|
+
* @param factory Function to create a new instance.
|
|
227
|
+
* @param reset Optional function to reset an object before reuse.
|
|
228
|
+
*/
|
|
229
|
+
constructor(factory: () => T, reset?: (item: T) => void);
|
|
230
|
+
/** Get an object from the pool (or create a new one). */
|
|
231
|
+
acquire(): T;
|
|
232
|
+
/** Return an object to the pool. Calls reset if configured. */
|
|
233
|
+
release(item: T): void;
|
|
234
|
+
/** Pre-allocate objects into the pool. */
|
|
235
|
+
prewarm(count: number): void;
|
|
236
|
+
/** Number of available objects in the pool. */
|
|
237
|
+
get available(): number;
|
|
238
|
+
/** Clear all pooled objects. */
|
|
239
|
+
clear(): void;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export { AppLifecycle, Clock, type CoroutineGenerator, type CoroutineHandle, CoroutineManager, type CoroutineYield, GameLoop, type GameLoopConfig, Pool, type ScaleMode, type ScreenConfig, ScreenManager };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { Signal } from '@flare-engine/events';
|
|
2
|
+
import { Rect, Vec2 } from '@flare-engine/math';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Clock tracks frame timing: delta time, elapsed time, fps, timescale.
|
|
6
|
+
* One instance per GameLoop. Updated once per frame.
|
|
7
|
+
*/
|
|
8
|
+
declare class Clock {
|
|
9
|
+
/** Unscaled delta time in seconds since last frame. */
|
|
10
|
+
rawDt: number;
|
|
11
|
+
/** Scaled delta time in seconds (rawDt * timescale). */
|
|
12
|
+
dt: number;
|
|
13
|
+
/** Fixed timestep in seconds for physics updates (default 1/60). */
|
|
14
|
+
fixedDt: number;
|
|
15
|
+
/** Total elapsed time in seconds (scaled). */
|
|
16
|
+
elapsed: number;
|
|
17
|
+
/** Smoothed frames per second. */
|
|
18
|
+
fps: number;
|
|
19
|
+
/** Time scale multiplier. 1 = normal, 0 = paused, 0.5 = slow-mo. */
|
|
20
|
+
timescale: number;
|
|
21
|
+
/** Current frame number (monotonically increasing). */
|
|
22
|
+
frame: number;
|
|
23
|
+
private schedules;
|
|
24
|
+
private fpsAlpha;
|
|
25
|
+
/** Called once per frame by the GameLoop with raw unscaled dt. */
|
|
26
|
+
update(rawDt: number): void;
|
|
27
|
+
/** Schedule a one-shot callback after `delay` seconds (scaled time). */
|
|
28
|
+
schedule(delay: number, callback: () => void): () => void;
|
|
29
|
+
/** Schedule a repeating callback every `interval` seconds (scaled time). */
|
|
30
|
+
repeat(interval: number, callback: () => void): () => void;
|
|
31
|
+
/** Reset clock to initial state. */
|
|
32
|
+
reset(): void;
|
|
33
|
+
private tickSchedules;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Configuration for the game loop. */
|
|
37
|
+
interface GameLoopConfig {
|
|
38
|
+
/** Fixed timestep in seconds (default 1/60). */
|
|
39
|
+
fixedDt?: number;
|
|
40
|
+
/** Maximum raw dt cap in seconds to prevent spiral of death (default 0.25). */
|
|
41
|
+
maxDt?: number;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* GameLoop drives the update cycle using fixed + variable timestep.
|
|
45
|
+
*
|
|
46
|
+
* Fixed updates run at a constant rate (accumulator pattern).
|
|
47
|
+
* Variable updates run once per frame with the actual dt.
|
|
48
|
+
*/
|
|
49
|
+
declare class GameLoop {
|
|
50
|
+
readonly clock: Clock;
|
|
51
|
+
/** Fixed-timestep update (physics, deterministic logic). */
|
|
52
|
+
readonly onFixedUpdate: Signal<number>;
|
|
53
|
+
/** Variable-timestep update (movement, AI, game logic). */
|
|
54
|
+
readonly onUpdate: Signal<number>;
|
|
55
|
+
/** Runs after onUpdate (camera follow, post-processing logic). */
|
|
56
|
+
readonly onLateUpdate: Signal<number>;
|
|
57
|
+
/** Runs before rendering. */
|
|
58
|
+
readonly onPreRender: Signal<void>;
|
|
59
|
+
/** Runs after rendering. */
|
|
60
|
+
readonly onPostRender: Signal<void>;
|
|
61
|
+
private running;
|
|
62
|
+
private paused;
|
|
63
|
+
private accumulator;
|
|
64
|
+
private maxDt;
|
|
65
|
+
private rafId;
|
|
66
|
+
private lastTime;
|
|
67
|
+
/** External tick function — set this for testing or custom scheduling. */
|
|
68
|
+
tick: ((callback: (time: number) => void) => number) | undefined;
|
|
69
|
+
/** External cancel function — paired with tick. */
|
|
70
|
+
cancelTick: ((id: number) => void) | undefined;
|
|
71
|
+
constructor(config?: GameLoopConfig);
|
|
72
|
+
/** Start the game loop. */
|
|
73
|
+
start(): void;
|
|
74
|
+
/** Stop the game loop entirely and reset. */
|
|
75
|
+
stop(): void;
|
|
76
|
+
/** Pause — signals stop firing but loop stays alive. */
|
|
77
|
+
pause(): void;
|
|
78
|
+
/** Resume from pause. */
|
|
79
|
+
resume(): void;
|
|
80
|
+
/** Whether the loop is currently running (started and not stopped). */
|
|
81
|
+
get isRunning(): boolean;
|
|
82
|
+
/** Whether the loop is paused. */
|
|
83
|
+
get isPaused(): boolean;
|
|
84
|
+
/** Process a single frame. Exposed for testing. */
|
|
85
|
+
step(rawDt: number): void;
|
|
86
|
+
private scheduleFrame;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* AppLifecycle bridges React Native AppState to the game loop.
|
|
91
|
+
*
|
|
92
|
+
* - Auto-pauses the loop when app goes to background
|
|
93
|
+
* - Auto-resumes when app returns to foreground
|
|
94
|
+
* - Caps dt on resume to prevent huge time jumps
|
|
95
|
+
*
|
|
96
|
+
* In non-RN environments (tests, web), use the manual methods.
|
|
97
|
+
*/
|
|
98
|
+
declare class AppLifecycle {
|
|
99
|
+
/** Fires when app enters background. */
|
|
100
|
+
readonly onBackground: Signal<void>;
|
|
101
|
+
/** Fires when app returns to foreground. */
|
|
102
|
+
readonly onForeground: Signal<void>;
|
|
103
|
+
/** Fires on memory warning (RN-specific). */
|
|
104
|
+
readonly onMemoryWarning: Signal<void>;
|
|
105
|
+
private loop;
|
|
106
|
+
private wasRunningBeforeBackground;
|
|
107
|
+
constructor(loop: GameLoop);
|
|
108
|
+
/** Call when app goes to background. */
|
|
109
|
+
background(): void;
|
|
110
|
+
/** Call when app returns to foreground. */
|
|
111
|
+
foreground(): void;
|
|
112
|
+
/** Call on memory warning. */
|
|
113
|
+
memoryWarning(): void;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Scale mode for mapping design resolution to device screen. */
|
|
117
|
+
type ScaleMode = 'fit' | 'fill' | 'stretch';
|
|
118
|
+
/** Screen configuration. */
|
|
119
|
+
interface ScreenConfig {
|
|
120
|
+
/** Design resolution width in logical pixels. */
|
|
121
|
+
designWidth: number;
|
|
122
|
+
/** Design resolution height in logical pixels. */
|
|
123
|
+
designHeight: number;
|
|
124
|
+
/** How to fit design resolution to device (default: 'fit'). */
|
|
125
|
+
scaleMode?: ScaleMode;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* ScreenManager maps a fixed design resolution to the actual device screen.
|
|
129
|
+
*
|
|
130
|
+
* - FIT: letterbox (scale to fit, black bars)
|
|
131
|
+
* - FILL: crop (scale to fill, content cropped)
|
|
132
|
+
* - STRETCH: distort (stretch to fill, aspect ratio distorted)
|
|
133
|
+
*/
|
|
134
|
+
declare class ScreenManager {
|
|
135
|
+
/** Design resolution width. */
|
|
136
|
+
readonly designWidth: number;
|
|
137
|
+
/** Design resolution height. */
|
|
138
|
+
readonly designHeight: number;
|
|
139
|
+
/** Current scale mode. */
|
|
140
|
+
scaleMode: ScaleMode;
|
|
141
|
+
/** Device pixel ratio. */
|
|
142
|
+
pixelRatio: number;
|
|
143
|
+
/** Safe area insets (top, right, bottom, left) in device pixels. */
|
|
144
|
+
safeArea: Rect;
|
|
145
|
+
/** Fires when screen dimensions change. */
|
|
146
|
+
readonly onResize: Signal<void>;
|
|
147
|
+
/** Computed scale factor X (design → screen). */
|
|
148
|
+
scaleX: number;
|
|
149
|
+
/** Computed scale factor Y (design → screen). */
|
|
150
|
+
scaleY: number;
|
|
151
|
+
/** Offset X in screen pixels (for letterboxing). */
|
|
152
|
+
offsetX: number;
|
|
153
|
+
/** Offset Y in screen pixels (for letterboxing). */
|
|
154
|
+
offsetY: number;
|
|
155
|
+
private screenWidth;
|
|
156
|
+
private screenHeight;
|
|
157
|
+
constructor(config: ScreenConfig);
|
|
158
|
+
private safeInsetTop;
|
|
159
|
+
private safeInsetRight;
|
|
160
|
+
private safeInsetBottom;
|
|
161
|
+
private safeInsetLeft;
|
|
162
|
+
/** Update with actual device screen dimensions. Call on mount and resize. */
|
|
163
|
+
resize(screenWidth: number, screenHeight: number, pixelRatio?: number): void;
|
|
164
|
+
/**
|
|
165
|
+
* Set safe-area insets in screen pixels. Call this from the RN host with values from
|
|
166
|
+
* `react-native-safe-area-context`. Populates `safeArea` as the usable region.
|
|
167
|
+
*/
|
|
168
|
+
setSafeAreaInsets(top: number, right: number, bottom: number, left: number): void;
|
|
169
|
+
private recalculateSafeArea;
|
|
170
|
+
/** Convert design-space point to screen-space. */
|
|
171
|
+
designToScreen(point: Vec2): Vec2;
|
|
172
|
+
/** Convert screen-space point to design-space. */
|
|
173
|
+
screenToDesign(point: Vec2): Vec2;
|
|
174
|
+
/** Viewport rect in design coordinates (visible area). */
|
|
175
|
+
get viewport(): Rect;
|
|
176
|
+
private recalculate;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Generator-based coroutines for game logic.
|
|
181
|
+
*
|
|
182
|
+
* - yield number → wait N seconds (scaled time)
|
|
183
|
+
* - yield undefined → wait 1 frame
|
|
184
|
+
* - yield Promise → wait until resolved
|
|
185
|
+
*/
|
|
186
|
+
/** A running coroutine handle. */
|
|
187
|
+
interface CoroutineHandle {
|
|
188
|
+
/** Whether the coroutine has completed or been cancelled. */
|
|
189
|
+
readonly done: boolean;
|
|
190
|
+
/** Cancel the coroutine. It will not resume. */
|
|
191
|
+
cancel(): void;
|
|
192
|
+
}
|
|
193
|
+
/** Yield type for coroutine generators. */
|
|
194
|
+
type CoroutineYield = number | undefined | Promise<unknown>;
|
|
195
|
+
/** A coroutine generator function. */
|
|
196
|
+
type CoroutineGenerator = Generator<CoroutineYield, void, unknown>;
|
|
197
|
+
/**
|
|
198
|
+
* CoroutineManager runs generator-based coroutines.
|
|
199
|
+
* Call update(dt) each frame to advance all active coroutines.
|
|
200
|
+
*/
|
|
201
|
+
declare class CoroutineManager {
|
|
202
|
+
private coroutines;
|
|
203
|
+
/** Start a new coroutine. Returns a handle to check status or cancel. */
|
|
204
|
+
start(gen: CoroutineGenerator): CoroutineHandle;
|
|
205
|
+
/** Update all running coroutines. Call once per frame with scaled dt. */
|
|
206
|
+
update(dt: number): void;
|
|
207
|
+
/** Cancel all running coroutines. */
|
|
208
|
+
clear(): void;
|
|
209
|
+
/** Number of active coroutines. */
|
|
210
|
+
get count(): number;
|
|
211
|
+
private advance;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Generic object pool for zero-alloc gameplay.
|
|
216
|
+
*
|
|
217
|
+
* - acquire() returns a recycled or newly created object
|
|
218
|
+
* - release(obj) returns it to the pool
|
|
219
|
+
* - prewarm(count) pre-allocates objects
|
|
220
|
+
*/
|
|
221
|
+
declare class Pool<T> {
|
|
222
|
+
private items;
|
|
223
|
+
private factory;
|
|
224
|
+
private resetFn;
|
|
225
|
+
/**
|
|
226
|
+
* @param factory Function to create a new instance.
|
|
227
|
+
* @param reset Optional function to reset an object before reuse.
|
|
228
|
+
*/
|
|
229
|
+
constructor(factory: () => T, reset?: (item: T) => void);
|
|
230
|
+
/** Get an object from the pool (or create a new one). */
|
|
231
|
+
acquire(): T;
|
|
232
|
+
/** Return an object to the pool. Calls reset if configured. */
|
|
233
|
+
release(item: T): void;
|
|
234
|
+
/** Pre-allocate objects into the pool. */
|
|
235
|
+
prewarm(count: number): void;
|
|
236
|
+
/** Number of available objects in the pool. */
|
|
237
|
+
get available(): number;
|
|
238
|
+
/** Clear all pooled objects. */
|
|
239
|
+
clear(): void;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export { AppLifecycle, Clock, type CoroutineGenerator, type CoroutineHandle, CoroutineManager, type CoroutineYield, GameLoop, type GameLoopConfig, Pool, type ScaleMode, type ScreenConfig, ScreenManager };
|