@celljs/ta-games 0.0.0-stage → 4.12.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/README.en.md +47 -0
- package/README.md +46 -2
- package/host/index.mjs +65 -0
- package/package.json +58 -4
- package/skills/ta-game/SKILL.md +127 -0
- package/skills/ta-game/examples/dino-run/package.json +22 -0
- package/skills/ta-game/examples/dino-run/web.js +389 -0
- package/skills/ta-game/examples/stack/package.json +22 -0
- package/skills/ta-game/examples/stack/web.js +423 -0
- package/skills/ta-game/phaser/LICENSE.md +22 -0
- package/skills/ta-game/phaser/actions-and-utilities/SKILL.md +343 -0
- package/skills/ta-game/phaser/animations/SKILL.md +481 -0
- package/skills/ta-game/phaser/audio-and-sound/SKILL.md +453 -0
- package/skills/ta-game/phaser/cameras/SKILL.md +497 -0
- package/skills/ta-game/phaser/cameras/references/REFERENCE.md +128 -0
- package/skills/ta-game/phaser/curves-and-paths/SKILL.md +359 -0
- package/skills/ta-game/phaser/data-manager/SKILL.md +406 -0
- package/skills/ta-game/phaser/events-system/SKILL.md +497 -0
- package/skills/ta-game/phaser/filters-and-postfx/SKILL.md +449 -0
- package/skills/ta-game/phaser/game-object-components/SKILL.md +478 -0
- package/skills/ta-game/phaser/game-object-components/references/REFERENCE.md +76 -0
- package/skills/ta-game/phaser/game-setup-and-config/SKILL.md +485 -0
- package/skills/ta-game/phaser/geometry-and-math/SKILL.md +497 -0
- package/skills/ta-game/phaser/graphics-and-shapes/SKILL.md +473 -0
- package/skills/ta-game/phaser/groups-and-containers/SKILL.md +418 -0
- package/skills/ta-game/phaser/input-keyboard-mouse-touch/SKILL.md +500 -0
- package/skills/ta-game/phaser/input-keyboard-mouse-touch/references/REFERENCE.md +372 -0
- package/skills/ta-game/phaser/loading-assets/SKILL.md +474 -0
- package/skills/ta-game/phaser/loading-assets/references/REFERENCE.md +148 -0
- package/skills/ta-game/phaser/particles/SKILL.md +499 -0
- package/skills/ta-game/phaser/particles/references/REFERENCE.md +17 -0
- package/skills/ta-game/phaser/physics-arcade/SKILL.md +468 -0
- package/skills/ta-game/phaser/physics-matter/SKILL.md +499 -0
- package/skills/ta-game/phaser/render-textures/SKILL.md +441 -0
- package/skills/ta-game/phaser/scale-and-responsive/SKILL.md +371 -0
- package/skills/ta-game/phaser/scenes/SKILL.md +450 -0
- package/skills/ta-game/phaser/scenes/references/REFERENCE.md +78 -0
- package/skills/ta-game/phaser/sprites-and-images/SKILL.md +417 -0
- package/skills/ta-game/phaser/sprites-and-images/references/REFERENCE.md +160 -0
- package/skills/ta-game/phaser/text-and-bitmaptext/SKILL.md +460 -0
- package/skills/ta-game/phaser/tilemaps/SKILL.md +440 -0
- package/skills/ta-game/phaser/time-and-timers/SKILL.md +496 -0
- package/skills/ta-game/phaser/tweens/SKILL.md +414 -0
- package/skills/ta-game/phaser/v3-to-v4-migration/SKILL.md +430 -0
- package/skills/ta-game/phaser/v4-new-features/SKILL.md +384 -0
- package/skills/ta-game/phaser/v4-new-features/references/REFERENCE.md +69 -0
- package/web/index.js +431 -0
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
// Stack, a web extension of Ta on Three.js (pi.ui.Game3D): the example of a 3D game of the ta-game skill.
|
|
2
|
+
// The rules (StackRules) are apart from what draws them (setup), so that they can be tested; the score and
|
|
3
|
+
// the messages are drawn by React over the game.
|
|
4
|
+
|
|
5
|
+
export const STACK_TUNING = {
|
|
6
|
+
size: 3,
|
|
7
|
+
height: 0.5,
|
|
8
|
+
/** How far the moving block goes on either side of the tower. */
|
|
9
|
+
range: 4.5,
|
|
10
|
+
speed: 3.2,
|
|
11
|
+
/** How much faster each block. */
|
|
12
|
+
speedUp: 0.12,
|
|
13
|
+
maxSpeed: 9,
|
|
14
|
+
/** Closer than that to the block under it, it lands right on top and keeps its size. */
|
|
15
|
+
perfect: 0.12,
|
|
16
|
+
restartAfter: 0.5,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const GRAVITY = 22;
|
|
20
|
+
/** Blocks drawn under the top, at most: the rest are out of sight below. */
|
|
21
|
+
const KEEP = 40;
|
|
22
|
+
/** How tall the pedestal under the first block is. */
|
|
23
|
+
const PEDESTAL = 5;
|
|
24
|
+
/** The key of the package data that holds the best score, the same on every device. */
|
|
25
|
+
const BEST_KEY = 'best';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The rules: a block (`{ x, z, width, depth, y }`, its middle) slides over the tower, along x and z by turns;
|
|
29
|
+
* a tap drops it, and what hangs over is cut off. A block that misses the tower ends the run.
|
|
30
|
+
* `tap()` says what it did: `{ kind: 'none' | 'start' | 'restart' }`, `{ kind: 'placed', block, cut?, perfect }`
|
|
31
|
+
* or `{ kind: 'missed', cut }`. Time goes by `step` (seconds).
|
|
32
|
+
*/
|
|
33
|
+
export class StackRules {
|
|
34
|
+
constructor(tuning = STACK_TUNING) {
|
|
35
|
+
this.tuning = tuning;
|
|
36
|
+
this.restart();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
get score() {
|
|
40
|
+
return this.blocks.length - 1;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
get top() {
|
|
44
|
+
return this.blocks[this.blocks.length - 1];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
restart() {
|
|
48
|
+
const { size } = this.tuning;
|
|
49
|
+
/** `ready`, `running` or `over`. */
|
|
50
|
+
this.state = 'ready';
|
|
51
|
+
this.blocks = [{ x: 0, z: 0, width: size, depth: size, y: 0 }];
|
|
52
|
+
/** The block that slides, while running. */
|
|
53
|
+
this.moving = undefined;
|
|
54
|
+
this.axis = 'x';
|
|
55
|
+
this.speed = this.tuning.speed;
|
|
56
|
+
/** Blocks landed right on top in a row. */
|
|
57
|
+
this.streak = 0;
|
|
58
|
+
this.direction = 1;
|
|
59
|
+
this.since = 0;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
tap() {
|
|
63
|
+
if (this.state === 'ready') {
|
|
64
|
+
this.state = 'running';
|
|
65
|
+
this.spawn();
|
|
66
|
+
return { kind: 'start' };
|
|
67
|
+
}
|
|
68
|
+
if (this.state === 'over') {
|
|
69
|
+
if (this.since < this.tuning.restartAfter) {
|
|
70
|
+
return { kind: 'none' };
|
|
71
|
+
}
|
|
72
|
+
this.restart();
|
|
73
|
+
this.state = 'running';
|
|
74
|
+
this.spawn();
|
|
75
|
+
return { kind: 'restart' };
|
|
76
|
+
}
|
|
77
|
+
return this.drop();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
step(seconds) {
|
|
81
|
+
const dt = Math.min(seconds, 0.05);
|
|
82
|
+
if (this.state === 'over') {
|
|
83
|
+
this.since += dt;
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const moving = this.moving;
|
|
87
|
+
if (this.state !== 'running' || !moving) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const { range } = this.tuning;
|
|
91
|
+
const center = this.top[this.axis];
|
|
92
|
+
let position = moving[this.axis] + this.direction * this.speed * dt;
|
|
93
|
+
if (position > center + range) {
|
|
94
|
+
position = center + range;
|
|
95
|
+
this.direction = -1;
|
|
96
|
+
} else if (position < center - range) {
|
|
97
|
+
position = center - range;
|
|
98
|
+
this.direction = 1;
|
|
99
|
+
}
|
|
100
|
+
moving[this.axis] = position;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
spawn() {
|
|
104
|
+
const top = this.top;
|
|
105
|
+
this.moving = {
|
|
106
|
+
...top,
|
|
107
|
+
y: top.y + this.tuning.height,
|
|
108
|
+
[this.axis]: top[this.axis] - this.tuning.range,
|
|
109
|
+
};
|
|
110
|
+
this.direction = 1;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
drop() {
|
|
114
|
+
const moving = this.moving;
|
|
115
|
+
const top = this.top;
|
|
116
|
+
const along = this.axis;
|
|
117
|
+
const extent = along === 'x' ? 'width' : 'depth';
|
|
118
|
+
let delta = moving[along] - top[along];
|
|
119
|
+
const perfect = Math.abs(delta) <= this.tuning.perfect;
|
|
120
|
+
if (perfect) {
|
|
121
|
+
delta = 0;
|
|
122
|
+
}
|
|
123
|
+
const overlap = top[extent] - Math.abs(delta);
|
|
124
|
+
if (overlap <= 0) {
|
|
125
|
+
this.state = 'over';
|
|
126
|
+
this.since = 0;
|
|
127
|
+
this.moving = undefined;
|
|
128
|
+
this.streak = 0;
|
|
129
|
+
return { kind: 'missed', cut: moving };
|
|
130
|
+
}
|
|
131
|
+
const block = { ...moving, [along]: top[along] + delta / 2, [extent]: overlap };
|
|
132
|
+
let cut;
|
|
133
|
+
if (delta !== 0) {
|
|
134
|
+
const size = Math.abs(delta);
|
|
135
|
+
cut = {
|
|
136
|
+
...moving,
|
|
137
|
+
[along]: block[along] + Math.sign(delta) * (overlap / 2 + size / 2),
|
|
138
|
+
[extent]: size,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
this.streak = perfect ? this.streak + 1 : 0;
|
|
142
|
+
this.blocks.push(block);
|
|
143
|
+
this.axis = this.axis === 'x' ? 'z' : 'x';
|
|
144
|
+
this.speed = Math.min(this.tuning.maxSpeed, this.speed + this.tuning.speedUp);
|
|
145
|
+
this.spawn();
|
|
146
|
+
return { kind: 'placed', block, cut, perfect };
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* The setup for `pi.ui.Game3D`: the tower in the green of the theme, its hue turning slowly up, a light from
|
|
152
|
+
* above, the camera rising with the tower, the parts cut off falling away. Taps, clicks, space and enter drop.
|
|
153
|
+
* `hooks.change(state, score, perfect)` after each move, `hooks.over(score)` when a run ends.
|
|
154
|
+
*/
|
|
155
|
+
export function stackSetup(hooks) {
|
|
156
|
+
return context => {
|
|
157
|
+
const { THREE, scene, element } = context;
|
|
158
|
+
const rules = new StackRules();
|
|
159
|
+
// One geometry for every block: its size is its scale.
|
|
160
|
+
const unit = new THREE.BoxGeometry(1, 1, 1);
|
|
161
|
+
const meshes = [];
|
|
162
|
+
const falling = [];
|
|
163
|
+
let moving;
|
|
164
|
+
let page = context.page;
|
|
165
|
+
let pulse = 0;
|
|
166
|
+
let eye = 0;
|
|
167
|
+
const camera = new THREE.PerspectiveCamera(40, context.width / context.height, 0.1, 200);
|
|
168
|
+
context.camera = camera;
|
|
169
|
+
scene.add(new THREE.HemisphereLight(0xffffff, 0x707070, 1.7));
|
|
170
|
+
const sun = new THREE.DirectionalLight(0xffffff, 1.6);
|
|
171
|
+
scene.add(sun, sun.target);
|
|
172
|
+
|
|
173
|
+
const colorOf = index => {
|
|
174
|
+
const color = new THREE.Color(page.colors.green);
|
|
175
|
+
const hsl = { h: 0, s: 0, l: 0 };
|
|
176
|
+
color.getHSL(hsl);
|
|
177
|
+
const light = page.theme === 'dark' ? 0.4 : 0.44;
|
|
178
|
+
return color.setHSL((hsl.h + index * 0.018) % 1, Math.min(0.62, hsl.s), light + Math.sin(index / 3) * 0.06);
|
|
179
|
+
};
|
|
180
|
+
const place = (mesh, block, pedestal = false) => {
|
|
181
|
+
const height = rules.tuning.height;
|
|
182
|
+
mesh.scale.set(block.width, pedestal ? PEDESTAL : height, block.depth);
|
|
183
|
+
mesh.position.set(block.x, pedestal ? block.y + height / 2 - PEDESTAL / 2 : block.y, block.z);
|
|
184
|
+
};
|
|
185
|
+
const add = (block, index) => {
|
|
186
|
+
const mesh = new THREE.Mesh(unit, new THREE.MeshLambertMaterial({ color: colorOf(index) }));
|
|
187
|
+
place(mesh, block, index === 0);
|
|
188
|
+
mesh.userData.index = index;
|
|
189
|
+
scene.add(mesh);
|
|
190
|
+
return mesh;
|
|
191
|
+
};
|
|
192
|
+
// Taken out of the scene, its material is ours to dispose; what stays the page disposes at the end.
|
|
193
|
+
const remove = mesh => {
|
|
194
|
+
scene.remove(mesh);
|
|
195
|
+
mesh.material.dispose();
|
|
196
|
+
};
|
|
197
|
+
const rebuild = () => {
|
|
198
|
+
for (const mesh of [...meshes, ...(moving ? [moving] : [])]) {
|
|
199
|
+
remove(mesh);
|
|
200
|
+
}
|
|
201
|
+
meshes.length = 0;
|
|
202
|
+
moving = undefined;
|
|
203
|
+
for (const [index, block] of rules.blocks.entries()) {
|
|
204
|
+
meshes.push(add(block, index));
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
const fall = (block, index) => {
|
|
208
|
+
const spin = page.reduceMotion ? new THREE.Vector3() : new THREE.Vector3(Math.random() - 0.5, 0, Math.random() - 0.5).multiplyScalar(4);
|
|
209
|
+
falling.push({ mesh: add(block, index), speed: 0, spin });
|
|
210
|
+
};
|
|
211
|
+
const dropMoving = () => {
|
|
212
|
+
if (moving) {
|
|
213
|
+
remove(moving);
|
|
214
|
+
moving = undefined;
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const apply = move => {
|
|
219
|
+
if (move.kind === 'restart') {
|
|
220
|
+
rebuild();
|
|
221
|
+
} else if (move.kind === 'placed') {
|
|
222
|
+
dropMoving();
|
|
223
|
+
const index = rules.blocks.length - 1;
|
|
224
|
+
meshes.push(add(move.block, index));
|
|
225
|
+
if (move.cut) {
|
|
226
|
+
fall(move.cut, index);
|
|
227
|
+
}
|
|
228
|
+
pulse = move.perfect && !page.reduceMotion ? 1 : 0;
|
|
229
|
+
while (meshes.length > KEEP) {
|
|
230
|
+
remove(meshes.shift());
|
|
231
|
+
}
|
|
232
|
+
} else if (move.kind === 'missed') {
|
|
233
|
+
dropMoving();
|
|
234
|
+
fall(move.cut, rules.blocks.length);
|
|
235
|
+
hooks.over(rules.score);
|
|
236
|
+
}
|
|
237
|
+
if (move.kind !== 'none') {
|
|
238
|
+
hooks.change(rules.state, rules.score, move.kind === 'placed' && move.perfect);
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
// The mouse drops as it goes down; a finger as it lifts: one that slides scrolls the page, and the
|
|
242
|
+
// browser cancels its pointer (touch-action: pan-y), so that it never lifts here.
|
|
243
|
+
const onPointer = event => {
|
|
244
|
+
if (event.button === 0 && (event.pointerType === 'mouse') === (event.type === 'pointerdown')) {
|
|
245
|
+
apply(rules.tap());
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
const onKey = event => {
|
|
249
|
+
if (event.code === 'Space' || event.code === 'Enter') {
|
|
250
|
+
event.preventDefault();
|
|
251
|
+
if (!event.repeat) {
|
|
252
|
+
apply(rules.tap());
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
element.addEventListener('pointerdown', onPointer);
|
|
257
|
+
element.addEventListener('pointerup', onPointer);
|
|
258
|
+
element.addEventListener('keydown', onKey);
|
|
259
|
+
rebuild();
|
|
260
|
+
hooks.change(rules.state, rules.score, false);
|
|
261
|
+
|
|
262
|
+
return {
|
|
263
|
+
update(seconds) {
|
|
264
|
+
rules.step(seconds);
|
|
265
|
+
if (rules.state === 'running' && rules.moving) {
|
|
266
|
+
moving ??= add(rules.moving, rules.blocks.length);
|
|
267
|
+
place(moving, rules.moving);
|
|
268
|
+
}
|
|
269
|
+
for (const item of [...falling]) {
|
|
270
|
+
item.speed += GRAVITY * seconds;
|
|
271
|
+
item.mesh.position.y -= item.speed * seconds;
|
|
272
|
+
item.mesh.rotation.x += item.spin.x * seconds;
|
|
273
|
+
item.mesh.rotation.z += item.spin.z * seconds;
|
|
274
|
+
if (item.mesh.position.y < rules.top.y - 30) {
|
|
275
|
+
remove(item.mesh);
|
|
276
|
+
falling.splice(falling.indexOf(item), 1);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
const top = meshes[meshes.length - 1];
|
|
280
|
+
if (top && pulse > 0) {
|
|
281
|
+
pulse = Math.max(0, pulse - seconds * 4);
|
|
282
|
+
top.scale.x = rules.top.width * (1 + pulse * 0.06);
|
|
283
|
+
top.scale.z = rules.top.depth * (1 + pulse * 0.06);
|
|
284
|
+
}
|
|
285
|
+
// The camera rises with the tower; at once with less motion.
|
|
286
|
+
eye = page.reduceMotion ? rules.top.y : eye + (rules.top.y - eye) * Math.min(1, seconds * 4);
|
|
287
|
+
camera.position.set(8, eye + 8, 8);
|
|
288
|
+
camera.lookAt(0, eye - 1, 0);
|
|
289
|
+
sun.position.set(4, eye + 10, 6);
|
|
290
|
+
sun.target.position.set(0, eye, 0);
|
|
291
|
+
},
|
|
292
|
+
page(next) {
|
|
293
|
+
page = next;
|
|
294
|
+
for (const mesh of [...meshes, ...(moving ? [moving] : []), ...falling.map(item => item.mesh)]) {
|
|
295
|
+
mesh.material.color.copy(colorOf(mesh.userData.index));
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
dispose() {
|
|
299
|
+
element.removeEventListener('pointerdown', onPointer);
|
|
300
|
+
element.removeEventListener('pointerup', onPointer);
|
|
301
|
+
element.removeEventListener('keydown', onKey);
|
|
302
|
+
},
|
|
303
|
+
};
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const WORDS = {
|
|
308
|
+
en: {
|
|
309
|
+
title: 'Stack',
|
|
310
|
+
best: 'Best {score}',
|
|
311
|
+
startKey: 'Space or tap to drop a block',
|
|
312
|
+
startTouch: 'Tap to drop a block',
|
|
313
|
+
over: 'It fell! Tap to build again',
|
|
314
|
+
perfect: 'Perfect!',
|
|
315
|
+
},
|
|
316
|
+
zh: {
|
|
317
|
+
title: '叠方块',
|
|
318
|
+
best: '最高 {score}',
|
|
319
|
+
startKey: '按空格键或轻点放下方块',
|
|
320
|
+
startTouch: '轻点放下方块',
|
|
321
|
+
over: '塌了!轻点重新开始',
|
|
322
|
+
perfect: '完美!',
|
|
323
|
+
},
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
const STYLE = `
|
|
327
|
+
.stk-scroll { height: 100%; overflow-y: auto; overscroll-behavior: contain; }
|
|
328
|
+
.stk { display: flex; flex-direction: column; width: 100%; max-width: 760px; margin: 0 auto; padding: 24px 20px 40px; box-sizing: border-box; }
|
|
329
|
+
.stk-head { display: flex; align-items: center; gap: 10px; margin: 0 2px 10px; }
|
|
330
|
+
.stk-title { flex: 1; color: var(--ta-text); font-size: calc(15px * var(--ta-font-scale)); line-height: calc(22px * var(--ta-font-scale)); font-weight: 600; }
|
|
331
|
+
.stk-best { display: inline-flex; align-items: center; gap: 5px; color: var(--ta-muted); font-size: calc(13px * var(--ta-font-scale)); font-variant-numeric: tabular-nums; }
|
|
332
|
+
.stk-stage { position: relative; display: flex; flex-direction: column; aspect-ratio: 16 / 9; border: 1px solid var(--ta-border); border-radius: 12px; background: var(--ta-card); overflow: hidden; }
|
|
333
|
+
:root[data-handheld] .stk-stage { aspect-ratio: 1 / 1; }
|
|
334
|
+
/* A finger sliding up or down over the game scrolls the page; a tap plays as it lifts. */
|
|
335
|
+
.stk-stage .ta-game { touch-action: pan-y; }
|
|
336
|
+
.stk-overlay { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; pointer-events: none; }
|
|
337
|
+
.stk-score { margin-top: 14px; color: var(--ta-text); font-size: calc(32px * var(--ta-font-scale)); line-height: calc(38px * var(--ta-font-scale)); font-weight: 600; font-variant-numeric: tabular-nums; }
|
|
338
|
+
.stk-perfect { color: var(--ta-green); font-size: calc(14px * var(--ta-font-scale)); line-height: calc(20px * var(--ta-font-scale)); font-weight: 600; animation: stk-pop 900ms ease-out forwards; }
|
|
339
|
+
.stk-message { margin-top: auto; margin-bottom: 18px; padding: 6px 12px; border-radius: 8px; background: color-mix(in srgb, var(--ta-card) 82%, transparent); color: var(--ta-secondary); font-size: calc(14px * var(--ta-font-scale)); line-height: calc(20px * var(--ta-font-scale)); }
|
|
340
|
+
@keyframes stk-pop { 0% { opacity: 0; transform: translateY(4px) scale(0.96); } 20% { opacity: 1; transform: none; } 100% { opacity: 0; transform: translateY(-6px); } }
|
|
341
|
+
`;
|
|
342
|
+
|
|
343
|
+
export default function (pi) {
|
|
344
|
+
const { React } = pi;
|
|
345
|
+
const h = React.createElement;
|
|
346
|
+
const style = document.createElement('style');
|
|
347
|
+
style.textContent = STYLE;
|
|
348
|
+
document.head.appendChild(style);
|
|
349
|
+
|
|
350
|
+
function Page() {
|
|
351
|
+
const words = pi.ui.language.startsWith('zh') ? WORDS.zh : WORDS.en;
|
|
352
|
+
const touch = pi.ui.useDevice().touch;
|
|
353
|
+
const [best, setBest] = React.useState(0);
|
|
354
|
+
const bestRef = React.useRef(0);
|
|
355
|
+
const [play, setPlay] = React.useState({ state: 'ready', score: 0, perfect: 0 });
|
|
356
|
+
React.useEffect(() => {
|
|
357
|
+
let live = true;
|
|
358
|
+
const read = () =>
|
|
359
|
+
pi.storage.get(BEST_KEY).then(value => {
|
|
360
|
+
if (live && typeof value === 'number') {
|
|
361
|
+
bestRef.current = value;
|
|
362
|
+
setBest(value);
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
read();
|
|
366
|
+
const off = pi.storage.onChange(keys => keys.includes(BEST_KEY) && read());
|
|
367
|
+
return () => {
|
|
368
|
+
live = false;
|
|
369
|
+
off();
|
|
370
|
+
};
|
|
371
|
+
}, []);
|
|
372
|
+
const setup = React.useMemo(
|
|
373
|
+
() =>
|
|
374
|
+
stackSetup({
|
|
375
|
+
change: (state, score, perfect) => setPlay(last => ({ state, score, perfect: perfect ? last.perfect + 1 : 0 })),
|
|
376
|
+
over: score => {
|
|
377
|
+
if (score > bestRef.current) {
|
|
378
|
+
bestRef.current = score;
|
|
379
|
+
setBest(score);
|
|
380
|
+
pi.storage.set(BEST_KEY, score);
|
|
381
|
+
}
|
|
382
|
+
},
|
|
383
|
+
}),
|
|
384
|
+
[],
|
|
385
|
+
);
|
|
386
|
+
const message = play.state === 'ready' ? (touch ? words.startTouch : words.startKey) : play.state === 'over' ? words.over : '';
|
|
387
|
+
const Trophy = pi.ui.icons.Trophy;
|
|
388
|
+
return h(
|
|
389
|
+
'div',
|
|
390
|
+
{ className: 'stk-scroll' },
|
|
391
|
+
h(
|
|
392
|
+
'div',
|
|
393
|
+
{ className: 'stk' },
|
|
394
|
+
h(
|
|
395
|
+
'div',
|
|
396
|
+
{ className: 'stk-head' },
|
|
397
|
+
// The bar of the page shows its title: here the best score only.
|
|
398
|
+
h('div', { className: 'stk-title' }),
|
|
399
|
+
h('div', { className: 'stk-best' }, Trophy ? h(Trophy, { size: 14 }) : null, words.best.replace('{score}', best)),
|
|
400
|
+
),
|
|
401
|
+
h(
|
|
402
|
+
'div',
|
|
403
|
+
{ className: 'stk-stage' },
|
|
404
|
+
h(pi.ui.Game3D, { setup }),
|
|
405
|
+
h(
|
|
406
|
+
'div',
|
|
407
|
+
{ className: 'stk-overlay', 'aria-live': 'polite' },
|
|
408
|
+
h('div', { className: 'stk-score' }, play.state === 'ready' ? '' : play.score),
|
|
409
|
+
play.perfect > 0 && play.state === 'running' ? h('div', { key: play.perfect, className: 'stk-perfect' }, words.perfect) : null,
|
|
410
|
+
message ? h('div', { className: 'stk-message' }, message) : null,
|
|
411
|
+
),
|
|
412
|
+
),
|
|
413
|
+
),
|
|
414
|
+
);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
pi.registerView('stack', {
|
|
418
|
+
title: () => (pi.ui.language.startsWith('zh') ? WORDS.zh.title : WORDS.en.title),
|
|
419
|
+
icon: pi.ui.icons.Gamepad2,
|
|
420
|
+
render: Page,
|
|
421
|
+
});
|
|
422
|
+
return () => style.remove();
|
|
423
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The skills of Phaser 4.2.1, copied from its npm package.
|
|
2
|
+
|
|
3
|
+
The MIT License (MIT)
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2026 Richard Davey, Phaser Studio Inc.
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
8
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
9
|
+
the Software without restriction, including without limitation the rights to
|
|
10
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
11
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
12
|
+
subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
19
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
20
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
21
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
22
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|