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,143 @@
1
+ import type { ICtx } from "@threenative/core";
2
+ import {
3
+ CharacterBody3D,
4
+ CollisionShape3D,
5
+ type IPhysicsBodyHandle,
6
+ type IPhysicsContext,
7
+ } from "@threenative/physics";
8
+ import { type Group, Vector3 } from "three";
9
+ import { directSpaceState } from "../physics.js";
10
+ import { type RpgMaterials, createEnemyVisual } from "../render/shapes.js";
11
+ import type { GameState } from "../state.js";
12
+ import { HOSTILE_LAYER, PLAYER_LAYER, WORLD_LAYER } from "./Player.js";
13
+
14
+ export type EnemyState = "idle" | "aggro" | "attack" | "dead";
15
+
16
+ type GameCtx = ICtx<GameState, IPhysicsContext>;
17
+
18
+ interface IEnemyOptions {
19
+ readonly boss?: boolean;
20
+ readonly health?: number;
21
+ readonly onAttack: (amount: number) => void;
22
+ readonly onDeath: (enemy: Enemy) => void;
23
+ }
24
+
25
+ export class Enemy {
26
+ readonly body: CharacterBody3D;
27
+ readonly boss: boolean;
28
+ readonly mesh: Group;
29
+ readonly tags = ["enemy", "hostile"];
30
+ health: number;
31
+ state: EnemyState = "idle";
32
+ lineOfSight = false;
33
+ lineOfSightBlocked = false;
34
+ alive = true;
35
+ #attackTimer = 0.8;
36
+ #onAttack: (amount: number) => void;
37
+ #onDeath: (enemy: Enemy) => void;
38
+ #playerBody: IPhysicsBodyHandle;
39
+ #rangeShape = CollisionShape3D.sphere(4.8);
40
+
41
+ constructor(
42
+ ctx: GameCtx,
43
+ materials: RpgMaterials,
44
+ position: Vector3,
45
+ playerBody: IPhysicsBodyHandle,
46
+ options: IEnemyOptions,
47
+ ) {
48
+ this.boss = options.boss ?? false;
49
+ this.health = options.health ?? (this.boss ? 64 : 28);
50
+ this.#onAttack = options.onAttack;
51
+ this.#onDeath = options.onDeath;
52
+ this.#playerBody = playerBody;
53
+ this.mesh = createEnemyVisual(materials, this.boss);
54
+ this.mesh.position.copy(position);
55
+ ctx.add(this.mesh);
56
+ this.body = new CharacterBody3D({
57
+ collisionLayer: HOSTILE_LAYER,
58
+ collisionMask: WORLD_LAYER,
59
+ gravity: 0,
60
+ object: this.mesh,
61
+ physics: ctx.physics,
62
+ shape: CollisionShape3D.capsule(this.boss ? 0.58 : 0.4, this.boss ? 0.5 : 0.32),
63
+ });
64
+ }
65
+
66
+ update(ctx: GameCtx, dt: number, playerPosition: Vector3): void {
67
+ if (!this.alive) return;
68
+ const rangeHits = directSpaceState(ctx.physics).intersectShape({
69
+ collisionMask: PLAYER_LAYER,
70
+ maxResults: 4,
71
+ position: this.mesh.position,
72
+ shape: this.#rangeShape,
73
+ });
74
+ const inRange = rangeHits.some((hit) => hit.body.id === this.#playerBody.id);
75
+ this.lineOfSight = false;
76
+ this.lineOfSightBlocked = false;
77
+ if (inRange) {
78
+ const from = this.mesh.position.clone().add(new Vector3(0, this.boss ? 0.9 : 0.5, 0));
79
+ const to = playerPosition.clone().add(new Vector3(0, 0.5, 0));
80
+ const ray = directSpaceState(ctx.physics).intersectRay({
81
+ collisionMask: WORLD_LAYER | PLAYER_LAYER,
82
+ from,
83
+ to,
84
+ });
85
+ this.lineOfSight = ray?.body.id === this.#playerBody.id;
86
+ this.lineOfSightBlocked = !this.lineOfSight && ray !== undefined;
87
+ }
88
+
89
+ if (!this.lineOfSight) {
90
+ this.state = "idle";
91
+ this.body.velocity.set(0, 0, 0);
92
+ this.body.moveAndSlide(dt);
93
+ return;
94
+ }
95
+
96
+ const direction = playerPosition.clone().sub(this.mesh.position).setY(0);
97
+ const distance = direction.length();
98
+ if (distance <= 1.45) {
99
+ this.state = "attack";
100
+ this.body.velocity.set(0, 0, 0);
101
+ this.#attackTimer -= dt;
102
+ if (this.#attackTimer <= 0) {
103
+ this.#attackTimer = this.boss ? 1.2 : 1.5;
104
+ this.#onAttack(this.boss ? 10 : 5);
105
+ }
106
+ this.body.moveAndSlide(dt);
107
+ return;
108
+ }
109
+
110
+ this.state = "aggro";
111
+ if (distance > 0.001) direction.normalize().multiplyScalar(this.boss ? 2.1 : 2.7);
112
+ this.body.velocity.set(direction.x, 0, direction.z);
113
+ this.body.moveAndSlide(dt);
114
+ }
115
+
116
+ takeDamage(amount: number): boolean {
117
+ if (!this.alive || !Number.isFinite(amount) || amount <= 0) return false;
118
+ this.health = Math.max(0, this.health - amount);
119
+ if (this.health > 0) return false;
120
+ this.alive = false;
121
+ this.state = "dead";
122
+ this.mesh.visible = false;
123
+ this.body.dispose();
124
+ this.#onDeath(this);
125
+ return true;
126
+ }
127
+
128
+ debug(): Record<string, unknown> {
129
+ return {
130
+ alive: this.alive,
131
+ boss: this.boss,
132
+ lineOfSight: this.lineOfSight,
133
+ lineOfSightBlocked: this.lineOfSightBlocked,
134
+ position: this.mesh.position.toArray(),
135
+ state: this.state,
136
+ };
137
+ }
138
+
139
+ dispose(): void {
140
+ this.body.dispose();
141
+ this.mesh.removeFromParent();
142
+ }
143
+ }
@@ -0,0 +1,83 @@
1
+ import type { ICtx } from "@threenative/core";
2
+ import { CharacterBody3D, CollisionShape3D, type IPhysicsContext } from "@threenative/physics";
3
+ import { type Group, Vector3 } from "three";
4
+ import { createPlayerVisual } from "../render/shapes.js";
5
+ import type { RpgMaterials } from "../render/shapes.js";
6
+ import type { GameState } from "../state.js";
7
+
8
+ export const WORLD_LAYER = 1;
9
+ export const PLAYER_LAYER = 2;
10
+ export const HOSTILE_LAYER = 4;
11
+
12
+ type GameCtx = ICtx<GameState, IPhysicsContext>;
13
+
14
+ export class Player {
15
+ readonly body: CharacterBody3D;
16
+ readonly maxHealth = 100;
17
+ readonly mesh: Group;
18
+ health: number;
19
+ dead = false;
20
+ equippedItem = "";
21
+ #onDamage: (amount: number) => void;
22
+ #onDeath: () => void;
23
+
24
+ constructor(
25
+ ctx: GameCtx,
26
+ materials: RpgMaterials,
27
+ spawn: Vector3,
28
+ health: number,
29
+ onDamage: (amount: number) => void,
30
+ onDeath: () => void,
31
+ ) {
32
+ this.#onDamage = onDamage;
33
+ this.#onDeath = onDeath;
34
+ this.health = Math.max(0, Math.min(this.maxHealth, health));
35
+ this.mesh = createPlayerVisual(materials);
36
+ this.mesh.position.copy(spawn);
37
+ ctx.add(this.mesh);
38
+ this.body = new CharacterBody3D({
39
+ collisionLayer: PLAYER_LAYER,
40
+ collisionMask: WORLD_LAYER | HOSTILE_LAYER,
41
+ gravity: 0,
42
+ object: this.mesh,
43
+ physics: ctx.physics,
44
+ shape: CollisionShape3D.capsule(0.38, 0.34),
45
+ });
46
+ }
47
+
48
+ update(ctx: GameCtx, dt: number): void {
49
+ if (this.dead) return;
50
+ const move = ctx.input.vector("move");
51
+ this.body.velocity.set(move.x * 4.8, 0, -move.y * 4.8);
52
+ this.body.moveAndSlide(dt);
53
+ }
54
+
55
+ takeDamage(amount: number): void {
56
+ if (this.dead || !Number.isFinite(amount) || amount <= 0) return;
57
+ this.health = Math.max(0, this.health - amount);
58
+ this.#onDamage(amount);
59
+ if (this.health > 0) return;
60
+ this.dead = true;
61
+ this.mesh.visible = false;
62
+ this.body.velocity.set(0, 0, 0);
63
+ this.#onDeath();
64
+ }
65
+
66
+ attackOrigin(): Vector3 {
67
+ return this.mesh.position.clone().add(new Vector3(0, 0.8, -0.9));
68
+ }
69
+
70
+ debug(): Record<string, unknown> {
71
+ return {
72
+ dead: this.dead,
73
+ equippedItem: this.equippedItem,
74
+ health: this.health,
75
+ position: this.mesh.position.toArray(),
76
+ };
77
+ }
78
+
79
+ dispose(): void {
80
+ this.body.dispose();
81
+ this.mesh.removeFromParent();
82
+ }
83
+ }
@@ -0,0 +1,40 @@
1
+ import { defineGame } from "@threenative/core";
2
+ import { playtest } from "@threenative/core/playtest";
3
+ import type { IPhysicsContext } from "@threenative/physics";
4
+ import { rapier } from "@threenative/physics";
5
+ import config from "../threenative.config.js";
6
+ import { drainPlaytestEvents } from "./playtest-events.js";
7
+ import { Boot } from "./scenes/Boot.js";
8
+ import { Play } from "./scenes/Play.js";
9
+ import type { GameState } from "./state.js";
10
+
11
+ const game = defineGame<GameState, IPhysicsContext>({
12
+ input: {
13
+ // The four directions of `input.vector("move")`. Declared rather than inherited from the
14
+ // default binding, so the axis every scene reads is visible where the game is defined.
15
+ move: {
16
+ down: ["ArrowDown", "KeyS"],
17
+ left: ["ArrowLeft", "KeyA"],
18
+ right: ["ArrowRight", "KeyD"],
19
+ up: ["ArrowUp", "KeyW"],
20
+ },
21
+ ability: { buttons: [1], keys: ["KeyE"] },
22
+ attack: { buttons: [0], keys: ["Space", "KeyF"] },
23
+ damage: { keys: ["KeyH"] },
24
+ dropProbe: { keys: ["KeyT"] },
25
+ equip: { keys: ["KeyQ"] },
26
+ fill: { keys: ["KeyP"] },
27
+ lethal: { keys: ["KeyX"] },
28
+ loot: { keys: ["KeyL"] },
29
+ restart: { keys: ["KeyR"] },
30
+ save: { keys: ["KeyC"] },
31
+ unequip: { keys: ["KeyU"] },
32
+ },
33
+ plugins: [rapier({ gravity: { x: 0, y: 0, z: 0 } }), playtest({ events: drainPlaytestEvents })],
34
+ render: config.renderer,
35
+ scenes: { boot: Boot, play: Play },
36
+ seed: 93093,
37
+ start: "boot",
38
+ });
39
+
40
+ export default game;
@@ -0,0 +1,163 @@
1
+ export interface ItemDefinition {
2
+ readonly attackBonus?: number;
3
+ readonly id: string;
4
+ readonly label: string;
5
+ readonly maxStack: number;
6
+ readonly kind: "consumable" | "weapon";
7
+ }
8
+
9
+ export const ITEMS = {
10
+ emberBlade: {
11
+ attackBonus: 6,
12
+ id: "ember-blade",
13
+ label: "Ember Blade",
14
+ maxStack: 1,
15
+ kind: "weapon",
16
+ },
17
+ potion: {
18
+ id: "potion",
19
+ label: "Red Potion",
20
+ maxStack: 3,
21
+ kind: "consumable",
22
+ },
23
+ rustedBlade: {
24
+ attackBonus: 0,
25
+ id: "rusted-blade",
26
+ label: "Rusted Blade",
27
+ maxStack: 1,
28
+ kind: "weapon",
29
+ },
30
+ } as const satisfies Record<string, ItemDefinition>;
31
+
32
+ export type ItemId = (typeof ITEMS)[keyof typeof ITEMS]["id"];
33
+
34
+ export type ItemStack = {
35
+ readonly itemId: ItemId;
36
+ readonly quantity: number;
37
+ };
38
+
39
+ function item(itemId: ItemId): ItemDefinition {
40
+ const found = Object.values(ITEMS).find((candidate) => candidate.id === itemId);
41
+ if (found === undefined) throw new Error(`Unknown item: ${itemId}`);
42
+ return found;
43
+ }
44
+
45
+ export class Inventory {
46
+ readonly capacity: number;
47
+ #slots: Array<ItemStack | undefined>;
48
+ #equipped: ItemStack | undefined;
49
+
50
+ constructor(capacity: number) {
51
+ if (!Number.isInteger(capacity) || capacity < 1)
52
+ throw new RangeError("Invalid inventory capacity.");
53
+ this.capacity = capacity;
54
+ this.#slots = Array.from({ length: capacity });
55
+ }
56
+
57
+ add(itemId: ItemId, quantity = 1): number {
58
+ if (!Number.isInteger(quantity) || quantity < 1)
59
+ throw new RangeError("Item quantity must be positive.");
60
+ let remaining = quantity;
61
+ const definition = item(itemId);
62
+ for (let index = 0; index < this.#slots.length && remaining > 0; index += 1) {
63
+ const stack = this.#slots[index];
64
+ if (stack?.itemId !== itemId || stack.quantity >= definition.maxStack) continue;
65
+ const amount = Math.min(remaining, definition.maxStack - stack.quantity);
66
+ this.#slots[index] = { itemId, quantity: stack.quantity + amount };
67
+ remaining -= amount;
68
+ }
69
+ for (let index = 0; index < this.#slots.length && remaining > 0; index += 1) {
70
+ if (this.#slots[index] !== undefined) continue;
71
+ const amount = Math.min(remaining, definition.maxStack);
72
+ this.#slots[index] = { itemId, quantity: amount };
73
+ remaining -= amount;
74
+ }
75
+ return remaining;
76
+ }
77
+
78
+ swapFirst(stack: ItemStack): ItemStack | undefined {
79
+ const definition = item(stack.itemId);
80
+ if (
81
+ !Number.isInteger(stack.quantity) ||
82
+ stack.quantity < 1 ||
83
+ stack.quantity > definition.maxStack ||
84
+ this.#slots.some((slot) => slot === undefined)
85
+ )
86
+ return undefined;
87
+ const index = this.#slots.findIndex((slot) => slot !== undefined);
88
+ const displaced = this.#slots[index];
89
+ if (displaced === undefined) return undefined;
90
+ this.#slots[index] = { ...stack };
91
+ return displaced;
92
+ }
93
+
94
+ equip(itemId: ItemId): boolean {
95
+ const index = this.#slots.findIndex((stack) => stack?.itemId === itemId);
96
+ if (index < 0 || item(itemId).kind !== "weapon") return false;
97
+ const previous = this.#equipped;
98
+ if (previous !== undefined && this.add(previous.itemId, previous.quantity) > 0) return false;
99
+ const stack = this.#slots[index];
100
+ if (stack === undefined) return false;
101
+ this.#slots[index] =
102
+ stack.quantity === 1 ? undefined : { ...stack, quantity: stack.quantity - 1 };
103
+ this.#equipped = { itemId, quantity: 1 };
104
+ return true;
105
+ }
106
+
107
+ unequip(): boolean {
108
+ if (this.#equipped === undefined) return false;
109
+ const equipped = this.#equipped;
110
+ if (this.add(equipped.itemId, equipped.quantity) > 0) return false;
111
+ this.#equipped = undefined;
112
+ return true;
113
+ }
114
+
115
+ fill(itemId: ItemId): void {
116
+ for (let index = 0; index < this.capacity * 3; index += 1) {
117
+ if (this.add(itemId, 1) > 0 || this.#slots.every((slot) => slot !== undefined)) break;
118
+ }
119
+ }
120
+
121
+ restore(slots: readonly ItemStack[], equippedItem?: ItemId): void {
122
+ if (slots.length > this.capacity)
123
+ throw new RangeError("Serialized inventory exceeds its capacity.");
124
+ const restoredSlots: Array<ItemStack | undefined> = Array.from({ length: this.capacity });
125
+ for (const [index, stack] of slots.entries()) {
126
+ const definition = item(stack.itemId);
127
+ if (
128
+ !Number.isInteger(stack.quantity) ||
129
+ stack.quantity < 1 ||
130
+ stack.quantity > definition.maxStack
131
+ )
132
+ throw new RangeError(`Invalid quantity for serialized item: ${stack.itemId}.`);
133
+ restoredSlots[index] = { ...stack };
134
+ }
135
+ let restoredEquipped: ItemStack | undefined;
136
+ if (equippedItem !== undefined) {
137
+ if (item(equippedItem).kind !== "weapon")
138
+ throw new TypeError("Only weapons can be serialized as equipped items.");
139
+ restoredEquipped = { itemId: equippedItem, quantity: 1 };
140
+ }
141
+ this.#slots = restoredSlots;
142
+ this.#equipped = restoredEquipped;
143
+ }
144
+
145
+ get equipped(): ItemStack | undefined {
146
+ return this.#equipped;
147
+ }
148
+
149
+ get slots(): readonly (ItemStack | undefined)[] {
150
+ return this.#slots;
151
+ }
152
+
153
+ labels(): string[] {
154
+ return this.#slots.flatMap((stack) => {
155
+ if (stack === undefined) return [];
156
+ return [`${item(stack.itemId).label} x${stack.quantity}`];
157
+ });
158
+ }
159
+
160
+ snapshot(): ItemStack[] {
161
+ return this.#slots.flatMap((stack) => (stack === undefined ? [] : [{ ...stack }]));
162
+ }
163
+ }
@@ -0,0 +1,40 @@
1
+ import type { ItemId } from "../items/Inventory.js";
2
+
3
+ export interface IDrop {
4
+ readonly itemId: ItemId;
5
+ readonly quantity: number;
6
+ }
7
+
8
+ const TABLE: readonly { readonly itemId: ItemId; readonly weight: number }[] = [
9
+ { itemId: "potion", weight: 70 },
10
+ { itemId: "ember-blade", weight: 30 },
11
+ ];
12
+
13
+ function next(seed: number): number {
14
+ return (Math.imul(seed, 1_664_525) + 1_013_904_223) >>> 0;
15
+ }
16
+
17
+ export function rollDrops(seed: number, rolls = 2): IDrop[] {
18
+ if (!Number.isInteger(seed) || seed < 0)
19
+ throw new RangeError("Drop seeds must be non-negative integers.");
20
+ if (!Number.isInteger(rolls) || rolls < 1) throw new RangeError("Drop rolls must be positive.");
21
+ let state = seed >>> 0;
22
+ const drops: IDrop[] = [];
23
+ for (let index = 0; index < rolls; index += 1) {
24
+ state = next(state);
25
+ const pick = (state / 4_294_967_296) * 100;
26
+ let cursor = 0;
27
+ for (const entry of TABLE) {
28
+ cursor += entry.weight;
29
+ if (pick < cursor) {
30
+ drops.push({ itemId: entry.itemId, quantity: 1 });
31
+ break;
32
+ }
33
+ }
34
+ }
35
+ return drops;
36
+ }
37
+
38
+ export function describeDrops(drops: readonly IDrop[]): string {
39
+ return drops.map((drop) => `${drop.itemId} x${drop.quantity}`).join(", ");
40
+ }
@@ -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,40 @@
1
+ import type { CollisionShape3D, IPhysicsBodyHandle, IPhysicsContext } from "@threenative/physics";
2
+ import type { Vector3 } from "three";
3
+
4
+ export type SpacePoint = Pick<Vector3, "x" | "y" | "z">;
5
+
6
+ export interface IRayHit {
7
+ readonly body: IPhysicsBodyHandle;
8
+ readonly distance: number;
9
+ readonly normal: SpacePoint;
10
+ readonly position: SpacePoint;
11
+ }
12
+
13
+ export interface IShapeHit {
14
+ readonly body: IPhysicsBodyHandle;
15
+ readonly position?: SpacePoint;
16
+ }
17
+
18
+ export interface IPhysicsDirectSpaceState {
19
+ intersectRay(options: {
20
+ readonly collisionMask?: number;
21
+ readonly from: SpacePoint;
22
+ readonly maxResults?: number;
23
+ readonly to: SpacePoint;
24
+ }): IRayHit | undefined;
25
+ intersectShape(options: {
26
+ readonly collisionMask?: number;
27
+ readonly maxResults?: number;
28
+ readonly position: SpacePoint;
29
+ readonly shape: CollisionShape3D;
30
+ }): readonly IShapeHit[];
31
+ }
32
+
33
+ type QueryPhysics = IPhysicsContext & { readonly directSpaceState: IPhysicsDirectSpaceState };
34
+
35
+ export function directSpaceState(physics: IPhysicsContext): IPhysicsDirectSpaceState {
36
+ const state = (physics as QueryPhysics).directSpaceState;
37
+ if (state === undefined)
38
+ throw new Error("TN_ACTION_RPG_SPACE_MISSING: directSpaceState is required.");
39
+ return state;
40
+ }
@@ -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,35 @@
1
+ const SAVE_KEY = "threenative.action-rpg.progress";
2
+
3
+ interface IReadableStore<T> {
4
+ getState(): T;
5
+ }
6
+
7
+ function storage(): Storage | undefined {
8
+ try {
9
+ return globalThis.localStorage;
10
+ } catch {
11
+ return undefined;
12
+ }
13
+ }
14
+
15
+ export function loadProgress<T extends Record<string, unknown>>(initial: T): T {
16
+ const raw = storage()?.getItem(SAVE_KEY);
17
+ if (raw === null || raw === undefined) return { ...initial };
18
+ try {
19
+ const parsed: unknown = JSON.parse(raw);
20
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
21
+ return { ...initial };
22
+ return { ...initial, ...(parsed as Partial<T>) };
23
+ } catch {
24
+ return { ...initial };
25
+ }
26
+ }
27
+
28
+ /** ctx.state is the core createGameStore instance; this kit only serialises its JSON projection. */
29
+ export function saveProgress<T extends Record<string, unknown>>(store: IReadableStore<T>): void {
30
+ storage()?.setItem(SAVE_KEY, JSON.stringify(store.getState()));
31
+ }
32
+
33
+ export function clearProgress(): void {
34
+ storage()?.removeItem(SAVE_KEY);
35
+ }
@@ -0,0 +1,26 @@
1
+ import type { PerspectiveCamera, Vector3 } from "three";
2
+ import { Vector3 as Vec3 } from "three";
3
+
4
+ export interface ICameraRig {
5
+ readonly follow: (target: Vector3, dt: number) => void;
6
+ readonly snap: (target: Vector3) => void;
7
+ }
8
+
9
+ export function createDungeonCamera(camera: PerspectiveCamera): ICameraRig {
10
+ const offset = new Vec3(0, 8.4, 8.8);
11
+ const lookAhead = new Vec3(0, 0.2, -1.2);
12
+ const desired = new Vec3();
13
+ const aim = new Vec3();
14
+ const pose = (target: Vector3): void => {
15
+ camera.position.copy(target).add(offset);
16
+ camera.lookAt(aim.copy(target).add(lookAhead));
17
+ };
18
+ return {
19
+ follow: (target, dt) => {
20
+ desired.copy(target).add(offset);
21
+ camera.position.lerp(desired, 1 - Math.exp(-dt / 0.2));
22
+ camera.lookAt(aim.copy(target).add(lookAhead));
23
+ },
24
+ snap: pose,
25
+ };
26
+ }
@@ -0,0 +1,30 @@
1
+ import { AmbientLight, DirectionalLight, PCFSoftShadowMap, type Scene } from "three";
2
+ import { palette } from "./palette.js";
3
+
4
+ type ShadowRenderer = { shadowMap: { enabled: boolean; type: number } };
5
+
6
+ export function setupLighting(scene: Scene, renderer: ShadowRenderer): void {
7
+ renderer.shadowMap.enabled = true;
8
+ renderer.shadowMap.type = PCFSoftShadowMap;
9
+
10
+ const key = new DirectionalLight(palette.accent, 3.2);
11
+ key.name = "key-light";
12
+ key.position.set(-8, 12, 6);
13
+ key.castShadow = true;
14
+ key.shadow.mapSize.set(1024, 1024);
15
+ key.shadow.camera.near = 0.5;
16
+ key.shadow.camera.far = 70;
17
+ key.shadow.camera.left = -24;
18
+ key.shadow.camera.right = 24;
19
+ key.shadow.camera.top = 20;
20
+ key.shadow.camera.bottom = -20;
21
+ key.shadow.bias = -0.0008;
22
+ key.shadow.normalBias = 0.04;
23
+ scene.add(key);
24
+
25
+ const rim = new DirectionalLight(palette.player, 1.35);
26
+ rim.name = "rim-light";
27
+ rim.position.set(12, 6, -12);
28
+ scene.add(rim);
29
+ scene.add(new AmbientLight(palette.skyHigh, 0.72));
30
+ }