@celljs/ta-games 0.0.0-stage → 4.11.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.
Files changed (47) hide show
  1. package/README.en.md +47 -0
  2. package/README.md +46 -2
  3. package/host/index.mjs +65 -0
  4. package/package.json +58 -4
  5. package/skills/ta-game/SKILL.md +127 -0
  6. package/skills/ta-game/examples/dino-run/package.json +22 -0
  7. package/skills/ta-game/examples/dino-run/web.js +389 -0
  8. package/skills/ta-game/examples/stack/package.json +22 -0
  9. package/skills/ta-game/examples/stack/web.js +423 -0
  10. package/skills/ta-game/phaser/LICENSE.md +22 -0
  11. package/skills/ta-game/phaser/actions-and-utilities/SKILL.md +343 -0
  12. package/skills/ta-game/phaser/animations/SKILL.md +481 -0
  13. package/skills/ta-game/phaser/audio-and-sound/SKILL.md +453 -0
  14. package/skills/ta-game/phaser/cameras/SKILL.md +497 -0
  15. package/skills/ta-game/phaser/cameras/references/REFERENCE.md +128 -0
  16. package/skills/ta-game/phaser/curves-and-paths/SKILL.md +359 -0
  17. package/skills/ta-game/phaser/data-manager/SKILL.md +406 -0
  18. package/skills/ta-game/phaser/events-system/SKILL.md +497 -0
  19. package/skills/ta-game/phaser/filters-and-postfx/SKILL.md +449 -0
  20. package/skills/ta-game/phaser/game-object-components/SKILL.md +478 -0
  21. package/skills/ta-game/phaser/game-object-components/references/REFERENCE.md +76 -0
  22. package/skills/ta-game/phaser/game-setup-and-config/SKILL.md +485 -0
  23. package/skills/ta-game/phaser/geometry-and-math/SKILL.md +497 -0
  24. package/skills/ta-game/phaser/graphics-and-shapes/SKILL.md +473 -0
  25. package/skills/ta-game/phaser/groups-and-containers/SKILL.md +418 -0
  26. package/skills/ta-game/phaser/input-keyboard-mouse-touch/SKILL.md +500 -0
  27. package/skills/ta-game/phaser/input-keyboard-mouse-touch/references/REFERENCE.md +372 -0
  28. package/skills/ta-game/phaser/loading-assets/SKILL.md +474 -0
  29. package/skills/ta-game/phaser/loading-assets/references/REFERENCE.md +148 -0
  30. package/skills/ta-game/phaser/particles/SKILL.md +499 -0
  31. package/skills/ta-game/phaser/particles/references/REFERENCE.md +17 -0
  32. package/skills/ta-game/phaser/physics-arcade/SKILL.md +468 -0
  33. package/skills/ta-game/phaser/physics-matter/SKILL.md +499 -0
  34. package/skills/ta-game/phaser/render-textures/SKILL.md +441 -0
  35. package/skills/ta-game/phaser/scale-and-responsive/SKILL.md +371 -0
  36. package/skills/ta-game/phaser/scenes/SKILL.md +450 -0
  37. package/skills/ta-game/phaser/scenes/references/REFERENCE.md +78 -0
  38. package/skills/ta-game/phaser/sprites-and-images/SKILL.md +417 -0
  39. package/skills/ta-game/phaser/sprites-and-images/references/REFERENCE.md +160 -0
  40. package/skills/ta-game/phaser/text-and-bitmaptext/SKILL.md +460 -0
  41. package/skills/ta-game/phaser/tilemaps/SKILL.md +440 -0
  42. package/skills/ta-game/phaser/time-and-timers/SKILL.md +496 -0
  43. package/skills/ta-game/phaser/tweens/SKILL.md +414 -0
  44. package/skills/ta-game/phaser/v3-to-v4-migration/SKILL.md +430 -0
  45. package/skills/ta-game/phaser/v4-new-features/SKILL.md +384 -0
  46. package/skills/ta-game/phaser/v4-new-features/references/REFERENCE.md +69 -0
  47. package/web/index.js +431 -0
