@pix3/cli 1.6.0 → 1.6.3

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 (126) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +788 -754
  3. package/dist/index.js +208 -203
  4. package/dist/runtime-types.json +1 -1
  5. package/dist/smoke/prebuilt/{chunk-J2SJVMDO.mjs → chunk-CBFYKDS7.mjs} +1 -1
  6. package/dist/smoke/prebuilt/chunk-VJHJ5BCO.mjs +239 -0
  7. package/dist/smoke/prebuilt/runtime.mjs +1 -1
  8. package/dist/smoke/prebuilt/smoke-worker.mjs +1 -1
  9. package/dist/smoke/prebuilt/tree-defaults.mjs +1 -1
  10. package/dist/validate/prebuilt/bundle-main.js +13 -13
  11. package/dist/validate/prebuilt/chunk-CFQFEI52.js +239 -0
  12. package/dist/validate/prebuilt/runtime.js +1 -1
  13. package/kit/files/.claude/skills/pix3-nodes/SKILL.md +210 -210
  14. package/kit/files/.claude/skills/pix3-nodes/reference.md +39 -24
  15. package/kit/files/.claude/skills/pix3-scene-format/SKILL.md +238 -235
  16. package/kit/files/.claude/skills/pix3-scene-format/reference.md +43 -28
  17. package/kit/files/.claude/skills/pix3-scripts/SKILL.md +285 -281
  18. package/kit/files/.claude/skills/pix3-scripts/reference.md +69 -52
  19. package/kit/files/.claude/skills/pix3-verify/SKILL.md +298 -256
  20. package/kit/files/.claude/skills/pix3-verify/reference.md +21 -21
  21. package/kit/files/AGENTS.md +203 -203
  22. package/kit/files/CLAUDE.md +1 -1
  23. package/kit/kit.json +2 -2
  24. package/package.json +54 -54
  25. package/templates/empty-2d/files/README.md +20 -20
  26. package/templates/empty-2d/files/scenes/main.pix3scene +68 -68
  27. package/templates/empty-2d/template.yaml +7 -7
  28. package/templates/empty-3d/files/README.md +20 -20
  29. package/templates/empty-3d/files/scenes/main.pix3scene +108 -108
  30. package/templates/empty-3d/template.yaml +7 -7
  31. package/templates/idea-blank/files/README.md +22 -22
  32. package/templates/idea-blank/files/scenes/main.pix3scene +37 -37
  33. package/templates/idea-blank/template.yaml +10 -10
  34. package/templates/minigame-2d/files/README.md +44 -44
  35. package/templates/minigame-2d/files/design/tests/reachability.json +1 -1
  36. package/templates/minigame-2d/files/design/tests/routines/mute-music.json +44 -44
  37. package/templates/minigame-2d/files/scenes/main.pix3scene +84 -84
  38. package/templates/minigame-2d/files/scenes/menu.pix3scene +118 -118
  39. package/templates/minigame-2d/files/scenes/ui/settings-window.pix3scene +138 -138
  40. package/templates/minigame-2d/files/scripts/GameFlow.ts +133 -133
  41. package/templates/minigame-2d/files/scripts/MenuFlow.ts +217 -217
  42. package/templates/minigame-2d/files/scripts/SettingsWindow.ts +198 -198
  43. package/templates/minigame-2d/template.yaml +10 -10
  44. package/templates/playable-2d/files/README.md +30 -30
  45. package/templates/playable-2d/files/design/recipe.md +83 -83
  46. package/templates/playable-2d/files/design/tests/reachability.json +4 -4
  47. package/templates/playable-2d/files/design/tests/routines/intro-to-cta.json +32 -32
  48. package/templates/playable-2d/files/scenes/main.pix3scene +111 -111
  49. package/templates/playable-2d/files/scenes/ui/end-screen.pix3scene +91 -91
  50. package/templates/playable-2d/files/scenes/ui/intro.pix3scene +65 -65
  51. package/templates/playable-2d/files/scripts/GameFlow.ts +256 -256
  52. package/templates/playable-2d/template.yaml +9 -9
  53. package/templates/playable-3d/files/README.md +32 -32
  54. package/templates/playable-3d/files/design/recipe.md +127 -127
  55. package/templates/playable-3d/files/design/tests/reachability.json +4 -4
  56. package/templates/playable-3d/files/design/tests/routines/intro-to-cta.json +32 -32
  57. package/templates/playable-3d/files/scenes/main.pix3scene +230 -230
  58. package/templates/playable-3d/files/scenes/ui/end-screen.pix3scene +91 -91
  59. package/templates/playable-3d/files/scenes/ui/intro.pix3scene +65 -65
  60. package/templates/playable-3d/files/scripts/GameFlow.ts +309 -309
  61. package/templates/playable-3d/files/scripts/GameRules.ts +290 -290
  62. package/templates/playable-3d/files/scripts/ScoreHud.ts +113 -113
  63. package/templates/playable-3d/template.yaml +9 -9
  64. package/templates/recipe-arena-2d/files/README.md +48 -48
  65. package/templates/recipe-arena-2d/files/design/recipe.md +127 -127
  66. package/templates/recipe-arena-2d/files/scenes/main.pix3scene +292 -292
  67. package/templates/recipe-arena-2d/files/scenes/menu.pix3scene +100 -100
  68. package/templates/recipe-arena-2d/files/scenes/prefabs/hazard.pix3scene +42 -42
  69. package/templates/recipe-arena-2d/files/scenes/prefabs/pickup.pix3scene +45 -45
  70. package/templates/recipe-arena-2d/files/scenes/ui/result.pix3scene +85 -85
  71. package/templates/recipe-arena-2d/files/scripts/GameRules.ts +368 -368
  72. package/templates/recipe-arena-2d/files/scripts/MenuFlow.ts +106 -106
  73. package/templates/recipe-arena-2d/files/scripts/PlayerController.ts +151 -151
  74. package/templates/recipe-arena-2d/files/scripts/ScoreHud.ts +94 -94
  75. package/templates/recipe-arena-2d/files/scripts/Spawner.ts +214 -214
  76. package/templates/recipe-arena-2d/files/scripts/TouchRules.ts +174 -174
  77. package/templates/recipe-arena-2d/template.yaml +10 -10
  78. package/templates/recipe-blank-2d/files/README.md +61 -61
  79. package/templates/recipe-blank-2d/files/design/recipe.md +148 -148
  80. package/templates/recipe-blank-2d/files/design/tests/reachability.json +4 -4
  81. package/templates/recipe-blank-2d/files/design/tests/routines/restart.json +27 -27
  82. package/templates/recipe-blank-2d/files/scenes/main.pix3scene +224 -224
  83. package/templates/recipe-blank-2d/files/scenes/ui/result.pix3scene +85 -85
  84. package/templates/recipe-blank-2d/files/scripts/GameRules.ts +350 -350
  85. package/templates/recipe-blank-2d/files/scripts/ScoreHud.ts +110 -110
  86. package/templates/recipe-blank-2d/template.yaml +10 -10
  87. package/templates/recipe-bouncer-2d/files/README.md +81 -81
  88. package/templates/recipe-bouncer-2d/files/design/recipe.md +146 -146
  89. package/templates/recipe-bouncer-2d/files/scenes/main.pix3scene +615 -615
  90. package/templates/recipe-bouncer-2d/files/scenes/menu.pix3scene +191 -191
  91. package/templates/recipe-bouncer-2d/files/scenes/ui/result.pix3scene +87 -87
  92. package/templates/recipe-bouncer-2d/files/scripts/BallBody.ts +298 -298
  93. package/templates/recipe-bouncer-2d/files/scripts/GameRules.ts +377 -377
  94. package/templates/recipe-bouncer-2d/files/scripts/MenuFlow.ts +109 -109
  95. package/templates/recipe-bouncer-2d/files/scripts/PaddleController.ts +112 -112
  96. package/templates/recipe-bouncer-2d/files/scripts/ScoreHud.ts +94 -94
  97. package/templates/recipe-bouncer-2d/files/scripts/TouchRules.ts +263 -263
  98. package/templates/recipe-bouncer-2d/files/scripts/ball-collision.ts +307 -307
  99. package/templates/recipe-bouncer-2d/template.yaml +10 -10
  100. package/templates/recipe-grid-3d/files/README.md +22 -22
  101. package/templates/recipe-grid-3d/files/design/recipe.md +110 -110
  102. package/templates/recipe-grid-3d/files/scenes/main.pix3scene +169 -169
  103. package/templates/recipe-grid-3d/files/scenes/menu.pix3scene +100 -100
  104. package/templates/recipe-grid-3d/files/scenes/ui/result.pix3scene +105 -105
  105. package/templates/recipe-grid-3d/files/scripts/GridBoard.ts +319 -319
  106. package/templates/recipe-grid-3d/files/scripts/GridRules.ts +278 -278
  107. package/templates/recipe-grid-3d/files/scripts/MenuFlow.ts +106 -106
  108. package/templates/recipe-grid-3d/files/scripts/ScoreHud.ts +94 -94
  109. package/templates/recipe-grid-3d/template.yaml +10 -10
  110. package/templates/recipe-tapper-2d/files/README.md +39 -39
  111. package/templates/recipe-tapper-2d/files/design/recipe.md +132 -132
  112. package/templates/recipe-tapper-2d/files/design/tests/reachability.json +4 -4
  113. package/templates/recipe-tapper-2d/files/design/tests/routines/terminal-retry.json +23 -23
  114. package/templates/recipe-tapper-2d/files/scenes/main.pix3scene +269 -269
  115. package/templates/recipe-tapper-2d/files/scenes/menu.pix3scene +100 -100
  116. package/templates/recipe-tapper-2d/files/scenes/prefabs/hazard.pix3scene +42 -42
  117. package/templates/recipe-tapper-2d/files/scenes/prefabs/target.pix3scene +45 -45
  118. package/templates/recipe-tapper-2d/files/scenes/ui/result.pix3scene +85 -85
  119. package/templates/recipe-tapper-2d/files/scripts/GameRules.ts +368 -368
  120. package/templates/recipe-tapper-2d/files/scripts/MenuFlow.ts +106 -106
  121. package/templates/recipe-tapper-2d/files/scripts/ScoreHud.ts +94 -94
  122. package/templates/recipe-tapper-2d/files/scripts/Spawner.ts +214 -214
  123. package/templates/recipe-tapper-2d/files/scripts/TouchRules.ts +192 -192
  124. package/templates/recipe-tapper-2d/template.yaml +10 -10
  125. package/dist/smoke/prebuilt/chunk-OJXQXIIQ.mjs +0 -239
  126. package/dist/validate/prebuilt/chunk-BS2OZM7U.js +0 -239
