create-threenative 0.2.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 (320) hide show
  1. package/README.md +16 -0
  2. package/dist/index.d.ts +30 -0
  3. package/dist/index.js +283 -0
  4. package/dist/threenative.d.ts +6 -0
  5. package/dist/threenative.js +757 -0
  6. package/package.json +44 -0
  7. package/templates/action-rpg/.mcp.json +16 -0
  8. package/templates/action-rpg/AGENTS.md +40 -0
  9. package/templates/action-rpg/CLAUDE.md +42 -0
  10. package/templates/action-rpg/index.html +13 -0
  11. package/templates/action-rpg/kit.json +7 -0
  12. package/templates/action-rpg/package.json +47 -0
  13. package/templates/action-rpg/playtests/combat.playtest.json +61 -0
  14. package/templates/action-rpg/playtests/fail.playtest.json +21 -0
  15. package/templates/action-rpg/playtests/inventory.playtest.json +111 -0
  16. package/templates/action-rpg/playtests/progress.playtest.json +99 -0
  17. package/templates/action-rpg/playtests/survives.playtest.json +32 -0
  18. package/templates/action-rpg/playtests/win.playtest.json +30 -0
  19. package/templates/action-rpg/public/favicon.svg +5 -0
  20. package/templates/action-rpg/src/abilities/Ability.ts +45 -0
  21. package/templates/action-rpg/src/entities/Enemy.ts +143 -0
  22. package/templates/action-rpg/src/entities/Player.ts +83 -0
  23. package/templates/action-rpg/src/game.ts +40 -0
  24. package/templates/action-rpg/src/items/Inventory.ts +163 -0
  25. package/templates/action-rpg/src/loot/drops.ts +40 -0
  26. package/templates/action-rpg/src/main.ts +15 -0
  27. package/templates/action-rpg/src/physics.ts +40 -0
  28. package/templates/action-rpg/src/playtest-events.ts +15 -0
  29. package/templates/action-rpg/src/progress.ts +35 -0
  30. package/templates/action-rpg/src/render/camera.ts +26 -0
  31. package/templates/action-rpg/src/render/lighting.ts +30 -0
  32. package/templates/action-rpg/src/render/loading.ts +90 -0
  33. package/templates/action-rpg/src/render/materials.ts +40 -0
  34. package/templates/action-rpg/src/render/palette.ts +9 -0
  35. package/templates/action-rpg/src/render/postprocessing.ts +18 -0
  36. package/templates/action-rpg/src/render/shapes.ts +110 -0
  37. package/templates/action-rpg/src/render/sky.ts +18 -0
  38. package/templates/action-rpg/src/render/types.ts +3 -0
  39. package/templates/action-rpg/src/scenes/Boot.ts +12 -0
  40. package/templates/action-rpg/src/scenes/Play.ts +449 -0
  41. package/templates/action-rpg/src/state.ts +40 -0
  42. package/templates/action-rpg/src/stats/StatBlock.ts +57 -0
  43. package/templates/action-rpg/src/style.css +32 -0
  44. package/templates/action-rpg/src/ui/AbilityBar.tsx +28 -0
  45. package/templates/action-rpg/src/ui/App.tsx +21 -0
  46. package/templates/action-rpg/src/ui/CharacterPanel.tsx +31 -0
  47. package/templates/action-rpg/src/ui/Hud.tsx +29 -0
  48. package/templates/action-rpg/src/ui/InventoryPanel.tsx +37 -0
  49. package/templates/action-rpg/tailwind.config.ts +7 -0
  50. package/templates/action-rpg/threenative.config.ts +29 -0
  51. package/templates/action-rpg/tsconfig.json +17 -0
  52. package/templates/action-rpg/vite.config.ts +8 -0
  53. package/templates/defense/.mcp.json +16 -0
  54. package/templates/defense/AGENTS.md +53 -0
  55. package/templates/defense/CLAUDE.md +55 -0
  56. package/templates/defense/README.md +9 -0
  57. package/templates/defense/index.html +13 -0
  58. package/templates/defense/kit.json +7 -0
  59. package/templates/defense/package.json +46 -0
  60. package/templates/defense/playtests/leak.playtest.json +18 -0
  61. package/templates/defense/playtests/placement.playtest.json +40 -0
  62. package/templates/defense/playtests/scan.playtest.json +20 -0
  63. package/templates/defense/playtests/survive.playtest.json +26 -0
  64. package/templates/defense/playtests/survives.playtest.json +36 -0
  65. package/templates/defense/public/favicon.svg +5 -0
  66. package/templates/defense/src/attackers/Attacker.ts +104 -0
  67. package/templates/defense/src/board/Route.ts +80 -0
  68. package/templates/defense/src/economy.ts +36 -0
  69. package/templates/defense/src/entities/Player.ts +44 -0
  70. package/templates/defense/src/game.ts +30 -0
  71. package/templates/defense/src/main.ts +15 -0
  72. package/templates/defense/src/physics.ts +43 -0
  73. package/templates/defense/src/placement/Buildable.ts +49 -0
  74. package/templates/defense/src/playtest-events.ts +15 -0
  75. package/templates/defense/src/render/camera.ts +10 -0
  76. package/templates/defense/src/render/lighting.ts +32 -0
  77. package/templates/defense/src/render/loading.ts +89 -0
  78. package/templates/defense/src/render/materials.ts +13 -0
  79. package/templates/defense/src/render/palette.ts +21 -0
  80. package/templates/defense/src/render/postprocessing.ts +14 -0
  81. package/templates/defense/src/render/shapes.ts +89 -0
  82. package/templates/defense/src/render/sky.ts +45 -0
  83. package/templates/defense/src/scenes/Boot.ts +12 -0
  84. package/templates/defense/src/scenes/Defense.ts +165 -0
  85. package/templates/defense/src/state.ts +50 -0
  86. package/templates/defense/src/style.css +32 -0
  87. package/templates/defense/src/towers/Tower.ts +96 -0
  88. package/templates/defense/src/towers/targeting.ts +53 -0
  89. package/templates/defense/src/ui/App.tsx +17 -0
  90. package/templates/defense/src/ui/Hud.tsx +61 -0
  91. package/templates/defense/src/ui/Menu.tsx +7 -0
  92. package/templates/defense/src/waves.ts +40 -0
  93. package/templates/defense/tailwind.config.ts +1 -0
  94. package/templates/defense/threenative.config.ts +29 -0
  95. package/templates/defense/tsconfig.json +17 -0
  96. package/templates/defense/vite.config.ts +5 -0
  97. package/templates/minimal/.mcp.json +16 -0
  98. package/templates/minimal/AGENTS.md +400 -0
  99. package/templates/minimal/CLAUDE.md +402 -0
  100. package/templates/minimal/assets/.gitkeep +0 -0
  101. package/templates/minimal/index.html +14 -0
  102. package/templates/minimal/kit.json +7 -0
  103. package/templates/minimal/package.json +40 -0
  104. package/templates/minimal/playtests/play.playtest.json +32 -0
  105. package/templates/minimal/playtests/survives.playtest.json +32 -0
  106. package/templates/minimal/public/.gitkeep +0 -0
  107. package/templates/minimal/public/favicon.svg +4 -0
  108. package/templates/minimal/public/icon.png +0 -0
  109. package/templates/minimal/src/entities/Player.ts +66 -0
  110. package/templates/minimal/src/game.ts +28 -0
  111. package/templates/minimal/src/main.ts +17 -0
  112. package/templates/minimal/src/render/camera.ts +11 -0
  113. package/templates/minimal/src/render/hud.ts +53 -0
  114. package/templates/minimal/src/render/lighting.ts +46 -0
  115. package/templates/minimal/src/render/loading.ts +113 -0
  116. package/templates/minimal/src/render/materials.ts +16 -0
  117. package/templates/minimal/src/render/palette.ts +9 -0
  118. package/templates/minimal/src/render/postprocessing.ts +20 -0
  119. package/templates/minimal/src/render/sky.ts +41 -0
  120. package/templates/minimal/src/scenes/Boot.ts +5 -0
  121. package/templates/minimal/src/scenes/Play.ts +65 -0
  122. package/templates/minimal/src/state.ts +1 -0
  123. package/templates/minimal/src/style.css +41 -0
  124. package/templates/minimal/threenative.config.ts +32 -0
  125. package/templates/minimal/tsconfig.json +16 -0
  126. package/templates/minimal/vite.config.ts +3 -0
  127. package/templates/platformer/.mcp.json +16 -0
  128. package/templates/platformer/AGENTS.md +199 -0
  129. package/templates/platformer/CLAUDE.md +201 -0
  130. package/templates/platformer/assets/.gitkeep +0 -0
  131. package/templates/platformer/index.html +13 -0
  132. package/templates/platformer/kit.json +7 -0
  133. package/templates/platformer/package.json +50 -0
  134. package/templates/platformer/playtests/avoidance.playtest.json +23 -0
  135. package/templates/platformer/playtests/buffer.playtest.json +17 -0
  136. package/templates/platformer/playtests/chase.playtest.json +21 -0
  137. package/templates/platformer/playtests/collect.playtest.json +56 -0
  138. package/templates/platformer/playtests/collision-layers.playtest.json +36 -0
  139. package/templates/platformer/playtests/coyote.playtest.json +21 -0
  140. package/templates/platformer/playtests/damage.playtest.json +31 -0
  141. package/templates/platformer/playtests/dash.playtest.json +21 -0
  142. package/templates/platformer/playtests/jump.playtest.json +21 -0
  143. package/templates/platformer/playtests/oneway.playtest.json +17 -0
  144. package/templates/platformer/playtests/patrol.playtest.json +13 -0
  145. package/templates/platformer/playtests/performance.playtest.json +21 -0
  146. package/templates/platformer/playtests/physics.playtest.json +14 -0
  147. package/templates/platformer/playtests/respawn.playtest.json +31 -0
  148. package/templates/platformer/playtests/stomp-rise.playtest.json +41 -0
  149. package/templates/platformer/playtests/stomp.playtest.json +34 -0
  150. package/templates/platformer/playtests/survives.playtest.json +32 -0
  151. package/templates/platformer/playtests/terminal-loop-fail.playtest.json +29 -0
  152. package/templates/platformer/playtests/terminal-loop-win.playtest.json +52 -0
  153. package/templates/platformer/public/.gitkeep +0 -0
  154. package/templates/platformer/public/favicon.svg +4 -0
  155. package/templates/platformer/public/icon.png +0 -0
  156. package/templates/platformer/src/entities/Character.ts +218 -0
  157. package/templates/platformer/src/entities/Chaser.ts +118 -0
  158. package/templates/platformer/src/entities/Patrol.ts +88 -0
  159. package/templates/platformer/src/entities/Pickup.ts +72 -0
  160. package/templates/platformer/src/game.ts +27 -0
  161. package/templates/platformer/src/level/Checkpoints.ts +66 -0
  162. package/templates/platformer/src/level/Platform.ts +34 -0
  163. package/templates/platformer/src/main.ts +14 -0
  164. package/templates/platformer/src/playtest-events.ts +15 -0
  165. package/templates/platformer/src/render/camera.ts +8 -0
  166. package/templates/platformer/src/render/lighting.ts +38 -0
  167. package/templates/platformer/src/render/loading.ts +113 -0
  168. package/templates/platformer/src/render/materials.ts +11 -0
  169. package/templates/platformer/src/render/palette.ts +54 -0
  170. package/templates/platformer/src/render/postprocessing.ts +18 -0
  171. package/templates/platformer/src/render/rig.ts +60 -0
  172. package/templates/platformer/src/render/sky.ts +47 -0
  173. package/templates/platformer/src/render/terrain.ts +31 -0
  174. package/templates/platformer/src/render/touch-controls.ts +157 -0
  175. package/templates/platformer/src/render/touch-layout.ts +63 -0
  176. package/templates/platformer/src/scenes/Boot.ts +12 -0
  177. package/templates/platformer/src/scenes/Level.ts +176 -0
  178. package/templates/platformer/src/state.ts +23 -0
  179. package/templates/platformer/src/style.css +31 -0
  180. package/templates/platformer/src/ui/App.tsx +17 -0
  181. package/templates/platformer/src/ui/Hud.tsx +41 -0
  182. package/templates/platformer/src/ui/Menu.tsx +7 -0
  183. package/templates/platformer/tailwind.config.ts +2 -0
  184. package/templates/platformer/threenative.config.ts +32 -0
  185. package/templates/platformer/tsconfig.json +17 -0
  186. package/templates/platformer/vite.config.ts +5 -0
  187. package/templates/racing/.mcp.json +16 -0
  188. package/templates/racing/AGENTS.md +48 -0
  189. package/templates/racing/CLAUDE.md +50 -0
  190. package/templates/racing/index.html +13 -0
  191. package/templates/racing/kit.json +7 -0
  192. package/templates/racing/package.json +46 -0
  193. package/templates/racing/playtests/boost.playtest.json +45 -0
  194. package/templates/racing/playtests/outcome.playtest.json +78 -0
  195. package/templates/racing/playtests/ranking.playtest.json +37 -0
  196. package/templates/racing/playtests/rescue.playtest.json +28 -0
  197. package/templates/racing/playtests/reverse.playtest.json +37 -0
  198. package/templates/racing/playtests/shortcut.playtest.json +44 -0
  199. package/templates/racing/playtests/survives.playtest.json +32 -0
  200. package/templates/racing/public/favicon.svg +6 -0
  201. package/templates/racing/src/entities/RacingCar.ts +114 -0
  202. package/templates/racing/src/entities/Rival.ts +46 -0
  203. package/templates/racing/src/game.ts +29 -0
  204. package/templates/racing/src/kart/boost.ts +38 -0
  205. package/templates/racing/src/main.ts +15 -0
  206. package/templates/racing/src/playtest-events.ts +15 -0
  207. package/templates/racing/src/render/camera.ts +28 -0
  208. package/templates/racing/src/render/lighting.ts +32 -0
  209. package/templates/racing/src/render/loading.ts +58 -0
  210. package/templates/racing/src/render/materials.ts +15 -0
  211. package/templates/racing/src/render/palette.ts +30 -0
  212. package/templates/racing/src/render/postprocessing.ts +14 -0
  213. package/templates/racing/src/render/shapes.ts +37 -0
  214. package/templates/racing/src/render/sky.ts +45 -0
  215. package/templates/racing/src/scenes/Boot.ts +12 -0
  216. package/templates/racing/src/scenes/Race.ts +180 -0
  217. package/templates/racing/src/state.ts +35 -0
  218. package/templates/racing/src/style.css +32 -0
  219. package/templates/racing/src/track/Checkline.ts +19 -0
  220. package/templates/racing/src/track/Lap.ts +104 -0
  221. package/templates/racing/src/track/Ranking.ts +35 -0
  222. package/templates/racing/src/track/Track.ts +229 -0
  223. package/templates/racing/src/track/TrackSector.ts +77 -0
  224. package/templates/racing/src/track/rescue.ts +21 -0
  225. package/templates/racing/src/ui/App.tsx +17 -0
  226. package/templates/racing/src/ui/Hud.tsx +71 -0
  227. package/templates/racing/src/ui/Menu.tsx +7 -0
  228. package/templates/racing/tailwind.config.ts +2 -0
  229. package/templates/racing/threenative.config.ts +29 -0
  230. package/templates/racing/tsconfig.json +17 -0
  231. package/templates/racing/vite.config.ts +5 -0
  232. package/templates/shooter/.mcp.json +16 -0
  233. package/templates/shooter/AGENTS.md +69 -0
  234. package/templates/shooter/CLAUDE.md +71 -0
  235. package/templates/shooter/index.html +13 -0
  236. package/templates/shooter/kit.json +7 -0
  237. package/templates/shooter/package.json +46 -0
  238. package/templates/shooter/playtests/fail.playtest.json +30 -0
  239. package/templates/shooter/playtests/shooter.playtest.json +89 -0
  240. package/templates/shooter/playtests/survives.playtest.json +32 -0
  241. package/templates/shooter/public/favicon.svg +4 -0
  242. package/templates/shooter/src/combat/damage.ts +41 -0
  243. package/templates/shooter/src/entities/Pickup.ts +70 -0
  244. package/templates/shooter/src/entities/Player.ts +93 -0
  245. package/templates/shooter/src/entities/Target.ts +103 -0
  246. package/templates/shooter/src/game.ts +36 -0
  247. package/templates/shooter/src/level/SpawnPoints.ts +28 -0
  248. package/templates/shooter/src/main.ts +26 -0
  249. package/templates/shooter/src/physics.ts +49 -0
  250. package/templates/shooter/src/playtest-events.ts +15 -0
  251. package/templates/shooter/src/render/camera.ts +29 -0
  252. package/templates/shooter/src/render/lighting.ts +38 -0
  253. package/templates/shooter/src/render/loading.ts +86 -0
  254. package/templates/shooter/src/render/materials.ts +38 -0
  255. package/templates/shooter/src/render/palette.ts +9 -0
  256. package/templates/shooter/src/render/postprocessing.ts +19 -0
  257. package/templates/shooter/src/render/shapes.ts +194 -0
  258. package/templates/shooter/src/render/sky.ts +37 -0
  259. package/templates/shooter/src/render/types.ts +3 -0
  260. package/templates/shooter/src/render/ui.css +212 -0
  261. package/templates/shooter/src/render/ui.ts +59 -0
  262. package/templates/shooter/src/scenes/Boot.ts +12 -0
  263. package/templates/shooter/src/scenes/Play.ts +428 -0
  264. package/templates/shooter/src/state.ts +29 -0
  265. package/templates/shooter/src/ui/App.tsx +25 -0
  266. package/templates/shooter/src/ui/Hud.tsx +49 -0
  267. package/templates/shooter/src/ui/Menu.tsx +26 -0
  268. package/templates/shooter/src/waves.ts +50 -0
  269. package/templates/shooter/src/weapons/Hitscan.ts +43 -0
  270. package/templates/shooter/src/weapons/Projectile.ts +76 -0
  271. package/templates/shooter/tailwind.config.ts +5 -0
  272. package/templates/shooter/threenative.config.ts +29 -0
  273. package/templates/shooter/tsconfig.json +17 -0
  274. package/templates/shooter/vite.config.ts +8 -0
  275. package/templates/starter/.mcp.json +16 -0
  276. package/templates/starter/AGENTS.md +450 -0
  277. package/templates/starter/CLAUDE.md +452 -0
  278. package/templates/starter/assets/.gitkeep +0 -0
  279. package/templates/starter/index.html +13 -0
  280. package/templates/starter/kit.json +7 -0
  281. package/templates/starter/package.json +49 -0
  282. package/templates/starter/playtests/buffer.playtest.json +29 -0
  283. package/templates/starter/playtests/coyote.playtest.json +35 -0
  284. package/templates/starter/playtests/forward.playtest.json +15 -0
  285. package/templates/starter/playtests/hot-reload.playtest.json +28 -0
  286. package/templates/starter/playtests/look.playtest.json +43 -0
  287. package/templates/starter/playtests/monitoring.playtest.json +41 -0
  288. package/templates/starter/playtests/pause.playtest.json +33 -0
  289. package/templates/starter/playtests/play.playtest.json +16 -0
  290. package/templates/starter/playtests/respawn.playtest.json +28 -0
  291. package/templates/starter/playtests/restart.playtest.json +36 -0
  292. package/templates/starter/playtests/seed.playtest.json +18 -0
  293. package/templates/starter/playtests/survives.playtest.json +32 -0
  294. package/templates/starter/public/.gitkeep +0 -0
  295. package/templates/starter/public/favicon.svg +4 -0
  296. package/templates/starter/public/icon.png +0 -0
  297. package/templates/starter/public/native-proof.glb +0 -0
  298. package/templates/starter/public/native-proof.png +0 -0
  299. package/templates/starter/public/pickup.ogg +0 -0
  300. package/templates/starter/src/entities/Crate.ts +34 -0
  301. package/templates/starter/src/entities/Player.ts +80 -0
  302. package/templates/starter/src/game.ts +30 -0
  303. package/templates/starter/src/main.ts +15 -0
  304. package/templates/starter/src/render/camera.ts +57 -0
  305. package/templates/starter/src/render/lighting.ts +55 -0
  306. package/templates/starter/src/render/materials.ts +12 -0
  307. package/templates/starter/src/render/palette.ts +9 -0
  308. package/templates/starter/src/render/postprocessing.ts +18 -0
  309. package/templates/starter/src/render/shapes.ts +147 -0
  310. package/templates/starter/src/render/sky.ts +56 -0
  311. package/templates/starter/src/scenes/Play.ts +161 -0
  312. package/templates/starter/src/state.ts +10 -0
  313. package/templates/starter/src/style.css +45 -0
  314. package/templates/starter/src/ui/App.tsx +19 -0
  315. package/templates/starter/src/ui/Hud.tsx +37 -0
  316. package/templates/starter/src/ui/Menu.tsx +43 -0
  317. package/templates/starter/tailwind.config.ts +2 -0
  318. package/templates/starter/threenative.config.ts +31 -0
  319. package/templates/starter/tsconfig.json +17 -0
  320. package/templates/starter/vite.config.ts +8 -0
