@flare-engine/react 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 +36 -0
- package/dist/index.d.mts +379 -0
- package/dist/index.d.ts +379 -0
- package/dist/index.js +1035 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1024 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +58 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,1024 @@
|
|
|
1
|
+
// src/game-context.ts
|
|
2
|
+
import { createContext } from "react";
|
|
3
|
+
var GameContext = createContext(null);
|
|
4
|
+
|
|
5
|
+
// src/game-view.tsx
|
|
6
|
+
import { AssetManager } from "@flare-engine/assets";
|
|
7
|
+
import { AudioManager } from "@flare-engine/audio";
|
|
8
|
+
import { GameLoop } from "@flare-engine/core";
|
|
9
|
+
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";
|
|
12
|
+
import { SceneManager } from "@flare-engine/scene";
|
|
13
|
+
import {
|
|
14
|
+
Canvas,
|
|
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";
|
|
23
|
+
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
|
24
|
+
|
|
25
|
+
// src/safe-area.ts
|
|
26
|
+
import { createContext as createContext2, useContext } from "react";
|
|
27
|
+
var SafeAreaContext = createContext2(null);
|
|
28
|
+
function useSafeArea() {
|
|
29
|
+
const value = useContext(SafeAreaContext);
|
|
30
|
+
if (value === null) {
|
|
31
|
+
throw new Error("useSafeArea must be used inside a <GameView>");
|
|
32
|
+
}
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// src/skia-bridge.ts
|
|
37
|
+
import {
|
|
38
|
+
CAVE_INTERIOR_SKSL,
|
|
39
|
+
FIRE_SKSL,
|
|
40
|
+
FOG_SKSL,
|
|
41
|
+
FOREST_CANOPY_SKSL,
|
|
42
|
+
MOON_CRATER_SKSL,
|
|
43
|
+
MOUNTAIN_HILLS_SKSL,
|
|
44
|
+
RIVER_WAVE_SKSL,
|
|
45
|
+
ROOFS_CITY_SKSL,
|
|
46
|
+
WATER_SKSL,
|
|
47
|
+
flashIntensity
|
|
48
|
+
} from "@flare-engine/render";
|
|
49
|
+
function detachSnapshot(surface) {
|
|
50
|
+
const snap = surface.makeImageSnapshot();
|
|
51
|
+
if (typeof snap.makeNonTextureImage === "function") {
|
|
52
|
+
return snap.makeNonTextureImage();
|
|
53
|
+
}
|
|
54
|
+
return snap;
|
|
55
|
+
}
|
|
56
|
+
var BLEND_SRC_OVER = 3;
|
|
57
|
+
var BLEND_SRC_IN = 5;
|
|
58
|
+
var OUTLINE_OFFSETS = [
|
|
59
|
+
[1, 0],
|
|
60
|
+
[-1, 0],
|
|
61
|
+
[0, 1],
|
|
62
|
+
[0, -1],
|
|
63
|
+
[Math.SQRT1_2, Math.SQRT1_2],
|
|
64
|
+
[Math.SQRT1_2, -Math.SQRT1_2],
|
|
65
|
+
[-Math.SQRT1_2, Math.SQRT1_2],
|
|
66
|
+
[-Math.SQRT1_2, -Math.SQRT1_2]
|
|
67
|
+
];
|
|
68
|
+
function createSkiaFrameRenderer(Skia2) {
|
|
69
|
+
const defaultPaint = Skia2.Paint();
|
|
70
|
+
let tintPaint = null;
|
|
71
|
+
let flashPaint = null;
|
|
72
|
+
let grayscalePaint = null;
|
|
73
|
+
let outlinePaint = null;
|
|
74
|
+
let customPaint = null;
|
|
75
|
+
let waterPaint = null;
|
|
76
|
+
let waterRuntimeEffect = null;
|
|
77
|
+
let fogPaint = null;
|
|
78
|
+
let fogRuntimeEffect = null;
|
|
79
|
+
let firePaint = null;
|
|
80
|
+
let fireRuntimeEffect = null;
|
|
81
|
+
let roofsCityPaint = null;
|
|
82
|
+
let roofsCityEffect = null;
|
|
83
|
+
let forestCanopyPaint = null;
|
|
84
|
+
let forestCanopyEffect = null;
|
|
85
|
+
let riverWavePaint = null;
|
|
86
|
+
let riverWaveEffect = null;
|
|
87
|
+
let moonCraterPaint = null;
|
|
88
|
+
let moonCraterEffect = null;
|
|
89
|
+
let caveInteriorPaint = null;
|
|
90
|
+
let caveInteriorEffect = null;
|
|
91
|
+
let mountainHillsPaint = null;
|
|
92
|
+
let mountainHillsEffect = null;
|
|
93
|
+
const roofsCityUniforms = new Array(10).fill(0);
|
|
94
|
+
const forestCanopyUniforms = new Array(10).fill(0);
|
|
95
|
+
const riverWaveUniforms = new Array(10).fill(0);
|
|
96
|
+
const moonCraterUniforms = new Array(10).fill(0);
|
|
97
|
+
const caveInteriorUniforms = new Array(10).fill(0);
|
|
98
|
+
const mountainHillsUniforms = new Array(10).fill(0);
|
|
99
|
+
const recorder = Skia2.PictureRecorder();
|
|
100
|
+
const recordingBounds = Skia2.XYWHRect(0, 0, 0, 0);
|
|
101
|
+
const srcRects = [];
|
|
102
|
+
const xforms = [];
|
|
103
|
+
const stretchSrcRects = [];
|
|
104
|
+
const stretchDstRects = [];
|
|
105
|
+
let stretchPaint = null;
|
|
106
|
+
const clearColor = new Float32Array(4);
|
|
107
|
+
const tintColor = new Float32Array(4);
|
|
108
|
+
const flashColor = new Float32Array(4);
|
|
109
|
+
const outlineColor = new Float32Array(4);
|
|
110
|
+
const grayMatrix = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0];
|
|
111
|
+
const customUniforms = [];
|
|
112
|
+
const waterUniforms = new Array(11).fill(0);
|
|
113
|
+
const fogUniforms = new Array(9).fill(0);
|
|
114
|
+
const fireUniforms = new Array(11).fill(0);
|
|
115
|
+
let currentFrameTime = 0;
|
|
116
|
+
let current = null;
|
|
117
|
+
let _boundRuntime = null;
|
|
118
|
+
let _boundChain = null;
|
|
119
|
+
const _loggedOnce = /* @__PURE__ */ new Set();
|
|
120
|
+
function logOnce(msg) {
|
|
121
|
+
if (_loggedOnce.has(msg)) return;
|
|
122
|
+
_loggedOnce.add(msg);
|
|
123
|
+
console.warn(`[postfx] ${msg}`);
|
|
124
|
+
}
|
|
125
|
+
const canvas = {
|
|
126
|
+
save: () => current?.save() ?? 0,
|
|
127
|
+
restore: () => {
|
|
128
|
+
current?.restore();
|
|
129
|
+
},
|
|
130
|
+
translate: (x, y) => {
|
|
131
|
+
current?.translate(x, y);
|
|
132
|
+
},
|
|
133
|
+
scale: (sx, sy) => {
|
|
134
|
+
current?.scale(sx, sy);
|
|
135
|
+
},
|
|
136
|
+
rotate: (radians, cx, cy) => {
|
|
137
|
+
current?.rotate(radians, cx, cy);
|
|
138
|
+
},
|
|
139
|
+
clear: (argb) => {
|
|
140
|
+
if (current === null) return;
|
|
141
|
+
clearColor[0] = (argb >>> 16 & 255) / 255;
|
|
142
|
+
clearColor[1] = (argb >>> 8 & 255) / 255;
|
|
143
|
+
clearColor[2] = (argb & 255) / 255;
|
|
144
|
+
clearColor[3] = (argb >>> 24 & 255) / 255;
|
|
145
|
+
current.clear(clearColor);
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
function ensurePoolCapacity(n) {
|
|
149
|
+
while (srcRects.length < n) {
|
|
150
|
+
srcRects.push(Skia2.XYWHRect(0, 0, 0, 0));
|
|
151
|
+
xforms.push(Skia2.RSXform(1, 0, 0, 0));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function ensureStretchPoolCapacity(n) {
|
|
155
|
+
while (stretchSrcRects.length < n) {
|
|
156
|
+
stretchSrcRects.push(Skia2.XYWHRect(0, 0, 0, 0));
|
|
157
|
+
stretchDstRects.push(Skia2.XYWHRect(0, 0, 0, 0));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function fillSrcRects(commands, n) {
|
|
161
|
+
for (let i = 0; i < n; i++) {
|
|
162
|
+
const cmd = commands[i];
|
|
163
|
+
srcRects[i]?.setXYWH(cmd.srcX, cmd.srcY, cmd.srcW, cmd.srcH);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
function fillXforms(commands, n, dx, dy) {
|
|
167
|
+
for (let i = 0; i < n; i++) {
|
|
168
|
+
const cmd = commands[i];
|
|
169
|
+
const scale = cmd.srcW === 0 ? 1 : cmd.dstW / cmd.srcW;
|
|
170
|
+
if (cmd.rotation === 0) {
|
|
171
|
+
xforms[i]?.set(scale, 0, cmd.dstX + dx, cmd.dstY + dy);
|
|
172
|
+
} else {
|
|
173
|
+
const c = Math.cos(cmd.rotation);
|
|
174
|
+
const s = Math.sin(cmd.rotation);
|
|
175
|
+
const scos = scale * c;
|
|
176
|
+
const ssin = scale * s;
|
|
177
|
+
const px = cmd.dstX + cmd.anchorX * cmd.dstW + dx;
|
|
178
|
+
const py = cmd.dstY + cmd.anchorY * cmd.dstH + dy;
|
|
179
|
+
const axSrc = cmd.anchorX * cmd.srcW;
|
|
180
|
+
const aySrc = cmd.anchorY * cmd.srcH;
|
|
181
|
+
xforms[i]?.set(
|
|
182
|
+
scos,
|
|
183
|
+
ssin,
|
|
184
|
+
px - scos * axSrc + ssin * aySrc,
|
|
185
|
+
py - ssin * axSrc - scos * aySrc
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function applyTint(effect) {
|
|
191
|
+
if (tintPaint === null) tintPaint = Skia2.Paint();
|
|
192
|
+
writeColor(tintColor, effect.color, effect.amount);
|
|
193
|
+
tintPaint.setColorFilter(Skia2.ColorFilter.MakeBlend(tintColor, BLEND_SRC_OVER));
|
|
194
|
+
return tintPaint;
|
|
195
|
+
}
|
|
196
|
+
function applyFlash(effect) {
|
|
197
|
+
if (flashPaint === null) flashPaint = Skia2.Paint();
|
|
198
|
+
writeColor(flashColor, effect.color, flashIntensity(effect));
|
|
199
|
+
flashPaint.setColorFilter(Skia2.ColorFilter.MakeBlend(flashColor, BLEND_SRC_OVER));
|
|
200
|
+
return flashPaint;
|
|
201
|
+
}
|
|
202
|
+
function applyGrayscale(effect) {
|
|
203
|
+
if (grayscalePaint === null) grayscalePaint = Skia2.Paint();
|
|
204
|
+
writeGrayscaleMatrix(grayMatrix, effect.amount);
|
|
205
|
+
grayscalePaint.setColorFilter(Skia2.ColorFilter.MakeMatrix(grayMatrix));
|
|
206
|
+
return grayscalePaint;
|
|
207
|
+
}
|
|
208
|
+
function applyOutlinePaint(effect) {
|
|
209
|
+
if (outlinePaint === null) outlinePaint = Skia2.Paint();
|
|
210
|
+
writeColor(outlineColor, effect.color, 1);
|
|
211
|
+
outlinePaint.setColorFilter(Skia2.ColorFilter.MakeBlend(outlineColor, BLEND_SRC_IN));
|
|
212
|
+
return outlinePaint;
|
|
213
|
+
}
|
|
214
|
+
function applyCustom(effect) {
|
|
215
|
+
if (customPaint === null) customPaint = Skia2.Paint();
|
|
216
|
+
const shader = effect.shader;
|
|
217
|
+
const floatCount = shader.getUniformFloatCount();
|
|
218
|
+
while (customUniforms.length < floatCount) customUniforms.push(0);
|
|
219
|
+
customUniforms.length = floatCount;
|
|
220
|
+
fillCustomUniforms(effect, shader, customUniforms);
|
|
221
|
+
customPaint.setShader(shader.makeShader(customUniforms));
|
|
222
|
+
return customPaint;
|
|
223
|
+
}
|
|
224
|
+
function applyWater(effect, time) {
|
|
225
|
+
if (Skia2.RuntimeEffect === void 0) return defaultPaint;
|
|
226
|
+
if (waterRuntimeEffect === null) {
|
|
227
|
+
const compiled = Skia2.RuntimeEffect.Make(WATER_SKSL);
|
|
228
|
+
if (compiled === null) return defaultPaint;
|
|
229
|
+
waterRuntimeEffect = compiled;
|
|
230
|
+
}
|
|
231
|
+
if (waterPaint === null) waterPaint = Skia2.Paint();
|
|
232
|
+
waterUniforms[0] = 0;
|
|
233
|
+
waterUniforms[1] = 0;
|
|
234
|
+
waterUniforms[2] = effect.color.r;
|
|
235
|
+
waterUniforms[3] = effect.color.g;
|
|
236
|
+
waterUniforms[4] = effect.color.b;
|
|
237
|
+
waterUniforms[5] = effect.foamColor.r;
|
|
238
|
+
waterUniforms[6] = effect.foamColor.g;
|
|
239
|
+
waterUniforms[7] = effect.foamColor.b;
|
|
240
|
+
waterUniforms[8] = effect.waveSpeed;
|
|
241
|
+
waterUniforms[9] = effect.waveAmplitude;
|
|
242
|
+
waterUniforms[10] = time;
|
|
243
|
+
waterPaint.setShader(waterRuntimeEffect.makeShader(waterUniforms));
|
|
244
|
+
return waterPaint;
|
|
245
|
+
}
|
|
246
|
+
function applyFog(effect, time) {
|
|
247
|
+
if (Skia2.RuntimeEffect === void 0) return defaultPaint;
|
|
248
|
+
if (fogRuntimeEffect === null) {
|
|
249
|
+
const compiled = Skia2.RuntimeEffect.Make(FOG_SKSL);
|
|
250
|
+
if (compiled === null) return defaultPaint;
|
|
251
|
+
fogRuntimeEffect = compiled;
|
|
252
|
+
}
|
|
253
|
+
if (fogPaint === null) fogPaint = Skia2.Paint();
|
|
254
|
+
fogUniforms[0] = 0;
|
|
255
|
+
fogUniforms[1] = 0;
|
|
256
|
+
fogUniforms[2] = effect.color.r;
|
|
257
|
+
fogUniforms[3] = effect.color.g;
|
|
258
|
+
fogUniforms[4] = effect.color.b;
|
|
259
|
+
fogUniforms[5] = effect.density;
|
|
260
|
+
fogUniforms[6] = effect.speed;
|
|
261
|
+
fogUniforms[7] = effect.scale;
|
|
262
|
+
fogUniforms[8] = time;
|
|
263
|
+
fogPaint.setShader(fogRuntimeEffect.makeShader(fogUniforms));
|
|
264
|
+
return fogPaint;
|
|
265
|
+
}
|
|
266
|
+
function applyFire(effect, time) {
|
|
267
|
+
if (Skia2.RuntimeEffect === void 0) return defaultPaint;
|
|
268
|
+
if (fireRuntimeEffect === null) {
|
|
269
|
+
const compiled = Skia2.RuntimeEffect.Make(FIRE_SKSL);
|
|
270
|
+
if (compiled === null) return defaultPaint;
|
|
271
|
+
fireRuntimeEffect = compiled;
|
|
272
|
+
}
|
|
273
|
+
if (firePaint === null) firePaint = Skia2.Paint();
|
|
274
|
+
fireUniforms[0] = 0;
|
|
275
|
+
fireUniforms[1] = 0;
|
|
276
|
+
fireUniforms[2] = effect.color.r;
|
|
277
|
+
fireUniforms[3] = effect.color.g;
|
|
278
|
+
fireUniforms[4] = effect.color.b;
|
|
279
|
+
fireUniforms[5] = effect.tipColor.r;
|
|
280
|
+
fireUniforms[6] = effect.tipColor.g;
|
|
281
|
+
fireUniforms[7] = effect.tipColor.b;
|
|
282
|
+
fireUniforms[8] = effect.speed;
|
|
283
|
+
fireUniforms[9] = effect.intensity;
|
|
284
|
+
fireUniforms[10] = time;
|
|
285
|
+
firePaint.setShader(fireRuntimeEffect.makeShader(fireUniforms));
|
|
286
|
+
return firePaint;
|
|
287
|
+
}
|
|
288
|
+
function applyRoofsCity(effect) {
|
|
289
|
+
if (Skia2.RuntimeEffect === void 0) return defaultPaint;
|
|
290
|
+
if (roofsCityEffect === null) {
|
|
291
|
+
const compiled = Skia2.RuntimeEffect.Make(ROOFS_CITY_SKSL);
|
|
292
|
+
if (compiled === null) return defaultPaint;
|
|
293
|
+
roofsCityEffect = compiled;
|
|
294
|
+
}
|
|
295
|
+
if (roofsCityPaint === null) roofsCityPaint = Skia2.Paint();
|
|
296
|
+
roofsCityUniforms[0] = effect.resolutionW;
|
|
297
|
+
roofsCityUniforms[1] = effect.resolutionH;
|
|
298
|
+
roofsCityUniforms[2] = effect.skyColor.r;
|
|
299
|
+
roofsCityUniforms[3] = effect.skyColor.g;
|
|
300
|
+
roofsCityUniforms[4] = effect.skyColor.b;
|
|
301
|
+
roofsCityUniforms[5] = effect.roofColor.r;
|
|
302
|
+
roofsCityUniforms[6] = effect.roofColor.g;
|
|
303
|
+
roofsCityUniforms[7] = effect.roofColor.b;
|
|
304
|
+
roofsCityUniforms[8] = effect.scrollOffsetY;
|
|
305
|
+
roofsCityUniforms[9] = effect.density;
|
|
306
|
+
roofsCityPaint.setShader(roofsCityEffect.makeShader(roofsCityUniforms));
|
|
307
|
+
return roofsCityPaint;
|
|
308
|
+
}
|
|
309
|
+
function applyForestCanopy(effect) {
|
|
310
|
+
if (Skia2.RuntimeEffect === void 0) return defaultPaint;
|
|
311
|
+
if (forestCanopyEffect === null) {
|
|
312
|
+
const compiled = Skia2.RuntimeEffect.Make(FOREST_CANOPY_SKSL);
|
|
313
|
+
if (compiled === null) return defaultPaint;
|
|
314
|
+
forestCanopyEffect = compiled;
|
|
315
|
+
}
|
|
316
|
+
if (forestCanopyPaint === null) forestCanopyPaint = Skia2.Paint();
|
|
317
|
+
forestCanopyUniforms[0] = effect.resolutionW;
|
|
318
|
+
forestCanopyUniforms[1] = effect.resolutionH;
|
|
319
|
+
forestCanopyUniforms[2] = effect.skyColor.r;
|
|
320
|
+
forestCanopyUniforms[3] = effect.skyColor.g;
|
|
321
|
+
forestCanopyUniforms[4] = effect.skyColor.b;
|
|
322
|
+
forestCanopyUniforms[5] = effect.leafColor.r;
|
|
323
|
+
forestCanopyUniforms[6] = effect.leafColor.g;
|
|
324
|
+
forestCanopyUniforms[7] = effect.leafColor.b;
|
|
325
|
+
forestCanopyUniforms[8] = effect.scrollOffsetY;
|
|
326
|
+
forestCanopyUniforms[9] = effect.density;
|
|
327
|
+
forestCanopyPaint.setShader(forestCanopyEffect.makeShader(forestCanopyUniforms));
|
|
328
|
+
return forestCanopyPaint;
|
|
329
|
+
}
|
|
330
|
+
function applyRiverWave(effect) {
|
|
331
|
+
if (Skia2.RuntimeEffect === void 0) return defaultPaint;
|
|
332
|
+
if (riverWaveEffect === null) {
|
|
333
|
+
const compiled = Skia2.RuntimeEffect.Make(RIVER_WAVE_SKSL);
|
|
334
|
+
if (compiled === null) return defaultPaint;
|
|
335
|
+
riverWaveEffect = compiled;
|
|
336
|
+
}
|
|
337
|
+
if (riverWavePaint === null) riverWavePaint = Skia2.Paint();
|
|
338
|
+
riverWaveUniforms[0] = effect.resolutionW;
|
|
339
|
+
riverWaveUniforms[1] = effect.resolutionH;
|
|
340
|
+
riverWaveUniforms[2] = effect.waterColor.r;
|
|
341
|
+
riverWaveUniforms[3] = effect.waterColor.g;
|
|
342
|
+
riverWaveUniforms[4] = effect.waterColor.b;
|
|
343
|
+
riverWaveUniforms[5] = effect.foamColor.r;
|
|
344
|
+
riverWaveUniforms[6] = effect.foamColor.g;
|
|
345
|
+
riverWaveUniforms[7] = effect.foamColor.b;
|
|
346
|
+
riverWaveUniforms[8] = effect.scrollOffsetY;
|
|
347
|
+
riverWaveUniforms[9] = effect.waveScale;
|
|
348
|
+
riverWavePaint.setShader(riverWaveEffect.makeShader(riverWaveUniforms));
|
|
349
|
+
return riverWavePaint;
|
|
350
|
+
}
|
|
351
|
+
function applyMoonCrater(effect) {
|
|
352
|
+
if (Skia2.RuntimeEffect === void 0) return defaultPaint;
|
|
353
|
+
if (moonCraterEffect === null) {
|
|
354
|
+
const compiled = Skia2.RuntimeEffect.Make(MOON_CRATER_SKSL);
|
|
355
|
+
if (compiled === null) return defaultPaint;
|
|
356
|
+
moonCraterEffect = compiled;
|
|
357
|
+
}
|
|
358
|
+
if (moonCraterPaint === null) moonCraterPaint = Skia2.Paint();
|
|
359
|
+
moonCraterUniforms[0] = effect.resolutionW;
|
|
360
|
+
moonCraterUniforms[1] = effect.resolutionH;
|
|
361
|
+
moonCraterUniforms[2] = effect.moonColor.r;
|
|
362
|
+
moonCraterUniforms[3] = effect.moonColor.g;
|
|
363
|
+
moonCraterUniforms[4] = effect.moonColor.b;
|
|
364
|
+
moonCraterUniforms[5] = effect.craterColor.r;
|
|
365
|
+
moonCraterUniforms[6] = effect.craterColor.g;
|
|
366
|
+
moonCraterUniforms[7] = effect.craterColor.b;
|
|
367
|
+
moonCraterUniforms[8] = effect.scrollOffsetY;
|
|
368
|
+
moonCraterUniforms[9] = effect.craterCount;
|
|
369
|
+
moonCraterPaint.setShader(moonCraterEffect.makeShader(moonCraterUniforms));
|
|
370
|
+
return moonCraterPaint;
|
|
371
|
+
}
|
|
372
|
+
function applyCaveInterior(effect) {
|
|
373
|
+
if (Skia2.RuntimeEffect === void 0) return defaultPaint;
|
|
374
|
+
if (caveInteriorEffect === null) {
|
|
375
|
+
const compiled = Skia2.RuntimeEffect.Make(CAVE_INTERIOR_SKSL);
|
|
376
|
+
if (compiled === null) return defaultPaint;
|
|
377
|
+
caveInteriorEffect = compiled;
|
|
378
|
+
}
|
|
379
|
+
if (caveInteriorPaint === null) caveInteriorPaint = Skia2.Paint();
|
|
380
|
+
caveInteriorUniforms[0] = effect.resolutionW;
|
|
381
|
+
caveInteriorUniforms[1] = effect.resolutionH;
|
|
382
|
+
caveInteriorUniforms[2] = effect.caveColor.r;
|
|
383
|
+
caveInteriorUniforms[3] = effect.caveColor.g;
|
|
384
|
+
caveInteriorUniforms[4] = effect.caveColor.b;
|
|
385
|
+
caveInteriorUniforms[5] = effect.glowColor.r;
|
|
386
|
+
caveInteriorUniforms[6] = effect.glowColor.g;
|
|
387
|
+
caveInteriorUniforms[7] = effect.glowColor.b;
|
|
388
|
+
caveInteriorUniforms[8] = effect.scrollOffsetY;
|
|
389
|
+
caveInteriorUniforms[9] = effect.stalactiteCount;
|
|
390
|
+
caveInteriorPaint.setShader(caveInteriorEffect.makeShader(caveInteriorUniforms));
|
|
391
|
+
return caveInteriorPaint;
|
|
392
|
+
}
|
|
393
|
+
function applyMountainHills(effect) {
|
|
394
|
+
if (Skia2.RuntimeEffect === void 0) return defaultPaint;
|
|
395
|
+
if (mountainHillsEffect === null) {
|
|
396
|
+
const compiled = Skia2.RuntimeEffect.Make(MOUNTAIN_HILLS_SKSL);
|
|
397
|
+
if (compiled === null) return defaultPaint;
|
|
398
|
+
mountainHillsEffect = compiled;
|
|
399
|
+
}
|
|
400
|
+
if (mountainHillsPaint === null) mountainHillsPaint = Skia2.Paint();
|
|
401
|
+
mountainHillsUniforms[0] = effect.resolutionW;
|
|
402
|
+
mountainHillsUniforms[1] = effect.resolutionH;
|
|
403
|
+
mountainHillsUniforms[2] = effect.skyColor.r;
|
|
404
|
+
mountainHillsUniforms[3] = effect.skyColor.g;
|
|
405
|
+
mountainHillsUniforms[4] = effect.skyColor.b;
|
|
406
|
+
mountainHillsUniforms[5] = effect.hillColor.r;
|
|
407
|
+
mountainHillsUniforms[6] = effect.hillColor.g;
|
|
408
|
+
mountainHillsUniforms[7] = effect.hillColor.b;
|
|
409
|
+
mountainHillsUniforms[8] = effect.scrollOffsetY;
|
|
410
|
+
mountainHillsUniforms[9] = effect.ridgeScale;
|
|
411
|
+
mountainHillsPaint.setShader(mountainHillsEffect.makeShader(mountainHillsUniforms));
|
|
412
|
+
return mountainHillsPaint;
|
|
413
|
+
}
|
|
414
|
+
function drawOutlined(canvas2, image, commands, n, effect, alpha) {
|
|
415
|
+
const paint = applyOutlinePaint(effect);
|
|
416
|
+
paint.setAlphaf(alpha);
|
|
417
|
+
const srcs = srcRects.length === n ? srcRects : srcRects.slice(0, n);
|
|
418
|
+
for (let k = 0; k < OUTLINE_OFFSETS.length; k++) {
|
|
419
|
+
const offset = OUTLINE_OFFSETS[k];
|
|
420
|
+
fillXforms(commands, n, offset[0] * effect.width, offset[1] * effect.width);
|
|
421
|
+
const dsts = xforms.length === n ? xforms : xforms.slice(0, n);
|
|
422
|
+
canvas2.drawAtlas(image, srcs, dsts, paint);
|
|
423
|
+
}
|
|
424
|
+
paint.setAlphaf(1);
|
|
425
|
+
defaultPaint.setAlphaf(alpha);
|
|
426
|
+
fillXforms(commands, n, 0, 0);
|
|
427
|
+
const mainDsts = xforms.length === n ? xforms : xforms.slice(0, n);
|
|
428
|
+
canvas2.drawAtlas(image, srcs, mainDsts, defaultPaint);
|
|
429
|
+
defaultPaint.setAlphaf(1);
|
|
430
|
+
}
|
|
431
|
+
function resolveEffectPaint(effect) {
|
|
432
|
+
switch (effect.type) {
|
|
433
|
+
case "tint":
|
|
434
|
+
return applyTint(effect);
|
|
435
|
+
case "flash":
|
|
436
|
+
return applyFlash(effect);
|
|
437
|
+
case "grayscale":
|
|
438
|
+
return applyGrayscale(effect);
|
|
439
|
+
case "custom":
|
|
440
|
+
return applyCustom(effect);
|
|
441
|
+
case "water":
|
|
442
|
+
return applyWater(effect, currentFrameTime);
|
|
443
|
+
case "fog":
|
|
444
|
+
return applyFog(effect, currentFrameTime);
|
|
445
|
+
case "fire":
|
|
446
|
+
return applyFire(effect, currentFrameTime);
|
|
447
|
+
case "outline":
|
|
448
|
+
return applyOutlinePaint(effect);
|
|
449
|
+
case "roofs-city":
|
|
450
|
+
return applyRoofsCity(effect);
|
|
451
|
+
case "forest-canopy":
|
|
452
|
+
return applyForestCanopy(effect);
|
|
453
|
+
case "river-wave":
|
|
454
|
+
return applyRiverWave(effect);
|
|
455
|
+
case "moon-crater":
|
|
456
|
+
return applyMoonCrater(effect);
|
|
457
|
+
case "cave-interior":
|
|
458
|
+
return applyCaveInterior(effect);
|
|
459
|
+
case "mountain-hills":
|
|
460
|
+
return applyMountainHills(effect);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
return {
|
|
464
|
+
canvas,
|
|
465
|
+
beginFrame(viewportWidth, viewportHeight, time = 0, pixelDensity = 1) {
|
|
466
|
+
if (viewportWidth === 0 || viewportHeight === 0) return;
|
|
467
|
+
_boundRuntime = null;
|
|
468
|
+
_boundChain = null;
|
|
469
|
+
currentFrameTime = time;
|
|
470
|
+
const pd = pixelDensity;
|
|
471
|
+
recordingBounds.setXYWH(0, 0, viewportWidth * pd, viewportHeight * pd);
|
|
472
|
+
current = recorder.beginRecording(recordingBounds);
|
|
473
|
+
if (pd !== 1) current.scale(pd, pd);
|
|
474
|
+
},
|
|
475
|
+
drawAtlas(image, commands) {
|
|
476
|
+
if (current === null) return;
|
|
477
|
+
const n = commands.length;
|
|
478
|
+
if (n === 0) return;
|
|
479
|
+
const effect = commands[0]?.effect;
|
|
480
|
+
const alpha = commands[0]?.alpha ?? 1;
|
|
481
|
+
const skImage = image;
|
|
482
|
+
if (effect?.type === "roofs-city" || effect?.type === "forest-canopy" || effect?.type === "river-wave" || effect?.type === "moon-crater" || effect?.type === "cave-interior" || effect?.type === "mountain-hills") {
|
|
483
|
+
const paint2 = resolveEffectPaint(effect);
|
|
484
|
+
paint2.setAlphaf(alpha);
|
|
485
|
+
for (let i = 0; i < n; i++) {
|
|
486
|
+
const cmd = commands[i];
|
|
487
|
+
const rect = Skia2.XYWHRect(cmd.dstX, cmd.dstY, cmd.dstW, cmd.dstH);
|
|
488
|
+
current.drawRect(rect, paint2);
|
|
489
|
+
}
|
|
490
|
+
paint2.setAlphaf(1);
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
if (commands[0]?.stretch) {
|
|
494
|
+
if (stretchPaint === null) stretchPaint = Skia2.Paint();
|
|
495
|
+
ensureStretchPoolCapacity(n);
|
|
496
|
+
stretchPaint.setAlphaf(alpha);
|
|
497
|
+
for (let i = 0; i < n; i++) {
|
|
498
|
+
const cmd = commands[i];
|
|
499
|
+
const srcR = stretchSrcRects[i];
|
|
500
|
+
const dstR = stretchDstRects[i];
|
|
501
|
+
srcR.setXYWH(cmd.srcX, cmd.srcY, cmd.srcW, cmd.srcH);
|
|
502
|
+
dstR.setXYWH(cmd.dstX, cmd.dstY, cmd.dstW, cmd.dstH);
|
|
503
|
+
current.drawImageRect(skImage, srcR, dstR, stretchPaint);
|
|
504
|
+
}
|
|
505
|
+
stretchPaint.setAlphaf(1);
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
ensurePoolCapacity(n);
|
|
509
|
+
fillSrcRects(commands, n);
|
|
510
|
+
if (effect?.type === "outline") {
|
|
511
|
+
drawOutlined(current, skImage, commands, n, effect, alpha);
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
fillXforms(commands, n, 0, 0);
|
|
515
|
+
const srcs = srcRects.length === n ? srcRects : srcRects.slice(0, n);
|
|
516
|
+
const dsts = xforms.length === n ? xforms : xforms.slice(0, n);
|
|
517
|
+
const paint = effect === void 0 ? defaultPaint : resolveEffectPaint(effect);
|
|
518
|
+
paint.setAlphaf(alpha);
|
|
519
|
+
current.drawAtlas(skImage, srcs, dsts, paint);
|
|
520
|
+
paint.setAlphaf(1);
|
|
521
|
+
},
|
|
522
|
+
setPostFxBinding(runtime, chain) {
|
|
523
|
+
_boundRuntime = runtime;
|
|
524
|
+
_boundChain = chain;
|
|
525
|
+
},
|
|
526
|
+
buildPostFxPasses(runtime, chain, width, height) {
|
|
527
|
+
const rt = runtime ?? _boundRuntime;
|
|
528
|
+
const ch = chain ?? _boundChain;
|
|
529
|
+
if (rt === null || !rt.supported) return [];
|
|
530
|
+
if (rt.buildPass === void 0) {
|
|
531
|
+
logOnce("buildPostFxPasses: runtime.supported but buildPass absent \u2014 no passes");
|
|
532
|
+
return [];
|
|
533
|
+
}
|
|
534
|
+
if (ch === null) return [];
|
|
535
|
+
const passes = rt.buildPass(ch, width, height);
|
|
536
|
+
if (passes.length === 0) return [];
|
|
537
|
+
const out = [];
|
|
538
|
+
for (const pass of passes) {
|
|
539
|
+
const shader = pass.shader;
|
|
540
|
+
const uniformCount = typeof shader.getUniformCount === "function" ? shader.getUniformCount() : 0;
|
|
541
|
+
const uniforms = {};
|
|
542
|
+
for (let i = 0; i < uniformCount; i++) {
|
|
543
|
+
const name = typeof shader.getUniformName === "function" ? shader.getUniformName(i) : String(i);
|
|
544
|
+
const info = typeof shader.getUniform === "function" ? shader.getUniform(i) : { columns: 1, rows: 1, slot: i };
|
|
545
|
+
const size = info.columns * info.rows;
|
|
546
|
+
if (size === 1) {
|
|
547
|
+
uniforms[name] = pass.uniforms[info.slot] ?? 0;
|
|
548
|
+
} else {
|
|
549
|
+
const vec = [];
|
|
550
|
+
for (let k = 0; k < size; k++) {
|
|
551
|
+
vec.push(pass.uniforms[info.slot + k] ?? 0);
|
|
552
|
+
}
|
|
553
|
+
uniforms[name] = vec;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
out.push({
|
|
557
|
+
id: pass.id,
|
|
558
|
+
source: shader,
|
|
559
|
+
uniforms,
|
|
560
|
+
children: pass.children
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
return out;
|
|
564
|
+
},
|
|
565
|
+
endFrame() {
|
|
566
|
+
if (current === null) return null;
|
|
567
|
+
current = null;
|
|
568
|
+
return recorder.finishRecordingAsPicture();
|
|
569
|
+
}
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
function renderShapesToImage(skia, opts) {
|
|
573
|
+
if (skia.Surface === void 0) return null;
|
|
574
|
+
const surface = skia.Surface.Make(opts.width, opts.height);
|
|
575
|
+
if (surface === null) return null;
|
|
576
|
+
const scratch = new Float32Array(4);
|
|
577
|
+
const canvas = surface.getCanvas();
|
|
578
|
+
unpackArgb(scratch, opts.clearColor);
|
|
579
|
+
canvas.clear(scratch);
|
|
580
|
+
const paint = skia.Paint();
|
|
581
|
+
for (const shape of opts.shapes) {
|
|
582
|
+
unpackArgb(scratch, shape.color);
|
|
583
|
+
paint.setColor(scratch);
|
|
584
|
+
const rect = skia.XYWHRect(shape.x, shape.y, shape.w, shape.h);
|
|
585
|
+
canvas.drawRect(rect, paint);
|
|
586
|
+
}
|
|
587
|
+
return detachSnapshot(surface);
|
|
588
|
+
}
|
|
589
|
+
function unpackArgb(out, argb) {
|
|
590
|
+
out[0] = (argb >>> 16 & 255) / 255;
|
|
591
|
+
out[1] = (argb >>> 8 & 255) / 255;
|
|
592
|
+
out[2] = (argb & 255) / 255;
|
|
593
|
+
out[3] = (argb >>> 24 & 255) / 255;
|
|
594
|
+
}
|
|
595
|
+
function writeColor(out, color, alphaMul) {
|
|
596
|
+
out[0] = color.r;
|
|
597
|
+
out[1] = color.g;
|
|
598
|
+
out[2] = color.b;
|
|
599
|
+
out[3] = color.a * alphaMul;
|
|
600
|
+
}
|
|
601
|
+
function writeGrayscaleMatrix(out, amount) {
|
|
602
|
+
const inv = 1 - amount;
|
|
603
|
+
const lr = 0.299 * amount;
|
|
604
|
+
const lg = 0.587 * amount;
|
|
605
|
+
const lb = 0.114 * amount;
|
|
606
|
+
out[0] = inv + lr;
|
|
607
|
+
out[1] = lg;
|
|
608
|
+
out[2] = lb;
|
|
609
|
+
out[3] = 0;
|
|
610
|
+
out[4] = 0;
|
|
611
|
+
out[5] = lr;
|
|
612
|
+
out[6] = inv + lg;
|
|
613
|
+
out[7] = lb;
|
|
614
|
+
out[8] = 0;
|
|
615
|
+
out[9] = 0;
|
|
616
|
+
out[10] = lr;
|
|
617
|
+
out[11] = lg;
|
|
618
|
+
out[12] = inv + lb;
|
|
619
|
+
out[13] = 0;
|
|
620
|
+
out[14] = 0;
|
|
621
|
+
out[15] = 0;
|
|
622
|
+
out[16] = 0;
|
|
623
|
+
out[17] = 0;
|
|
624
|
+
out[18] = 1;
|
|
625
|
+
out[19] = 0;
|
|
626
|
+
}
|
|
627
|
+
function fillCustomUniforms(effect, shader, out) {
|
|
628
|
+
const count = shader.getUniformCount();
|
|
629
|
+
for (let i = 0; i < out.length; i++) out[i] = 0;
|
|
630
|
+
for (let i = 0; i < count; i++) {
|
|
631
|
+
const info = shader.getUniform(i);
|
|
632
|
+
const name = shader.getUniformName(i);
|
|
633
|
+
const value = effect.uniforms[name];
|
|
634
|
+
const size = info.columns * info.rows;
|
|
635
|
+
if (value === void 0) continue;
|
|
636
|
+
if (typeof value === "number") {
|
|
637
|
+
out[info.slot] = value;
|
|
638
|
+
continue;
|
|
639
|
+
}
|
|
640
|
+
for (let k = 0; k < size; k++) {
|
|
641
|
+
out[info.slot + k] = value[k] ?? 0;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
// src/game-view.tsx
|
|
647
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
648
|
+
function GameView({
|
|
649
|
+
config,
|
|
650
|
+
onReady,
|
|
651
|
+
style,
|
|
652
|
+
children,
|
|
653
|
+
safeArea
|
|
654
|
+
}) {
|
|
655
|
+
const insets = useSafeAreaInsets();
|
|
656
|
+
const resolvedSafeArea = safeArea ?? insets;
|
|
657
|
+
const canvasRef = useCanvasRef();
|
|
658
|
+
const [picture, setPicture] = useState(null);
|
|
659
|
+
const [passes, setPasses] = useState([]);
|
|
660
|
+
const [viewportSize, setViewportSize] = useState({ w: 0, h: 0 });
|
|
661
|
+
const pd = PixelRatio.get();
|
|
662
|
+
const sceneSize = useMemo(
|
|
663
|
+
() => ({ width: viewportSize.w * pd, height: viewportSize.h * pd }),
|
|
664
|
+
[viewportSize.w, viewportSize.h, pd]
|
|
665
|
+
);
|
|
666
|
+
const sceneTexture = usePictureAsTexture(picture, sceneSize);
|
|
667
|
+
const ctx = useMemo(() => {
|
|
668
|
+
const loop = new GameLoop({
|
|
669
|
+
fixedDt: config?.fixedDt,
|
|
670
|
+
maxDt: config?.maxDt
|
|
671
|
+
});
|
|
672
|
+
const world = new World();
|
|
673
|
+
const camera = new Camera2D();
|
|
674
|
+
const input = new InputManager();
|
|
675
|
+
const audio = new AudioManager();
|
|
676
|
+
const noop = () => Promise.resolve(null);
|
|
677
|
+
const assets = new AssetManager(config?.assetLoader ?? noop);
|
|
678
|
+
const scenes = new SceneManager();
|
|
679
|
+
const renderer = new Renderer();
|
|
680
|
+
const batch = new SpriteBatch();
|
|
681
|
+
return { world, camera, input, audio, assets, scenes, loop, renderer, batch };
|
|
682
|
+
}, []);
|
|
683
|
+
useEffect(() => {
|
|
684
|
+
const bridge = createSkiaFrameRenderer(Skia);
|
|
685
|
+
ctx.renderer.setCanvas(bridge.canvas);
|
|
686
|
+
const clearColor = config?.clearColor ?? 4278190080;
|
|
687
|
+
const onRenderBackground = config?.onRenderBackground;
|
|
688
|
+
const onRenderPostFx = config?.onRenderPostFx;
|
|
689
|
+
const spriteQuery = ctx.world.query(Position, Sprite);
|
|
690
|
+
const renderSystem = createRenderSystem(ctx.world, spriteQuery, Position, Sprite, ctx.batch);
|
|
691
|
+
const unsubscribeLate = ctx.loop.onLateUpdate.add((dt) => {
|
|
692
|
+
ctx.camera.update(dt);
|
|
693
|
+
ctx.input.update();
|
|
694
|
+
});
|
|
695
|
+
const unsubscribePre = ctx.loop.onPreRender.add(() => {
|
|
696
|
+
const w = ctx.camera.viewportWidth;
|
|
697
|
+
const h = ctx.camera.viewportHeight;
|
|
698
|
+
bridge.beginFrame(w, h, 0, pd);
|
|
699
|
+
ctx.renderer.begin(clearColor);
|
|
700
|
+
ctx.renderer.applyCamera(
|
|
701
|
+
ctx.camera.effectiveX,
|
|
702
|
+
ctx.camera.effectiveY,
|
|
703
|
+
ctx.camera.zoom,
|
|
704
|
+
ctx.camera.rotation,
|
|
705
|
+
w,
|
|
706
|
+
h
|
|
707
|
+
);
|
|
708
|
+
if (onRenderBackground) onRenderBackground(ctx, ctx.renderer.canvas);
|
|
709
|
+
renderSystem();
|
|
710
|
+
ctx.batch.flush(bridge.drawAtlas, ctx.renderer);
|
|
711
|
+
if (onRenderPostFx) onRenderPostFx(ctx, ctx.renderer.canvas, bridge);
|
|
712
|
+
ctx.renderer.end();
|
|
713
|
+
const pic = bridge.endFrame();
|
|
714
|
+
if (pic !== null) setPicture(pic);
|
|
715
|
+
const built = bridge.buildPostFxPasses(null, null, w, h);
|
|
716
|
+
setPasses(built);
|
|
717
|
+
canvasRef.current?.redraw();
|
|
718
|
+
});
|
|
719
|
+
onReady?.(ctx);
|
|
720
|
+
ctx.loop.start();
|
|
721
|
+
return () => {
|
|
722
|
+
unsubscribeLate();
|
|
723
|
+
unsubscribePre();
|
|
724
|
+
ctx.loop.stop();
|
|
725
|
+
};
|
|
726
|
+
}, []);
|
|
727
|
+
const handleLayout = (event) => {
|
|
728
|
+
const { width, height } = event.nativeEvent.layout;
|
|
729
|
+
ctx.camera.setViewport(width, height);
|
|
730
|
+
setViewportSize({ w: width, h: height });
|
|
731
|
+
};
|
|
732
|
+
const onGrant = (e) => {
|
|
733
|
+
ctx.input.onPointerDown(0, e.nativeEvent.locationX, e.nativeEvent.locationY);
|
|
734
|
+
};
|
|
735
|
+
const onMove = (e) => {
|
|
736
|
+
ctx.input.onPointerMove(0, e.nativeEvent.locationX, e.nativeEvent.locationY);
|
|
737
|
+
};
|
|
738
|
+
const onRelease = (e) => {
|
|
739
|
+
ctx.input.onPointerUp(0, e.nativeEvent.locationX, e.nativeEvent.locationY);
|
|
740
|
+
};
|
|
741
|
+
return /* @__PURE__ */ jsx(SafeAreaContext.Provider, { value: resolvedSafeArea, children: /* @__PURE__ */ jsx(GameContext.Provider, { value: ctx, children: /* @__PURE__ */ jsxs(
|
|
742
|
+
View,
|
|
743
|
+
{
|
|
744
|
+
style: [styles.container, style],
|
|
745
|
+
onLayout: handleLayout,
|
|
746
|
+
onStartShouldSetResponder: returnTrue,
|
|
747
|
+
onResponderGrant: onGrant,
|
|
748
|
+
onResponderMove: onMove,
|
|
749
|
+
onResponderRelease: onRelease,
|
|
750
|
+
onResponderTerminate: onRelease,
|
|
751
|
+
children: [
|
|
752
|
+
/* @__PURE__ */ jsx(
|
|
753
|
+
Canvas,
|
|
754
|
+
{
|
|
755
|
+
ref: canvasRef,
|
|
756
|
+
style: StyleSheet.absoluteFill,
|
|
757
|
+
pointerEvents: "none",
|
|
758
|
+
opaque: config?.opaque ?? false,
|
|
759
|
+
children: viewportSize.w === 0 || sceneTexture == null ? null : /* @__PURE__ */ jsx(
|
|
760
|
+
Image,
|
|
761
|
+
{
|
|
762
|
+
image: sceneTexture,
|
|
763
|
+
x: 0,
|
|
764
|
+
y: 0,
|
|
765
|
+
width: viewportSize.w,
|
|
766
|
+
height: viewportSize.h,
|
|
767
|
+
fit: "cover",
|
|
768
|
+
children: passes.map((p) => /* @__PURE__ */ jsx(RuntimeShader, { source: p.source, uniforms: p.uniforms }, p.id))
|
|
769
|
+
}
|
|
770
|
+
)
|
|
771
|
+
}
|
|
772
|
+
),
|
|
773
|
+
children
|
|
774
|
+
]
|
|
775
|
+
}
|
|
776
|
+
) }) });
|
|
777
|
+
}
|
|
778
|
+
function returnTrue() {
|
|
779
|
+
return true;
|
|
780
|
+
}
|
|
781
|
+
var styles = StyleSheet.create({
|
|
782
|
+
container: { flex: 1 }
|
|
783
|
+
});
|
|
784
|
+
|
|
785
|
+
// src/hooks.ts
|
|
786
|
+
import { useCallback, useContext as useContext2, useEffect as useEffect2, useRef, useState as useState2 } from "react";
|
|
787
|
+
function useGame() {
|
|
788
|
+
const ctx = useContext2(GameContext);
|
|
789
|
+
if (!ctx) {
|
|
790
|
+
throw new Error("useGame must be used inside a <GameView>");
|
|
791
|
+
}
|
|
792
|
+
return ctx;
|
|
793
|
+
}
|
|
794
|
+
function useEntity(query) {
|
|
795
|
+
const [entities, setEntities] = useState2(() => Array.from(query));
|
|
796
|
+
useEffect2(() => {
|
|
797
|
+
const refresh = () => setEntities(Array.from(query));
|
|
798
|
+
const unsubAdd = query.onEntityAdded.add(refresh);
|
|
799
|
+
const unsubRemove = query.onEntityRemoved.add(refresh);
|
|
800
|
+
refresh();
|
|
801
|
+
return () => {
|
|
802
|
+
unsubAdd();
|
|
803
|
+
unsubRemove();
|
|
804
|
+
};
|
|
805
|
+
}, [query]);
|
|
806
|
+
return entities;
|
|
807
|
+
}
|
|
808
|
+
function useGameLoop(callback) {
|
|
809
|
+
const { loop } = useGame();
|
|
810
|
+
const callbackRef = useRef(callback);
|
|
811
|
+
callbackRef.current = callback;
|
|
812
|
+
const stable = useCallback((dt) => {
|
|
813
|
+
callbackRef.current(dt);
|
|
814
|
+
}, []);
|
|
815
|
+
useEffect2(() => {
|
|
816
|
+
const unsub = loop.onUpdate.add(stable);
|
|
817
|
+
return () => {
|
|
818
|
+
unsub();
|
|
819
|
+
};
|
|
820
|
+
}, [loop, stable]);
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
// src/virtual-joystick.tsx
|
|
824
|
+
import {
|
|
825
|
+
createJoystickState,
|
|
826
|
+
releaseJoystick,
|
|
827
|
+
updateJoystick
|
|
828
|
+
} from "@flare-engine/input";
|
|
829
|
+
import { useCallback as useCallback2, useRef as useRef2, useState as useState3 } from "react";
|
|
830
|
+
import { StyleSheet as StyleSheet2, View as View2 } from "react-native";
|
|
831
|
+
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
832
|
+
function VirtualJoystick({
|
|
833
|
+
config,
|
|
834
|
+
onStateChange,
|
|
835
|
+
style,
|
|
836
|
+
base,
|
|
837
|
+
thumb
|
|
838
|
+
}) {
|
|
839
|
+
const stateRef = useRef2(createJoystickState());
|
|
840
|
+
const sizeRef = useRef2({ w: 0, h: 0 });
|
|
841
|
+
const [knob, setKnob] = useState3({ x: 0, y: 0 });
|
|
842
|
+
const handleLayout = useCallback2(
|
|
843
|
+
(e) => {
|
|
844
|
+
sizeRef.current = { w: e.nativeEvent.layout.width, h: e.nativeEvent.layout.height };
|
|
845
|
+
},
|
|
846
|
+
[]
|
|
847
|
+
);
|
|
848
|
+
const handleTouch = useCallback2(
|
|
849
|
+
(e) => {
|
|
850
|
+
const { locationX, locationY } = e.nativeEvent;
|
|
851
|
+
const { w, h } = sizeRef.current;
|
|
852
|
+
updateJoystick(stateRef.current, w / 2, h / 2, locationX, locationY, config);
|
|
853
|
+
onStateChange?.(stateRef.current);
|
|
854
|
+
const { x, y } = stateRef.current.offset;
|
|
855
|
+
setKnob((prev) => prev.x === x && prev.y === y ? prev : { x, y });
|
|
856
|
+
},
|
|
857
|
+
[config, onStateChange]
|
|
858
|
+
);
|
|
859
|
+
const handleRelease = useCallback2(() => {
|
|
860
|
+
releaseJoystick(stateRef.current);
|
|
861
|
+
onStateChange?.(stateRef.current);
|
|
862
|
+
setKnob((prev) => prev.x === 0 && prev.y === 0 ? prev : { x: 0, y: 0 });
|
|
863
|
+
}, [onStateChange]);
|
|
864
|
+
const maxRadius = config?.maxRadius ?? 50;
|
|
865
|
+
return /* @__PURE__ */ jsxs2(
|
|
866
|
+
View2,
|
|
867
|
+
{
|
|
868
|
+
style: [
|
|
869
|
+
styles2.center,
|
|
870
|
+
...base ? [] : [styles2.baseBg],
|
|
871
|
+
{
|
|
872
|
+
width: maxRadius * 2,
|
|
873
|
+
height: maxRadius * 2,
|
|
874
|
+
borderRadius: maxRadius
|
|
875
|
+
},
|
|
876
|
+
style
|
|
877
|
+
],
|
|
878
|
+
onLayout: handleLayout,
|
|
879
|
+
onStartShouldSetResponder: returnTrue2,
|
|
880
|
+
onMoveShouldSetResponder: returnTrue2,
|
|
881
|
+
onResponderGrant: handleTouch,
|
|
882
|
+
onResponderMove: handleTouch,
|
|
883
|
+
onResponderRelease: handleRelease,
|
|
884
|
+
onResponderTerminate: handleRelease,
|
|
885
|
+
children: [
|
|
886
|
+
base ? /* @__PURE__ */ jsx2(View2, { style: StyleSheet2.absoluteFill, pointerEvents: "none", children: base }) : null,
|
|
887
|
+
thumb ? /* @__PURE__ */ jsx2(
|
|
888
|
+
View2,
|
|
889
|
+
{
|
|
890
|
+
style: { transform: [{ translateX: knob.x }, { translateY: knob.y }] },
|
|
891
|
+
pointerEvents: "none",
|
|
892
|
+
children: thumb
|
|
893
|
+
}
|
|
894
|
+
) : /* @__PURE__ */ jsx2(
|
|
895
|
+
View2,
|
|
896
|
+
{
|
|
897
|
+
style: [styles2.knob, { transform: [{ translateX: knob.x }, { translateY: knob.y }] }]
|
|
898
|
+
}
|
|
899
|
+
)
|
|
900
|
+
]
|
|
901
|
+
}
|
|
902
|
+
);
|
|
903
|
+
}
|
|
904
|
+
function returnTrue2() {
|
|
905
|
+
return true;
|
|
906
|
+
}
|
|
907
|
+
var styles2 = StyleSheet2.create({
|
|
908
|
+
center: {
|
|
909
|
+
alignItems: "center",
|
|
910
|
+
justifyContent: "center"
|
|
911
|
+
},
|
|
912
|
+
baseBg: {
|
|
913
|
+
backgroundColor: "rgba(255,255,255,0.2)"
|
|
914
|
+
},
|
|
915
|
+
knob: {
|
|
916
|
+
width: 40,
|
|
917
|
+
height: 40,
|
|
918
|
+
borderRadius: 20,
|
|
919
|
+
backgroundColor: "rgba(255,255,255,0.6)"
|
|
920
|
+
}
|
|
921
|
+
});
|
|
922
|
+
|
|
923
|
+
// src/virtual-button.tsx
|
|
924
|
+
import {
|
|
925
|
+
createButtonState,
|
|
926
|
+
pressButton,
|
|
927
|
+
releaseButton,
|
|
928
|
+
resetButtonFrame
|
|
929
|
+
} from "@flare-engine/input";
|
|
930
|
+
import { useCallback as useCallback3, useRef as useRef3 } from "react";
|
|
931
|
+
import { StyleSheet as StyleSheet3, View as View3 } from "react-native";
|
|
932
|
+
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
933
|
+
function VirtualButton({
|
|
934
|
+
onStateChange,
|
|
935
|
+
style,
|
|
936
|
+
children
|
|
937
|
+
}) {
|
|
938
|
+
const stateRef = useRef3(createButtonState());
|
|
939
|
+
const handlePress = useCallback3(() => {
|
|
940
|
+
resetButtonFrame(stateRef.current);
|
|
941
|
+
pressButton(stateRef.current);
|
|
942
|
+
onStateChange?.(stateRef.current);
|
|
943
|
+
}, [onStateChange]);
|
|
944
|
+
const handleRelease = useCallback3(() => {
|
|
945
|
+
resetButtonFrame(stateRef.current);
|
|
946
|
+
releaseButton(stateRef.current);
|
|
947
|
+
onStateChange?.(stateRef.current);
|
|
948
|
+
}, [onStateChange]);
|
|
949
|
+
return /* @__PURE__ */ jsx3(
|
|
950
|
+
View3,
|
|
951
|
+
{
|
|
952
|
+
style: [styles3.button, style],
|
|
953
|
+
onStartShouldSetResponder: returnTrue3,
|
|
954
|
+
onResponderGrant: handlePress,
|
|
955
|
+
onResponderRelease: handleRelease,
|
|
956
|
+
onResponderTerminate: handleRelease,
|
|
957
|
+
children
|
|
958
|
+
}
|
|
959
|
+
);
|
|
960
|
+
}
|
|
961
|
+
function returnTrue3() {
|
|
962
|
+
return true;
|
|
963
|
+
}
|
|
964
|
+
var styles3 = StyleSheet3.create({
|
|
965
|
+
button: {
|
|
966
|
+
width: 60,
|
|
967
|
+
height: 60,
|
|
968
|
+
borderRadius: 30,
|
|
969
|
+
backgroundColor: "rgba(255,255,255,0.3)",
|
|
970
|
+
alignItems: "center",
|
|
971
|
+
justifyContent: "center"
|
|
972
|
+
}
|
|
973
|
+
});
|
|
974
|
+
|
|
975
|
+
// src/loading-screen.tsx
|
|
976
|
+
import { StyleSheet as StyleSheet4, View as View4 } from "react-native";
|
|
977
|
+
import { jsx as jsx4 } from "react/jsx-runtime";
|
|
978
|
+
function LoadingScreen({
|
|
979
|
+
progress,
|
|
980
|
+
style,
|
|
981
|
+
barColor = "#ffffff",
|
|
982
|
+
trackColor = "rgba(255,255,255,0.2)",
|
|
983
|
+
backgroundColor = "#000000"
|
|
984
|
+
}) {
|
|
985
|
+
const clamped = progress < 0 ? 0 : progress > 1 ? 1 : progress;
|
|
986
|
+
const percent = `${Math.round(clamped * 100)}%`;
|
|
987
|
+
return /* @__PURE__ */ jsx4(View4, { style: [styles4.container, { backgroundColor }, style], children: /* @__PURE__ */ jsx4(View4, { style: [styles4.track, { backgroundColor: trackColor }], children: /* @__PURE__ */ jsx4(View4, { style: [styles4.bar, { width: percent, backgroundColor: barColor }] }) }) });
|
|
988
|
+
}
|
|
989
|
+
var styles4 = StyleSheet4.create({
|
|
990
|
+
container: {
|
|
991
|
+
position: "absolute",
|
|
992
|
+
top: 0,
|
|
993
|
+
left: 0,
|
|
994
|
+
right: 0,
|
|
995
|
+
bottom: 0,
|
|
996
|
+
alignItems: "center",
|
|
997
|
+
justifyContent: "center",
|
|
998
|
+
padding: 32
|
|
999
|
+
},
|
|
1000
|
+
track: {
|
|
1001
|
+
width: "80%",
|
|
1002
|
+
height: 6,
|
|
1003
|
+
borderRadius: 3,
|
|
1004
|
+
overflow: "hidden"
|
|
1005
|
+
},
|
|
1006
|
+
bar: {
|
|
1007
|
+
height: "100%"
|
|
1008
|
+
}
|
|
1009
|
+
});
|
|
1010
|
+
export {
|
|
1011
|
+
GameContext,
|
|
1012
|
+
GameView,
|
|
1013
|
+
LoadingScreen,
|
|
1014
|
+
SafeAreaContext,
|
|
1015
|
+
VirtualButton,
|
|
1016
|
+
VirtualJoystick,
|
|
1017
|
+
createSkiaFrameRenderer,
|
|
1018
|
+
renderShapesToImage,
|
|
1019
|
+
useEntity,
|
|
1020
|
+
useGame,
|
|
1021
|
+
useGameLoop,
|
|
1022
|
+
useSafeArea
|
|
1023
|
+
};
|
|
1024
|
+
//# sourceMappingURL=index.mjs.map
|