@@ -0,0 +1,389 @@
1
+ // Dino Run, a web extension of Ta on Phaser 4 (pi.ui.Game): the example of a 2D game of the ta-game skill.
2
+ // The rules (DinoRules) are apart from what draws them (the scene in config), so that they can be tested.
3
+
4
+ /** The sizes and the feel of the run, in pixels of the world and seconds. */
5
+ export const RUN_TUNING = {
6
+ width: 600,
7
+ height: 200,
8
+ ground: 178,
9
+ dinoX: 56,
10
+ dinoSize: 46,
11
+ gravity: 2600,
12
+ jumpSpeed: 860,
13
+ startSpeed: 380,
14
+ acceleration: 9,
15
+ maxSpeed: 920,
16
+ /** A restart waits that long after a fall, so that a jump held on does not start again at once. */
17
+ restartAfter: 0.45,
18
+ };
19
+
20
+ /** Pixels of the world per point of the score. */
21
+ const PIXELS_PER_POINT = 40;
22
+ /** What a collision forgives on each side: the dinosaur is not a box. */
23
+ const FORGIVE = 7;
24
+ /** The key of the package data that holds the best score, the same on every device. */
25
+ const BEST_KEY = 'best';
26
+
27
+ /** The dinosaur in pixels, facing right: `#` its body, `o` its eye; then its legs, two frames of a step. */
28
+ const DINO = [
29
+ '..........######',
30
+ '.........##o####',
31
+ '.........#######',
32
+ '.........#######',
33
+ '.........####...',
34
+ '.........######.',
35
+ '#.......#####...',
36
+ '#......#######..',
37
+ '##...#########..',
38
+ '###.#######.#...',
39
+ '###########.....',
40
+ '.#########......',
41
+ '..#######.......',
42
+ '...#####........',
43
+ ];
44
+ const LEGS = [
45
+ ['...##.##........', '...#...#........', '...##..##.......'],
46
+ ['...##.##........', '...##...#.......', '.......##.......'],
47
+ ];
48
+
49
+ /**
50
+ * The rules of the run: the dinosaur jumps over cacti that come faster and faster. Time goes by `step`
51
+ * (seconds); `random` decides the cacti.
52
+ */
53
+ export class DinoRules {
54
+ constructor(tuning = RUN_TUNING, random = Math.random) {
55
+ this.tuning = tuning;
56
+ this.random = random;
57
+ this.restart();
58
+ }
59
+
60
+ get score() {
61
+ return Math.floor(this.distance / PIXELS_PER_POINT);
62
+ }
63
+
64
+ get onGround() {
65
+ return this.lift <= 0;
66
+ }
67
+
68
+ restart() {
69
+ /** `ready`, `running` or `over`. */
70
+ this.state = 'ready';
71
+ /** How high the dinosaur is above the ground, and how fast it goes up. */
72
+ this.lift = 0;
73
+ this.rise = 0;
74
+ this.speed = this.tuning.startSpeed;
75
+ this.distance = 0;
76
+ /** Each `{ x, width, height }`, standing on the ground. */
77
+ this.cacti = [];
78
+ this.gap = 0;
79
+ this.since = 0;
80
+ }
81
+
82
+ /** The tap or the key: starts, jumps, or starts again once a fall has settled. True when it did something. */
83
+ jump() {
84
+ if (this.state === 'ready') {
85
+ this.state = 'running';
86
+ this.rise = this.tuning.jumpSpeed;
87
+ return true;
88
+ }
89
+ if (this.state === 'over') {
90
+ if (this.since < this.tuning.restartAfter) {
91
+ return false;
92
+ }
93
+ this.restart();
94
+ this.state = 'running';
95
+ return true;
96
+ }
97
+ if (!this.onGround) {
98
+ return false;
99
+ }
100
+ this.rise = this.tuning.jumpSpeed;
101
+ return true;
102
+ }
103
+
104
+ /** `seconds` of the game; a long one (a tab left a while) counts as a short one. True when it fell now. */
105
+ step(seconds) {
106
+ const dt = Math.min(seconds, 0.05);
107
+ if (this.state === 'over') {
108
+ this.since += dt;
109
+ return false;
110
+ }
111
+ if (this.state === 'ready') {
112
+ return false;
113
+ }
114
+ const { tuning } = this;
115
+ this.rise -= tuning.gravity * dt;
116
+ this.lift = Math.max(0, this.lift + this.rise * dt);
117
+ if (this.lift === 0 && this.rise < 0) {
118
+ this.rise = 0;
119
+ }
120
+ this.speed = Math.min(tuning.maxSpeed, this.speed + tuning.acceleration * dt);
121
+ const moved = this.speed * dt;
122
+ this.distance += moved;
123
+ for (const cactus of this.cacti) {
124
+ cactus.x -= moved;
125
+ }
126
+ this.cacti = this.cacti.filter(cactus => cactus.x + cactus.width > -10);
127
+ this.gap -= moved;
128
+ if (this.gap <= 0) {
129
+ this.cacti.push(this.cactus());
130
+ this.gap = this.nextGap();
131
+ }
132
+ if (this.hit()) {
133
+ this.state = 'over';
134
+ this.since = 0;
135
+ return true;
136
+ }
137
+ return false;
138
+ }
139
+
140
+ /** Whether the dinosaur touches a cactus. */
141
+ hit() {
142
+ const { dinoX, dinoSize, ground } = this.tuning;
143
+ const left = dinoX + FORGIVE;
144
+ const right = dinoX + dinoSize - FORGIVE;
145
+ const bottom = ground - this.lift - FORGIVE / 2;
146
+ return this.cacti.some(cactus => cactus.x < right && cactus.x + cactus.width > left && bottom > ground - cactus.height);
147
+ }
148
+
149
+ cactus() {
150
+ const tall = this.random() < 0.35;
151
+ const width = 14 + Math.round(this.random() * (tall ? 10 : 22));
152
+ const height = tall ? 46 + Math.round(this.random() * 12) : 28 + Math.round(this.random() * 14);
153
+ return { x: this.tuning.width + 20, width, height };
154
+ }
155
+
156
+ /** Room enough to land and jump again at this speed: the time of a jump in the air, times the speed, and a random part more. */
157
+ nextGap() {
158
+ const air = (2 * this.tuning.jumpSpeed) / this.tuning.gravity;
159
+ const least = this.speed * air + 60;
160
+ return least + this.random() * least * 1.2;
161
+ }
162
+ }
163
+
164
+ const hex = color => `#${color.toString(16).padStart(6, '0')}`;
165
+
166
+ const WORDS = {
167
+ en: {
168
+ title: 'Dino Run',
169
+ best: 'Best {score}',
170
+ startKey: 'Space or tap to start',
171
+ startTouch: 'Tap to start',
172
+ over: 'Ouch! Tap to run again',
173
+ },
174
+ zh: {
175
+ title: '恐龙快跑',
176
+ best: '最高 {score}',
177
+ startKey: '按空格键或轻点开始',
178
+ startTouch: '轻点开始',
179
+ over: '撞上了!轻点再跑一次',
180
+ },
181
+ };
182
+
183
+ const STYLE = `
184
+ .dnr-scroll { height: 100%; overflow-y: auto; overscroll-behavior: contain; }
185
+ .dnr { display: flex; flex-direction: column; width: 100%; max-width: 760px; margin: 0 auto; padding: 24px 20px 40px; box-sizing: border-box; }
186
+ .dnr-head { display: flex; align-items: center; gap: 10px; margin: 0 2px 10px; }
187
+ .dnr-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; }
188
+ .dnr-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; }
189
+ .dnr-stage { display: flex; flex-direction: column; aspect-ratio: 3 / 1; border: 1px solid var(--ta-border); border-radius: 12px; background: var(--ta-card); overflow: hidden; }
190
+ /* A finger sliding up or down over the game scrolls the page; a tap plays as it lifts. */
191
+ .dnr-stage .ta-game { touch-action: pan-y; }
192
+ `;
193
+
194
+ export default function (pi) {
195
+ const { React } = pi;
196
+ const h = React.createElement;
197
+ const style = document.createElement('style');
198
+ style.textContent = STYLE;
199
+ document.head.appendChild(style);
200
+
201
+ /** The dinosaur in the green of the theme, a texture of each frame (`dino0`, `dino1`), drawn at the size the screen shows it. */
202
+ function drawDino(scene, page, zoom) {
203
+ const rows = DINO.length + LEGS[0].length;
204
+ const cell = Math.ceil((RUN_TUNING.dinoSize / rows) * zoom);
205
+ for (const [frame, legs] of LEGS.entries()) {
206
+ const key = `dino${frame}`;
207
+ if (scene.textures.exists(key)) {
208
+ scene.textures.remove(key);
209
+ }
210
+ const art = [...DINO, ...legs];
211
+ const texture = scene.textures.createCanvas(key, art[0].length * cell, rows * cell);
212
+ const context = texture.getContext();
213
+ art.forEach((row, y) => {
214
+ [...row].forEach((pixel, x) => {
215
+ if (pixel !== '.') {
216
+ context.fillStyle = hex(pixel === 'o' ? page.colors.card : page.colors.green);
217
+ context.fillRect(x * cell, y * cell, cell, cell);
218
+ }
219
+ });
220
+ });
221
+ texture.refresh();
222
+ }
223
+ }
224
+
225
+ /** The scene of the run, a class of the engine, which loads with the first game. */
226
+ function makeScene(Phaser, hooks, zoom) {
227
+ const { width, height, ground, dinoX, dinoSize } = RUN_TUNING;
228
+ // As wide as the art is to its height.
229
+ const dinoWidth = (dinoSize * DINO[0].length) / (DINO.length + LEGS[0].length);
230
+ return class DinoRunScene extends Phaser.Scene {
231
+ create() {
232
+ this.rules = new DinoRules();
233
+ this.page = this.registry.get('page');
234
+ this.cameras.main.setZoom(zoom).centerOn(width / 2, height / 2);
235
+ this.world = this.add.graphics();
236
+ drawDino(this, this.page, zoom);
237
+ this.dino = this.add.image(dinoX, ground, 'dino0').setOrigin(0, 1).setDisplaySize(dinoWidth, dinoSize);
238
+ const font = getComputedStyle(document.body).fontFamily || 'sans-serif';
239
+ this.score = this.add.text(width - 14, 12, '', { fontFamily: font, fontSize: '17px', resolution: zoom }).setOrigin(1, 0);
240
+ this.message = this.add
241
+ .text(width / 2, 82, '', {
242
+ fontFamily: font,
243
+ fontSize: '20px',
244
+ resolution: zoom,
245
+ align: 'center',
246
+ lineSpacing: 6,
247
+ })
248
+ .setOrigin(0.5);
249
+ this.time0 = 0;
250
+ this.paint();
251
+ // The mouse jumps as it goes down; a finger as it lifts, a slide being the page's scroll.
252
+ this.input.on('pointerdown', pointer => !pointer.wasTouch && this.rules.jump());
253
+ this.input.on('pointerup', pointer => pointer.wasTouch && this.rules.jump());
254
+ this.input.keyboard.on('keydown-SPACE', event => {
255
+ event.preventDefault();
256
+ this.rules.jump();
257
+ });
258
+ this.input.keyboard.on('keydown-UP', () => this.rules.jump());
259
+ this.registry.events.on('changedata-page', (_parent, page) => {
260
+ this.page = page;
261
+ drawDino(this, page, zoom);
262
+ this.paint();
263
+ });
264
+ }
265
+
266
+ update(_time, delta) {
267
+ const { rules } = this;
268
+ if (rules.step(delta / 1000)) {
269
+ hooks.over(rules.score);
270
+ }
271
+ const motion = !this.page.reduceMotion;
272
+ this.time0 += delta;
273
+ const step = motion && rules.state === 'running' && rules.onGround ? Math.floor(this.time0 / 90) % 2 : 0;
274
+ this.dino.setTexture(`dino${step}`).setDisplaySize(dinoWidth, dinoSize);
275
+ this.dino.y = ground - rules.lift;
276
+ this.dino.setAlpha(rules.state === 'over' ? 0.55 : 1);
277
+ this.drawWorld();
278
+ this.score.setText(String(rules.score).padStart(5, '0'));
279
+ const words = hooks.words();
280
+ this.message.setText(rules.state === 'ready' ? words.start : rules.state === 'over' ? `${words.over}\n${words.best}` : '');
281
+ }
282
+
283
+ paint() {
284
+ this.score.setColor(hex(this.page.colors.muted));
285
+ this.message.setColor(hex(this.page.colors.secondary));
286
+ }
287
+
288
+ drawWorld() {
289
+ const { colors } = this.page;
290
+ const { world, rules } = this;
291
+ world.clear();
292
+ world.fillStyle(colors.border, 1);
293
+ world.fillRect(0, ground, width, 1.5);
294
+ // Pebbles that go by with the ground.
295
+ const offset = rules.distance % 48;
296
+ for (let x = -offset; x < width; x += 48) {
297
+ world.fillRect(x + 11, ground + 8, 6, 1.5);
298
+ world.fillRect(x + 33, ground + 14, 3, 1.5);
299
+ }
300
+ world.fillStyle(colors.secondary, 1);
301
+ for (const cactus of rules.cacti) {
302
+ const top = ground - cactus.height;
303
+ world.fillRoundedRect(cactus.x, top, cactus.width, cactus.height + 1, Math.min(6, cactus.width / 2));
304
+ if (cactus.height > 34) {
305
+ const arm = Math.max(4, Math.round(cactus.width / 3));
306
+ world.fillRoundedRect(cactus.x - arm + 1, top + 12, arm, 12, 2);
307
+ world.fillRoundedRect(cactus.x + cactus.width - 1, top + 18, arm, 10, 2);
308
+ }
309
+ }
310
+ }
311
+ };
312
+ }
313
+
314
+ function Page() {
315
+ const words = pi.ui.language.startsWith('zh') ? WORDS.zh : WORDS.en;
316
+ const touch = pi.ui.useDevice().touch;
317
+ const [best, setBest] = React.useState(0);
318
+ const kept = React.useRef({ best: 0, words, touch });
319
+ kept.current = { ...kept.current, words, touch };
320
+ React.useEffect(() => {
321
+ let live = true;
322
+ const read = () =>
323
+ pi.storage.get(BEST_KEY).then(value => {
324
+ if (live && typeof value === 'number') {
325
+ kept.current.best = value;
326
+ setBest(value);
327
+ }
328
+ });
329
+ read();
330
+ const off = pi.storage.onChange(keys => keys.includes(BEST_KEY) && read());
331
+ return () => {
332
+ live = false;
333
+ off();
334
+ };
335
+ }, []);
336
+ const config = React.useCallback((Phaser, _page) => {
337
+ // Drawn at twice the world on a screen of two, so that lines and text stay sharp.
338
+ const zoom = Math.max(1, Math.min(2, Math.round(globalThis.devicePixelRatio || 1)));
339
+ const hooks = {
340
+ over: score => {
341
+ if (score > kept.current.best) {
342
+ kept.current.best = score;
343
+ setBest(score);
344
+ pi.storage.set(BEST_KEY, score);
345
+ }
346
+ },
347
+ words: () => {
348
+ const { words, touch, best } = kept.current;
349
+ return {
350
+ start: touch ? words.startTouch : words.startKey,
351
+ over: words.over,
352
+ best: words.best.replace('{score}', best),
353
+ };
354
+ },
355
+ };
356
+ return {
357
+ width: RUN_TUNING.width * zoom,
358
+ height: RUN_TUNING.height * zoom,
359
+ // A finger sliding over the game scrolls the page: Phaser leaves its touches alone.
360
+ input: { touch: { capture: false } },
361
+ scene: makeScene(Phaser, hooks, zoom),
362
+ };
363
+ }, []);
364
+ const Trophy = pi.ui.icons.Trophy;
365
+ return h(
366
+ 'div',
367
+ { className: 'dnr-scroll' },
368
+ h(
369
+ 'div',
370
+ { className: 'dnr' },
371
+ h(
372
+ 'div',
373
+ { className: 'dnr-head' },
374
+ // The bar of the page shows its title: here the best score only.
375
+ h('div', { className: 'dnr-title' }),
376
+ h('div', { className: 'dnr-best' }, Trophy ? h(Trophy, { size: 14 }) : null, words.best.replace('{score}', best)),
377
+ ),
378
+ h('div', { className: 'dnr-stage' }, h(pi.ui.Game, { config })),
379
+ ),
380
+ );
381
+ }
382
+
383
+ pi.registerView('dino-run', {
384
+ title: () => (pi.ui.language.startsWith('zh') ? WORDS.zh.title : WORDS.en.title),
385
+ icon: pi.ui.icons.Gamepad2,
386
+ render: Page,
387
+ });
388
+ return () => style.remove();
389
+ }
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "stack",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "description": "Stack, a 3D game: drop the sliding blocks on the tower, what hangs over is cut off",
6
+ "pi": {
7
+ "title": {
8
+ "en": "Stack",
9
+ "zh-CN": "叠方块"
10
+ },
11
+ "summary": {
12
+ "en": "A 3D game: tap to drop the block that slides over the tower; what hangs over is cut off.",
13
+ "zh-CN": "3D 小游戏:轻点放下在塔顶滑动的方块,伸出去的部分会被切掉。"
14
+ },
15
+ "icon": "Gamepad2",
16
+ "category": "game",
17
+ "webActivation": "open",
18
+ "web": [
19
+ "./web.js"
20
+ ]
21
+ }
22
+ }