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,180 @@
|
|
|
1
|
+
import { type ICtx, Scene, type SceneFrame } from "@threenative/core";
|
|
2
|
+
import type { IPhysicsContext } from "@threenative/physics";
|
|
3
|
+
import { type PerspectiveCamera, Vector3 } from "three";
|
|
4
|
+
import { RACING_FEEL, RacingCar } from "../entities/RacingCar.js";
|
|
5
|
+
import { Rival } from "../entities/Rival.js";
|
|
6
|
+
import { emitPlaytestEvent } from "../playtest-events.js";
|
|
7
|
+
import { cameraRoll, chaseCamera, setupCamera } from "../render/camera.js";
|
|
8
|
+
import { setupLighting } from "../render/lighting.js";
|
|
9
|
+
import { createLoadingScreen } from "../render/loading.js";
|
|
10
|
+
import { createMaterials } from "../render/materials.js";
|
|
11
|
+
import { setupPost } from "../render/postprocessing.js";
|
|
12
|
+
import { flag } from "../render/shapes.js";
|
|
13
|
+
import { setupSky } from "../render/sky.js";
|
|
14
|
+
import { type GameState, type RaceStatus, resolveRaceStatus } from "../state.js";
|
|
15
|
+
import { Lap } from "../track/Lap.js";
|
|
16
|
+
import { rankRacers } from "../track/Ranking.js";
|
|
17
|
+
import { TOTAL_LAPS, buildTrack, intersectRay, roadRayProbe } from "../track/Track.js";
|
|
18
|
+
import { TrackSector } from "../track/TrackSector.js";
|
|
19
|
+
|
|
20
|
+
export type GameCtx = ICtx<GameState, IPhysicsContext>;
|
|
21
|
+
|
|
22
|
+
const SPAWN = new Vector3(2, 0.55, -18);
|
|
23
|
+
const TIME_LIMIT = 90;
|
|
24
|
+
|
|
25
|
+
export class Race extends Scene<GameState, IPhysicsContext> {
|
|
26
|
+
static override readonly initialState: GameState = {
|
|
27
|
+
boostActive: false,
|
|
28
|
+
boostPeakSpeed: 0,
|
|
29
|
+
boostUses: 0,
|
|
30
|
+
completedLaps: 0,
|
|
31
|
+
elapsed: 0,
|
|
32
|
+
lastOnRoad: [SPAWN.x, SPAWN.y, SPAWN.z],
|
|
33
|
+
place: 1,
|
|
34
|
+
position: "P1",
|
|
35
|
+
raceStatus: "RACING",
|
|
36
|
+
rescueHeading: 0,
|
|
37
|
+
rescueHeadingError: -1,
|
|
38
|
+
rescuePositionError: -1,
|
|
39
|
+
rescues: 0,
|
|
40
|
+
shortcutRejects: 0,
|
|
41
|
+
speed: 0,
|
|
42
|
+
speedAfterBoost: 0,
|
|
43
|
+
sameDistanceRanking: "",
|
|
44
|
+
topSpeed: 0,
|
|
45
|
+
totalLaps: TOTAL_LAPS,
|
|
46
|
+
trackProgress: 0,
|
|
47
|
+
reverseRejects: 0,
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
override enter(ctx: GameCtx): SceneFrame<GameState, IPhysicsContext> {
|
|
51
|
+
setupSky(ctx.scene);
|
|
52
|
+
setupLighting(ctx.scene, ctx.renderer.raw as Parameters<typeof setupLighting>[1]);
|
|
53
|
+
setupPost(ctx.renderer, ctx.scene, ctx.camera);
|
|
54
|
+
const loading = createLoadingScreen(ctx);
|
|
55
|
+
const camera = ctx.camera as PerspectiveCamera;
|
|
56
|
+
setupCamera(camera);
|
|
57
|
+
ctx.add(camera);
|
|
58
|
+
const track = buildTrack(ctx);
|
|
59
|
+
const car = new RacingCar(ctx, SPAWN);
|
|
60
|
+
const spawnDistance = track.route.project(SPAWN).distanceFromStart % track.route.totalLength;
|
|
61
|
+
const rival = new Rival(track.route, spawnDistance + track.route.totalLength);
|
|
62
|
+
rival.update(0);
|
|
63
|
+
rival.mesh.position.copy(car.mesh.position);
|
|
64
|
+
ctx.add(rival.mesh);
|
|
65
|
+
ctx.entities.add("player", car);
|
|
66
|
+
ctx.entities.add("rival", rival);
|
|
67
|
+
const flagMaterial = createMaterials().boost;
|
|
68
|
+
for (const point of [new Vector3(14, 0, -18), new Vector3(-14, 0, 18)]) {
|
|
69
|
+
const marker = flag(flagMaterial);
|
|
70
|
+
marker.position.copy(point);
|
|
71
|
+
ctx.add(marker);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const lap = new Lap(
|
|
75
|
+
{ body: car.body, forward: car.travelDirection },
|
|
76
|
+
track.gates,
|
|
77
|
+
TOTAL_LAPS,
|
|
78
|
+
(completed) => emitPlaytestEvent({ entity: "player", lap: completed, name: "lap-completed" }),
|
|
79
|
+
);
|
|
80
|
+
const rankRace = () =>
|
|
81
|
+
rankRacers(track.route, [
|
|
82
|
+
{ id: "player", lap: lap.completed, position: car.mesh.position },
|
|
83
|
+
{ id: "rival", lap: rival.lap, position: rival.mesh.position },
|
|
84
|
+
]);
|
|
85
|
+
const initialRanked = rankRace();
|
|
86
|
+
const initialPlayer = initialRanked.find((entry) => entry.id === "player");
|
|
87
|
+
if (initialPlayer === undefined) throw new Error("Race ranking lost the player at spawn.");
|
|
88
|
+
const fallbackProbe = roadRayProbe(track.roadMeshes);
|
|
89
|
+
const sector = new TrackSector({
|
|
90
|
+
route: track.route,
|
|
91
|
+
intersectRay: intersectRay(ctx.physics, fallbackProbe),
|
|
92
|
+
});
|
|
93
|
+
track.boostArea.on("bodyEntered", (body) => {
|
|
94
|
+
if (body === car.body) {
|
|
95
|
+
car.boost.activate();
|
|
96
|
+
emitPlaytestEvent({ entity: "player", name: "boost-applied" });
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
car.boost.update(0);
|
|
100
|
+
let elapsed = 0;
|
|
101
|
+
let status: RaceStatus = "RACING";
|
|
102
|
+
let rescues = 0;
|
|
103
|
+
let rescueHeadingError = -1;
|
|
104
|
+
let rescuePositionError = -1;
|
|
105
|
+
let speedAfterBoost = 0;
|
|
106
|
+
let boostSettled = false;
|
|
107
|
+
let sameDistanceRanking = initialRanked[0]?.id === "rival" ? "lap-ahead" : "lap-behind";
|
|
108
|
+
const observedPosition = SPAWN.clone();
|
|
109
|
+
sector.update(SPAWN, car.forward, 0);
|
|
110
|
+
chaseCamera(camera, car.mesh.position, car.forward, 1);
|
|
111
|
+
|
|
112
|
+
const advanceRace = (frameCtx: GameCtx, dt: number): void => {
|
|
113
|
+
if (status !== "RACING") return;
|
|
114
|
+
lap.observe(observedPosition, car.mesh.position);
|
|
115
|
+
observedPosition.copy(car.mesh.position);
|
|
116
|
+
car.update(frameCtx, dt);
|
|
117
|
+
rival.update(dt);
|
|
118
|
+
sector.update(car.mesh.position, car.forward, dt);
|
|
119
|
+
if (sector.rescue(car)) {
|
|
120
|
+
rescues += 1;
|
|
121
|
+
rescuePositionError = Number(
|
|
122
|
+
car.mesh.position.distanceTo(sector.lastOnRoadPosition).toFixed(4),
|
|
123
|
+
);
|
|
124
|
+
rescueHeadingError = Number(car.forward.angleTo(sector.lastOnRoadHeading).toFixed(4));
|
|
125
|
+
emitPlaytestEvent({ entity: "player", name: "rescued" });
|
|
126
|
+
}
|
|
127
|
+
if (elapsed >= TIME_LIMIT || rescues >= 4) status = "DNF";
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
return (frameCtx, dt) => {
|
|
131
|
+
loading.update();
|
|
132
|
+
if (frameCtx.input.justPressed("restart")) {
|
|
133
|
+
frameCtx.state.set(Race.initialState);
|
|
134
|
+
frameCtx.state.flush();
|
|
135
|
+
void frameCtx.goto("race");
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
elapsed += dt;
|
|
139
|
+
advanceRace(frameCtx, dt);
|
|
140
|
+
if (!car.boost.active && car.boost.uses > 0 && !boostSettled) {
|
|
141
|
+
const settledSpeed = car.speedAfterBoost();
|
|
142
|
+
if (Math.abs(settledSpeed - RACING_FEEL.maxSpeed) < 0.001) {
|
|
143
|
+
speedAfterBoost = settledSpeed;
|
|
144
|
+
boostSettled = true;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const ranked = rankRace();
|
|
148
|
+
const player = ranked.find((entry) => entry.id === "player");
|
|
149
|
+
if (player === undefined) throw new Error("Race ranking lost the player.");
|
|
150
|
+
sameDistanceRanking = ranked[0]?.id === "rival" ? "lap-ahead" : "lap-behind";
|
|
151
|
+
status = resolveRaceStatus(status, lap.completed, TOTAL_LAPS, player.place);
|
|
152
|
+
const previous = frameCtx.state.getState();
|
|
153
|
+
const boostPeakSpeed = Math.max(previous.boostPeakSpeed, car.boost.active ? car.speed : 0);
|
|
154
|
+
frameCtx.state.set({
|
|
155
|
+
boostActive: car.boost.active,
|
|
156
|
+
boostPeakSpeed,
|
|
157
|
+
boostUses: car.boost.uses,
|
|
158
|
+
completedLaps: lap.completed,
|
|
159
|
+
elapsed,
|
|
160
|
+
lastOnRoad: sector.lastOnRoadPosition.toArray() as [number, number, number],
|
|
161
|
+
place: player.place,
|
|
162
|
+
position: `P${player.place}`,
|
|
163
|
+
raceStatus: status,
|
|
164
|
+
rescueHeading: Math.atan2(sector.lastOnRoadHeading.z, sector.lastOnRoadHeading.x),
|
|
165
|
+
rescueHeadingError,
|
|
166
|
+
rescuePositionError,
|
|
167
|
+
rescues,
|
|
168
|
+
shortcutRejects: lap.shortcutRejects,
|
|
169
|
+
speed: car.speed,
|
|
170
|
+
speedAfterBoost,
|
|
171
|
+
sameDistanceRanking,
|
|
172
|
+
topSpeed: Math.max(previous.topSpeed, car.topSpeed),
|
|
173
|
+
trackProgress: player.routeProgress,
|
|
174
|
+
reverseRejects: lap.reverseRejects,
|
|
175
|
+
});
|
|
176
|
+
chaseCamera(camera, car.mesh.position, car.forward, dt);
|
|
177
|
+
cameraRoll(camera, car.forward);
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export type RaceStatus = "DNF" | "RACING" | "WON";
|
|
2
|
+
|
|
3
|
+
export function resolveRaceStatus(
|
|
4
|
+
currentStatus: RaceStatus,
|
|
5
|
+
completedLaps: number,
|
|
6
|
+
totalLaps: number,
|
|
7
|
+
playerPlace: number,
|
|
8
|
+
): RaceStatus {
|
|
9
|
+
if (currentStatus !== "RACING" || completedLaps < totalLaps) return currentStatus;
|
|
10
|
+
return playerPlace === 1 ? "WON" : "DNF";
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type GameState = {
|
|
14
|
+
boostActive: boolean;
|
|
15
|
+
boostPeakSpeed: number;
|
|
16
|
+
speedAfterBoost: number;
|
|
17
|
+
boostUses: number;
|
|
18
|
+
completedLaps: number;
|
|
19
|
+
elapsed: number;
|
|
20
|
+
lastOnRoad: [number, number, number];
|
|
21
|
+
place: number;
|
|
22
|
+
position: string;
|
|
23
|
+
raceStatus: RaceStatus;
|
|
24
|
+
rescueHeading: number;
|
|
25
|
+
rescueHeadingError: number;
|
|
26
|
+
rescuePositionError: number;
|
|
27
|
+
rescues: number;
|
|
28
|
+
shortcutRejects: number;
|
|
29
|
+
speed: number;
|
|
30
|
+
topSpeed: number;
|
|
31
|
+
totalLaps: number;
|
|
32
|
+
trackProgress: number;
|
|
33
|
+
reverseRejects: number;
|
|
34
|
+
sameDistanceRanking: string;
|
|
35
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
@import "tailwindcss";
|
|
2
|
+
|
|
3
|
+
@source "../index.html";
|
|
4
|
+
@source "./";
|
|
5
|
+
|
|
6
|
+
@theme {
|
|
7
|
+
--color-ink: #07111d;
|
|
8
|
+
--color-panel: #0b1b2a;
|
|
9
|
+
--color-line: #1b3a50;
|
|
10
|
+
--color-accent: #ffcf4a;
|
|
11
|
+
--color-win: #74e0ad;
|
|
12
|
+
--color-danger: #ff6c6c;
|
|
13
|
+
--color-text: #c2d6df;
|
|
14
|
+
--color-muted: #6b8c9b;
|
|
15
|
+
--font-mono: ui-monospace, SFMono-Regular, Menlo, monospace;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
html,
|
|
19
|
+
body,
|
|
20
|
+
#root {
|
|
21
|
+
width: 100%;
|
|
22
|
+
height: 100%;
|
|
23
|
+
margin: 0;
|
|
24
|
+
overflow: hidden;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
body {
|
|
28
|
+
background: var(--color-ink);
|
|
29
|
+
color: var(--color-text);
|
|
30
|
+
font-family: var(--font-mono);
|
|
31
|
+
-webkit-font-smoothing: antialiased;
|
|
32
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Area3D } from "@threenative/physics";
|
|
2
|
+
import type { Vector3 } from "three";
|
|
3
|
+
|
|
4
|
+
export type ChecklineId = "finish" | "mid";
|
|
5
|
+
|
|
6
|
+
/** An ordered crossing plane used by Lap to reject shortcuts. */
|
|
7
|
+
export class Checkline {
|
|
8
|
+
readonly at: Vector3;
|
|
9
|
+
readonly area: Area3D;
|
|
10
|
+
readonly forward: Vector3;
|
|
11
|
+
readonly id: ChecklineId;
|
|
12
|
+
|
|
13
|
+
constructor(id: ChecklineId, at: Vector3, forward: Vector3, area: Area3D) {
|
|
14
|
+
this.id = id;
|
|
15
|
+
this.at = at.clone();
|
|
16
|
+
this.forward = forward.clone().setY(0).normalize();
|
|
17
|
+
this.area = area;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import type { PhysicsBody3D } from "@threenative/physics";
|
|
2
|
+
import type { Vector3 } from "three";
|
|
3
|
+
import type { Checkline } from "./Checkline.js";
|
|
4
|
+
|
|
5
|
+
export interface ILapTarget {
|
|
6
|
+
readonly body: PhysicsBody3D;
|
|
7
|
+
readonly forward: Vector3;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class Lap {
|
|
11
|
+
readonly totalLaps: number;
|
|
12
|
+
completed = 0;
|
|
13
|
+
expectedGate = 0;
|
|
14
|
+
shortcutRejects = 0;
|
|
15
|
+
reverseRejects = 0;
|
|
16
|
+
#target: ILapTarget;
|
|
17
|
+
#gates: readonly Checkline[];
|
|
18
|
+
#onLap: (lap: number) => void;
|
|
19
|
+
#unsubscribe: (() => void)[] = [];
|
|
20
|
+
|
|
21
|
+
constructor(
|
|
22
|
+
target: ILapTarget,
|
|
23
|
+
gates: readonly Checkline[],
|
|
24
|
+
totalLaps = 3,
|
|
25
|
+
onLap: (lap: number) => void = () => undefined,
|
|
26
|
+
) {
|
|
27
|
+
if (gates.length === 0) throw new Error("Lap requires at least one ordered gate.");
|
|
28
|
+
if (!Number.isInteger(totalLaps) || totalLaps <= 0)
|
|
29
|
+
throw new Error("Lap totalLaps must be positive.");
|
|
30
|
+
this.#target = target;
|
|
31
|
+
this.#gates = gates;
|
|
32
|
+
this.totalLaps = totalLaps;
|
|
33
|
+
this.#onLap = onLap;
|
|
34
|
+
for (const [index, gate] of gates.entries()) {
|
|
35
|
+
const expected = gate.forward.clone().setY(0).normalize();
|
|
36
|
+
this.#unsubscribe.push(
|
|
37
|
+
gate.area.on("bodyEntered", (body) => {
|
|
38
|
+
if (body !== this.#target.body || this.completed >= this.totalLaps) return;
|
|
39
|
+
this.cross(index, expected);
|
|
40
|
+
}),
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
cross(index: number, gateForward?: Vector3): boolean {
|
|
46
|
+
const gate = this.#gates[index];
|
|
47
|
+
if (gate === undefined) {
|
|
48
|
+
this.shortcutRejects += 1;
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
const direction = gateForward ?? gate.forward;
|
|
52
|
+
const travel = this.#target.forward
|
|
53
|
+
.clone()
|
|
54
|
+
.setY(0)
|
|
55
|
+
.normalize()
|
|
56
|
+
.dot(direction.clone().setY(0).normalize());
|
|
57
|
+
if (travel <= 0.2) {
|
|
58
|
+
this.reverseRejects += 1;
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
if (index !== this.expectedGate) {
|
|
62
|
+
this.shortcutRejects += 1;
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
this.expectedGate += 1;
|
|
66
|
+
if (this.expectedGate < this.#gates.length) return true;
|
|
67
|
+
this.expectedGate = 0;
|
|
68
|
+
this.completed += 1;
|
|
69
|
+
this.#onLap(this.completed);
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Sweeps a car transform between frames so a fast body cannot skip a thin sensor. */
|
|
74
|
+
observe(previous: Vector3, current: Vector3): void {
|
|
75
|
+
for (const [index, gate] of this.#gates.entries()) {
|
|
76
|
+
const normal = gate.forward.clone().setY(0).normalize();
|
|
77
|
+
const before = previous.clone().sub(gate.at).dot(normal);
|
|
78
|
+
const after = current.clone().sub(gate.at).dot(normal);
|
|
79
|
+
const crossedForward = before < 0 && after >= 0;
|
|
80
|
+
const crossedReverse = before >= 0 && after < 0;
|
|
81
|
+
if (crossedForward || crossedReverse) this.cross(index);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
get won(): boolean {
|
|
86
|
+
return this.completed >= this.totalLaps;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
debug(): Record<string, unknown> {
|
|
90
|
+
return {
|
|
91
|
+
completed: this.completed,
|
|
92
|
+
expectedGate: this.expectedGate,
|
|
93
|
+
reverseRejects: this.reverseRejects,
|
|
94
|
+
shortcutRejects: this.shortcutRejects,
|
|
95
|
+
totalLaps: this.totalLaps,
|
|
96
|
+
won: this.won,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
dispose(): void {
|
|
101
|
+
for (const unsubscribe of this.#unsubscribe) unsubscribe();
|
|
102
|
+
this.#unsubscribe = [];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { PathFollow3D } from "@threenative/core";
|
|
2
|
+
import type { Vector3 } from "three";
|
|
3
|
+
|
|
4
|
+
export interface IRacerProgress {
|
|
5
|
+
readonly id: string;
|
|
6
|
+
readonly lap: number;
|
|
7
|
+
readonly position: Vector3;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface IRankedRacer extends IRacerProgress {
|
|
11
|
+
readonly place: number;
|
|
12
|
+
readonly routeProgress: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function routeProgress(route: PathFollow3D, position: Vector3, lap: number): number {
|
|
16
|
+
if (!Number.isInteger(lap) || lap < 0)
|
|
17
|
+
throw new Error("Racing route lap must be a non-negative integer.");
|
|
18
|
+
return lap * route.totalLength + route.project(position).distanceFromStart;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function rankRacers(
|
|
22
|
+
route: PathFollow3D,
|
|
23
|
+
racers: readonly IRacerProgress[],
|
|
24
|
+
): readonly IRankedRacer[] {
|
|
25
|
+
const ranked = racers
|
|
26
|
+
.map((racer) => ({
|
|
27
|
+
...racer,
|
|
28
|
+
place: 0,
|
|
29
|
+
routeProgress: routeProgress(route, racer.position, racer.lap),
|
|
30
|
+
}))
|
|
31
|
+
.sort(
|
|
32
|
+
(left, right) => right.routeProgress - left.routeProgress || left.id.localeCompare(right.id),
|
|
33
|
+
);
|
|
34
|
+
return ranked.map((racer, index) => ({ ...racer, place: index + 1 }));
|
|
35
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { type ICtx, PathFollow3D } from "@threenative/core";
|
|
2
|
+
import { Area3D, CollisionShape3D, type IPhysicsContext, RigidBody3D } from "@threenative/physics";
|
|
3
|
+
import { BoxGeometry, Color, Group, Mesh, MeshStandardMaterial, Raycaster, Vector3 } from "three";
|
|
4
|
+
import { Boost } from "../kart/boost.js";
|
|
5
|
+
import { createMaterials } from "../render/materials.js";
|
|
6
|
+
import { palette } from "../render/palette.js";
|
|
7
|
+
import type { GameState } from "../state.js";
|
|
8
|
+
import { Checkline } from "./Checkline.js";
|
|
9
|
+
import type { IRayHit, IntersectRay } from "./TrackSector.js";
|
|
10
|
+
|
|
11
|
+
export type TrackCtx = ICtx<GameState, IPhysicsContext>;
|
|
12
|
+
|
|
13
|
+
export const TRACK_WIDTH = 7;
|
|
14
|
+
export const TOTAL_LAPS = 3;
|
|
15
|
+
export const ROUTE_POINTS = [
|
|
16
|
+
new Vector3(10, 0, -18),
|
|
17
|
+
new Vector3(18, 0, -18),
|
|
18
|
+
new Vector3(18, 0, 18),
|
|
19
|
+
new Vector3(-18, 0, 18),
|
|
20
|
+
new Vector3(-18, 0, -18),
|
|
21
|
+
] as const;
|
|
22
|
+
|
|
23
|
+
export const RACING_LINE = new PathFollow3D({ loop: true, points: ROUTE_POINTS });
|
|
24
|
+
|
|
25
|
+
export interface ITrackBuild {
|
|
26
|
+
readonly boost: Boost;
|
|
27
|
+
readonly boostArea: Area3D;
|
|
28
|
+
readonly gates: readonly Checkline[];
|
|
29
|
+
readonly route: PathFollow3D;
|
|
30
|
+
readonly roadMeshes: readonly Mesh[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function roadSegment(
|
|
34
|
+
ctx: TrackCtx,
|
|
35
|
+
a: Vector3,
|
|
36
|
+
b: Vector3,
|
|
37
|
+
materials: ReturnType<typeof createMaterials>,
|
|
38
|
+
): Mesh {
|
|
39
|
+
const midpoint = a.clone().lerp(b, 0.5);
|
|
40
|
+
const length = a.distanceTo(b);
|
|
41
|
+
const road = new Mesh(new BoxGeometry(length, 0.28, TRACK_WIDTH), materials.road);
|
|
42
|
+
road.position.set(midpoint.x, -0.14, midpoint.z);
|
|
43
|
+
road.rotation.y = Math.atan2(b.z - a.z, b.x - a.x);
|
|
44
|
+
road.receiveShadow = true;
|
|
45
|
+
ctx.add(road);
|
|
46
|
+
new RigidBody3D({
|
|
47
|
+
collisionLayer: 2,
|
|
48
|
+
collisionMask: 1,
|
|
49
|
+
object: road,
|
|
50
|
+
physics: ctx.physics,
|
|
51
|
+
shape: CollisionShape3D.box(length, 0.28, TRACK_WIDTH),
|
|
52
|
+
type: "fixed",
|
|
53
|
+
});
|
|
54
|
+
return road;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function addCurb(
|
|
58
|
+
ctx: TrackCtx,
|
|
59
|
+
a: Vector3,
|
|
60
|
+
b: Vector3,
|
|
61
|
+
side: number,
|
|
62
|
+
materials: ReturnType<typeof createMaterials>,
|
|
63
|
+
): void {
|
|
64
|
+
const direction = b.clone().sub(a).setY(0).normalize();
|
|
65
|
+
const normal = new Vector3(-direction.z, 0, direction.x).multiplyScalar(
|
|
66
|
+
side * (TRACK_WIDTH / 2 + 0.28),
|
|
67
|
+
);
|
|
68
|
+
const midpoint = a.clone().lerp(b, 0.5).add(normal);
|
|
69
|
+
const length = a.distanceTo(b);
|
|
70
|
+
const curb = new Mesh(new BoxGeometry(length, 0.18, 0.42), materials.curb);
|
|
71
|
+
curb.position.set(midpoint.x, 0.08, midpoint.z);
|
|
72
|
+
curb.rotation.y = Math.atan2(direction.z, direction.x);
|
|
73
|
+
curb.castShadow = true;
|
|
74
|
+
ctx.add(curb);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function gate(
|
|
78
|
+
ctx: TrackCtx,
|
|
79
|
+
id: Checkline["id"],
|
|
80
|
+
at: Vector3,
|
|
81
|
+
forward: Vector3,
|
|
82
|
+
width: number,
|
|
83
|
+
): Checkline {
|
|
84
|
+
const group = new Group();
|
|
85
|
+
const material = new MeshStandardMaterial({
|
|
86
|
+
color: id === "finish" ? palette.accent : palette.skyLow,
|
|
87
|
+
metalness: 0.2,
|
|
88
|
+
roughness: 0.45,
|
|
89
|
+
});
|
|
90
|
+
const post = new Mesh(new BoxGeometry(0.24, 2.5, 0.24), material);
|
|
91
|
+
const otherPost = post.clone();
|
|
92
|
+
const top = new Mesh(new BoxGeometry(width, 0.24, 0.24), material);
|
|
93
|
+
post.position.set(-width / 2, 1.25, 0);
|
|
94
|
+
otherPost.position.set(width / 2, 1.25, 0);
|
|
95
|
+
top.position.y = 2.45;
|
|
96
|
+
group.add(post, otherPost, top);
|
|
97
|
+
group.position.set(at.x, 0, at.z);
|
|
98
|
+
if (Math.abs(forward.z) > Math.abs(forward.x)) group.rotation.y = Math.PI / 2;
|
|
99
|
+
ctx.add(group);
|
|
100
|
+
const area = new Area3D({
|
|
101
|
+
collisionMask: 1,
|
|
102
|
+
entity: `gate.${id}`,
|
|
103
|
+
physics: ctx.physics,
|
|
104
|
+
position: { x: at.x, y: 0.7, z: at.z },
|
|
105
|
+
shape: CollisionShape3D.box(
|
|
106
|
+
Math.abs(forward.z) > Math.abs(forward.x) ? 0.65 : width / 2,
|
|
107
|
+
1.1,
|
|
108
|
+
Math.abs(forward.z) > Math.abs(forward.x) ? width / 2 : 0.65,
|
|
109
|
+
),
|
|
110
|
+
});
|
|
111
|
+
return new Checkline(id, at, forward, area);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function boostPad(
|
|
115
|
+
ctx: TrackCtx,
|
|
116
|
+
at: Vector3,
|
|
117
|
+
materials: ReturnType<typeof createMaterials>,
|
|
118
|
+
): { area: Area3D; boost: Boost } {
|
|
119
|
+
const pad = new Mesh(new BoxGeometry(3.2, 0.08, TRACK_WIDTH - 0.6), materials.boost);
|
|
120
|
+
pad.position.set(at.x, 0.12, at.z);
|
|
121
|
+
pad.receiveShadow = true;
|
|
122
|
+
ctx.add(pad);
|
|
123
|
+
const boost = new Boost();
|
|
124
|
+
const area = new Area3D({
|
|
125
|
+
collisionMask: 1,
|
|
126
|
+
entity: "boost-pad",
|
|
127
|
+
physics: ctx.physics,
|
|
128
|
+
position: { x: at.x, y: 0.35, z: at.z },
|
|
129
|
+
shape: CollisionShape3D.box(1.6, 0.45, TRACK_WIDTH / 2),
|
|
130
|
+
});
|
|
131
|
+
return { area, boost };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function normalizeRayHit(value: unknown): IRayHit | undefined {
|
|
135
|
+
if (typeof value !== "object" || value === null) return undefined;
|
|
136
|
+
const record = value as Record<string, unknown>;
|
|
137
|
+
const distance = record.distance;
|
|
138
|
+
if (typeof distance !== "number" || !Number.isFinite(distance)) return undefined;
|
|
139
|
+
const normal = record.normal;
|
|
140
|
+
const normalY =
|
|
141
|
+
typeof normal === "object" &&
|
|
142
|
+
normal !== null &&
|
|
143
|
+
typeof (normal as { y?: unknown }).y === "number"
|
|
144
|
+
? (normal as { y: number }).y
|
|
145
|
+
: undefined;
|
|
146
|
+
return { distance, ...(normalY === undefined ? {} : { normalY }) };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
type PhysicsQueryPoint = Pick<Vector3, "x" | "y" | "z">;
|
|
150
|
+
|
|
151
|
+
interface IPhysicsDirectSpaceState {
|
|
152
|
+
intersectRay(options: {
|
|
153
|
+
readonly collisionMask?: number;
|
|
154
|
+
readonly from: PhysicsQueryPoint;
|
|
155
|
+
readonly to: PhysicsQueryPoint;
|
|
156
|
+
}): unknown;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
type QueryPhysics = IPhysicsContext & {
|
|
160
|
+
readonly directSpaceState: IPhysicsDirectSpaceState;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
/** Uses PRD-088's optional physics query when installed, with a local visual probe fallback. */
|
|
164
|
+
export function intersectRay(physics: IPhysicsContext, fallback: IntersectRay): IntersectRay {
|
|
165
|
+
const directSpaceState = (physics as Partial<QueryPhysics>).directSpaceState;
|
|
166
|
+
if (directSpaceState === undefined) return fallback;
|
|
167
|
+
return (origin, direction, maxDistance) => {
|
|
168
|
+
const to = origin.clone().addScaledVector(direction, maxDistance);
|
|
169
|
+
const result = directSpaceState.intersectRay({
|
|
170
|
+
collisionMask: 2,
|
|
171
|
+
from: origin,
|
|
172
|
+
to,
|
|
173
|
+
});
|
|
174
|
+
return normalizeRayHit(result);
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function roadRayProbe(meshes: readonly Mesh[]): IntersectRay {
|
|
179
|
+
const raycaster = new Raycaster();
|
|
180
|
+
return (origin, direction, maxDistance) => {
|
|
181
|
+
raycaster.set(origin, direction);
|
|
182
|
+
const hit = raycaster.intersectObjects([...meshes], false)[0];
|
|
183
|
+
if (hit === undefined || hit.distance > maxDistance) return undefined;
|
|
184
|
+
return { distance: hit.distance, normalY: hit.face?.normal.y };
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export function buildTrack(ctx: TrackCtx): ITrackBuild {
|
|
189
|
+
const materials = createMaterials();
|
|
190
|
+
const field = new Mesh(new BoxGeometry(100, 0.3, 100), materials.field);
|
|
191
|
+
field.position.y = -0.62;
|
|
192
|
+
field.receiveShadow = true;
|
|
193
|
+
ctx.add(field);
|
|
194
|
+
new RigidBody3D({
|
|
195
|
+
collisionLayer: 4,
|
|
196
|
+
collisionMask: 1,
|
|
197
|
+
object: field,
|
|
198
|
+
physics: ctx.physics,
|
|
199
|
+
shape: CollisionShape3D.box(50, 0.15, 50),
|
|
200
|
+
type: "fixed",
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const roadMeshes: Mesh[] = [];
|
|
204
|
+
for (let index = 0; index < ROUTE_POINTS.length; index += 1) {
|
|
205
|
+
const a = ROUTE_POINTS[index];
|
|
206
|
+
const b = ROUTE_POINTS[(index + 1) % ROUTE_POINTS.length];
|
|
207
|
+
if (a === undefined || b === undefined) continue;
|
|
208
|
+
roadMeshes.push(roadSegment(ctx, a, b, materials));
|
|
209
|
+
addCurb(ctx, a, b, 1, materials);
|
|
210
|
+
addCurb(ctx, a, b, -1, materials);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const gates = [
|
|
214
|
+
gate(ctx, "mid", new Vector3(18, 0, 0), new Vector3(0, 0, 1), TRACK_WIDTH),
|
|
215
|
+
gate(ctx, "finish", new Vector3(10, 0, -18), new Vector3(1, 0, 0), TRACK_WIDTH),
|
|
216
|
+
];
|
|
217
|
+
const pad = boostPad(ctx, new Vector3(14, 0, -18), materials);
|
|
218
|
+
for (const [index, point] of ROUTE_POINTS.entries()) {
|
|
219
|
+
const marker = new Mesh(
|
|
220
|
+
new BoxGeometry(0.2, 1.2, 0.2),
|
|
221
|
+
new MeshStandardMaterial({
|
|
222
|
+
color: new Color(index % 2 === 0 ? palette.accent : palette.skyLow),
|
|
223
|
+
}),
|
|
224
|
+
);
|
|
225
|
+
marker.position.set(point.x, 0.6, point.z);
|
|
226
|
+
ctx.add(marker);
|
|
227
|
+
}
|
|
228
|
+
return { boost: pad.boost, boostArea: pad.area, gates, roadMeshes, route: RACING_LINE };
|
|
229
|
+
}
|