@@ -1,106 +1,106 @@
1
- /**
2
- * MenuFlow — drives the menu scene (the project's entry/export scene).
3
- *
4
- * PLAY transitions to the game scene with a fade. That is all it does: keep the
5
- * menu dumb so the game scene stays directly playable on its own (agents and
6
- * the editor open `main.pix3scene` and press play without going through here).
7
- *
8
- * PLAY does not call `startGame()` directly: it dispatches the command
9
- * `start-game` (`scene.commands`). A test can then leave the menu without a tap,
10
- * every raised intent lands in `scene.commands.log` with its frame, and one real
11
- * tap is enough to prove the button is still wired to the intent it claims.
12
- *
13
- * It registers a debug provider too. `registerGameDebug` is global and
14
- * last-wins, and only the running scene's flow script registers one — so
15
- * without this, everything a tool reads while the menu is up would come back
16
- * empty (or, worse, stale from the previous scene).
17
- */
18
- import { Script, registerGameDebug, type PropertySchema } from '@pix3/runtime';
19
-
20
- export class MenuFlow extends Script {
21
- private playButtonReady = false;
22
- private disposeDebug: (() => void) | null = null;
23
- private disposeCommands: (() => void)[] = [];
24
-
25
- constructor(id: string, type: string) {
26
- super(id, type);
27
- this.config = {
28
- gameScene: 'res://scenes/main.pix3scene',
29
- playButton: 'play-button',
30
- };
31
- }
32
-
33
- static getPropertySchema(): PropertySchema {
34
- const str = (name: string, label: string) => ({
35
- name,
36
- type: 'string' as const,
37
- ui: { label, group: 'Menu' },
38
- getValue: (s: unknown) => (s as MenuFlow).config[name],
39
- setValue: (s: unknown, v: unknown) => {
40
- (s as MenuFlow).config[name] = typeof v === 'string' ? v : '';
41
- },
42
- });
43
-
44
- return {
45
- nodeType: 'MenuFlow',
46
- properties: [str('gameScene', 'Game Scene'), str('playButton', 'Play Button')],
47
- groups: { Menu: { label: 'Menu Flow', expanded: true } },
48
- };
49
- }
50
-
51
- onStart(): void {
52
- const commands = this.scene?.commands;
53
- this.disposeCommands = [
54
- commands?.register('start-game', () => this.startGame(), {
55
- description: 'Leave the menu and transition into the game scene.',
56
- }),
57
- ].filter((dispose): dispose is () => void => dispose !== undefined);
58
-
59
- this.disposeDebug = registerGameDebug({
60
- name: 'recipe-arena-2d',
61
- version: 1,
62
- // The registry IS the action list — never a second, hand-kept copy.
63
- actions: () => this.scene?.commands.list().map(command => command.name) ?? [],
64
- // `scene` is what tells a reader which flow answered: the game scene's
65
- // GameRules publishes under the same name.
66
- snapshot: () => ({
67
- scene: 'menu',
68
- gameScene: String(this.config.gameScene ?? ''),
69
- playButtonReady: this.playButtonReady,
70
- }),
71
- });
72
-
73
- const button = this.findNode(String(this.config.playButton ?? ''));
74
- if (!button) {
75
- console.warn(`[MenuFlow] Button "${this.config.playButton}" not found.`);
76
- return;
77
- }
78
- this.playButtonReady = true;
79
- button.connect('pressed', this, () => {
80
- this.scene?.commands.dispatch('start-game');
81
- });
82
- }
83
-
84
- onDetach(): void {
85
- // Safe in any order: the disposer clears the global only while it still
86
- // holds *this* provider, so the next scene's registration is never wiped.
87
- this.disposeDebug?.();
88
- this.disposeDebug = null;
89
- // The scene's registry is cleared on stop anyway; this covers the other
90
- // case — this script being detached while the scene keeps running.
91
- for (const dispose of this.disposeCommands) {
92
- dispose();
93
- }
94
- this.disposeCommands = [];
95
- this.playButtonReady = false;
96
- super.onDetach();
97
- }
98
-
99
- /** Transition into the game scene. */
100
- startGame(): void {
101
- void this.scene?.changeScene(String(this.config.gameScene ?? ''), {
102
- transition: 'fade',
103
- durationSec: 0.3,
104
- });
105
- }
106
- }
1
+ /**
2
+ * MenuFlow — drives the menu scene (the project's entry/export scene).
3
+ *
4
+ * PLAY transitions to the game scene with a fade. That is all it does: keep the
5
+ * menu dumb so the game scene stays directly playable on its own (agents and
6
+ * the editor open `main.pix3scene` and press play without going through here).
7
+ *
8
+ * PLAY does not call `startGame()` directly: it dispatches the command
9
+ * `start-game` (`scene.commands`). A test can then leave the menu without a tap,
10
+ * every raised intent lands in `scene.commands.log` with its frame, and one real
11
+ * tap is enough to prove the button is still wired to the intent it claims.
12
+ *
13
+ * It registers a debug provider too. `registerGameDebug` is global and
14
+ * last-wins, and only the running scene's flow script registers one — so
15
+ * without this, everything a tool reads while the menu is up would come back
16
+ * empty (or, worse, stale from the previous scene).
17
+ */
18
+ import { Script, registerGameDebug, type PropertySchema } from '@pix3/runtime';
19
+
20
+ export class MenuFlow extends Script {
21
+ private playButtonReady = false;
22
+ private disposeDebug: (() => void) | null = null;
23
+ private disposeCommands: (() => void)[] = [];
24
+
25
+ constructor(id: string, type: string) {
26
+ super(id, type);
27
+ this.config = {
28
+ gameScene: 'res://scenes/main.pix3scene',
29
+ playButton: 'play-button',
30
+ };
31
+ }
32
+
33
+ static getPropertySchema(): PropertySchema {
34
+ const str = (name: string, label: string) => ({
35
+ name,
36
+ type: 'string' as const,
37
+ ui: { label, group: 'Menu' },
38
+ getValue: (s: unknown) => (s as MenuFlow).config[name],
39
+ setValue: (s: unknown, v: unknown) => {
40
+ (s as MenuFlow).config[name] = typeof v === 'string' ? v : '';
41
+ },
42
+ });
43
+
44
+ return {
45
+ nodeType: 'MenuFlow',
46
+ properties: [str('gameScene', 'Game Scene'), str('playButton', 'Play Button')],
47
+ groups: { Menu: { label: 'Menu Flow', expanded: true } },
48
+ };
49
+ }
50
+
51
+ onStart(): void {
52
+ const commands = this.scene?.commands;
53
+ this.disposeCommands = [
54
+ commands?.register('start-game', () => this.startGame(), {
55
+ description: 'Leave the menu and transition into the game scene.',
56
+ }),
57
+ ].filter((dispose): dispose is () => void => dispose !== undefined);
58
+
59
+ this.disposeDebug = registerGameDebug({
60
+ name: 'recipe-arena-2d',
61
+ version: 1,
62
+ // The registry IS the action list — never a second, hand-kept copy.
63
+ actions: () => this.scene?.commands.list().map(command => command.name) ?? [],
64
+ // `scene` is what tells a reader which flow answered: the game scene's
65
+ // GameRules publishes under the same name.
66
+ snapshot: () => ({
67
+ scene: 'menu',
68
+ gameScene: String(this.config.gameScene ?? ''),
69
+ playButtonReady: this.playButtonReady,
70
+ }),
71
+ });
72
+
73
+ const button = this.findNode(String(this.config.playButton ?? ''));
74
+ if (!button) {
75
+ console.warn(`[MenuFlow] Button "${this.config.playButton}" not found.`);
76
+ return;
77
+ }
78
+ this.playButtonReady = true;
79
+ button.connect('pressed', this, () => {
80
+ this.scene?.commands.dispatch('start-game');
81
+ });
82
+ }
83
+
84
+ onDetach(): void {
85
+ // Safe in any order: the disposer clears the global only while it still
86
+ // holds *this* provider, so the next scene's registration is never wiped.
87
+ this.disposeDebug?.();
88
+ this.disposeDebug = null;
89
+ // The scene's registry is cleared on stop anyway; this covers the other
90
+ // case — this script being detached while the scene keeps running.
91
+ for (const dispose of this.disposeCommands) {
92
+ dispose();
93
+ }
94
+ this.disposeCommands = [];
95
+ this.playButtonReady = false;
96
+ super.onDetach();
97
+ }
98
+
99
+ /** Transition into the game scene. */
100
+ startGame(): void {
101
+ void this.scene?.changeScene(String(this.config.gameScene ?? ''), {
102
+ transition: 'fade',
103
+ durationSec: 0.3,
104
+ });
105
+ }
106
+ }
@@ -1,151 +1,151 @@
1
- /**
2
- * PlayerController — steers the avatar inside a bounded field.
3
- *
4
- * Two locomotion modes, chosen in the inspector:
5
- * - `pointer` — the avatar chases the pointer/finger at `speed` px/s.
6
- * - `keys` — 8-direction WASD / arrow movement at `speed` px/s.
7
- *
8
- * The avatar is clamped to `boundsNode` (a Group2D — normally the node it is
9
- * parented to) inset by `radius`, so it can never leave the field.
10
- *
11
- * This file is deliberately the ONLY thing that decides how the avatar moves:
12
- * swap it wholesale for a grid stepper, a jumper or an auto-runner and the rest
13
- * of the recipe (spawners, touch rules, HUD, win/lose) keeps working unchanged.
14
- */
15
- import { Group2D, Script, type PropertySchema } from '@pix3/runtime';
16
-
17
- type LocomotionMode = 'pointer' | 'keys';
18
-
19
- export class PlayerController extends Script {
20
- constructor(id: string, type: string) {
21
- super(id, type);
22
- this.config = {
23
- // 'pointer' = follow the finger/mouse, 'keys' = 8-direction WASD/arrows.
24
- mode: 'pointer',
25
- // Movement speed in logical pixels per second.
26
- speed: 620,
27
- // Collision/inset radius: keeps the avatar fully inside the field.
28
- radius: 56,
29
- // Node id/name of the Group2D whose rect bounds the avatar.
30
- boundsNode: 'board',
31
- // Pointer mode only: stop chasing once this close (px) to avoid jitter.
32
- deadZone: 4,
33
- };
34
- }
35
-
36
- static getPropertySchema(): PropertySchema {
37
- const numberProp = (name: string, label: string, min: number, max: number, step = 1) => ({
38
- name,
39
- type: 'number' as const,
40
- ui: { label, group: 'Movement', min, max, step, slider: true },
41
- getValue: (s: unknown) => (s as PlayerController).config[name],
42
- setValue: (s: unknown, v: unknown) => {
43
- const n = Number(v);
44
- (s as PlayerController).config[name] = Math.min(max, Math.max(min, Number.isFinite(n) ? n : min));
45
- },
46
- });
47
-
48
- return {
49
- nodeType: 'PlayerController',
50
- properties: [
51
- {
52
- name: 'mode',
53
- type: 'select',
54
- ui: {
55
- label: 'Mode',
56
- description: 'pointer = follow the finger, keys = 8-direction WASD/arrows',
57
- group: 'Movement',
58
- options: ['pointer', 'keys'],
59
- },
60
- getValue: s => (s as PlayerController).config.mode,
61
- setValue: (s, v) => {
62
- (s as PlayerController).config.mode = v === 'keys' ? 'keys' : 'pointer';
63
- },
64
- },
65
- numberProp('speed', 'Speed (px/s)', 60, 2000, 10),
66
- numberProp('radius', 'Radius', 4, 400, 1),
67
- numberProp('deadZone', 'Dead Zone', 0, 60, 1),
68
- {
69
- name: 'boundsNode',
70
- type: 'string',
71
- ui: { label: 'Bounds Node', group: 'Movement' },
72
- getValue: s => (s as PlayerController).config.boundsNode,
73
- setValue: (s, v) => {
74
- (s as PlayerController).config.boundsNode = typeof v === 'string' ? v : '';
75
- },
76
- },
77
- ],
78
- groups: { Movement: { label: 'Movement', expanded: true } },
79
- };
80
- }
81
-
82
- onUpdate(dt: number): void {
83
- const node = this.node;
84
- if (!node || dt <= 0) {
85
- return;
86
- }
87
- const speed = Math.max(0, Number(this.config.speed) || 0);
88
- const mode: LocomotionMode = this.config.mode === 'keys' ? 'keys' : 'pointer';
89
-
90
- if (mode === 'keys') {
91
- const ax = (this.held('Key_ArrowRight', 'Key_D') ? 1 : 0) - (this.held('Key_ArrowLeft', 'Key_A') ? 1 : 0);
92
- const ay = (this.held('Key_ArrowUp', 'Key_W') ? 1 : 0) - (this.held('Key_ArrowDown', 'Key_S') ? 1 : 0);
93
- const len = Math.hypot(ax, ay);
94
- if (len > 0) {
95
- node.position.x += (ax / len) * speed * dt;
96
- node.position.y += (ay / len) * speed * dt;
97
- }
98
- } else {
99
- const pointer = this.scene?.getPointer2DWorldPosition() ?? null;
100
- if (pointer) {
101
- const target = this.worldToParent(pointer.x, pointer.y);
102
- const dx = target.x - node.position.x;
103
- const dy = target.y - node.position.y;
104
- const dist = Math.hypot(dx, dy);
105
- const deadZone = Math.max(0, Number(this.config.deadZone) || 0);
106
- if (dist > deadZone) {
107
- const step = Math.min(dist, speed * dt);
108
- node.position.x += (dx / dist) * step;
109
- node.position.y += (dy / dist) * step;
110
- }
111
- }
112
- }
113
-
114
- this.clampToBounds();
115
- }
116
-
117
- private held(...buttons: string[]): boolean {
118
- return buttons.some(name => this.input?.getButton(name) ?? false);
119
- }
120
-
121
- /** Convert a 2D world point into the coordinate space of this node's parent. */
122
- private worldToParent(wx: number, wy: number): { x: number; y: number } {
123
- const parent = this.node?.parentNode ?? null;
124
- if (!parent) {
125
- return { x: wx, y: wy };
126
- }
127
- parent.updateWorldMatrix(true, false);
128
- const e = parent.matrixWorld.elements;
129
- const dx = wx - e[12];
130
- const dy = wy - e[13];
131
- const det = e[0] * e[5] - e[1] * e[4];
132
- if (Math.abs(det) < 1e-8) {
133
- return { x: dx, y: dy };
134
- }
135
- return { x: (e[5] * dx - e[4] * dy) / det, y: (e[0] * dy - e[1] * dx) / det };
136
- }
137
-
138
- /** Keep the avatar inside the bounds node's rect, inset by `radius`. */
139
- private clampToBounds(): void {
140
- const node = this.node;
141
- const bounds = this.findNode(String(this.config.boundsNode ?? ''));
142
- if (!node || !(bounds instanceof Group2D)) {
143
- return;
144
- }
145
- const radius = Math.max(0, Number(this.config.radius) || 0);
146
- const halfW = Math.max(0, bounds.width / 2 - radius);
147
- const halfH = Math.max(0, bounds.height / 2 - radius);
148
- node.position.x = Math.min(halfW, Math.max(-halfW, node.position.x));
149
- node.position.y = Math.min(halfH, Math.max(-halfH, node.position.y));
150
- }
151
- }
1
+ /**
2
+ * PlayerController — steers the avatar inside a bounded field.
3
+ *
4
+ * Two locomotion modes, chosen in the inspector:
5
+ * - `pointer` — the avatar chases the pointer/finger at `speed` px/s.
6
+ * - `keys` — 8-direction WASD / arrow movement at `speed` px/s.
7
+ *
8
+ * The avatar is clamped to `boundsNode` (a Group2D — normally the node it is
9
+ * parented to) inset by `radius`, so it can never leave the field.
10
+ *
11
+ * This file is deliberately the ONLY thing that decides how the avatar moves:
12
+ * swap it wholesale for a grid stepper, a jumper or an auto-runner and the rest
13
+ * of the recipe (spawners, touch rules, HUD, win/lose) keeps working unchanged.
14
+ */
15
+ import { Group2D, Script, type PropertySchema } from '@pix3/runtime';
16
+
17
+ type LocomotionMode = 'pointer' | 'keys';
18
+
19
+ export class PlayerController extends Script {
20
+ constructor(id: string, type: string) {
21
+ super(id, type);
22
+ this.config = {
23
+ // 'pointer' = follow the finger/mouse, 'keys' = 8-direction WASD/arrows.
24
+ mode: 'pointer',
25
+ // Movement speed in logical pixels per second.
26
+ speed: 620,
27
+ // Collision/inset radius: keeps the avatar fully inside the field.
28
+ radius: 56,
29
+ // Node id/name of the Group2D whose rect bounds the avatar.
30
+ boundsNode: 'board',
31
+ // Pointer mode only: stop chasing once this close (px) to avoid jitter.
32
+ deadZone: 4,
33
+ };
34
+ }
35
+
36
+ static getPropertySchema(): PropertySchema {
37
+ const numberProp = (name: string, label: string, min: number, max: number, step = 1) => ({
38
+ name,
39
+ type: 'number' as const,
40
+ ui: { label, group: 'Movement', min, max, step, slider: true },
41
+ getValue: (s: unknown) => (s as PlayerController).config[name],
42
+ setValue: (s: unknown, v: unknown) => {
43
+ const n = Number(v);
44
+ (s as PlayerController).config[name] = Math.min(max, Math.max(min, Number.isFinite(n) ? n : min));
45
+ },
46
+ });
47
+
48
+ return {
49
+ nodeType: 'PlayerController',
50
+ properties: [
51
+ {
52
+ name: 'mode',
53
+ type: 'select',
54
+ ui: {
55
+ label: 'Mode',
56
+ description: 'pointer = follow the finger, keys = 8-direction WASD/arrows',
57
+ group: 'Movement',
58
+ options: ['pointer', 'keys'],
59
+ },
60
+ getValue: s => (s as PlayerController).config.mode,
61
+ setValue: (s, v) => {
62
+ (s as PlayerController).config.mode = v === 'keys' ? 'keys' : 'pointer';
63
+ },
64
+ },
65
+ numberProp('speed', 'Speed (px/s)', 60, 2000, 10),
66
+ numberProp('radius', 'Radius', 4, 400, 1),
67
+ numberProp('deadZone', 'Dead Zone', 0, 60, 1),
68
+ {
69
+ name: 'boundsNode',
70
+ type: 'string',
71
+ ui: { label: 'Bounds Node', group: 'Movement' },
72
+ getValue: s => (s as PlayerController).config.boundsNode,
73
+ setValue: (s, v) => {
74
+ (s as PlayerController).config.boundsNode = typeof v === 'string' ? v : '';
75
+ },
76
+ },
77
+ ],
78
+ groups: { Movement: { label: 'Movement', expanded: true } },
79
+ };
80
+ }
81
+
82
+ onUpdate(dt: number): void {
83
+ const node = this.node;
84
+ if (!node || dt <= 0) {
85
+ return;
86
+ }
87
+ const speed = Math.max(0, Number(this.config.speed) || 0);
88
+ const mode: LocomotionMode = this.config.mode === 'keys' ? 'keys' : 'pointer';
89
+
90
+ if (mode === 'keys') {
91
+ const ax = (this.held('Key_ArrowRight', 'Key_D') ? 1 : 0) - (this.held('Key_ArrowLeft', 'Key_A') ? 1 : 0);
92
+ const ay = (this.held('Key_ArrowUp', 'Key_W') ? 1 : 0) - (this.held('Key_ArrowDown', 'Key_S') ? 1 : 0);
93
+ const len = Math.hypot(ax, ay);
94
+ if (len > 0) {
95
+ node.position.x += (ax / len) * speed * dt;
96
+ node.position.y += (ay / len) * speed * dt;
97
+ }
98
+ } else {
99
+ const pointer = this.scene?.getPointer2DWorldPosition() ?? null;
100
+ if (pointer) {
101
+ const target = this.worldToParent(pointer.x, pointer.y);
102
+ const dx = target.x - node.position.x;
103
+ const dy = target.y - node.position.y;
104
+ const dist = Math.hypot(dx, dy);
105
+ const deadZone = Math.max(0, Number(this.config.deadZone) || 0);
106
+ if (dist > deadZone) {
107
+ const step = Math.min(dist, speed * dt);
108
+ node.position.x += (dx / dist) * step;
109
+ node.position.y += (dy / dist) * step;
110
+ }
111
+ }
112
+ }
113
+
114
+ this.clampToBounds();
115
+ }
116
+
117
+ private held(...buttons: string[]): boolean {
118
+ return buttons.some(name => this.input?.getButton(name) ?? false);
119
+ }
120
+
121
+ /** Convert a 2D world point into the coordinate space of this node's parent. */
122
+ private worldToParent(wx: number, wy: number): { x: number; y: number } {
123
+ const parent = this.node?.parentNode ?? null;
124
+ if (!parent) {
125
+ return { x: wx, y: wy };
126
+ }
127
+ parent.updateWorldMatrix(true, false);
128
+ const e = parent.matrixWorld.elements;
129
+ const dx = wx - e[12];
130
+ const dy = wy - e[13];
131
+ const det = e[0] * e[5] - e[1] * e[4];
132
+ if (Math.abs(det) < 1e-8) {
133
+ return { x: dx, y: dy };
134
+ }
135
+ return { x: (e[5] * dx - e[4] * dy) / det, y: (e[0] * dy - e[1] * dx) / det };
136
+ }
137
+
138
+ /** Keep the avatar inside the bounds node's rect, inset by `radius`. */
139
+ private clampToBounds(): void {
140
+ const node = this.node;
141
+ const bounds = this.findNode(String(this.config.boundsNode ?? ''));
142
+ if (!node || !(bounds instanceof Group2D)) {
143
+ return;
144
+ }
145
+ const radius = Math.max(0, Number(this.config.radius) || 0);
146
+ const halfW = Math.max(0, bounds.width / 2 - radius);
147
+ const halfH = Math.max(0, bounds.height / 2 - radius);
148
+ node.position.x = Math.min(halfW, Math.max(-halfW, node.position.x));
149
+ node.position.y = Math.min(halfH, Math.max(-halfH, node.position.y));
150
+ }
151
+ }