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.
- package/README.md +16 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +283 -0
- package/dist/threenative.d.ts +6 -0
- package/dist/threenative.js +757 -0
- package/package.json +44 -0
- package/templates/action-rpg/.mcp.json +16 -0
- package/templates/action-rpg/AGENTS.md +40 -0
- package/templates/action-rpg/CLAUDE.md +42 -0
- package/templates/action-rpg/index.html +13 -0
- package/templates/action-rpg/kit.json +7 -0
- package/templates/action-rpg/package.json +47 -0
- package/templates/action-rpg/playtests/combat.playtest.json +61 -0
- package/templates/action-rpg/playtests/fail.playtest.json +21 -0
- package/templates/action-rpg/playtests/inventory.playtest.json +111 -0
- package/templates/action-rpg/playtests/progress.playtest.json +99 -0
- package/templates/action-rpg/playtests/survives.playtest.json +32 -0
- package/templates/action-rpg/playtests/win.playtest.json +30 -0
- package/templates/action-rpg/public/favicon.svg +5 -0
- package/templates/action-rpg/src/abilities/Ability.ts +45 -0
- package/templates/action-rpg/src/entities/Enemy.ts +143 -0
- package/templates/action-rpg/src/entities/Player.ts +83 -0
- package/templates/action-rpg/src/game.ts +40 -0
- package/templates/action-rpg/src/items/Inventory.ts +163 -0
- package/templates/action-rpg/src/loot/drops.ts +40 -0
- package/templates/action-rpg/src/main.ts +15 -0
- package/templates/action-rpg/src/physics.ts +40 -0
- package/templates/action-rpg/src/playtest-events.ts +15 -0
- package/templates/action-rpg/src/progress.ts +35 -0
- package/templates/action-rpg/src/render/camera.ts +26 -0
- package/templates/action-rpg/src/render/lighting.ts +30 -0
- package/templates/action-rpg/src/render/loading.ts +90 -0
- package/templates/action-rpg/src/render/materials.ts +40 -0
- package/templates/action-rpg/src/render/palette.ts +9 -0
- package/templates/action-rpg/src/render/postprocessing.ts +18 -0
- package/templates/action-rpg/src/render/shapes.ts +110 -0
- package/templates/action-rpg/src/render/sky.ts +18 -0
- package/templates/action-rpg/src/render/types.ts +3 -0
- package/templates/action-rpg/src/scenes/Boot.ts +12 -0
- package/templates/action-rpg/src/scenes/Play.ts +449 -0
- package/templates/action-rpg/src/state.ts +40 -0
- package/templates/action-rpg/src/stats/StatBlock.ts +57 -0
- package/templates/action-rpg/src/style.css +32 -0
- package/templates/action-rpg/src/ui/AbilityBar.tsx +28 -0
- package/templates/action-rpg/src/ui/App.tsx +21 -0
- package/templates/action-rpg/src/ui/CharacterPanel.tsx +31 -0
- package/templates/action-rpg/src/ui/Hud.tsx +29 -0
- package/templates/action-rpg/src/ui/InventoryPanel.tsx +37 -0
- package/templates/action-rpg/tailwind.config.ts +7 -0
- package/templates/action-rpg/threenative.config.ts +29 -0
- package/templates/action-rpg/tsconfig.json +17 -0
- package/templates/action-rpg/vite.config.ts +8 -0
- package/templates/defense/.mcp.json +16 -0
- package/templates/defense/AGENTS.md +53 -0
- package/templates/defense/CLAUDE.md +55 -0
- package/templates/defense/README.md +9 -0
- package/templates/defense/index.html +13 -0
- package/templates/defense/kit.json +7 -0
- package/templates/defense/package.json +46 -0
- package/templates/defense/playtests/leak.playtest.json +18 -0
- package/templates/defense/playtests/placement.playtest.json +40 -0
- package/templates/defense/playtests/scan.playtest.json +20 -0
- package/templates/defense/playtests/survive.playtest.json +26 -0
- package/templates/defense/playtests/survives.playtest.json +36 -0
- package/templates/defense/public/favicon.svg +5 -0
- package/templates/defense/src/attackers/Attacker.ts +104 -0
- package/templates/defense/src/board/Route.ts +80 -0
- package/templates/defense/src/economy.ts +36 -0
- package/templates/defense/src/entities/Player.ts +44 -0
- package/templates/defense/src/game.ts +30 -0
- package/templates/defense/src/main.ts +15 -0
- package/templates/defense/src/physics.ts +43 -0
- package/templates/defense/src/placement/Buildable.ts +49 -0
- package/templates/defense/src/playtest-events.ts +15 -0
- package/templates/defense/src/render/camera.ts +10 -0
- package/templates/defense/src/render/lighting.ts +32 -0
- package/templates/defense/src/render/loading.ts +89 -0
- package/templates/defense/src/render/materials.ts +13 -0
- package/templates/defense/src/render/palette.ts +21 -0
- package/templates/defense/src/render/postprocessing.ts +14 -0
- package/templates/defense/src/render/shapes.ts +89 -0
- package/templates/defense/src/render/sky.ts +45 -0
- package/templates/defense/src/scenes/Boot.ts +12 -0
- package/templates/defense/src/scenes/Defense.ts +165 -0
- package/templates/defense/src/state.ts +50 -0
- package/templates/defense/src/style.css +32 -0
- package/templates/defense/src/towers/Tower.ts +96 -0
- package/templates/defense/src/towers/targeting.ts +53 -0
- package/templates/defense/src/ui/App.tsx +17 -0
- package/templates/defense/src/ui/Hud.tsx +61 -0
- package/templates/defense/src/ui/Menu.tsx +7 -0
- package/templates/defense/src/waves.ts +40 -0
- package/templates/defense/tailwind.config.ts +1 -0
- package/templates/defense/threenative.config.ts +29 -0
- package/templates/defense/tsconfig.json +17 -0
- package/templates/defense/vite.config.ts +5 -0
- package/templates/minimal/.mcp.json +16 -0
- package/templates/minimal/AGENTS.md +400 -0
- package/templates/minimal/CLAUDE.md +402 -0
- package/templates/minimal/assets/.gitkeep +0 -0
- package/templates/minimal/index.html +14 -0
- package/templates/minimal/kit.json +7 -0
- package/templates/minimal/package.json +40 -0
- package/templates/minimal/playtests/play.playtest.json +32 -0
- package/templates/minimal/playtests/survives.playtest.json +32 -0
- package/templates/minimal/public/.gitkeep +0 -0
- package/templates/minimal/public/favicon.svg +4 -0
- package/templates/minimal/public/icon.png +0 -0
- package/templates/minimal/src/entities/Player.ts +66 -0
- package/templates/minimal/src/game.ts +28 -0
- package/templates/minimal/src/main.ts +17 -0
- package/templates/minimal/src/render/camera.ts +11 -0
- package/templates/minimal/src/render/hud.ts +53 -0
- package/templates/minimal/src/render/lighting.ts +46 -0
- package/templates/minimal/src/render/loading.ts +113 -0
- package/templates/minimal/src/render/materials.ts +16 -0
- package/templates/minimal/src/render/palette.ts +9 -0
- package/templates/minimal/src/render/postprocessing.ts +20 -0
- package/templates/minimal/src/render/sky.ts +41 -0
- package/templates/minimal/src/scenes/Boot.ts +5 -0
- package/templates/minimal/src/scenes/Play.ts +65 -0
- package/templates/minimal/src/state.ts +1 -0
- package/templates/minimal/src/style.css +41 -0
- package/templates/minimal/threenative.config.ts +32 -0
- package/templates/minimal/tsconfig.json +16 -0
- package/templates/minimal/vite.config.ts +3 -0
- package/templates/platformer/.mcp.json +16 -0
- package/templates/platformer/AGENTS.md +199 -0
- package/templates/platformer/CLAUDE.md +201 -0
- package/templates/platformer/assets/.gitkeep +0 -0
- package/templates/platformer/index.html +13 -0
- package/templates/platformer/kit.json +7 -0
- package/templates/platformer/package.json +50 -0
- package/templates/platformer/playtests/avoidance.playtest.json +23 -0
- package/templates/platformer/playtests/buffer.playtest.json +17 -0
- package/templates/platformer/playtests/chase.playtest.json +21 -0
- package/templates/platformer/playtests/collect.playtest.json +56 -0
- package/templates/platformer/playtests/collision-layers.playtest.json +36 -0
- package/templates/platformer/playtests/coyote.playtest.json +21 -0
- package/templates/platformer/playtests/damage.playtest.json +31 -0
- package/templates/platformer/playtests/dash.playtest.json +21 -0
- package/templates/platformer/playtests/jump.playtest.json +21 -0
- package/templates/platformer/playtests/oneway.playtest.json +17 -0
- package/templates/platformer/playtests/patrol.playtest.json +13 -0
- package/templates/platformer/playtests/performance.playtest.json +21 -0
- package/templates/platformer/playtests/physics.playtest.json +14 -0
- package/templates/platformer/playtests/respawn.playtest.json +31 -0
- package/templates/platformer/playtests/stomp-rise.playtest.json +41 -0
- package/templates/platformer/playtests/stomp.playtest.json +34 -0
- package/templates/platformer/playtests/survives.playtest.json +32 -0
- package/templates/platformer/playtests/terminal-loop-fail.playtest.json +29 -0
- package/templates/platformer/playtests/terminal-loop-win.playtest.json +52 -0
- package/templates/platformer/public/.gitkeep +0 -0
- package/templates/platformer/public/favicon.svg +4 -0
- package/templates/platformer/public/icon.png +0 -0
- package/templates/platformer/src/entities/Character.ts +218 -0
- package/templates/platformer/src/entities/Chaser.ts +118 -0
- package/templates/platformer/src/entities/Patrol.ts +88 -0
- package/templates/platformer/src/entities/Pickup.ts +72 -0
- package/templates/platformer/src/game.ts +27 -0
- package/templates/platformer/src/level/Checkpoints.ts +66 -0
- package/templates/platformer/src/level/Platform.ts +34 -0
- package/templates/platformer/src/main.ts +14 -0
- package/templates/platformer/src/playtest-events.ts +15 -0
- package/templates/platformer/src/render/camera.ts +8 -0
- package/templates/platformer/src/render/lighting.ts +38 -0
- package/templates/platformer/src/render/loading.ts +113 -0
- package/templates/platformer/src/render/materials.ts +11 -0
- package/templates/platformer/src/render/palette.ts +54 -0
- package/templates/platformer/src/render/postprocessing.ts +18 -0
- package/templates/platformer/src/render/rig.ts +60 -0
- package/templates/platformer/src/render/sky.ts +47 -0
- package/templates/platformer/src/render/terrain.ts +31 -0
- package/templates/platformer/src/render/touch-controls.ts +157 -0
- package/templates/platformer/src/render/touch-layout.ts +63 -0
- package/templates/platformer/src/scenes/Boot.ts +12 -0
- package/templates/platformer/src/scenes/Level.ts +176 -0
- package/templates/platformer/src/state.ts +23 -0
- package/templates/platformer/src/style.css +31 -0
- package/templates/platformer/src/ui/App.tsx +17 -0
- package/templates/platformer/src/ui/Hud.tsx +41 -0
- package/templates/platformer/src/ui/Menu.tsx +7 -0
- package/templates/platformer/tailwind.config.ts +2 -0
- package/templates/platformer/threenative.config.ts +32 -0
- package/templates/platformer/tsconfig.json +17 -0
- package/templates/platformer/vite.config.ts +5 -0
- package/templates/racing/.mcp.json +16 -0
- package/templates/racing/AGENTS.md +48 -0
- package/templates/racing/CLAUDE.md +50 -0
- package/templates/racing/index.html +13 -0
- package/templates/racing/kit.json +7 -0
- package/templates/racing/package.json +46 -0
- package/templates/racing/playtests/boost.playtest.json +45 -0
- package/templates/racing/playtests/outcome.playtest.json +78 -0
- package/templates/racing/playtests/ranking.playtest.json +37 -0
- package/templates/racing/playtests/rescue.playtest.json +28 -0
- package/templates/racing/playtests/reverse.playtest.json +37 -0
- package/templates/racing/playtests/shortcut.playtest.json +44 -0
- package/templates/racing/playtests/survives.playtest.json +32 -0
- package/templates/racing/public/favicon.svg +6 -0
- package/templates/racing/src/entities/RacingCar.ts +114 -0
- package/templates/racing/src/entities/Rival.ts +46 -0
- package/templates/racing/src/game.ts +29 -0
- package/templates/racing/src/kart/boost.ts +38 -0
- package/templates/racing/src/main.ts +15 -0
- package/templates/racing/src/playtest-events.ts +15 -0
- package/templates/racing/src/render/camera.ts +28 -0
- package/templates/racing/src/render/lighting.ts +32 -0
- package/templates/racing/src/render/loading.ts +58 -0
- package/templates/racing/src/render/materials.ts +15 -0
- package/templates/racing/src/render/palette.ts +30 -0
- package/templates/racing/src/render/postprocessing.ts +14 -0
- package/templates/racing/src/render/shapes.ts +37 -0
- package/templates/racing/src/render/sky.ts +45 -0
- package/templates/racing/src/scenes/Boot.ts +12 -0
- package/templates/racing/src/scenes/Race.ts +180 -0
- package/templates/racing/src/state.ts +35 -0
- package/templates/racing/src/style.css +32 -0
- package/templates/racing/src/track/Checkline.ts +19 -0
- package/templates/racing/src/track/Lap.ts +104 -0
- package/templates/racing/src/track/Ranking.ts +35 -0
- package/templates/racing/src/track/Track.ts +229 -0
- package/templates/racing/src/track/TrackSector.ts +77 -0
- package/templates/racing/src/track/rescue.ts +21 -0
- package/templates/racing/src/ui/App.tsx +17 -0
- package/templates/racing/src/ui/Hud.tsx +71 -0
- package/templates/racing/src/ui/Menu.tsx +7 -0
- package/templates/racing/tailwind.config.ts +2 -0
- package/templates/racing/threenative.config.ts +29 -0
- package/templates/racing/tsconfig.json +17 -0
- package/templates/racing/vite.config.ts +5 -0
- package/templates/shooter/.mcp.json +16 -0
- package/templates/shooter/AGENTS.md +69 -0
- package/templates/shooter/CLAUDE.md +71 -0
- package/templates/shooter/index.html +13 -0
- package/templates/shooter/kit.json +7 -0
- package/templates/shooter/package.json +46 -0
- package/templates/shooter/playtests/fail.playtest.json +30 -0
- package/templates/shooter/playtests/shooter.playtest.json +89 -0
- package/templates/shooter/playtests/survives.playtest.json +32 -0
- package/templates/shooter/public/favicon.svg +4 -0
- package/templates/shooter/src/combat/damage.ts +41 -0
- package/templates/shooter/src/entities/Pickup.ts +70 -0
- package/templates/shooter/src/entities/Player.ts +93 -0
- package/templates/shooter/src/entities/Target.ts +103 -0
- package/templates/shooter/src/game.ts +36 -0
- package/templates/shooter/src/level/SpawnPoints.ts +28 -0
- package/templates/shooter/src/main.ts +26 -0
- package/templates/shooter/src/physics.ts +49 -0
- package/templates/shooter/src/playtest-events.ts +15 -0
- package/templates/shooter/src/render/camera.ts +29 -0
- package/templates/shooter/src/render/lighting.ts +38 -0
- package/templates/shooter/src/render/loading.ts +86 -0
- package/templates/shooter/src/render/materials.ts +38 -0
- package/templates/shooter/src/render/palette.ts +9 -0
- package/templates/shooter/src/render/postprocessing.ts +19 -0
- package/templates/shooter/src/render/shapes.ts +194 -0
- package/templates/shooter/src/render/sky.ts +37 -0
- package/templates/shooter/src/render/types.ts +3 -0
- package/templates/shooter/src/render/ui.css +212 -0
- package/templates/shooter/src/render/ui.ts +59 -0
- package/templates/shooter/src/scenes/Boot.ts +12 -0
- package/templates/shooter/src/scenes/Play.ts +428 -0
- package/templates/shooter/src/state.ts +29 -0
- package/templates/shooter/src/ui/App.tsx +25 -0
- package/templates/shooter/src/ui/Hud.tsx +49 -0
- package/templates/shooter/src/ui/Menu.tsx +26 -0
- package/templates/shooter/src/waves.ts +50 -0
- package/templates/shooter/src/weapons/Hitscan.ts +43 -0
- package/templates/shooter/src/weapons/Projectile.ts +76 -0
- package/templates/shooter/tailwind.config.ts +5 -0
- package/templates/shooter/threenative.config.ts +29 -0
- package/templates/shooter/tsconfig.json +17 -0
- package/templates/shooter/vite.config.ts +8 -0
- package/templates/starter/.mcp.json +16 -0
- package/templates/starter/AGENTS.md +450 -0
- package/templates/starter/CLAUDE.md +452 -0
- package/templates/starter/assets/.gitkeep +0 -0
- package/templates/starter/index.html +13 -0
- package/templates/starter/kit.json +7 -0
- package/templates/starter/package.json +49 -0
- package/templates/starter/playtests/buffer.playtest.json +29 -0
- package/templates/starter/playtests/coyote.playtest.json +35 -0
- package/templates/starter/playtests/forward.playtest.json +15 -0
- package/templates/starter/playtests/hot-reload.playtest.json +28 -0
- package/templates/starter/playtests/look.playtest.json +43 -0
- package/templates/starter/playtests/monitoring.playtest.json +41 -0
- package/templates/starter/playtests/pause.playtest.json +33 -0
- package/templates/starter/playtests/play.playtest.json +16 -0
- package/templates/starter/playtests/respawn.playtest.json +28 -0
- package/templates/starter/playtests/restart.playtest.json +36 -0
- package/templates/starter/playtests/seed.playtest.json +18 -0
- package/templates/starter/playtests/survives.playtest.json +32 -0
- package/templates/starter/public/.gitkeep +0 -0
- package/templates/starter/public/favicon.svg +4 -0
- package/templates/starter/public/icon.png +0 -0
- package/templates/starter/public/native-proof.glb +0 -0
- package/templates/starter/public/native-proof.png +0 -0
- package/templates/starter/public/pickup.ogg +0 -0
- package/templates/starter/src/entities/Crate.ts +34 -0
- package/templates/starter/src/entities/Player.ts +80 -0
- package/templates/starter/src/game.ts +30 -0
- package/templates/starter/src/main.ts +15 -0
- package/templates/starter/src/render/camera.ts +57 -0
- package/templates/starter/src/render/lighting.ts +55 -0
- package/templates/starter/src/render/materials.ts +12 -0
- package/templates/starter/src/render/palette.ts +9 -0
- package/templates/starter/src/render/postprocessing.ts +18 -0
- package/templates/starter/src/render/shapes.ts +147 -0
- package/templates/starter/src/render/sky.ts +56 -0
- package/templates/starter/src/scenes/Play.ts +161 -0
- package/templates/starter/src/state.ts +10 -0
- package/templates/starter/src/style.css +45 -0
- package/templates/starter/src/ui/App.tsx +19 -0
- package/templates/starter/src/ui/Hud.tsx +37 -0
- package/templates/starter/src/ui/Menu.tsx +43 -0
- package/templates/starter/tailwind.config.ts +2 -0
- package/templates/starter/threenative.config.ts +31 -0
- package/templates/starter/tsconfig.json +17 -0
- package/templates/starter/vite.config.ts +8 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { type ICtx, PathFollow3D } from "@threenative/core";
|
|
2
|
+
import { CharacterBody3D, CollisionShape3D, type IPhysicsContext } from "@threenative/physics";
|
|
3
|
+
import { Group, Mesh, SphereGeometry, Vector3 } from "three";
|
|
4
|
+
import { createMaterials } from "../render/materials.js";
|
|
5
|
+
import { PLAYER_LAYER } from "./Character.js";
|
|
6
|
+
|
|
7
|
+
type GameCtx = ICtx<Record<string, unknown>, IPhysicsContext>;
|
|
8
|
+
|
|
9
|
+
const SPEED = 3.4;
|
|
10
|
+
const ROUTE_REACH_DISTANCE = 0.35;
|
|
11
|
+
const STOP_DISTANCE = 0.7;
|
|
12
|
+
const NO_TARGET_DISTANCE = 1_000_000;
|
|
13
|
+
|
|
14
|
+
export class Chaser {
|
|
15
|
+
readonly mesh: Group;
|
|
16
|
+
readonly body: CharacterBody3D;
|
|
17
|
+
readonly tags = ["enemy", "chaser"];
|
|
18
|
+
readonly #direction = new Vector3();
|
|
19
|
+
readonly #space: IPhysicsContext["directSpaceState"];
|
|
20
|
+
readonly #targetShape = CollisionShape3D.sphere(64);
|
|
21
|
+
readonly #route: PathFollow3D;
|
|
22
|
+
readonly #separation = new Vector3();
|
|
23
|
+
#targetDistance = NO_TARGET_DISTANCE;
|
|
24
|
+
|
|
25
|
+
constructor(ctx: GameCtx, spawn: Vector3);
|
|
26
|
+
constructor(
|
|
27
|
+
ctx: GameCtx,
|
|
28
|
+
player: { readonly mesh: { readonly position: Vector3 } },
|
|
29
|
+
spawn: Vector3,
|
|
30
|
+
);
|
|
31
|
+
constructor(
|
|
32
|
+
ctx: GameCtx,
|
|
33
|
+
playerOrSpawn: Vector3 | { readonly mesh: { readonly position: Vector3 } },
|
|
34
|
+
spawn?: Vector3,
|
|
35
|
+
) {
|
|
36
|
+
const start = spawn ?? (playerOrSpawn as Vector3);
|
|
37
|
+
this.#space = ctx.physics.directSpaceState;
|
|
38
|
+
const side = start.z >= 0.35 ? 3.05 : -3.05;
|
|
39
|
+
this.#route = new PathFollow3D({
|
|
40
|
+
points: [
|
|
41
|
+
new Vector3(4.15, start.y, side),
|
|
42
|
+
new Vector3(3.4, start.y, side),
|
|
43
|
+
new Vector3(2.65, start.y, side),
|
|
44
|
+
],
|
|
45
|
+
speed: SPEED,
|
|
46
|
+
});
|
|
47
|
+
this.mesh = new Group();
|
|
48
|
+
this.mesh.position.copy(start);
|
|
49
|
+
const visual = new Mesh(new SphereGeometry(0.44, 12, 8), createMaterials().accent);
|
|
50
|
+
visual.scale.y = 0.8;
|
|
51
|
+
visual.castShadow = true;
|
|
52
|
+
ctx.add(this.mesh.add(visual));
|
|
53
|
+
this.body = new CharacterBody3D({
|
|
54
|
+
collisionLayer: 4,
|
|
55
|
+
collisionMask: 0xfffc,
|
|
56
|
+
gravity: 0,
|
|
57
|
+
object: this.mesh,
|
|
58
|
+
physics: ctx.physics,
|
|
59
|
+
shape: CollisionShape3D.capsule(0.35, 0.3).setSensor(true),
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
update(dt: number): void {
|
|
64
|
+
const position = this.mesh.position;
|
|
65
|
+
const targetHit = this.#space
|
|
66
|
+
.intersectShape({
|
|
67
|
+
collisionMask: PLAYER_LAYER,
|
|
68
|
+
maxResults: 16,
|
|
69
|
+
position,
|
|
70
|
+
shape: this.#targetShape,
|
|
71
|
+
})
|
|
72
|
+
.find((hit) => hit.entity === "player");
|
|
73
|
+
const target = targetHit?.position;
|
|
74
|
+
const routeSample = this.#route.sample();
|
|
75
|
+
if (!this.#route.completed && position.distanceTo(routeSample.point) <= ROUTE_REACH_DISTANCE)
|
|
76
|
+
this.#route.progressTo(this.#route.progress + SPEED * dt);
|
|
77
|
+
const goal = this.#route.completed ? (target ?? position) : this.#route.sample().point;
|
|
78
|
+
this.#targetDistance =
|
|
79
|
+
target === undefined
|
|
80
|
+
? NO_TARGET_DISTANCE
|
|
81
|
+
: Math.hypot(position.x - target.x, position.y - target.y, position.z - target.z);
|
|
82
|
+
if (this.#route.completed && this.#targetDistance <= STOP_DISTANCE) {
|
|
83
|
+
this.body.velocity.set(0, this.body.velocity.y, 0);
|
|
84
|
+
this.body.moveAndSlide(dt);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
this.#direction.subVectors(goal, position).setY(0);
|
|
88
|
+
const peer = this.mesh.userData.peer;
|
|
89
|
+
if (peer !== undefined) {
|
|
90
|
+
const separation = this.#separation.subVectors(position, peer.position).setY(0);
|
|
91
|
+
const distance = separation.length();
|
|
92
|
+
if (distance > 0.001 && distance < 1.1) {
|
|
93
|
+
this.#direction.addScaledVector(separation.normalize(), (1.1 - distance) * 2);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (this.#direction.lengthSq() > 0.0001) this.#direction.normalize().multiplyScalar(SPEED);
|
|
97
|
+
this.body.velocity.set(this.#direction.x, this.body.velocity.y, this.#direction.z);
|
|
98
|
+
this.body.moveAndSlide(dt);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
debug(): Record<string, unknown> {
|
|
102
|
+
const peer = this.mesh.userData.peer;
|
|
103
|
+
return {
|
|
104
|
+
position: this.mesh.position.toArray(),
|
|
105
|
+
routeDistance: this.#route.progress,
|
|
106
|
+
routeComplete: this.#route.completed,
|
|
107
|
+
routeSample: this.#route.sample().point.toArray(),
|
|
108
|
+
separation: this.mesh.position.distanceTo(peer?.position ?? this.mesh.position),
|
|
109
|
+
steeringFinished: this.#targetDistance <= STOP_DISTANCE,
|
|
110
|
+
targetDistance: this.#targetDistance,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
dispose(): void {
|
|
115
|
+
this.body.dispose();
|
|
116
|
+
this.mesh.removeFromParent();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { ICtx } from "@threenative/core";
|
|
2
|
+
import { Area3D, CollisionShape3D, type IPhysicsContext } from "@threenative/physics";
|
|
3
|
+
import { Group, Mesh, SphereGeometry, type Vector3 } from "three";
|
|
4
|
+
import { createMaterials } from "../render/materials.js";
|
|
5
|
+
import type { GameState } from "../state.js";
|
|
6
|
+
import type { Character, PLATFORMER_FEEL } from "./Character.js";
|
|
7
|
+
|
|
8
|
+
type GameCtx = ICtx<GameState, IPhysicsContext>;
|
|
9
|
+
|
|
10
|
+
export class Patrol {
|
|
11
|
+
readonly mesh: Group;
|
|
12
|
+
readonly area: Area3D;
|
|
13
|
+
readonly tags = ["enemy", "patrol"];
|
|
14
|
+
defeated = false;
|
|
15
|
+
#from: Vector3;
|
|
16
|
+
#to: Vector3;
|
|
17
|
+
#direction = 1;
|
|
18
|
+
#feel: typeof PLATFORMER_FEEL;
|
|
19
|
+
#unsubscribe: () => void;
|
|
20
|
+
|
|
21
|
+
constructor(
|
|
22
|
+
ctx: GameCtx,
|
|
23
|
+
player: Character,
|
|
24
|
+
from: Vector3,
|
|
25
|
+
to: Vector3,
|
|
26
|
+
onStomp: () => void,
|
|
27
|
+
onTouch: (fromX: number) => void,
|
|
28
|
+
feel: typeof PLATFORMER_FEEL,
|
|
29
|
+
) {
|
|
30
|
+
this.#from = from.clone();
|
|
31
|
+
this.#to = to.clone();
|
|
32
|
+
this.#feel = feel;
|
|
33
|
+
this.mesh = new Group();
|
|
34
|
+
const body = new Mesh(new SphereGeometry(0.46, 12, 8), createMaterials().accent);
|
|
35
|
+
body.scale.y = 0.72;
|
|
36
|
+
body.castShadow = true;
|
|
37
|
+
this.mesh.add(body);
|
|
38
|
+
this.mesh.position.copy(from);
|
|
39
|
+
ctx.add(this.mesh);
|
|
40
|
+
this.area = new Area3D({
|
|
41
|
+
entity: "patrol",
|
|
42
|
+
physics: ctx.physics,
|
|
43
|
+
position: from,
|
|
44
|
+
shape: CollisionShape3D.sphere(0.62),
|
|
45
|
+
});
|
|
46
|
+
this.#unsubscribe = this.area.on("bodyEntered", (body) => {
|
|
47
|
+
if (this.defeated || body !== player.body) return;
|
|
48
|
+
const stomped =
|
|
49
|
+
player.mesh.position.y > this.mesh.position.y + this.#feel.stompHeight &&
|
|
50
|
+
player.body.velocity.y < this.#feel.stompFallSpeed;
|
|
51
|
+
if (stomped) {
|
|
52
|
+
this.defeated = true;
|
|
53
|
+
this.mesh.visible = false;
|
|
54
|
+
player.bounce();
|
|
55
|
+
onStomp();
|
|
56
|
+
ctx.entities.queueFree(this);
|
|
57
|
+
} else onTouch(this.mesh.position.x);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
update(dt: number): void {
|
|
62
|
+
if (this.defeated) return;
|
|
63
|
+
const next = this.mesh.position.x + this.#direction * this.#feel.patrolSpeed * dt;
|
|
64
|
+
if (next >= this.#to.x) {
|
|
65
|
+
this.mesh.position.x = this.#to.x;
|
|
66
|
+
this.#direction = -1;
|
|
67
|
+
} else if (next <= this.#from.x) {
|
|
68
|
+
this.mesh.position.x = this.#from.x;
|
|
69
|
+
this.#direction = 1;
|
|
70
|
+
} else this.mesh.position.x = next;
|
|
71
|
+
this.mesh.rotation.y = this.#direction > 0 ? 0 : Math.PI;
|
|
72
|
+
this.area.setPosition(this.mesh.position);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
debug(): Record<string, unknown> {
|
|
76
|
+
return {
|
|
77
|
+
defeated: this.defeated,
|
|
78
|
+
direction: this.#direction,
|
|
79
|
+
position: this.mesh.position.toArray(),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
dispose(): void {
|
|
84
|
+
this.#unsubscribe();
|
|
85
|
+
this.area.dispose();
|
|
86
|
+
this.mesh.removeFromParent();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { ICtx } from "@threenative/core";
|
|
2
|
+
import { Area3D, CollisionShape3D, type IPhysicsContext } from "@threenative/physics";
|
|
3
|
+
import { CylinderGeometry, Group, Mesh, Vector3 } from "three";
|
|
4
|
+
import { createMaterials } from "../render/materials.js";
|
|
5
|
+
import type { GameState } from "../state.js";
|
|
6
|
+
import { PLAYER_LAYER } from "./Character.js";
|
|
7
|
+
type GameCtx = ICtx<GameState, IPhysicsContext>;
|
|
8
|
+
export class Pickup {
|
|
9
|
+
readonly mesh: Group;
|
|
10
|
+
readonly area: Area3D;
|
|
11
|
+
readonly tags = ["pickup", "coin"];
|
|
12
|
+
collected = false;
|
|
13
|
+
#base: Vector3;
|
|
14
|
+
#time = 0;
|
|
15
|
+
#unsubscribe: () => void;
|
|
16
|
+
#updateHandle!: ReturnType<GameCtx["every"]>;
|
|
17
|
+
|
|
18
|
+
constructor(ctx: GameCtx, at: Vector3, onCollect: () => void) {
|
|
19
|
+
this.#base = at.clone();
|
|
20
|
+
this.mesh = new Group();
|
|
21
|
+
const materials = createMaterials();
|
|
22
|
+
const coin = new Mesh(
|
|
23
|
+
new CylinderGeometry(0.28, 0.28, 0.12, 18).rotateX(Math.PI / 2),
|
|
24
|
+
materials.accent,
|
|
25
|
+
);
|
|
26
|
+
coin.castShadow = true;
|
|
27
|
+
this.mesh.add(coin);
|
|
28
|
+
this.mesh.position.copy(at);
|
|
29
|
+
ctx.add(this.mesh);
|
|
30
|
+
this.area = new Area3D({
|
|
31
|
+
collisionMask: PLAYER_LAYER,
|
|
32
|
+
entity: "coin",
|
|
33
|
+
physics: ctx.physics,
|
|
34
|
+
position: at,
|
|
35
|
+
shape: CollisionShape3D.sphere(0.68),
|
|
36
|
+
});
|
|
37
|
+
this.#unsubscribe = this.area.on("bodyEntered", () => {
|
|
38
|
+
if (this.collected) return;
|
|
39
|
+
this.collected = true;
|
|
40
|
+
this.mesh.visible = false;
|
|
41
|
+
onCollect();
|
|
42
|
+
ctx.entities.queueFree(this);
|
|
43
|
+
});
|
|
44
|
+
this.#updateHandle = ctx.every((dt) => this.update(dt));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
update(dt: number): void {
|
|
48
|
+
if (this.collected) return;
|
|
49
|
+
this.#time += dt;
|
|
50
|
+
this.mesh.rotation.y += dt * 4;
|
|
51
|
+
this.mesh.position.y = this.#base.y + Math.sin(this.#time * 3 + this.#base.x) * 0.12;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
debug(): Record<string, unknown> {
|
|
55
|
+
return { collected: this.collected, position: this.mesh.position.toArray() };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
dispose(): void {
|
|
59
|
+
this.#updateHandle.cancel();
|
|
60
|
+
this.#unsubscribe();
|
|
61
|
+
this.area.dispose();
|
|
62
|
+
this.mesh.removeFromParent();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function coinArc(start: Vector3, count: number, span: number, rise: number): Vector3[] {
|
|
67
|
+
if (!Number.isInteger(count) || count <= 0) throw new Error("coinArc requires a positive count.");
|
|
68
|
+
return Array.from({ length: count }, (_, index) => {
|
|
69
|
+
const t = count === 1 ? 0.5 : index / (count - 1);
|
|
70
|
+
return new Vector3(start.x + t * span, start.y + Math.sin(t * Math.PI) * rise, start.z);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
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 as events } from "./playtest-events.js";
|
|
7
|
+
import { Boot } from "./scenes/Boot.js";
|
|
8
|
+
import { Level } from "./scenes/Level.js";
|
|
9
|
+
import type { GameState } from "./state.js";
|
|
10
|
+
export default defineGame<GameState, IPhysicsContext>({
|
|
11
|
+
input: {
|
|
12
|
+
// The four directions of `input.vector("move")`. Declared rather than inherited from the
|
|
13
|
+
// default binding, so the axis every scene reads is visible where the game is defined.
|
|
14
|
+
move: {
|
|
15
|
+
down: ["ArrowDown", "KeyS"],
|
|
16
|
+
left: ["ArrowLeft", "KeyA"],
|
|
17
|
+
right: ["ArrowRight", "KeyD"],
|
|
18
|
+
up: ["ArrowUp", "KeyW"],
|
|
19
|
+
},
|
|
20
|
+
dash: { buttons: [7], keys: ["ShiftLeft", "ShiftRight"] },
|
|
21
|
+
jump: { buttons: [0], keys: ["Space"] },
|
|
22
|
+
},
|
|
23
|
+
plugins: [rapier({ gravity: { x: 0, y: -26, z: 0 } }), playtest({ events })],
|
|
24
|
+
render: config.renderer,
|
|
25
|
+
scenes: { boot: Boot, level: Level },
|
|
26
|
+
start: "boot",
|
|
27
|
+
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { Vector3 } from "three";
|
|
2
|
+
import type { Character, PLATFORMER_FEEL } from "../entities/Character.js";
|
|
3
|
+
|
|
4
|
+
type RespawnTarget = Pick<Character, "body" | "mesh" | "visual">;
|
|
5
|
+
|
|
6
|
+
export class Checkpoints {
|
|
7
|
+
readonly points: readonly Vector3[];
|
|
8
|
+
readonly maxHearts: number;
|
|
9
|
+
hearts: number;
|
|
10
|
+
currentIndex = 0;
|
|
11
|
+
respawns = 0;
|
|
12
|
+
#invulnerable = 0;
|
|
13
|
+
#feel: typeof PLATFORMER_FEEL;
|
|
14
|
+
|
|
15
|
+
constructor(points: readonly Vector3[], maxHearts: number, feel: typeof PLATFORMER_FEEL) {
|
|
16
|
+
if (points.length === 0) throw new Error("Checkpoints requires at least one checkpoint.");
|
|
17
|
+
if (!Number.isInteger(maxHearts) || maxHearts <= 0)
|
|
18
|
+
throw new Error("Checkpoints requires a positive heart count.");
|
|
19
|
+
this.points = points.map((point) => point.clone());
|
|
20
|
+
this.maxHearts = maxHearts;
|
|
21
|
+
this.hearts = maxHearts;
|
|
22
|
+
this.#feel = feel;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
get invulnerable(): boolean {
|
|
26
|
+
return this.#invulnerable > 0;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
update(dt: number, target: RespawnTarget): void {
|
|
30
|
+
if (!Number.isFinite(dt) || dt < 0) throw new Error("Checkpoints.update requires a valid dt.");
|
|
31
|
+
this.#invulnerable = Math.max(0, this.#invulnerable - dt);
|
|
32
|
+
target.visual.visible =
|
|
33
|
+
this.#invulnerable <= 0 || Math.floor(this.#invulnerable * this.#feel.blinkRate) % 2 === 0;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
pass(position: Vector3): void {
|
|
37
|
+
while (this.currentIndex + 1 < this.points.length) {
|
|
38
|
+
const next = this.points[this.currentIndex + 1];
|
|
39
|
+
if (next === undefined || position.x < next.x) break;
|
|
40
|
+
this.currentIndex += 1;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
hurt(target: RespawnTarget, fromX: number): boolean {
|
|
45
|
+
if (this.#invulnerable > 0 || this.hearts <= 0) return false;
|
|
46
|
+
this.hearts -= 1;
|
|
47
|
+
this.#invulnerable = this.#feel.invulnerabilityTime;
|
|
48
|
+
const away = Math.sign(target.mesh.position.x - fromX) || -1;
|
|
49
|
+
target.body.velocity.set(
|
|
50
|
+
away * this.#feel.hurtHorizontalSpeed,
|
|
51
|
+
this.#feel.hurtVerticalSpeed,
|
|
52
|
+
0,
|
|
53
|
+
);
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
respawn(target: RespawnTarget): void {
|
|
58
|
+
if (this.hearts <= 0) return;
|
|
59
|
+
const point = this.points[this.currentIndex];
|
|
60
|
+
if (point === undefined) throw new Error(`Missing checkpoint ${this.currentIndex}.`);
|
|
61
|
+
target.body.velocity.set(0, 0, 0);
|
|
62
|
+
target.body.teleport(point);
|
|
63
|
+
this.#invulnerable = this.#feel.invulnerabilityTime;
|
|
64
|
+
this.respawns += 1;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { CollisionShape3D, type IPhysicsContext, RigidBody3D } from "@threenative/physics";
|
|
2
|
+
import type { Vector3 } from "three";
|
|
3
|
+
import { platform as platformMesh } from "../render/terrain.js";
|
|
4
|
+
import type { GameCtx } from "../scenes/Level.js";
|
|
5
|
+
|
|
6
|
+
export const ONE_WAY_LAYER = 2;
|
|
7
|
+
|
|
8
|
+
export function createPlatform(
|
|
9
|
+
ctx: GameCtx,
|
|
10
|
+
at: Vector3,
|
|
11
|
+
width: number,
|
|
12
|
+
options: { readonly depth?: number; readonly oneWay?: boolean; readonly seed?: number } = {},
|
|
13
|
+
) {
|
|
14
|
+
const depth = options.depth ?? 6;
|
|
15
|
+
const height = 0.4;
|
|
16
|
+
const visual = platformMesh(width, height, { depth, oneWay: options.oneWay, seed: options.seed });
|
|
17
|
+
visual.position.set(at.x, at.y - height / 2, at.z);
|
|
18
|
+
ctx.add(visual);
|
|
19
|
+
const body = new RigidBody3D({
|
|
20
|
+
collisionLayer: options.oneWay === true ? ONE_WAY_LAYER : undefined,
|
|
21
|
+
object: visual,
|
|
22
|
+
physics: ctx.physics,
|
|
23
|
+
shape: CollisionShape3D.box(width, height, depth),
|
|
24
|
+
type: "fixed",
|
|
25
|
+
});
|
|
26
|
+
return {
|
|
27
|
+
body,
|
|
28
|
+
dispose: () => {
|
|
29
|
+
body.dispose();
|
|
30
|
+
visual.removeFromParent();
|
|
31
|
+
},
|
|
32
|
+
visual,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
import.meta.hot?.accept();
|
|
8
|
+
acceptHotUpdate(game, import.meta.hot);
|
|
9
|
+
const root = document.getElementById("root");
|
|
10
|
+
if (root === null) throw new Error("Missing #root element.");
|
|
11
|
+
const appRoot = root as typeof root & { __threenativeRoot?: ReturnType<typeof createRoot> };
|
|
12
|
+
const reactRoot = appRoot.__threenativeRoot ?? createRoot(appRoot);
|
|
13
|
+
appRoot.__threenativeRoot = reactRoot;
|
|
14
|
+
reactRoot.render(createElement(App, { game }));
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
type PlaytestEvent = {
|
|
2
|
+
readonly [key: string]: string;
|
|
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,38 @@
|
|
|
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.skyHigh, palette.shadow, 1.4));
|
|
16
|
+
|
|
17
|
+
const key = new DirectionalLight(palette.accent, 3.2);
|
|
18
|
+
key.position.set(5, 8, 4);
|
|
19
|
+
key.castShadow = true;
|
|
20
|
+
// 1024² is one quarter of a 2048² map's texel storage and fill work. The
|
|
21
|
+
// 24-unit extent covers the generated opening route; widen both together
|
|
22
|
+
// when a larger level needs more shadow coverage, accepting softer shadows.
|
|
23
|
+
key.shadow.mapSize.set(1024, 1024);
|
|
24
|
+
key.shadow.camera.near = 0.5;
|
|
25
|
+
key.shadow.camera.far = 80;
|
|
26
|
+
const extent = 24;
|
|
27
|
+
key.shadow.camera.left = -extent;
|
|
28
|
+
key.shadow.camera.right = extent;
|
|
29
|
+
key.shadow.camera.top = extent;
|
|
30
|
+
key.shadow.camera.bottom = -extent;
|
|
31
|
+
key.shadow.normalBias = 0.04;
|
|
32
|
+
scene.add(key);
|
|
33
|
+
|
|
34
|
+
const rim = new DirectionalLight(palette.skyLow, 0.9);
|
|
35
|
+
rim.position.set(-5, 4, -7);
|
|
36
|
+
scene.add(rim);
|
|
37
|
+
scene.add(new AmbientLight(palette.shadow, 0.24));
|
|
38
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
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
|
+
/**
|
|
12
|
+
* What the framework hands a loading screen. Structural on purpose: nothing under `src/render/`
|
|
13
|
+
* imports the framework, and `ctx` already satisfies this shape.
|
|
14
|
+
*/
|
|
15
|
+
export interface LoadingHost {
|
|
16
|
+
readonly camera: Camera;
|
|
17
|
+
readonly scene: Scene;
|
|
18
|
+
readonly canvasLayer: {
|
|
19
|
+
readonly camera: OrthographicCamera;
|
|
20
|
+
readonly scene: Scene;
|
|
21
|
+
opaque: boolean;
|
|
22
|
+
};
|
|
23
|
+
readonly renderer: { compileAsync(scene: Scene, camera: Camera): Promise<void> };
|
|
24
|
+
readonly startup: { readonly progress: number; whenReady(): Promise<void> };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Draws the loading screen in the framework's pixel-sized canvas layer. Colours, layout, and the
|
|
29
|
+
* shape of the bar live here in generated source, so they remain the game's to change or delete.
|
|
30
|
+
*/
|
|
31
|
+
export function createLoadingScreen(host: LoadingHost) {
|
|
32
|
+
const layer = host.canvasLayer;
|
|
33
|
+
const camera = layer.camera;
|
|
34
|
+
const cover = (color: number, renderOrder: number) => {
|
|
35
|
+
const mesh = new Mesh(
|
|
36
|
+
new PlaneGeometry(1, 1),
|
|
37
|
+
new MeshBasicMaterial({ color, depthTest: false, depthWrite: false }),
|
|
38
|
+
);
|
|
39
|
+
mesh.frustumCulled = false;
|
|
40
|
+
mesh.renderOrder = renderOrder;
|
|
41
|
+
layer.scene.add(mesh);
|
|
42
|
+
return mesh;
|
|
43
|
+
};
|
|
44
|
+
const backdrop = cover(palette.skyLow, 0);
|
|
45
|
+
const track = cover(palette.skyHigh, 1);
|
|
46
|
+
const fill = cover(palette.accent, 2);
|
|
47
|
+
const parts = [backdrop, track, fill];
|
|
48
|
+
layer.opaque = true;
|
|
49
|
+
|
|
50
|
+
let width = 0;
|
|
51
|
+
let height = 0;
|
|
52
|
+
let lastProgress = 0;
|
|
53
|
+
|
|
54
|
+
const layout = (): void => {
|
|
55
|
+
const nextWidth = camera.right - camera.left;
|
|
56
|
+
const nextHeight = camera.top - camera.bottom;
|
|
57
|
+
width = Number.isFinite(nextWidth) && nextWidth > 0 ? nextWidth : 1;
|
|
58
|
+
height = Number.isFinite(nextHeight) && nextHeight > 0 ? nextHeight : 1;
|
|
59
|
+
backdrop.scale.set(width, height, 1);
|
|
60
|
+
track.scale.set(width * 0.5, height * 0.012, 1);
|
|
61
|
+
track.position.set(0, -height * 0.05, 0);
|
|
62
|
+
fill.position.set(0, -height * 0.05, 0);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const setProgress = (progress: number): void => {
|
|
66
|
+
// `Math.max(0, Math.min(1, NaN))` is NaN, so non-finite progress means no progress.
|
|
67
|
+
lastProgress = Number.isFinite(progress) ? Math.max(0, Math.min(1, progress)) : 0;
|
|
68
|
+
const full = width * 0.5;
|
|
69
|
+
const barWidth = Math.max(full * 0.002, full * lastProgress);
|
|
70
|
+
fill.scale.set(barWidth, height * 0.012, 1);
|
|
71
|
+
fill.position.x = -full / 2 + barWidth / 2;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
let laidOutWidth = camera.right - camera.left;
|
|
75
|
+
let laidOutHeight = camera.top - camera.bottom;
|
|
76
|
+
layout();
|
|
77
|
+
setProgress(0);
|
|
78
|
+
|
|
79
|
+
let done = false;
|
|
80
|
+
const finish = (): void => {
|
|
81
|
+
if (done) return;
|
|
82
|
+
done = true;
|
|
83
|
+
for (const mesh of parts) {
|
|
84
|
+
mesh.removeFromParent();
|
|
85
|
+
mesh.geometry.dispose();
|
|
86
|
+
mesh.material.dispose();
|
|
87
|
+
}
|
|
88
|
+
layer.opaque = false;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
void (async () => {
|
|
92
|
+
await host.startup.whenReady();
|
|
93
|
+
// Compile the collapsed world's pipelines while the opaque canvas layer still covers it.
|
|
94
|
+
await host.renderer.compileAsync(host.scene, host.camera).catch(() => undefined);
|
|
95
|
+
finish();
|
|
96
|
+
})();
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
update(): void {
|
|
100
|
+
if (done) return;
|
|
101
|
+
const nextWidth = camera.right - camera.left;
|
|
102
|
+
const nextHeight = camera.top - camera.bottom;
|
|
103
|
+
if (nextWidth !== laidOutWidth || nextHeight !== laidOutHeight) {
|
|
104
|
+
laidOutWidth = nextWidth;
|
|
105
|
+
laidOutHeight = nextHeight;
|
|
106
|
+
layout();
|
|
107
|
+
setProgress(lastProgress);
|
|
108
|
+
}
|
|
109
|
+
setProgress(host.startup.progress);
|
|
110
|
+
},
|
|
111
|
+
finish,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { MeshStandardMaterial } from "three";
|
|
2
|
+
import { palette, toon } from "./palette.js";
|
|
3
|
+
|
|
4
|
+
export function createMaterials() {
|
|
5
|
+
return {
|
|
6
|
+
accent: toon(palette.accent),
|
|
7
|
+
character: toon(palette.character),
|
|
8
|
+
ground: toon(palette.ground),
|
|
9
|
+
shadow: new MeshStandardMaterial({ color: palette.shadow, roughness: 0.9 }),
|
|
10
|
+
};
|
|
11
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { BoxGeometry, BufferAttribute, Group, Mesh, MeshStandardMaterial } from "three";
|
|
2
|
+
|
|
3
|
+
export const palette = {
|
|
4
|
+
skyHigh: 0x1458bd,
|
|
5
|
+
skyLow: 0x83d8f2,
|
|
6
|
+
ground: 0x55b935,
|
|
7
|
+
character: 0xf29a38,
|
|
8
|
+
accent: 0xffc83d,
|
|
9
|
+
shadow: 0x241d1a,
|
|
10
|
+
} as const;
|
|
11
|
+
|
|
12
|
+
const materialCache = new Map<number, MeshStandardMaterial>();
|
|
13
|
+
|
|
14
|
+
export function toon(color: number): MeshStandardMaterial {
|
|
15
|
+
const cached = materialCache.get(color);
|
|
16
|
+
if (cached !== undefined) return cached;
|
|
17
|
+
const material = new MeshStandardMaterial({ color, roughness: 0.78, metalness: 0 });
|
|
18
|
+
materialCache.set(color, material);
|
|
19
|
+
return material;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function mottle(mesh: Mesh, seed = 1): void {
|
|
23
|
+
const position = mesh.geometry.getAttribute("position");
|
|
24
|
+
if (position === undefined) return;
|
|
25
|
+
const colors = new Float32Array(position.count * 3);
|
|
26
|
+
for (let index = 0; index < position.count; index += 1) {
|
|
27
|
+
const value = 0.88 + ((index * 17 + seed * 13) % 7) * 0.02;
|
|
28
|
+
colors[index * 3] = value;
|
|
29
|
+
colors[index * 3 + 1] = value;
|
|
30
|
+
colors[index * 3 + 2] = value;
|
|
31
|
+
}
|
|
32
|
+
mesh.geometry.setAttribute("color", new BufferAttribute(colors, 3));
|
|
33
|
+
const material = (mesh.material as MeshStandardMaterial).clone();
|
|
34
|
+
material.vertexColors = true;
|
|
35
|
+
mesh.material = material;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function rockBox(width: number, height: number, depth: number, seed = 1): Group {
|
|
39
|
+
const group = new Group();
|
|
40
|
+
const top = new Mesh(new BoxGeometry(width, 0.28, depth), toon(palette.ground));
|
|
41
|
+
top.position.y = height / 2 - 0.14;
|
|
42
|
+
top.castShadow = true;
|
|
43
|
+
top.receiveShadow = true;
|
|
44
|
+
mottle(top, seed);
|
|
45
|
+
const stone = new Mesh(
|
|
46
|
+
new BoxGeometry(width * 0.84, height - 0.28, depth * 0.82),
|
|
47
|
+
toon(palette.shadow),
|
|
48
|
+
);
|
|
49
|
+
stone.position.y = -0.14;
|
|
50
|
+
stone.castShadow = true;
|
|
51
|
+
stone.receiveShadow = true;
|
|
52
|
+
group.add(top, stone);
|
|
53
|
+
return group;
|
|
54
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
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 = {
|
|
6
|
+
kind: string;
|
|
7
|
+
raw: unknown;
|
|
8
|
+
setOutputNode(node: unknown): void;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export function setupPost(renderer: OutputRenderer, scene: Scene, camera: Camera): void {
|
|
12
|
+
const raw = renderer.raw as { toneMapping?: number; toneMappingExposure?: number };
|
|
13
|
+
raw.toneMapping = ACESFilmicToneMapping;
|
|
14
|
+
raw.toneMappingExposure = 1.15;
|
|
15
|
+
if (renderer.kind !== "webgpu") return;
|
|
16
|
+
const colour = pass(scene, camera).getTextureNode();
|
|
17
|
+
renderer.setOutputNode(colour.add(bloom(colour, 0.45, 0.5, 0.2)));
|
|
18
|
+
}
|