@flare-engine/react 0.2.1 → 0.3.1
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 +201 -21
- package/NOTICE +2 -0
- package/README.md +29 -13
- package/dist/index.d.mts +202 -186
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +202 -186
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +762 -522
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +741 -494
- package/dist/index.mjs.map +1 -1
- package/package.json +39 -22
package/dist/index.mjs
CHANGED
|
@@ -1,526 +1,773 @@
|
|
|
1
|
-
|
|
2
|
-
import { createContext } from "react";
|
|
3
|
-
var GameContext = createContext(null);
|
|
4
|
-
|
|
5
|
-
// src/game-view.tsx
|
|
1
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
|
|
6
2
|
import { AssetManager } from "@flare-engine/assets";
|
|
7
3
|
import { AudioManager } from "@flare-engine/audio";
|
|
8
4
|
import { GameLoop } from "@flare-engine/core";
|
|
9
5
|
import { Position, World } from "@flare-engine/ecs";
|
|
10
|
-
import { InputManager } from "@flare-engine/input";
|
|
11
|
-
import { Camera2D, Renderer, Sprite, SpriteBatch, createRenderSystem } from "@flare-engine/render";
|
|
6
|
+
import { InputManager, createButtonState, createJoystickState, pressButton, releaseButton, releaseJoystick, resetButtonFrame, updateJoystick } from "@flare-engine/input";
|
|
7
|
+
import { Camera2D, Renderer, Sprite, SpriteBatch, createFrameRenderer, createRenderSystem } from "@flare-engine/render";
|
|
12
8
|
import { SceneManager } from "@flare-engine/scene";
|
|
13
|
-
import {
|
|
14
|
-
|
|
15
|
-
Image,
|
|
16
|
-
RuntimeShader,
|
|
17
|
-
Skia,
|
|
18
|
-
useCanvasRef,
|
|
19
|
-
usePictureAsTexture
|
|
20
|
-
} from "@shopify/react-native-skia";
|
|
21
|
-
import { useEffect, useMemo, useState } from "react";
|
|
22
|
-
import { PixelRatio, StyleSheet, View } from "react-native";
|
|
9
|
+
import { Canvas, Image, RuntimeShader, Skia, useCanvasRef, usePictureAsTexture } from "@shopify/react-native-skia";
|
|
10
|
+
import { PixelRatio, Platform, StyleSheet, View } from "react-native";
|
|
23
11
|
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
12
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
13
|
+
import { easeOutCubic } from "@flare-engine/math";
|
|
14
|
+
//#region src/game-context.ts
|
|
15
|
+
/**
|
|
16
|
+
* React context that provides access to core game objects.
|
|
17
|
+
* Null if accessed outside a <GameView>.
|
|
18
|
+
*/
|
|
19
|
+
const GameContext = createContext(null);
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/safe-area.ts
|
|
22
|
+
const SafeAreaContext = createContext(null);
|
|
28
23
|
function useSafeArea() {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
return value;
|
|
24
|
+
const value = useContext(SafeAreaContext);
|
|
25
|
+
if (value === null) throw new Error("useSafeArea must be used inside a <GameView>");
|
|
26
|
+
return value;
|
|
34
27
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/scene-texture.ts
|
|
30
|
+
/**
|
|
31
|
+
* `Symbol.dispose` is not in this project's ES2022 lib, so the well-known
|
|
32
|
+
* symbol is looked up at runtime instead of written as a computed key.
|
|
33
|
+
*/
|
|
34
|
+
const DISPOSE = Symbol.dispose;
|
|
35
|
+
function releaseSkiaObject(target) {
|
|
36
|
+
if (DISPOSE === void 0 || target === null || target === void 0) return;
|
|
37
|
+
const disposer = target[DISPOSE];
|
|
38
|
+
if (typeof disposer === "function") disposer.call(target);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Reuse one offscreen surface across frames instead of allocating per frame.
|
|
42
|
+
*
|
|
43
|
+
* On **web** this is a correctness requirement, not an optimisation. RN-Skia's
|
|
44
|
+
* web `MakeOffscreen` builds a fresh `OffscreenCanvas` and a fresh WebGL
|
|
45
|
+
* context on every call and releases neither; Chrome caps a page at 16 live
|
|
46
|
+
* WebGL contexts and evicts the oldest, which is the on-screen `<Canvas>`.
|
|
47
|
+
* A per-frame allocation therefore kills the visible canvas within ~16 frames
|
|
48
|
+
* while the game loop keeps running — the app looks alive and paints nothing.
|
|
49
|
+
* Measured and written up in docs/plans/web-target-2026-08.md §12.
|
|
50
|
+
*/
|
|
51
|
+
function createSceneSurfaceCache(Skia) {
|
|
52
|
+
let surface = null;
|
|
53
|
+
let currentWidth = 0;
|
|
54
|
+
let currentHeight = 0;
|
|
55
|
+
const dispose = () => {
|
|
56
|
+
if (surface === null) return;
|
|
57
|
+
releaseSkiaObject(surface);
|
|
58
|
+
surface = null;
|
|
59
|
+
currentWidth = 0;
|
|
60
|
+
currentHeight = 0;
|
|
61
|
+
};
|
|
62
|
+
return {
|
|
63
|
+
acquire(width, height) {
|
|
64
|
+
if (width <= 0 || height <= 0) return null;
|
|
65
|
+
if (surface !== null && currentWidth === width && currentHeight === height) return surface;
|
|
66
|
+
dispose();
|
|
67
|
+
const next = Skia.Surface.MakeOffscreen(width, height);
|
|
68
|
+
if (next === null) return null;
|
|
69
|
+
surface = next;
|
|
70
|
+
currentWidth = width;
|
|
71
|
+
currentHeight = height;
|
|
72
|
+
return surface;
|
|
73
|
+
},
|
|
74
|
+
dispose
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Capture the scene into a reused offscreen surface, releasing Skia objects as
|
|
79
|
+
* it goes.
|
|
80
|
+
*
|
|
81
|
+
* Two leaks have to be avoided here, and both were measured in a browser
|
|
82
|
+
* (docs/plans/web-target-2026-08.md §12):
|
|
83
|
+
*
|
|
84
|
+
* 1. **Per-frame surfaces.** RN-Skia's web `MakeOffscreen` builds an
|
|
85
|
+
* `OffscreenCanvas` + WebGL context per call and releases neither; Chrome
|
|
86
|
+
* caps a page at 16 live contexts and evicts the on-screen canvas. Hence the
|
|
87
|
+
* surface cache.
|
|
88
|
+
* 2. **Per-frame images.** `makeNonTextureImage()` reads the whole viewport back
|
|
89
|
+
* into the wasm heap. Leaving old CPU images unreleased exhausts that heap.
|
|
90
|
+
* A single-slot release strategy is not enough: RN-Skia's web `<Canvas>`
|
|
91
|
+
* paints on its own rAF, which fires *after* the capture, so the image handed
|
|
92
|
+
* to React on frame N is still in use when frame N+1 runs. Releasing it
|
|
93
|
+
* immediately causes `BindingError: Cannot pass deleted object as a pointer of
|
|
94
|
+
* type Image const*` from `JsiSkImage.width` every frame and a blank canvas.
|
|
95
|
+
* Measured at 2560×1215 / Chrome (WEB-3):
|
|
96
|
+
* hold=1 (one slot — what shipped at WEB-2) → blank, BindingError every frame
|
|
97
|
+
* hold=2 → blank
|
|
98
|
+
* hold=3 → 1378 frames, zero errors
|
|
99
|
+
* A FIFO ring of depth 3 is the measured minimum.
|
|
100
|
+
*
|
|
101
|
+
* The snapshot is released as soon as it has been copied. Frame images are
|
|
102
|
+
* released once the ring exceeds depth 3, ensuring the canvas's rAF always has
|
|
103
|
+
* a live image to paint from.
|
|
104
|
+
*/
|
|
105
|
+
const RETENTION_DEPTH = 3;
|
|
106
|
+
function createSceneCapture(Skia) {
|
|
107
|
+
const surfaces = createSceneSurfaceCache(Skia);
|
|
108
|
+
const ring = [];
|
|
109
|
+
return {
|
|
110
|
+
capture(picture, width, height) {
|
|
111
|
+
const surface = surfaces.acquire(width, height);
|
|
112
|
+
if (surface === null) return null;
|
|
113
|
+
surface.getCanvas().drawPicture(picture);
|
|
114
|
+
surface.flush();
|
|
115
|
+
const snapshot = surface.makeImageSnapshot();
|
|
116
|
+
const detachable = snapshot;
|
|
117
|
+
const detached = detachable.makeNonTextureImage ? detachable.makeNonTextureImage() : snapshot;
|
|
118
|
+
if (detached !== snapshot) releaseSkiaObject(snapshot);
|
|
119
|
+
ring.push(detached);
|
|
120
|
+
if (ring.length > RETENTION_DEPTH) releaseSkiaObject(ring.shift());
|
|
121
|
+
return detached;
|
|
122
|
+
},
|
|
123
|
+
dispose() {
|
|
124
|
+
for (const img of ring) releaseSkiaObject(img);
|
|
125
|
+
ring.length = 0;
|
|
126
|
+
surfaces.dispose();
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Web scene texture (imperative form): expose a `capture` callback that
|
|
132
|
+
* `GameView`'s `onPreRender` frame callback invokes directly instead of pushing
|
|
133
|
+
* a picture through React state.
|
|
134
|
+
*
|
|
135
|
+
* The previous reactive form called `setImage` from a `useEffect` keyed on
|
|
136
|
+
* `[Skia, picture, width, height]`. `onPreRender` would call `setPicture`,
|
|
137
|
+
* React would re-render, the effect would re-run, `setImage` would re-render
|
|
138
|
+
* again. That is a setState-in-a-passive-effect cascade; React's
|
|
139
|
+
* NESTED_PASSIVE_UPDATE_LIMIT (50) trips and logs `Maximum update depth
|
|
140
|
+
* exceeded` on every web load. It also costs two React renders per frame and
|
|
141
|
+
* delays the captured image by two hops.
|
|
142
|
+
*
|
|
143
|
+
* `GameLoop` schedules via `requestAnimationFrame`, so calling `setImage`
|
|
144
|
+
* directly from `onPreRender` is an ordinary root update that never touches the
|
|
145
|
+
* passive-update counter. Both hooks still run on both platforms so hook order
|
|
146
|
+
* never varies, but this hook is now inert on native simply because nothing
|
|
147
|
+
* calls `capture` on that platform.
|
|
148
|
+
*
|
|
149
|
+
* `makeNonTextureImage()` is required regardless of reuse: the offscreen surface
|
|
150
|
+
* has its own WebGL context, and a texture-backed image cannot be sampled by the
|
|
151
|
+
* on-screen canvas's context. It also makes reuse safe — the snapshot is copied
|
|
152
|
+
* off the GPU so next frame's draw into the same surface cannot disturb the image
|
|
153
|
+
* React still holds.
|
|
154
|
+
*
|
|
155
|
+
* The surface is never cleared here: `Renderer.begin()` records
|
|
156
|
+
* `canvas.clear(clearColor)` into the picture itself (packages/render
|
|
157
|
+
* `renderer.ts:75`), so replaying the picture repaints the full surface.
|
|
158
|
+
*/
|
|
159
|
+
function useWebSceneTexture(Skia) {
|
|
160
|
+
const captureRef = useRef(null);
|
|
161
|
+
const [image, setImage] = useState(null);
|
|
162
|
+
useEffect(() => {
|
|
163
|
+
return () => {
|
|
164
|
+
captureRef.current?.dispose();
|
|
165
|
+
captureRef.current = null;
|
|
166
|
+
};
|
|
167
|
+
}, []);
|
|
168
|
+
return {
|
|
169
|
+
image,
|
|
170
|
+
capture: useCallback((picture, width, height) => {
|
|
171
|
+
if (picture === null || picture === void 0) return;
|
|
172
|
+
if (captureRef.current === null) captureRef.current = createSceneCapture(Skia);
|
|
173
|
+
const next = captureRef.current.capture(picture, width, height);
|
|
174
|
+
if (next !== null) setImage(next);
|
|
175
|
+
}, [Skia])
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
//#endregion
|
|
179
|
+
//#region src/skia-bridge.ts
|
|
180
|
+
/**
|
|
181
|
+
* Take a snapshot of `surface` and detach it from the GPU context so it can
|
|
182
|
+
* be safely sampled from a different GrDirectContext (e.g. the on-screen
|
|
183
|
+
* Canvas). RN-Skia's MakeOffscreen allocates a private Gr context per
|
|
184
|
+
* surface; without this step Skia silently renders black when the texture is
|
|
185
|
+
* read from any other context.
|
|
186
|
+
*
|
|
187
|
+
* Calls `makeNonTextureImage()` when available (RN-Skia ≥ 0.1 on device).
|
|
188
|
+
* Falls back to the raw snapshot when the method is absent (CPU surfaces,
|
|
189
|
+
* test mocks) — no behavioural change in those paths.
|
|
190
|
+
*/
|
|
40
191
|
function detachSnapshot(surface) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
return snap;
|
|
192
|
+
const snap = surface.makeImageSnapshot();
|
|
193
|
+
if (typeof snap.makeNonTextureImage === "function") return snap.makeNonTextureImage();
|
|
194
|
+
return snap;
|
|
46
195
|
}
|
|
47
|
-
|
|
48
|
-
|
|
196
|
+
/**
|
|
197
|
+
* Create a `SkiaFrameRenderer`.
|
|
198
|
+
*
|
|
199
|
+
* `Skia` is passed in (rather than imported) so unit tests can supply a
|
|
200
|
+
* mock. Production code constructs this once inside `<GameView>` using the
|
|
201
|
+
* real `Skia` import from `@shopify/react-native-skia`.
|
|
202
|
+
*
|
|
203
|
+
* Delegates to `createFrameRenderer` from `@flare-engine/render` — the
|
|
204
|
+
* frame-renderer body is backend-neutral and shared with render-web.
|
|
205
|
+
*/
|
|
206
|
+
function createSkiaFrameRenderer(Skia) {
|
|
207
|
+
return createFrameRenderer(Skia);
|
|
49
208
|
}
|
|
209
|
+
/**
|
|
210
|
+
* Render a list of solid rectangles into a fresh offscreen surface and return
|
|
211
|
+
* the resulting image snapshot. Returns `null` if `Surface` factory is absent or
|
|
212
|
+
* fails. The surface is not cached — callers that need reuse should cache the
|
|
213
|
+
* returned image themselves.
|
|
214
|
+
*/
|
|
50
215
|
function renderShapesToImage(skia, opts) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
216
|
+
if (skia.Surface === void 0) return null;
|
|
217
|
+
const surface = skia.Surface.Make(opts.width, opts.height);
|
|
218
|
+
if (surface === null) return null;
|
|
219
|
+
const scratch = /* @__PURE__ */ new Float32Array(4);
|
|
220
|
+
const canvas = surface.getCanvas();
|
|
221
|
+
unpackArgb(scratch, opts.clearColor);
|
|
222
|
+
canvas.clear(scratch);
|
|
223
|
+
const paint = skia.Paint();
|
|
224
|
+
for (const shape of opts.shapes) {
|
|
225
|
+
unpackArgb(scratch, shape.color);
|
|
226
|
+
if (paint.setColor) paint.setColor(scratch);
|
|
227
|
+
const rect = skia.XYWHRect(shape.x, shape.y, shape.w, shape.h);
|
|
228
|
+
canvas.drawRect(rect, paint);
|
|
229
|
+
}
|
|
230
|
+
return detachSnapshot(surface);
|
|
66
231
|
}
|
|
232
|
+
/** Unpack a 0xAARRGGBB integer into a Float32Array [r, g, b, a] in 0..1 range. */
|
|
67
233
|
function unpackArgb(out, argb) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
234
|
+
out[0] = (argb >>> 16 & 255) / 255;
|
|
235
|
+
out[1] = (argb >>> 8 & 255) / 255;
|
|
236
|
+
out[2] = (argb & 255) / 255;
|
|
237
|
+
out[3] = (argb >>> 24 & 255) / 255;
|
|
72
238
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
239
|
+
//#endregion
|
|
240
|
+
//#region src/game-view.tsx
|
|
241
|
+
/**
|
|
242
|
+
* GameView — root component that sets up the Skia canvas, game loop,
|
|
243
|
+
* and all core game systems. Provides GameContext to descendants.
|
|
244
|
+
*
|
|
245
|
+
* Overlay controls (VirtualJoystick, VirtualButton) should be placed
|
|
246
|
+
* as children; they render on top of the Skia canvas.
|
|
247
|
+
*/
|
|
248
|
+
function GameView({ config, onReady, style, children, safeArea }) {
|
|
249
|
+
const insets = useSafeAreaInsets();
|
|
250
|
+
const resolvedSafeArea = safeArea ?? insets;
|
|
251
|
+
const canvasRef = useCanvasRef();
|
|
252
|
+
const [picture, setPicture] = useState(null);
|
|
253
|
+
const [passes, setPasses] = useState(EMPTY_PASSES);
|
|
254
|
+
const [viewportSize, setViewportSize] = useState({
|
|
255
|
+
w: 0,
|
|
256
|
+
h: 0
|
|
257
|
+
});
|
|
258
|
+
const pd = PixelRatio.get();
|
|
259
|
+
const sceneSize = useMemo(() => ({
|
|
260
|
+
width: viewportSize.w * pd,
|
|
261
|
+
height: viewportSize.h * pd
|
|
262
|
+
}), [
|
|
263
|
+
viewportSize.w,
|
|
264
|
+
viewportSize.h,
|
|
265
|
+
pd
|
|
266
|
+
]);
|
|
267
|
+
const isWeb = Platform.OS === "web";
|
|
268
|
+
const nativeTexture = usePictureAsTexture(isWeb ? null : picture, sceneSize);
|
|
269
|
+
const { image: webTexture, capture: captureWebScene } = useWebSceneTexture(Skia);
|
|
270
|
+
const sceneTexture = isWeb ? webTexture : nativeTexture;
|
|
271
|
+
const ctx = useMemo(() => {
|
|
272
|
+
const loop = new GameLoop({
|
|
273
|
+
fixedDt: config?.fixedDt,
|
|
274
|
+
maxDt: config?.maxDt
|
|
275
|
+
});
|
|
276
|
+
const world = new World();
|
|
277
|
+
const camera = new Camera2D();
|
|
278
|
+
const input = new InputManager();
|
|
279
|
+
const audio = new AudioManager();
|
|
280
|
+
const noop = () => Promise.resolve(null);
|
|
281
|
+
return {
|
|
282
|
+
world,
|
|
283
|
+
camera,
|
|
284
|
+
input,
|
|
285
|
+
audio,
|
|
286
|
+
assets: new AssetManager(config?.assetLoader ?? noop),
|
|
287
|
+
scenes: new SceneManager(),
|
|
288
|
+
loop,
|
|
289
|
+
renderer: new Renderer(),
|
|
290
|
+
batch: new SpriteBatch()
|
|
291
|
+
};
|
|
292
|
+
}, []);
|
|
293
|
+
const webPostFxWarnedRef = useRef(false);
|
|
294
|
+
useEffect(() => {
|
|
295
|
+
const bridge = createSkiaFrameRenderer(Skia);
|
|
296
|
+
ctx.renderer.setCanvas(bridge.canvas);
|
|
297
|
+
const clearColor = config?.clearColor ?? 4278190080;
|
|
298
|
+
const onRenderBackground = config?.onRenderBackground;
|
|
299
|
+
const onRenderPostFx = config?.onRenderPostFx;
|
|
300
|
+
const spriteQuery = ctx.world.query(Position, Sprite);
|
|
301
|
+
const renderSystem = createRenderSystem(ctx.world, spriteQuery, Position, Sprite, ctx.batch);
|
|
302
|
+
let prevBuilt = EMPTY_PASSES;
|
|
303
|
+
const unsubscribeLate = ctx.loop.onLateUpdate.add((dt) => {
|
|
304
|
+
ctx.camera.update(dt);
|
|
305
|
+
ctx.input.update();
|
|
306
|
+
});
|
|
307
|
+
const unsubscribePre = ctx.loop.onPreRender.add(() => {
|
|
308
|
+
const w = ctx.camera.viewportWidth;
|
|
309
|
+
const h = ctx.camera.viewportHeight;
|
|
310
|
+
bridge.beginFrame(w, h, ctx.loop.clock.elapsed, pd);
|
|
311
|
+
ctx.renderer.begin(clearColor);
|
|
312
|
+
ctx.renderer.applyCamera(ctx.camera.effectiveX, ctx.camera.effectiveY, ctx.camera.zoom, ctx.camera.rotation, w, h);
|
|
313
|
+
if (onRenderBackground) onRenderBackground(ctx, ctx.renderer.canvas);
|
|
314
|
+
renderSystem();
|
|
315
|
+
ctx.batch.flush(bridge.drawAtlas, ctx.renderer);
|
|
316
|
+
if (onRenderPostFx) onRenderPostFx(ctx, ctx.renderer.canvas, bridge);
|
|
317
|
+
ctx.renderer.end();
|
|
318
|
+
const pic = bridge.endFrame();
|
|
319
|
+
if (pic !== null) if (isWeb) captureWebScene(pic, w * pd, h * pd);
|
|
320
|
+
else setPicture(pic);
|
|
321
|
+
if (!isWeb) {
|
|
322
|
+
const built = bridge.buildPostFxPasses(null, null, w, h);
|
|
323
|
+
if (!samePasses(prevBuilt, built)) {
|
|
324
|
+
prevBuilt = built.length === 0 ? EMPTY_PASSES : built;
|
|
325
|
+
setPasses(prevBuilt);
|
|
326
|
+
}
|
|
327
|
+
} else if (bridge.hasPostFxBinding() && !webPostFxWarnedRef.current) {
|
|
328
|
+
console.warn("postfx chain skipped: Skia.RuntimeShaderBuilder and ImageFilter.MakeRuntimeShaderWithChildren both throw \"Not implemented on React Native Web\".");
|
|
329
|
+
webPostFxWarnedRef.current = true;
|
|
330
|
+
}
|
|
331
|
+
canvasRef.current?.redraw();
|
|
332
|
+
});
|
|
333
|
+
onReady?.(ctx);
|
|
334
|
+
ctx.loop.start();
|
|
335
|
+
return () => {
|
|
336
|
+
unsubscribeLate();
|
|
337
|
+
unsubscribePre();
|
|
338
|
+
ctx.loop.stop();
|
|
339
|
+
};
|
|
340
|
+
}, []);
|
|
341
|
+
const handleLayout = useCallback((event) => {
|
|
342
|
+
const { width, height } = event.nativeEvent.layout;
|
|
343
|
+
ctx.camera.setViewport(width, height);
|
|
344
|
+
setViewportSize({
|
|
345
|
+
w: width,
|
|
346
|
+
h: height
|
|
347
|
+
});
|
|
348
|
+
}, [ctx]);
|
|
349
|
+
const onGrant = useCallback((e) => {
|
|
350
|
+
const { changedTouches, identifier, locationX, locationY } = e.nativeEvent;
|
|
351
|
+
if (changedTouches.length === 0) {
|
|
352
|
+
ctx.input.onPointerDown(Number(identifier), locationX, locationY);
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
for (let i = 0; i < changedTouches.length; i++) {
|
|
356
|
+
const t = changedTouches[i];
|
|
357
|
+
if (t === void 0) continue;
|
|
358
|
+
ctx.input.onPointerDown(Number(t.identifier), t.locationX, t.locationY);
|
|
359
|
+
}
|
|
360
|
+
}, [ctx]);
|
|
361
|
+
const onMove = useCallback((e) => {
|
|
362
|
+
const { changedTouches, identifier, locationX, locationY } = e.nativeEvent;
|
|
363
|
+
if (changedTouches.length === 0) {
|
|
364
|
+
ctx.input.onPointerMove(Number(identifier), locationX, locationY);
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
for (let i = 0; i < changedTouches.length; i++) {
|
|
368
|
+
const t = changedTouches[i];
|
|
369
|
+
if (t === void 0) continue;
|
|
370
|
+
ctx.input.onPointerMove(Number(t.identifier), t.locationX, t.locationY);
|
|
371
|
+
}
|
|
372
|
+
}, [ctx]);
|
|
373
|
+
const onRelease = useCallback((e) => {
|
|
374
|
+
const { changedTouches, identifier, locationX, locationY } = e.nativeEvent;
|
|
375
|
+
if (changedTouches.length === 0) {
|
|
376
|
+
ctx.input.onPointerUp(Number(identifier), locationX, locationY);
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
for (let i = 0; i < changedTouches.length; i++) {
|
|
380
|
+
const t = changedTouches[i];
|
|
381
|
+
if (t === void 0) continue;
|
|
382
|
+
ctx.input.onPointerUp(Number(t.identifier), t.locationX, t.locationY);
|
|
383
|
+
}
|
|
384
|
+
}, [ctx]);
|
|
385
|
+
const containerStyle = useMemo(() => [styles$3.container, style], [style]);
|
|
386
|
+
const passElements = useMemo(() => passes.map((p) => /* @__PURE__ */ jsx(RuntimeShader, {
|
|
387
|
+
source: p.source,
|
|
388
|
+
uniforms: p.uniforms
|
|
389
|
+
}, p.id)), [passes]);
|
|
390
|
+
return /* @__PURE__ */ jsx(SafeAreaContext.Provider, {
|
|
391
|
+
value: resolvedSafeArea,
|
|
392
|
+
children: /* @__PURE__ */ jsx(GameContext.Provider, {
|
|
393
|
+
value: ctx,
|
|
394
|
+
children: /* @__PURE__ */ jsxs(View, {
|
|
395
|
+
style: containerStyle,
|
|
396
|
+
onLayout: handleLayout,
|
|
397
|
+
onStartShouldSetResponder: returnTrue$1,
|
|
398
|
+
onResponderGrant: onGrant,
|
|
399
|
+
onResponderMove: onMove,
|
|
400
|
+
onResponderRelease: onRelease,
|
|
401
|
+
onResponderTerminate: onRelease,
|
|
402
|
+
children: [/* @__PURE__ */ jsx(Canvas, {
|
|
403
|
+
ref: canvasRef,
|
|
404
|
+
style: StyleSheet.absoluteFill,
|
|
405
|
+
pointerEvents: "none",
|
|
406
|
+
opaque: config?.opaque ?? false,
|
|
407
|
+
children: viewportSize.w === 0 || sceneTexture == null ? null : /* @__PURE__ */ jsx(Image, {
|
|
408
|
+
image: sceneTexture,
|
|
409
|
+
x: 0,
|
|
410
|
+
y: 0,
|
|
411
|
+
width: viewportSize.w,
|
|
412
|
+
height: viewportSize.h,
|
|
413
|
+
fit: "cover",
|
|
414
|
+
children: passElements
|
|
415
|
+
})
|
|
416
|
+
}), children]
|
|
417
|
+
})
|
|
418
|
+
})
|
|
419
|
+
});
|
|
205
420
|
}
|
|
206
|
-
function returnTrue() {
|
|
207
|
-
|
|
421
|
+
function returnTrue$1() {
|
|
422
|
+
return true;
|
|
208
423
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
424
|
+
/** Stable empty-chain reference — an empty `buildPostFxPasses` result collapses
|
|
425
|
+
* to this so the `passes` state never changes for a postfx-free GameView. */
|
|
426
|
+
const EMPTY_PASSES = [];
|
|
427
|
+
/**
|
|
428
|
+
* Shallow structural equality of two pass lists. The signature is length +
|
|
429
|
+
* per-pass `id` + shader ref + child-image refs + uniform values. Uniforms are
|
|
430
|
+
* compared (not just `id`/`source`) because animating effects — the hit-flash
|
|
431
|
+
* decay and timed triggers — keep a constant `id`/`source` while only their
|
|
432
|
+
* uniform values change frame to frame; excluding them would freeze those
|
|
433
|
+
* animations. Cost is trivial: postfx chains hold a handful of scalar/short-vec
|
|
434
|
+
* uniforms.
|
|
435
|
+
*/
|
|
436
|
+
function samePasses(a, b) {
|
|
437
|
+
if (a.length !== b.length) return false;
|
|
438
|
+
for (let i = 0; i < a.length; i++) {
|
|
439
|
+
const pa = a[i];
|
|
440
|
+
const pb = b[i];
|
|
441
|
+
if (pa === void 0 || pb === void 0) return false;
|
|
442
|
+
if (pa.id !== pb.id || pa.source !== pb.source) return false;
|
|
443
|
+
if (!sameChildren(pa.children, pb.children)) return false;
|
|
444
|
+
if (!sameUniforms(pa.uniforms, pb.uniforms)) return false;
|
|
445
|
+
}
|
|
446
|
+
return true;
|
|
221
447
|
}
|
|
222
|
-
function
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
const unsubAdd = query.onEntityAdded.add(refresh);
|
|
227
|
-
const unsubRemove = query.onEntityRemoved.add(refresh);
|
|
228
|
-
refresh();
|
|
229
|
-
return () => {
|
|
230
|
-
unsubAdd();
|
|
231
|
-
unsubRemove();
|
|
232
|
-
};
|
|
233
|
-
}, [query]);
|
|
234
|
-
return entities;
|
|
448
|
+
function sameChildren(a, b) {
|
|
449
|
+
if (a.length !== b.length) return false;
|
|
450
|
+
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
|
|
451
|
+
return true;
|
|
235
452
|
}
|
|
236
|
-
function
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
453
|
+
function sameUniforms(a, b) {
|
|
454
|
+
const aKeys = Object.keys(a);
|
|
455
|
+
if (aKeys.length !== Object.keys(b).length) return false;
|
|
456
|
+
for (const key of aKeys) {
|
|
457
|
+
const va = a[key];
|
|
458
|
+
const vb = b[key];
|
|
459
|
+
if (va === void 0 || vb === void 0) return false;
|
|
460
|
+
if (typeof va === "number") {
|
|
461
|
+
if (va !== vb) return false;
|
|
462
|
+
} else {
|
|
463
|
+
if (typeof vb === "number" || va.length !== vb.length) return false;
|
|
464
|
+
for (let i = 0; i < va.length; i++) if (va[i] !== vb[i]) return false;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
return true;
|
|
249
468
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
config,
|
|
262
|
-
onStateChange,
|
|
263
|
-
style,
|
|
264
|
-
base,
|
|
265
|
-
thumb
|
|
266
|
-
}) {
|
|
267
|
-
const stateRef = useRef2(createJoystickState());
|
|
268
|
-
const sizeRef = useRef2({ w: 0, h: 0 });
|
|
269
|
-
const [knob, setKnob] = useState3({ x: 0, y: 0 });
|
|
270
|
-
const handleLayout = useCallback2(
|
|
271
|
-
(e) => {
|
|
272
|
-
sizeRef.current = { w: e.nativeEvent.layout.width, h: e.nativeEvent.layout.height };
|
|
273
|
-
},
|
|
274
|
-
[]
|
|
275
|
-
);
|
|
276
|
-
const handleTouch = useCallback2(
|
|
277
|
-
(e) => {
|
|
278
|
-
const { locationX, locationY } = e.nativeEvent;
|
|
279
|
-
const { w, h } = sizeRef.current;
|
|
280
|
-
updateJoystick(stateRef.current, w / 2, h / 2, locationX, locationY, config);
|
|
281
|
-
onStateChange?.(stateRef.current);
|
|
282
|
-
const { x, y } = stateRef.current.offset;
|
|
283
|
-
setKnob((prev) => prev.x === x && prev.y === y ? prev : { x, y });
|
|
284
|
-
},
|
|
285
|
-
[config, onStateChange]
|
|
286
|
-
);
|
|
287
|
-
const handleRelease = useCallback2(() => {
|
|
288
|
-
releaseJoystick(stateRef.current);
|
|
289
|
-
onStateChange?.(stateRef.current);
|
|
290
|
-
setKnob((prev) => prev.x === 0 && prev.y === 0 ? prev : { x: 0, y: 0 });
|
|
291
|
-
}, [onStateChange]);
|
|
292
|
-
const maxRadius = config?.maxRadius ?? 50;
|
|
293
|
-
return /* @__PURE__ */ jsxs2(
|
|
294
|
-
View2,
|
|
295
|
-
{
|
|
296
|
-
style: [
|
|
297
|
-
styles2.center,
|
|
298
|
-
...base ? [] : [styles2.baseBg],
|
|
299
|
-
{
|
|
300
|
-
width: maxRadius * 2,
|
|
301
|
-
height: maxRadius * 2,
|
|
302
|
-
borderRadius: maxRadius
|
|
303
|
-
},
|
|
304
|
-
style
|
|
305
|
-
],
|
|
306
|
-
onLayout: handleLayout,
|
|
307
|
-
onStartShouldSetResponder: returnTrue2,
|
|
308
|
-
onMoveShouldSetResponder: returnTrue2,
|
|
309
|
-
onResponderGrant: handleTouch,
|
|
310
|
-
onResponderMove: handleTouch,
|
|
311
|
-
onResponderRelease: handleRelease,
|
|
312
|
-
onResponderTerminate: handleRelease,
|
|
313
|
-
children: [
|
|
314
|
-
base ? /* @__PURE__ */ jsx2(View2, { style: StyleSheet2.absoluteFill, pointerEvents: "none", children: base }) : null,
|
|
315
|
-
thumb ? /* @__PURE__ */ jsx2(
|
|
316
|
-
View2,
|
|
317
|
-
{
|
|
318
|
-
style: { transform: [{ translateX: knob.x }, { translateY: knob.y }] },
|
|
319
|
-
pointerEvents: "none",
|
|
320
|
-
children: thumb
|
|
321
|
-
}
|
|
322
|
-
) : /* @__PURE__ */ jsx2(
|
|
323
|
-
View2,
|
|
324
|
-
{
|
|
325
|
-
style: [styles2.knob, { transform: [{ translateX: knob.x }, { translateY: knob.y }] }]
|
|
326
|
-
}
|
|
327
|
-
)
|
|
328
|
-
]
|
|
329
|
-
}
|
|
330
|
-
);
|
|
331
|
-
}
|
|
332
|
-
function returnTrue2() {
|
|
333
|
-
return true;
|
|
469
|
+
const styles$3 = StyleSheet.create({ container: { flex: 1 } });
|
|
470
|
+
//#endregion
|
|
471
|
+
//#region src/hooks.ts
|
|
472
|
+
/**
|
|
473
|
+
* useGame — returns the GameContextValue from the nearest <GameView>.
|
|
474
|
+
* Throws if called outside a <GameView>.
|
|
475
|
+
*/
|
|
476
|
+
function useGame() {
|
|
477
|
+
const ctx = useContext(GameContext);
|
|
478
|
+
if (!ctx) throw new Error("useGame must be used inside a <GameView>");
|
|
479
|
+
return ctx;
|
|
334
480
|
}
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
pressButton,
|
|
355
|
-
releaseButton,
|
|
356
|
-
resetButtonFrame
|
|
357
|
-
} from "@flare-engine/input";
|
|
358
|
-
import { useCallback as useCallback3, useRef as useRef3 } from "react";
|
|
359
|
-
import { StyleSheet as StyleSheet3, View as View3 } from "react-native";
|
|
360
|
-
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
361
|
-
function VirtualButton({
|
|
362
|
-
onStateChange,
|
|
363
|
-
style,
|
|
364
|
-
children
|
|
365
|
-
}) {
|
|
366
|
-
const stateRef = useRef3(createButtonState());
|
|
367
|
-
const handlePress = useCallback3(() => {
|
|
368
|
-
resetButtonFrame(stateRef.current);
|
|
369
|
-
pressButton(stateRef.current);
|
|
370
|
-
onStateChange?.(stateRef.current);
|
|
371
|
-
}, [onStateChange]);
|
|
372
|
-
const handleRelease = useCallback3(() => {
|
|
373
|
-
resetButtonFrame(stateRef.current);
|
|
374
|
-
releaseButton(stateRef.current);
|
|
375
|
-
onStateChange?.(stateRef.current);
|
|
376
|
-
}, [onStateChange]);
|
|
377
|
-
return /* @__PURE__ */ jsx3(
|
|
378
|
-
View3,
|
|
379
|
-
{
|
|
380
|
-
style: [styles3.button, style],
|
|
381
|
-
onStartShouldSetResponder: returnTrue3,
|
|
382
|
-
onResponderGrant: handlePress,
|
|
383
|
-
onResponderRelease: handleRelease,
|
|
384
|
-
onResponderTerminate: handleRelease,
|
|
385
|
-
children
|
|
386
|
-
}
|
|
387
|
-
);
|
|
481
|
+
/**
|
|
482
|
+
* useEntity — subscribes to an ECS Query and returns the current entity list.
|
|
483
|
+
* Re-renders when entities are added to or removed from the query.
|
|
484
|
+
*
|
|
485
|
+
* @param query An active Query obtained from world.query(...)
|
|
486
|
+
*/
|
|
487
|
+
function useEntity(query) {
|
|
488
|
+
const [entities, setEntities] = useState(() => Array.from(query));
|
|
489
|
+
useEffect(() => {
|
|
490
|
+
const refresh = () => setEntities(Array.from(query));
|
|
491
|
+
const unsubAdd = query.onEntityAdded.add(refresh);
|
|
492
|
+
const unsubRemove = query.onEntityRemoved.add(refresh);
|
|
493
|
+
refresh();
|
|
494
|
+
return () => {
|
|
495
|
+
unsubAdd();
|
|
496
|
+
unsubRemove();
|
|
497
|
+
};
|
|
498
|
+
}, [query]);
|
|
499
|
+
return entities;
|
|
388
500
|
}
|
|
389
|
-
|
|
390
|
-
|
|
501
|
+
/**
|
|
502
|
+
* useGameLoop — registers a per-frame callback on the game loop's onUpdate signal.
|
|
503
|
+
* The callback is called each frame with delta time in seconds.
|
|
504
|
+
* Automatically unregisters on unmount.
|
|
505
|
+
*
|
|
506
|
+
* @param callback Function called each frame with dt in seconds.
|
|
507
|
+
*/
|
|
508
|
+
function useGameLoop(callback) {
|
|
509
|
+
const { loop } = useGame();
|
|
510
|
+
const callbackRef = useRef(callback);
|
|
511
|
+
callbackRef.current = callback;
|
|
512
|
+
const stable = useCallback((dt) => {
|
|
513
|
+
callbackRef.current(dt);
|
|
514
|
+
}, []);
|
|
515
|
+
useEffect(() => {
|
|
516
|
+
const unsub = loop.onUpdate.add(stable);
|
|
517
|
+
return () => {
|
|
518
|
+
unsub();
|
|
519
|
+
};
|
|
520
|
+
}, [loop, stable]);
|
|
391
521
|
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
522
|
+
//#endregion
|
|
523
|
+
//#region src/loading-screen.tsx
|
|
524
|
+
/**
|
|
525
|
+
* LoadingScreen — full-screen asset-load overlay.
|
|
526
|
+
*
|
|
527
|
+
* Renders before `<GameView>` mounts its Skia canvas (or alongside it
|
|
528
|
+
* while `AssetManager.load()` resolves). Pure RN Views so it works
|
|
529
|
+
* without a live Skia context. Consumers drive `progress` from the
|
|
530
|
+
* AssetManager progress callback.
|
|
531
|
+
*/
|
|
532
|
+
function LoadingScreen({ progress, style, barColor = "#ffffff", trackColor = "rgba(255,255,255,0.2)", backgroundColor = "#000000" }) {
|
|
533
|
+
const percent = `${Math.round((progress < 0 ? 0 : progress > 1 ? 1 : progress) * 100)}%`;
|
|
534
|
+
return /* @__PURE__ */ jsx(View, {
|
|
535
|
+
style: [
|
|
536
|
+
styles$2.container,
|
|
537
|
+
{ backgroundColor },
|
|
538
|
+
style
|
|
539
|
+
],
|
|
540
|
+
children: /* @__PURE__ */ jsx(View, {
|
|
541
|
+
style: [styles$2.track, { backgroundColor: trackColor }],
|
|
542
|
+
children: /* @__PURE__ */ jsx(View, { style: [styles$2.bar, {
|
|
543
|
+
width: percent,
|
|
544
|
+
backgroundColor: barColor
|
|
545
|
+
}] })
|
|
546
|
+
})
|
|
547
|
+
});
|
|
416
548
|
}
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
height: "100%"
|
|
436
|
-
}
|
|
549
|
+
const styles$2 = StyleSheet.create({
|
|
550
|
+
container: {
|
|
551
|
+
position: "absolute",
|
|
552
|
+
top: 0,
|
|
553
|
+
left: 0,
|
|
554
|
+
right: 0,
|
|
555
|
+
bottom: 0,
|
|
556
|
+
alignItems: "center",
|
|
557
|
+
justifyContent: "center",
|
|
558
|
+
padding: 32
|
|
559
|
+
},
|
|
560
|
+
track: {
|
|
561
|
+
width: "80%",
|
|
562
|
+
height: 6,
|
|
563
|
+
borderRadius: 3,
|
|
564
|
+
overflow: "hidden"
|
|
565
|
+
},
|
|
566
|
+
bar: { height: "100%" }
|
|
437
567
|
});
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
import { easeOutCubic } from "@flare-engine/math";
|
|
441
|
-
import { useEffect as useEffect3, useRef as useRef4, useState as useState4 } from "react";
|
|
568
|
+
//#endregion
|
|
569
|
+
//#region src/overlay-hooks.ts
|
|
442
570
|
function useOverlayFade(visible, durationMs = 300) {
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
}, [target, durationMs]);
|
|
470
|
-
return opacity;
|
|
571
|
+
const target = visible ? 1 : 0;
|
|
572
|
+
const [opacity, setOpacity] = useState(target);
|
|
573
|
+
const opacityRef = useRef(opacity);
|
|
574
|
+
const startRef = useRef(null);
|
|
575
|
+
const rafRef = useRef(null);
|
|
576
|
+
opacityRef.current = opacity;
|
|
577
|
+
useEffect(() => {
|
|
578
|
+
if (typeof requestAnimationFrame === "undefined") {
|
|
579
|
+
setOpacity(target);
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
const from = opacityRef.current;
|
|
583
|
+
startRef.current = null;
|
|
584
|
+
if (rafRef.current !== null) cancelAnimationFrame(rafRef.current);
|
|
585
|
+
const step = (now) => {
|
|
586
|
+
if (startRef.current === null) startRef.current = now;
|
|
587
|
+
const t = Math.min((now - startRef.current) / durationMs, 1);
|
|
588
|
+
setOpacity(from + (target - from) * t);
|
|
589
|
+
if (t < 1) rafRef.current = requestAnimationFrame(step);
|
|
590
|
+
};
|
|
591
|
+
rafRef.current = requestAnimationFrame(step);
|
|
592
|
+
return () => {
|
|
593
|
+
if (rafRef.current !== null) cancelAnimationFrame(rafRef.current);
|
|
594
|
+
};
|
|
595
|
+
}, [target, durationMs]);
|
|
596
|
+
return opacity;
|
|
471
597
|
}
|
|
472
598
|
function useCountUp(target, durationMs = 600) {
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
}, [target, durationMs]);
|
|
499
|
-
return value;
|
|
599
|
+
const [value, setValue] = useState(target);
|
|
600
|
+
const valueRef = useRef(value);
|
|
601
|
+
const startRef = useRef(null);
|
|
602
|
+
const rafRef = useRef(null);
|
|
603
|
+
valueRef.current = value;
|
|
604
|
+
useEffect(() => {
|
|
605
|
+
if (typeof requestAnimationFrame === "undefined") {
|
|
606
|
+
setValue(target);
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
609
|
+
const from = valueRef.current;
|
|
610
|
+
startRef.current = null;
|
|
611
|
+
if (rafRef.current !== null) cancelAnimationFrame(rafRef.current);
|
|
612
|
+
const step = (now) => {
|
|
613
|
+
if (startRef.current === null) startRef.current = now;
|
|
614
|
+
const t = Math.min((now - startRef.current) / durationMs, 1);
|
|
615
|
+
setValue(from + (target - from) * easeOutCubic(t));
|
|
616
|
+
if (t < 1) rafRef.current = requestAnimationFrame(step);
|
|
617
|
+
};
|
|
618
|
+
rafRef.current = requestAnimationFrame(step);
|
|
619
|
+
return () => {
|
|
620
|
+
if (rafRef.current !== null) cancelAnimationFrame(rafRef.current);
|
|
621
|
+
};
|
|
622
|
+
}, [target, durationMs]);
|
|
623
|
+
return value;
|
|
500
624
|
}
|
|
501
625
|
function useSignalState(signal, initial) {
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
626
|
+
const [state, setState] = useState(initial);
|
|
627
|
+
useEffect(() => {
|
|
628
|
+
return signal.add(setState);
|
|
629
|
+
}, [signal]);
|
|
630
|
+
return state;
|
|
631
|
+
}
|
|
632
|
+
//#endregion
|
|
633
|
+
//#region src/skia-atlas.ts
|
|
634
|
+
/**
|
|
635
|
+
* The RN-Skia module adapted to the engine's structural atlas interface.
|
|
636
|
+
* Centralises the one boundary assertion so template code stays cast-free.
|
|
637
|
+
*/
|
|
638
|
+
const skiaAtlasModule = Skia;
|
|
639
|
+
//#endregion
|
|
640
|
+
//#region src/virtual-button.tsx
|
|
641
|
+
/**
|
|
642
|
+
* VirtualButton — an on-screen button overlay component.
|
|
643
|
+
*
|
|
644
|
+
* Uses raw `onTouchStart`/`onTouchEnd` so press and release fire regardless
|
|
645
|
+
* of which ancestor holds the RN responder — enabling simultaneous use with
|
|
646
|
+
* `<VirtualJoystick>` without responder conflicts.
|
|
647
|
+
*/
|
|
648
|
+
function VirtualButton({ onStateChange, style, children }) {
|
|
649
|
+
const stateRef = useRef(createButtonState());
|
|
650
|
+
const handlePress = useCallback(() => {
|
|
651
|
+
resetButtonFrame(stateRef.current);
|
|
652
|
+
pressButton(stateRef.current);
|
|
653
|
+
onStateChange?.(stateRef.current);
|
|
654
|
+
}, [onStateChange]);
|
|
655
|
+
const handleRelease = useCallback(() => {
|
|
656
|
+
resetButtonFrame(stateRef.current);
|
|
657
|
+
releaseButton(stateRef.current);
|
|
658
|
+
onStateChange?.(stateRef.current);
|
|
659
|
+
}, [onStateChange]);
|
|
660
|
+
return /* @__PURE__ */ jsx(View, {
|
|
661
|
+
style: [styles$1.button, style],
|
|
662
|
+
onTouchStart: handlePress,
|
|
663
|
+
onTouchEnd: handleRelease,
|
|
664
|
+
onTouchCancel: handleRelease,
|
|
665
|
+
children
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
const styles$1 = StyleSheet.create({ button: {
|
|
669
|
+
width: 60,
|
|
670
|
+
height: 60,
|
|
671
|
+
borderRadius: 30,
|
|
672
|
+
backgroundColor: "rgba(255,255,255,0.3)",
|
|
673
|
+
alignItems: "center",
|
|
674
|
+
justifyContent: "center"
|
|
675
|
+
} });
|
|
676
|
+
//#endregion
|
|
677
|
+
//#region src/virtual-joystick.tsx
|
|
678
|
+
/**
|
|
679
|
+
* VirtualJoystick — an on-screen joystick overlay component.
|
|
680
|
+
*
|
|
681
|
+
* Renders a base circle and a knob that follows the user's touch.
|
|
682
|
+
* Claims the React Native touch responder so children of a parent that
|
|
683
|
+
* also claims the responder (e.g. <GameView>) still receive move/end events.
|
|
684
|
+
*/
|
|
685
|
+
function VirtualJoystick({ config, onStateChange, style, base, thumb }) {
|
|
686
|
+
const stateRef = useRef(createJoystickState());
|
|
687
|
+
const sizeRef = useRef({
|
|
688
|
+
w: 0,
|
|
689
|
+
h: 0
|
|
690
|
+
});
|
|
691
|
+
const [knob, setKnob] = useState({
|
|
692
|
+
x: 0,
|
|
693
|
+
y: 0
|
|
694
|
+
});
|
|
695
|
+
const handleLayout = useCallback((e) => {
|
|
696
|
+
sizeRef.current = {
|
|
697
|
+
w: e.nativeEvent.layout.width,
|
|
698
|
+
h: e.nativeEvent.layout.height
|
|
699
|
+
};
|
|
700
|
+
}, []);
|
|
701
|
+
const handleTouch = useCallback((e) => {
|
|
702
|
+
const { locationX, locationY } = e.nativeEvent;
|
|
703
|
+
const { w, h } = sizeRef.current;
|
|
704
|
+
updateJoystick(stateRef.current, w / 2, h / 2, locationX, locationY, config);
|
|
705
|
+
onStateChange?.(stateRef.current);
|
|
706
|
+
const { x, y } = stateRef.current.offset;
|
|
707
|
+
setKnob((prev) => prev.x === x && prev.y === y ? prev : {
|
|
708
|
+
x,
|
|
709
|
+
y
|
|
710
|
+
});
|
|
711
|
+
}, [config, onStateChange]);
|
|
712
|
+
const handleRelease = useCallback(() => {
|
|
713
|
+
releaseJoystick(stateRef.current);
|
|
714
|
+
onStateChange?.(stateRef.current);
|
|
715
|
+
setKnob((prev) => prev.x === 0 && prev.y === 0 ? prev : {
|
|
716
|
+
x: 0,
|
|
717
|
+
y: 0
|
|
718
|
+
});
|
|
719
|
+
}, [onStateChange]);
|
|
720
|
+
const maxRadius = config?.maxRadius ?? 50;
|
|
721
|
+
return /* @__PURE__ */ jsxs(View, {
|
|
722
|
+
style: [
|
|
723
|
+
styles.center,
|
|
724
|
+
...base ? [] : [styles.baseBg],
|
|
725
|
+
{
|
|
726
|
+
width: maxRadius * 2,
|
|
727
|
+
height: maxRadius * 2,
|
|
728
|
+
borderRadius: maxRadius
|
|
729
|
+
},
|
|
730
|
+
style
|
|
731
|
+
],
|
|
732
|
+
onLayout: handleLayout,
|
|
733
|
+
onStartShouldSetResponder: returnTrue,
|
|
734
|
+
onMoveShouldSetResponder: returnTrue,
|
|
735
|
+
onResponderGrant: handleTouch,
|
|
736
|
+
onResponderMove: handleTouch,
|
|
737
|
+
onResponderRelease: handleRelease,
|
|
738
|
+
onResponderTerminate: handleRelease,
|
|
739
|
+
onResponderTerminationRequest: returnFalse,
|
|
740
|
+
children: [base ? /* @__PURE__ */ jsx(View, {
|
|
741
|
+
style: StyleSheet.absoluteFill,
|
|
742
|
+
pointerEvents: "none",
|
|
743
|
+
children: base
|
|
744
|
+
}) : null, thumb ? /* @__PURE__ */ jsx(View, {
|
|
745
|
+
style: { transform: [{ translateX: knob.x }, { translateY: knob.y }] },
|
|
746
|
+
pointerEvents: "none",
|
|
747
|
+
children: thumb
|
|
748
|
+
}) : /* @__PURE__ */ jsx(View, { style: [styles.knob, { transform: [{ translateX: knob.x }, { translateY: knob.y }] }] })]
|
|
749
|
+
});
|
|
750
|
+
}
|
|
751
|
+
function returnTrue() {
|
|
752
|
+
return true;
|
|
753
|
+
}
|
|
754
|
+
function returnFalse() {
|
|
755
|
+
return false;
|
|
508
756
|
}
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
};
|
|
757
|
+
const styles = StyleSheet.create({
|
|
758
|
+
center: {
|
|
759
|
+
alignItems: "center",
|
|
760
|
+
justifyContent: "center"
|
|
761
|
+
},
|
|
762
|
+
baseBg: { backgroundColor: "rgba(255,255,255,0.2)" },
|
|
763
|
+
knob: {
|
|
764
|
+
width: 40,
|
|
765
|
+
height: 40,
|
|
766
|
+
borderRadius: 20,
|
|
767
|
+
backgroundColor: "rgba(255,255,255,0.6)"
|
|
768
|
+
}
|
|
769
|
+
});
|
|
770
|
+
//#endregion
|
|
771
|
+
export { GameContext, GameView, LoadingScreen, SafeAreaContext, VirtualButton, VirtualJoystick, createSkiaFrameRenderer, renderShapesToImage, skiaAtlasModule, useCountUp, useEntity, useGame, useGameLoop, useOverlayFade, useSafeArea, useSignalState };
|
|
772
|
+
|
|
526
773
|
//# sourceMappingURL=index.mjs.map
|