@@ -0,0 +1,36 @@
1
+ export const STARTING_BALANCE = 120;
2
+ export const TOWER_COST = 40;
3
+ export const INCOME_RATE = 6;
4
+
5
+ export class Economy {
6
+ #balance = STARTING_BALANCE;
7
+ #income = 0;
8
+ #spent = 0;
9
+
10
+ get balance(): number {
11
+ return this.#balance;
12
+ }
13
+
14
+ get income(): number {
15
+ return this.#income;
16
+ }
17
+
18
+ get spent(): number {
19
+ return this.#spent;
20
+ }
21
+
22
+ update(dt: number): void {
23
+ if (!Number.isFinite(dt) || dt < 0) throw new Error("Economy.update requires a finite delta.");
24
+ this.#income += INCOME_RATE * dt;
25
+ this.#balance = STARTING_BALANCE + this.#income - this.#spent;
26
+ }
27
+
28
+ spend(amount: number): boolean {
29
+ if (!Number.isFinite(amount) || amount <= 0)
30
+ throw new Error("Economy.spend requires a positive amount.");
31
+ if (amount > this.#balance) return false;
32
+ this.#spent += amount;
33
+ this.#balance = STARTING_BALANCE + this.#income - this.#spent;
34
+ return true;
35
+ }
36
+ }
@@ -0,0 +1,44 @@
1
+ import type { ICtx } from "@threenative/core";
2
+ import { type Group, Vector3 } from "three";
3
+ import type { DefensePhysics } from "../physics.js";
4
+ import { commander as commanderMesh } from "../render/shapes.js";
5
+ import type { GameState } from "../state.js";
6
+
7
+ type GameCtx = ICtx<GameState, DefensePhysics>;
8
+
9
+ const MOVE_SPEED = 4;
10
+ const SPAWN = new Vector3(9, 0, 8);
11
+ const BOARD_BOUNDS = { maxX: 12, maxZ: 8, minX: -12, minZ: -8 } as const;
12
+
13
+ export class Player {
14
+ readonly mesh: Group;
15
+ readonly tags = ["player", "commander"];
16
+
17
+ constructor(ctx: GameCtx) {
18
+ this.mesh = commanderMesh();
19
+ this.mesh.name = "player";
20
+ this.mesh.position.copy(SPAWN);
21
+ ctx.add(this.mesh);
22
+ }
23
+
24
+ update(ctx: GameCtx, dt: number): void {
25
+ const move = ctx.input.vector("move");
26
+ this.mesh.position.x = Math.max(
27
+ BOARD_BOUNDS.minX,
28
+ Math.min(BOARD_BOUNDS.maxX, this.mesh.position.x + move.x * MOVE_SPEED * dt),
29
+ );
30
+ this.mesh.position.z = Math.max(
31
+ BOARD_BOUNDS.minZ,
32
+ Math.min(BOARD_BOUNDS.maxZ, this.mesh.position.z - move.y * MOVE_SPEED * dt),
33
+ );
34
+ if (move.lengthSq() > 0) this.mesh.rotation.y = Math.atan2(move.x, -move.y);
35
+ }
36
+
37
+ debug(): Record<string, unknown> {
38
+ return { position: this.mesh.position.toArray() };
39
+ }
40
+
41
+ dispose(): void {
42
+ this.mesh.removeFromParent();
43
+ }
44
+ }
@@ -0,0 +1,30 @@
1
+ import { defineGame } from "@threenative/core";
2
+ import { playtest } from "@threenative/core/playtest";
3
+ import { rapier } from "@threenative/physics";
4
+ import config from "../threenative.config.js";
5
+ import type { DefensePhysics } from "./physics.js";
6
+ import { drainPlaytestEvents as events } from "./playtest-events.js";
7
+ import { Boot } from "./scenes/Boot.js";
8
+ import { Defense } from "./scenes/Defense.js";
9
+ import type { GameState } from "./state.js";
10
+
11
+ export default defineGame<GameState, DefensePhysics>({
12
+ input: {
13
+ build: { pointer: true },
14
+ move: {
15
+ down: ["ArrowDown", "KeyS"],
16
+ left: ["ArrowLeft", "KeyA"],
17
+ right: ["ArrowRight", "KeyD"],
18
+ up: ["ArrowUp", "KeyW"],
19
+ },
20
+ overlapTest: { keys: ["KeyO"] },
21
+ restart: { keys: ["KeyR"] },
22
+ routeTest: { keys: ["KeyX"] },
23
+ safeBuild: { keys: ["KeyB"] },
24
+ },
25
+ plugins: [rapier({ gravity: { x: 0, y: -9.8, z: 0 } }), playtest({ events })],
26
+ render: config.renderer,
27
+ scenes: { boot: Boot, defense: Defense },
28
+ seed: 92092,
29
+ start: "boot",
30
+ });
@@ -0,0 +1,15 @@
1
+ import { acceptHotUpdate } from "@threenative/core/hot";
2
+ import { createElement } from "react";
3
+ import { createRoot } from "react-dom/client";
4
+ import game from "./game.js";
5
+ import { App } from "./ui/App.js";
6
+ import "./style.css";
7
+
8
+ import.meta.hot?.accept();
9
+ acceptHotUpdate(game, import.meta.hot);
10
+ const root = document.getElementById("root");
11
+ if (root === null) throw new Error("Missing #root element.");
12
+ const appRoot = root as typeof root & { __threenativeRoot?: ReturnType<typeof createRoot> };
13
+ const reactRoot = appRoot.__threenativeRoot ?? createRoot(appRoot);
14
+ appRoot.__threenativeRoot = reactRoot;
15
+ reactRoot.render(createElement(App, { game }));
@@ -0,0 +1,43 @@
1
+ import { type IPhysicsContext, RigidBody3D } from "@threenative/physics";
2
+ import type { CollisionShape3D } from "@threenative/physics";
3
+ import type { Vector3 } from "three";
4
+
5
+ export const GROUND_LAYER = 1;
6
+ export const ROUTE_LAYER = 2;
7
+ export const TOWER_LAYER = 4;
8
+ export const ATTACKER_LAYER = 8;
9
+
10
+ export interface IShapeHit {
11
+ readonly body: unknown;
12
+ readonly entity?: string;
13
+ readonly position?: Vector3;
14
+ }
15
+
16
+ export interface IPhysicsDirectSpaceState {
17
+ intersectShape(options: {
18
+ readonly collisionMask: number;
19
+ readonly maxResults?: number;
20
+ readonly position: Vector3;
21
+ readonly shape: CollisionShape3D;
22
+ }): readonly IShapeHit[];
23
+ }
24
+
25
+ export type DefensePhysics = IPhysicsContext & {
26
+ readonly directSpaceState: IPhysicsDirectSpaceState;
27
+ };
28
+
29
+ export type EntityBodyOptions = ConstructorParameters<typeof RigidBody3D>[0] & {
30
+ readonly entity?: string;
31
+ };
32
+
33
+ export function createEntityBody(options: EntityBodyOptions): RigidBody3D {
34
+ return new RigidBody3D(options as ConstructorParameters<typeof RigidBody3D>[0]);
35
+ }
36
+
37
+ export function directSpaceState(physics: IPhysicsContext): IPhysicsDirectSpaceState {
38
+ const state = (physics as Partial<DefensePhysics>).directSpaceState;
39
+ if (state === undefined) {
40
+ throw new Error("Defense requires PRD-088 directSpaceState.intersectShape.");
41
+ }
42
+ return state;
43
+ }
@@ -0,0 +1,49 @@
1
+ import { CollisionShape3D } from "@threenative/physics";
2
+ import type { Vector3 } from "three";
3
+ import { type IPhysicsDirectSpaceState, ROUTE_LAYER, TOWER_LAYER } from "../physics.js";
4
+
5
+ export type PlacementReason = "clear" | "overlap" | "route";
6
+
7
+ export interface IPlacementResult {
8
+ readonly reason: PlacementReason;
9
+ readonly accepted: boolean;
10
+ }
11
+
12
+ const BUILD_MASK = ROUTE_LAYER | TOWER_LAYER;
13
+ const BUILD_HEIGHT = 0.6;
14
+
15
+ export class Buildable {
16
+ readonly #query: IPhysicsDirectSpaceState;
17
+ readonly #shape = CollisionShape3D.box(1.55, 1.2, 1.55);
18
+ readonly #placed: Vector3[] = [];
19
+
20
+ constructor(query: IPhysicsDirectSpaceState) {
21
+ this.#query = query;
22
+ }
23
+
24
+ validate(position: Vector3): IPlacementResult {
25
+ if (![position.x, position.y, position.z].every(Number.isFinite))
26
+ throw new Error("Buildable position must be finite.");
27
+ const centre = position.clone().setY(BUILD_HEIGHT);
28
+ const hits = this.#query.intersectShape({
29
+ collisionMask: BUILD_MASK,
30
+ maxResults: 32,
31
+ position: centre,
32
+ shape: this.#shape,
33
+ });
34
+ if (hits.some((hit) => hit.entity?.startsWith("route.") === true))
35
+ return { accepted: false, reason: "route" };
36
+ if (
37
+ hits.some((hit) => hit.entity?.startsWith("tower.") === true) ||
38
+ this.#placed.some((placed) => placed.distanceToSquared(position) < 2.2)
39
+ )
40
+ return { accepted: false, reason: "overlap" };
41
+ return { accepted: true, reason: "clear" };
42
+ }
43
+
44
+ commit(position: Vector3): void {
45
+ if (!this.validate(position).accepted)
46
+ throw new Error("Buildable.commit requires a position that passed validation.");
47
+ this.#placed.push(position.clone());
48
+ }
49
+ }
@@ -0,0 +1,15 @@
1
+ type PlaytestEvent = {
2
+ readonly [key: string]: string | number;
3
+ readonly entity: string;
4
+ readonly name: string;
5
+ };
6
+
7
+ const pendingEvents: PlaytestEvent[] = [];
8
+
9
+ export function emitPlaytestEvent(event: PlaytestEvent): void {
10
+ pendingEvents.push(event);
11
+ }
12
+
13
+ export function drainPlaytestEvents(): PlaytestEvent[] {
14
+ return pendingEvents.splice(0);
15
+ }
@@ -0,0 +1,10 @@
1
+ import type { PerspectiveCamera } from "three";
2
+
3
+ export function setupCamera(camera: PerspectiveCamera): void {
4
+ camera.fov = 48;
5
+ camera.near = 0.1;
6
+ camera.far = 120;
7
+ camera.position.set(0, 22, 19);
8
+ camera.lookAt(0, 0, 0);
9
+ camera.updateProjectionMatrix();
10
+ }
@@ -0,0 +1,32 @@
1
+ import {
2
+ AmbientLight,
3
+ DirectionalLight,
4
+ HemisphereLight,
5
+ PCFSoftShadowMap,
6
+ type Scene,
7
+ } from "three";
8
+ import { palette } from "./palette.js";
9
+
10
+ type ShadowRenderer = { shadowMap: { enabled: boolean; type: number } };
11
+
12
+ export function setupLighting(scene: Scene, renderer: ShadowRenderer): void {
13
+ renderer.shadowMap.enabled = true;
14
+ renderer.shadowMap.type = PCFSoftShadowMap;
15
+ scene.add(new HemisphereLight(palette.skyLow, palette.shadow, 1.2));
16
+ const key = new DirectionalLight(palette.accent, 3.4);
17
+ key.position.set(-8, 18, 10);
18
+ key.castShadow = true;
19
+ key.shadow.mapSize.set(1024, 1024);
20
+ key.shadow.camera.near = 0.5;
21
+ key.shadow.camera.far = 80;
22
+ key.shadow.camera.left = -26;
23
+ key.shadow.camera.right = 26;
24
+ key.shadow.camera.top = 26;
25
+ key.shadow.camera.bottom = -26;
26
+ key.shadow.normalBias = 0.04;
27
+ scene.add(key);
28
+ const rim = new DirectionalLight(palette.skyLow, 1.15);
29
+ rim.position.set(12, 8, -14);
30
+ scene.add(rim);
31
+ scene.add(new AmbientLight(palette.shadow, 0.25));
32
+ }
@@ -0,0 +1,89 @@
1
+ import {
2
+ type Camera,
3
+ Mesh,
4
+ MeshBasicMaterial,
5
+ type OrthographicCamera,
6
+ PlaneGeometry,
7
+ type Scene,
8
+ } from "three";
9
+ import { palette } from "./palette.js";
10
+
11
+ export interface ILoadingHost {
12
+ readonly camera: Camera;
13
+ readonly canvasLayer: {
14
+ readonly camera: OrthographicCamera;
15
+ readonly scene: Scene;
16
+ opaque: boolean;
17
+ };
18
+ readonly renderer: { compileAsync(scene: Scene, camera: Camera): Promise<void> };
19
+ readonly scene: Scene;
20
+ readonly startup: { readonly progress: number; whenReady(): Promise<void> };
21
+ }
22
+
23
+ export function createLoadingScreen(host: ILoadingHost) {
24
+ const layer = host.canvasLayer;
25
+ const camera = layer.camera;
26
+ const cover = (color: number, order: number) => {
27
+ const mesh = new Mesh(
28
+ new PlaneGeometry(1, 1),
29
+ new MeshBasicMaterial({ color, depthTest: false, depthWrite: false }),
30
+ );
31
+ mesh.frustumCulled = false;
32
+ mesh.renderOrder = order;
33
+ layer.scene.add(mesh);
34
+ return mesh;
35
+ };
36
+ const backdrop = cover(palette.skyHigh, 0);
37
+ const track = cover(palette.route, 1);
38
+ const fill = cover(palette.accent, 2);
39
+ const parts = [backdrop, track, fill];
40
+ layer.opaque = true;
41
+ let width = 1;
42
+ let height = 1;
43
+ let lastProgress = 0;
44
+ const layout = (): void => {
45
+ width = Math.max(1, camera.right - camera.left);
46
+ height = Math.max(1, camera.top - camera.bottom);
47
+ backdrop.scale.set(width, height, 1);
48
+ track.scale.set(width * 0.5, height * 0.012, 1);
49
+ track.position.set(0, -height * 0.05, 0);
50
+ };
51
+ const setProgress = (progress: number): void => {
52
+ lastProgress = Number.isFinite(progress) ? Math.max(0, Math.min(1, progress)) : 0;
53
+ const full = width * 0.5;
54
+ const barWidth = Math.max(full * 0.002, full * lastProgress);
55
+ fill.scale.set(barWidth, height * 0.012, 1);
56
+ fill.position.set(-full / 2 + barWidth / 2, -height * 0.05, 0);
57
+ };
58
+ layout();
59
+ setProgress(0);
60
+ let done = false;
61
+ const finish = (): void => {
62
+ if (done) return;
63
+ done = true;
64
+ for (const mesh of parts) {
65
+ mesh.removeFromParent();
66
+ mesh.geometry.dispose();
67
+ mesh.material.dispose();
68
+ }
69
+ layer.opaque = false;
70
+ };
71
+ void (async () => {
72
+ await host.startup.whenReady();
73
+ await host.renderer.compileAsync(host.scene, host.camera).catch(() => undefined);
74
+ finish();
75
+ })();
76
+ return {
77
+ finish,
78
+ update(): void {
79
+ if (done) return;
80
+ const nextWidth = camera.right - camera.left;
81
+ const nextHeight = camera.top - camera.bottom;
82
+ if (nextWidth !== width || nextHeight !== height) {
83
+ layout();
84
+ setProgress(lastProgress);
85
+ }
86
+ setProgress(host.startup.progress);
87
+ },
88
+ };
89
+ }
@@ -0,0 +1,13 @@
1
+ import { MeshStandardMaterial } from "three";
2
+ import { palette, toon } from "./palette.js";
3
+
4
+ export function createMaterials() {
5
+ return {
6
+ accent: toon(palette.accent, 0.42),
7
+ attacker: toon(palette.skyLow, 0.58),
8
+ ground: toon(palette.ground, 0.94),
9
+ route: toon(palette.route, 0.88),
10
+ shadow: new MeshStandardMaterial({ color: palette.shadow, roughness: 1 }),
11
+ tower: toon(palette.accent, 0.56),
12
+ };
13
+ }
@@ -0,0 +1,21 @@
1
+ import { MeshStandardMaterial } from "three";
2
+
3
+ export const palette = {
4
+ skyHigh: 0x0b2236,
5
+ skyLow: 0x2b6a76,
6
+ ground: 0x173a3b,
7
+ route: 0x4b5660,
8
+ accent: 0xf5c451,
9
+ shadow: 0x07131d,
10
+ } as const;
11
+
12
+ const materials = new Map<string, MeshStandardMaterial>();
13
+
14
+ export function toon(color: number, roughness = 0.8): MeshStandardMaterial {
15
+ const key = `${color}:${roughness}`;
16
+ const cached = materials.get(key);
17
+ if (cached !== undefined) return cached;
18
+ const material = new MeshStandardMaterial({ color, roughness, metalness: 0.08 });
19
+ materials.set(key, material);
20
+ return material;
21
+ }
@@ -0,0 +1,14 @@
1
+ import { ACESFilmicToneMapping, type Camera, type Scene } from "three";
2
+ import { bloom } from "three/addons/tsl/display/BloomNode.js";
3
+ import { pass } from "three/tsl";
4
+
5
+ type OutputRenderer = { kind: string; raw: unknown; setOutputNode(node: unknown): void };
6
+
7
+ export function setupPost(renderer: OutputRenderer, scene: Scene, camera: Camera): void {
8
+ const raw = renderer.raw as { toneMapping?: number; toneMappingExposure?: number };
9
+ raw.toneMapping = ACESFilmicToneMapping;
10
+ raw.toneMappingExposure = 1.15;
11
+ if (renderer.kind !== "webgpu") return;
12
+ const colour = pass(scene, camera).getTextureNode();
13
+ renderer.setOutputNode(colour.add(bloom(colour, 0.42, 0.5, 0.2)));
14
+ }
@@ -0,0 +1,89 @@
1
+ import {
2
+ BoxGeometry,
3
+ CylinderGeometry,
4
+ Group,
5
+ Mesh,
6
+ type Object3D,
7
+ SphereGeometry,
8
+ TorusGeometry,
9
+ } from "three";
10
+ import { createMaterials } from "./materials.js";
11
+
12
+ export function board(width = 28, depth = 20): Mesh {
13
+ const mesh = new Mesh(new BoxGeometry(width, 0.2, depth), createMaterials().ground);
14
+ mesh.position.y = -0.1;
15
+ mesh.receiveShadow = true;
16
+ mesh.name = "build-ground";
17
+ return mesh;
18
+ }
19
+
20
+ export function routeSegment(length: number, width: number): Object3D {
21
+ const materials = createMaterials();
22
+ const group = new Group();
23
+ const road = new Mesh(new BoxGeometry(length, 0.14, width), materials.route);
24
+ road.position.y = 0.04;
25
+ road.receiveShadow = true;
26
+ group.add(road);
27
+ for (const side of [-1, 1]) {
28
+ const edge = new Mesh(new BoxGeometry(length, 0.04, 0.08), materials.accent);
29
+ edge.position.set(0, 0.13, side * (width / 2 - 0.12));
30
+ group.add(edge);
31
+ }
32
+ return group;
33
+ }
34
+
35
+ export function tower(): Group {
36
+ const materials = createMaterials();
37
+ const group = new Group();
38
+ const base = new Mesh(new CylinderGeometry(0.78, 0.92, 0.22, 8), materials.shadow);
39
+ base.position.y = 0.12;
40
+ const body = new Mesh(new CylinderGeometry(0.52, 0.62, 0.86, 8), materials.tower);
41
+ body.position.y = 0.61;
42
+ const head = new Mesh(new BoxGeometry(0.8, 0.22, 0.8), materials.accent);
43
+ head.position.y = 1.12;
44
+ for (const mesh of [base, body, head]) mesh.castShadow = mesh.receiveShadow = true;
45
+ group.add(base, body, head);
46
+ return group;
47
+ }
48
+
49
+ export function attacker(): Group {
50
+ const materials = createMaterials();
51
+ const group = new Group();
52
+ const body = new Mesh(new SphereGeometry(0.42, 12, 8), materials.attacker);
53
+ body.scale.y = 0.86;
54
+ body.position.y = 0.55;
55
+ body.castShadow = true;
56
+ const core = new Mesh(new SphereGeometry(0.14, 10, 6), materials.accent);
57
+ core.position.set(0, 0.58, -0.34);
58
+ core.castShadow = true;
59
+ group.add(body, core);
60
+ return group;
61
+ }
62
+
63
+ export function commander(): Group {
64
+ const materials = createMaterials();
65
+ const group = new Group();
66
+ const base = new Mesh(new CylinderGeometry(0.55, 0.7, 0.16, 8), materials.shadow);
67
+ base.position.y = 0.08;
68
+ const ring = new Mesh(new TorusGeometry(0.5, 0.06, 8, 16), materials.route);
69
+ ring.rotation.x = Math.PI / 2;
70
+ ring.position.y = 0.18;
71
+ const beacon = new Mesh(new SphereGeometry(0.3, 12, 8), materials.accent);
72
+ beacon.position.y = 0.5;
73
+ for (const mesh of [base, ring, beacon]) mesh.castShadow = mesh.receiveShadow = true;
74
+ group.add(base, ring, beacon);
75
+ return group;
76
+ }
77
+
78
+ export function base(): Group {
79
+ const materials = createMaterials();
80
+ const group = new Group();
81
+ const plinth = new Mesh(new CylinderGeometry(1.4, 1.6, 0.24, 8), materials.shadow);
82
+ plinth.position.y = 0.12;
83
+ const beacon = new Mesh(new CylinderGeometry(0.74, 0.92, 1.2, 8), materials.accent);
84
+ beacon.position.y = 0.72;
85
+ plinth.castShadow = plinth.receiveShadow = true;
86
+ beacon.castShadow = beacon.receiveShadow = true;
87
+ group.add(plinth, beacon);
88
+ return group;
89
+ }
@@ -0,0 +1,45 @@
1
+ import {
2
+ BackSide,
3
+ BufferAttribute,
4
+ Color,
5
+ Fog,
6
+ MathUtils,
7
+ Mesh,
8
+ MeshBasicMaterial,
9
+ type Scene,
10
+ SphereGeometry,
11
+ } from "three";
12
+ import { palette } from "./palette.js";
13
+
14
+ export function setupSky(scene: Scene): void {
15
+ const top = new Color(palette.skyHigh);
16
+ const bottom = new Color(palette.skyLow);
17
+ scene.background = top;
18
+ scene.fog = new Fog(bottom, 34, 90);
19
+
20
+ // A vertical gradient, not a flat fill. This dome used to be one solid colour, and a blind score
21
+ // of the first frame read it as exactly that: 8,001 unique colours in the whole
22
+ // 1280x720 frame, with a 240px vertical background sample running 177888, 187888, 197988, 1B7988.
23
+ // Sky is most of the frame, so a flat one costs more than anything else here. Edit or delete
24
+ // this — it is your file.
25
+ const radius = 80;
26
+ const geometry = new SphereGeometry(radius, 24, 12);
27
+ const positions = geometry.getAttribute("position");
28
+ const colors = new Float32Array(positions.count * 3);
29
+ const color = new Color();
30
+ for (let index = 0; index < positions.count; index += 1) {
31
+ const height = MathUtils.clamp((positions.getY(index) / radius + 0.2) / 0.65, 0, 1);
32
+ color.copy(bottom).lerp(top, height);
33
+ colors.set([color.r, color.g, color.b], index * 3);
34
+ }
35
+ geometry.setAttribute("color", new BufferAttribute(colors, 3));
36
+
37
+ const dome = new Mesh(
38
+ geometry,
39
+ // `fog: false` because the dome is the horizon; fogging it collapses the gradient above back
40
+ // into the single wash this replaced.
41
+ new MeshBasicMaterial({ fog: false, side: BackSide, toneMapped: false, vertexColors: true }),
42
+ );
43
+ dome.frustumCulled = false;
44
+ scene.add(dome);
45
+ }
@@ -0,0 +1,12 @@
1
+ import { Scene } from "@threenative/core";
2
+ import type { DefensePhysics } from "../physics.js";
3
+ import type { GameState } from "../state.js";
4
+ import { Defense, type GameCtx } from "./Defense.js";
5
+
6
+ export class Boot extends Scene<GameState, DefensePhysics> {
7
+ static override readonly initialState = Defense.initialState;
8
+
9
+ override enter(ctx: GameCtx): void {
10
+ void ctx.goto("defense");
11
+ }
12
+ }