create-threenative 0.2.5 → 0.2.7
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 +25 -3
- package/agent-docs/references/assertion-reference.md +40 -1
- package/agent-docs/references/build-profiles.md +126 -0
- package/agent-docs/references/capability-reference.md +1382 -65
- package/agent-docs/references/finding-assets.md +8 -1
- package/agent-docs/references/performance-basics.md +117 -0
- package/agent-docs/references/rigging-characters.md +51 -0
- package/agent-docs/references/sculpt-from-a-reference.md +1 -1
- package/agent-docs/references/trace-a-slow-frame.md +1 -1
- package/agent-docs/references/visual-baseline.md +8 -4
- package/agent-docs/references/webview-ui.md +3 -2
- package/agent-files/.agents/skills/file-engine-bug/SKILL.md +15 -4
- package/agent-files/.agents/skills/ponytail/SKILL.md +68 -0
- package/agent-files/.agents/skills/threenative-assets/SKILL.md +12 -4
- package/agent-files/.agents/skills/threenative-capabilities/SKILL.md +1 -1
- package/agent-files/.agents/skills/threenative-context/SKILL.md +9 -1
- package/agent-files/.agents/skills/threenative-performance/SKILL.md +26 -10
- package/agent-files/.agents/skills/threenative-playtest/SKILL.md +1 -1
- package/agent-files/.agents/skills/threenative-ui/SKILL.md +1 -1
- package/agent-files/.agents/skills/threenative-visuals/SKILL.md +4 -4
- package/agent-files/.claude/hooks/ponytail-context.mjs +51 -0
- package/agent-files/.claude/settings.json +38 -0
- package/agent-files/.codex/hooks.json +40 -0
- package/capabilities.json +1891 -101
- package/dist/index.d.ts +39 -1
- package/dist/index.js +480 -56
- package/dist/threenative.js +1247 -177
- package/package.json +6 -6
- package/template-assets/assets/mannequin-combat.glb +0 -0
- package/template-assets/assets/mannequin.glb +0 -0
- package/template-assets/assets/player-viewmodel.glb +0 -0
- package/template-assets/assets/sky.jpg +0 -0
- package/template-assets/clearwater/README.md +141 -0
- package/template-assets/clearwater/install.mjs +50 -0
- package/template-assets/clearwater/src/clearwater.ts +180 -0
- package/template-assets/clearwater/src/clearwaterDemo.ts +28 -0
- package/template-assets/clearwater/src/clearwaterLifetime.ts +28 -0
- package/{LICENSE → template-assets/clearwater/src/render/CLEARWATER-LICENSE.txt} +4 -1
- package/template-assets/clearwater/src/render/clearwater.ts +278 -0
- package/template-assets/clearwater/src/render/clearwaterCaustics.ts +138 -0
- package/template-assets/clearwater/src/render/clearwaterDemo.ts +65 -0
- package/template-assets/clearwater/src/render/clearwaterOptics.ts +33 -0
- package/template-assets/clearwater/src/render/clearwaterOptions.ts +177 -0
- package/template-assets/clearwater/src/render/clearwaterPass.ts +51 -0
- package/template-assets/patches/three@0.185.1.patch +2228 -105
- package/template-assets/worldEnvironment.ts +52 -1
- package/templates/action-rpg/AGENTS.md +43 -43
- package/templates/action-rpg/CLAUDE.md +43 -43
- package/templates/action-rpg/kit.json +1 -1
- package/templates/action-rpg/package.json +14 -8
- package/templates/action-rpg/playtests/combat.playtest.json +162 -23
- package/templates/action-rpg/playtests/performance.playtest.json +4 -3
- package/templates/action-rpg/playtests/survives.playtest.json +25 -8
- package/templates/action-rpg/src/conventions.ts +24 -18
- package/templates/action-rpg/src/entities/Enemy.ts +74 -24
- package/templates/action-rpg/src/entities/Fighter.ts +127 -0
- package/templates/action-rpg/src/entities/Player.ts +102 -32
- package/templates/action-rpg/src/game.ts +3 -3
- package/templates/action-rpg/src/render/camera.ts +18 -16
- package/templates/action-rpg/src/render/dungeon.ts +192 -0
- package/templates/action-rpg/src/render/lighting.ts +45 -28
- package/templates/action-rpg/src/render/loading.ts +4 -4
- package/templates/action-rpg/src/render/materials.ts +191 -47
- package/templates/action-rpg/src/render/palette.ts +14 -13
- package/templates/action-rpg/src/render/postprocessing.ts +12 -2
- package/templates/action-rpg/src/render/props.ts +117 -0
- package/templates/action-rpg/src/render/quality.ts +40 -82
- package/templates/action-rpg/src/render/sky.ts +36 -13
- package/templates/action-rpg/src/render/touch-controls.ts +1 -1
- package/templates/action-rpg/src/render/worldEnvironment.ts +52 -1
- package/templates/action-rpg/src/scenes/Play.ts +61 -39
- package/templates/action-rpg/src/style.css +9 -7
- package/templates/action-rpg/src/ui/AbilityBar.tsx +3 -1
- package/templates/action-rpg/src/ui/Hud.tsx +3 -3
- package/templates/action-rpg/threenative.config.ts +14 -1
- package/templates/action-rpg/vite.config.ts +4 -1
- package/templates/minimal/AGENTS.md +12 -12
- package/templates/minimal/CLAUDE.md +12 -12
- package/templates/minimal/package.json +13 -7
- package/templates/minimal/playtests/play.playtest.json +6 -5
- package/templates/minimal/playtests/sky.playtest.json +27 -0
- package/templates/minimal/src/conventions.ts +6 -3
- package/templates/minimal/src/entities/Player.ts +73 -27
- package/templates/minimal/src/main.ts +2 -3
- package/templates/minimal/src/render/camera.ts +22 -4
- package/templates/minimal/src/render/lighting.ts +24 -48
- package/templates/minimal/src/render/materials.ts +158 -13
- package/templates/minimal/src/render/palette.ts +10 -7
- package/templates/minimal/src/render/postprocessing.ts +12 -25
- package/templates/minimal/src/render/quality.ts +35 -69
- package/templates/minimal/src/render/sky.ts +35 -54
- package/templates/minimal/src/render/touch-controls.ts +1 -1
- package/templates/minimal/src/render/worldEnvironment.ts +52 -1
- package/templates/minimal/src/scenes/Play.ts +121 -111
- package/templates/minimal/src/state.ts +0 -4
- package/templates/minimal/src/style.css +0 -10
- package/templates/minimal/threenative.config.ts +15 -1
- package/templates/minimal/vite.config.ts +4 -1
- package/templates/platformer/AGENTS.md +18 -56
- package/templates/platformer/CLAUDE.md +18 -56
- package/templates/platformer/kit.json +2 -2
- package/templates/platformer/package.json +14 -10
- package/templates/platformer/playtests/buffer.playtest.json +15 -5
- package/templates/platformer/playtests/collect.playtest.json +8 -34
- package/templates/platformer/playtests/coyote.playtest.json +12 -34
- package/templates/platformer/playtests/damage.playtest.json +40 -15
- package/templates/platformer/playtests/hud.playtest.json +34 -0
- package/templates/platformer/playtests/move.playtest.json +24 -0
- package/templates/platformer/playtests/performance.playtest.json +24 -7
- package/templates/platformer/playtests/respawn.playtest.json +46 -11
- package/templates/platformer/playtests/stomp.playtest.json +49 -12
- package/templates/platformer/playtests/survives.playtest.json +3 -19
- package/templates/platformer/playtests/world.playtest.json +26 -0
- package/templates/platformer/src/entities/Fox.ts +273 -0
- package/templates/platformer/src/entities/Pickup.ts +43 -60
- package/templates/platformer/src/entities/Walker.ts +88 -0
- package/templates/platformer/src/game.ts +11 -8
- package/templates/platformer/src/level/Checkpoints.ts +29 -16
- package/templates/platformer/src/level/Stage.ts +469 -0
- package/templates/platformer/src/render/blocks.ts +94 -0
- package/templates/platformer/src/render/camera.ts +28 -3
- package/templates/platformer/src/render/fox.ts +196 -0
- package/templates/platformer/src/render/lighting.ts +42 -33
- package/templates/platformer/src/render/materials.ts +115 -5
- package/templates/platformer/src/render/palette.ts +68 -53
- package/templates/platformer/src/render/pickups.ts +142 -0
- package/templates/platformer/src/render/postprocessing.ts +12 -2
- package/templates/platformer/src/render/props.ts +147 -0
- package/templates/platformer/src/render/quality.ts +47 -99
- package/templates/platformer/src/render/scenery.ts +172 -0
- package/templates/platformer/src/render/sky.ts +120 -31
- package/templates/platformer/src/render/touch-controls.ts +5 -2
- package/templates/platformer/src/render/walkers.ts +96 -0
- package/templates/platformer/src/render/waterfall.ts +85 -0
- package/templates/platformer/src/render/worldEnvironment.ts +52 -1
- package/templates/platformer/src/scenes/Boot.ts +3 -4
- package/templates/platformer/src/scenes/Play.ts +362 -0
- package/templates/platformer/src/state.ts +16 -14
- package/templates/platformer/src/style.css +9 -8
- package/templates/platformer/src/ui/Hud.tsx +62 -29
- package/templates/platformer/src/ui/Menu.tsx +2 -2
- package/templates/platformer/threenative.config.ts +14 -1
- package/templates/platformer/vite.config.ts +4 -1
- package/templates/puzzle/AGENTS.md +21 -22
- package/templates/puzzle/CLAUDE.md +21 -22
- package/templates/puzzle/assets/native-proof.glb +0 -0
- package/templates/puzzle/assets/native-proof.png +0 -0
- package/templates/puzzle/kit.json +1 -1
- package/templates/puzzle/package.json +14 -8
- package/templates/puzzle/playtests/axis.playtest.json +20 -0
- package/templates/puzzle/playtests/blocked.playtest.json +73 -0
- package/templates/puzzle/playtests/caused.playtest.json +65 -0
- package/templates/puzzle/playtests/lit.playtest.json +74 -0
- package/templates/puzzle/playtests/performance.playtest.json +24 -7
- package/templates/{starter/playtests/respawn.playtest.json → puzzle/playtests/push.playtest.json} +24 -19
- package/templates/puzzle/playtests/replay.playtest.json +81 -0
- package/templates/puzzle/playtests/restart.playtest.json +108 -0
- package/templates/puzzle/playtests/seal.playtest.json +98 -0
- package/templates/puzzle/playtests/settle.playtest.json +100 -0
- package/templates/puzzle/playtests/survives.playtest.json +6 -29
- package/templates/{starter/playtests/hot-reload.playtest.json → puzzle/playtests/travel.playtest.json} +6 -5
- package/templates/puzzle/src/entities/Crate.ts +99 -42
- package/templates/puzzle/src/entities/Seal.ts +69 -0
- package/templates/puzzle/src/entities/Warden.ts +135 -0
- package/templates/puzzle/src/game.ts +15 -11
- package/templates/puzzle/src/render/camera.ts +66 -31
- package/templates/puzzle/src/render/crateShape.ts +70 -0
- package/templates/puzzle/src/render/lighting.ts +41 -41
- package/templates/puzzle/src/render/loading.ts +4 -4
- package/templates/puzzle/src/render/materials.ts +131 -27
- package/templates/puzzle/src/render/palette.ts +19 -29
- package/templates/puzzle/src/render/postprocessing.ts +15 -3
- package/templates/puzzle/src/render/quality.ts +71 -80
- package/templates/puzzle/src/render/shapes.ts +109 -108
- package/templates/puzzle/src/render/sky.ts +18 -10
- package/templates/puzzle/src/render/touch-controls.ts +8 -51
- package/templates/puzzle/src/render/vault.ts +315 -0
- package/templates/puzzle/src/render/warden.ts +46 -0
- package/templates/puzzle/src/render/worldEnvironment.ts +52 -1
- package/templates/puzzle/src/scenes/Play.ts +793 -0
- package/templates/puzzle/src/state.ts +55 -24
- package/templates/puzzle/src/style.css +7 -8
- package/templates/puzzle/src/ui/App.tsx +2 -2
- package/templates/puzzle/src/ui/Hud.tsx +92 -40
- package/templates/puzzle/src/ui/Menu.tsx +40 -2
- package/templates/puzzle/threenative.config.ts +14 -1
- package/templates/puzzle/vite.config.ts +4 -1
- package/templates/racing/AGENTS.md +20 -21
- package/templates/racing/CLAUDE.md +20 -21
- package/templates/racing/package.json +14 -8
- package/templates/racing/playtests/boost.playtest.json +33 -12
- package/templates/racing/playtests/outcome.playtest.json +32 -50
- package/templates/racing/playtests/performance.playtest.json +2 -1
- package/templates/racing/playtests/ranking.playtest.json +5 -14
- package/templates/racing/playtests/rescue.playtest.json +2 -2
- package/templates/racing/playtests/reverse.playtest.json +30 -8
- package/templates/racing/playtests/shortcut.playtest.json +54 -9
- package/templates/racing/playtests/touch-controls.playtest.json +32 -5
- package/templates/racing/src/entities/CarBody.ts +327 -0
- package/templates/racing/src/entities/LineDriver.ts +220 -0
- package/templates/racing/src/entities/RacingCar.ts +150 -89
- package/templates/racing/src/entities/Rival.ts +61 -24
- package/templates/racing/src/game.ts +5 -0
- package/templates/racing/src/render/camera.ts +49 -7
- package/templates/racing/src/render/lighting.ts +36 -37
- package/templates/racing/src/render/materials.ts +239 -19
- package/templates/racing/src/render/palette.ts +19 -21
- package/templates/racing/src/render/postprocessing.ts +12 -2
- package/templates/racing/src/render/quality.ts +43 -82
- package/templates/racing/src/render/shapes.ts +20 -6
- package/templates/racing/src/render/sky.ts +47 -39
- package/templates/racing/src/render/touch-controls.ts +1 -1
- package/templates/racing/src/render/worldEnvironment.ts +52 -1
- package/templates/racing/src/scenes/Race.ts +107 -54
- package/templates/racing/src/state.ts +2 -0
- package/templates/racing/src/track/Checkline.ts +22 -7
- package/templates/racing/src/track/Lap.ts +64 -36
- package/templates/racing/src/track/Ranking.ts +20 -11
- package/templates/racing/src/track/Track.ts +809 -361
- package/templates/racing/src/track/TrackSector.ts +40 -11
- package/templates/racing/src/track/circuit.ts +477 -0
- package/templates/racing/src/track/rescue.ts +16 -4
- package/templates/racing/threenative.config.ts +14 -1
- package/templates/racing/vite.config.ts +4 -1
- package/templates/rts/AGENTS.md +96 -0
- package/templates/rts/CLAUDE.md +98 -0
- package/templates/rts/kit.json +7 -0
- package/templates/{defense → rts}/package.json +14 -8
- package/templates/{defense → rts}/playtests/performance.playtest.json +28 -26
- package/templates/rts/playtests/rts-ai-attack.playtest.json +24 -0
- package/templates/rts/playtests/rts-attack.playtest.json +75 -0
- package/templates/rts/playtests/rts-build.playtest.json +68 -0
- package/templates/rts/playtests/rts-orders.playtest.json +150 -0
- package/templates/rts/playtests/rts-train.playtest.json +62 -0
- package/templates/rts/playtests/survives.playtest.json +58 -0
- package/templates/rts/public/favicon.svg +4 -0
- package/templates/rts/src/game.ts +64 -0
- package/templates/rts/src/orders.ts +22 -0
- package/templates/rts/src/render/army.ts +346 -0
- package/templates/rts/src/render/camera.ts +123 -0
- package/templates/rts/src/render/lighting.ts +61 -0
- package/templates/rts/src/render/loading.ts +33 -0
- package/templates/rts/src/render/materials.ts +152 -0
- package/templates/rts/src/render/models.ts +705 -0
- package/templates/rts/src/render/palette.ts +17 -0
- package/templates/rts/src/render/picking.ts +93 -0
- package/templates/{defense → rts}/src/render/postprocessing.ts +15 -3
- package/templates/rts/src/render/quality.ts +128 -0
- package/templates/rts/src/render/resources.ts +141 -0
- package/templates/rts/src/render/sky.ts +44 -0
- package/templates/rts/src/render/terrain.ts +116 -0
- package/templates/{defense → rts}/src/render/worldEnvironment.ts +52 -1
- package/templates/rts/src/scenes/Play.ts +650 -0
- package/templates/rts/src/sim/ai.ts +1140 -0
- package/templates/rts/src/sim/combat.ts +416 -0
- package/templates/rts/src/sim/construction.ts +374 -0
- package/templates/rts/src/sim/economy.ts +167 -0
- package/templates/rts/src/sim/game.ts +1082 -0
- package/templates/rts/src/sim/movement.ts +371 -0
- package/templates/rts/src/sim/nav.ts +276 -0
- package/templates/rts/src/sim/production.ts +180 -0
- package/templates/rts/src/sim/terrain.ts +170 -0
- package/templates/rts/src/sim/types.ts +647 -0
- package/templates/rts/src/sim/vision.ts +46 -0
- package/templates/rts/src/state.ts +100 -0
- package/templates/{defense → rts}/src/style.css +21 -8
- package/templates/rts/src/ui/App.tsx +21 -0
- package/templates/rts/src/ui/Commands.tsx +129 -0
- package/templates/rts/src/ui/GameUi.tsx +22 -0
- package/templates/rts/src/ui/Hud.tsx +165 -0
- package/templates/rts/src/ui/Minimap.tsx +84 -0
- package/templates/rts/tailwind.config.ts +2 -0
- package/templates/rts/threenative.config.ts +56 -0
- package/templates/rts/tsconfig.json +17 -0
- package/templates/{defense → rts}/vite.config.ts +4 -1
- package/templates/runner/AGENTS.md +8 -12
- package/templates/runner/CLAUDE.md +8 -12
- package/templates/runner/README.md +3 -3
- package/templates/runner/package.json +14 -8
- package/templates/runner/playtests/performance.playtest.json +2 -2
- package/templates/runner/src/conventions.ts +4 -3
- package/templates/runner/src/entities/Runner.ts +54 -7
- package/templates/runner/src/game.ts +2 -0
- package/templates/runner/src/render/camera.ts +4 -2
- package/templates/runner/src/render/dust.ts +5 -2
- package/templates/runner/src/render/lighting.ts +41 -45
- package/templates/runner/src/render/loading.ts +2 -2
- package/templates/runner/src/render/materials.ts +165 -25
- package/templates/runner/src/render/palette.ts +17 -28
- package/templates/runner/src/render/postprocessing.ts +12 -2
- package/templates/runner/src/render/quality.ts +42 -96
- package/templates/runner/src/render/shapes.ts +59 -37
- package/templates/runner/src/render/sky.ts +32 -41
- package/templates/runner/src/render/touch-controls.ts +1 -1
- package/templates/runner/src/render/worldEnvironment.ts +52 -1
- package/templates/runner/src/scenes/Run.ts +26 -6
- package/templates/runner/src/track.ts +6 -1
- package/templates/runner/threenative.config.ts +14 -1
- package/templates/runner/vite.config.ts +4 -1
- package/templates/sailing/AGENTS.md +14 -14
- package/templates/sailing/CLAUDE.md +14 -14
- package/templates/sailing/assets/ship.glb +0 -0
- package/templates/sailing/package.json +14 -8
- package/templates/sailing/playtests/float-after-the-run-ends.playtest.json +10 -6
- package/templates/sailing/playtests/sails.playtest.json +33 -0
- package/templates/sailing/playtests/win.playtest.json +156 -8
- package/templates/sailing/src/conventions.ts +29 -2
- package/templates/sailing/src/entities/Ship.ts +381 -57
- package/templates/sailing/src/game.ts +14 -0
- package/templates/sailing/src/render/camera.ts +63 -9
- package/templates/sailing/src/render/lighting.ts +49 -27
- package/templates/sailing/src/render/materials.ts +50 -15
- package/templates/sailing/src/render/ocean.ts +647 -60
- package/templates/sailing/src/render/palette.ts +16 -12
- package/templates/sailing/src/render/postprocessing.ts +12 -2
- package/templates/sailing/src/render/props.ts +316 -442
- package/templates/sailing/src/render/quality.ts +14 -4
- package/templates/sailing/src/render/sky.ts +116 -23
- package/templates/sailing/src/render/worldEnvironment.ts +52 -1
- package/templates/sailing/src/scenes/Boot.ts +21 -0
- package/templates/sailing/src/scenes/Sailing.ts +173 -39
- package/templates/sailing/src/state.ts +6 -0
- package/templates/sailing/src/ui/Hud.tsx +41 -9
- package/templates/sailing/threenative.config.ts +14 -1
- package/templates/sailing/vite.config.ts +4 -1
- package/templates/shooter/AGENTS.md +29 -44
- package/templates/shooter/CLAUDE.md +29 -44
- package/templates/shooter/index.html +1 -0
- package/templates/shooter/package.json +14 -8
- package/templates/shooter/playtests/aim-alignment.playtest.json +58 -0
- package/templates/shooter/playtests/breakable-shatters.playtest.json +62 -0
- package/templates/shooter/playtests/bullet-holes.playtest.json +56 -0
- package/templates/shooter/playtests/camera-tracks-body-vertically.playtest.json +61 -0
- package/templates/shooter/playtests/crouch-lowers-the-eye.playtest.json +41 -0
- package/templates/shooter/playtests/death-no-snap.playtest.json +77 -0
- package/templates/shooter/playtests/debug-ads.playtest.json +58 -0
- package/templates/shooter/playtests/debug-clear.playtest.json +383 -0
- package/templates/shooter/playtests/debug-endurance.playtest.json +61 -0
- package/templates/shooter/playtests/debug-fire.playtest.json +119 -0
- package/templates/shooter/playtests/debug-restart.playtest.json +24 -0
- package/templates/shooter/playtests/enemy-body-is-a-body.playtest.json +79 -0
- package/templates/shooter/playtests/enemy-death-settles.playtest.json +64 -0
- package/templates/shooter/playtests/enemy-flinches-when-hit.playtest.json +75 -0
- package/templates/shooter/playtests/enemy-foot-contact.playtest.json +41 -0
- package/templates/shooter/playtests/enemy-material-is-not-shared.playtest.json +59 -0
- package/templates/shooter/playtests/enemy-reaches-walkway.playtest.json +80 -0
- package/templates/shooter/playtests/enemy-rifle-grip.playtest.json +101 -0
- package/templates/shooter/playtests/enemy-scale.playtest.json +56 -0
- package/templates/shooter/playtests/enemy-stops-without-snapping.playtest.json +486 -0
- package/templates/shooter/playtests/enemy-stride-matches-speed.playtest.json +198 -0
- package/templates/shooter/playtests/enemy-stride-under-crouch.playtest.json +87 -0
- package/templates/shooter/playtests/enemy-uses-crouch-clips.playtest.json +74 -0
- package/templates/shooter/playtests/five-soldiers.playtest.json +83 -0
- package/templates/shooter/playtests/flash-retires.playtest.json +62 -0
- package/templates/shooter/playtests/headshot-hits-head.playtest.json +132 -0
- package/templates/shooter/playtests/hold-to-fire.playtest.json +45 -0
- package/templates/shooter/playtests/input-control.playtest.json +43 -98
- package/templates/shooter/playtests/minimap-state.playtest.json +75 -0
- package/templates/shooter/playtests/performance.playtest.json +62 -26
- package/templates/{starter/playtests/forward.playtest.json → shooter/playtests/round-clock.playtest.json} +11 -16
- package/templates/shooter/playtests/shot-tracks-crosshair.playtest.json +79 -0
- package/templates/shooter/playtests/survives.playtest.json +36 -30
- package/templates/shooter/playtests/walkway-reachable.playtest.json +67 -0
- package/templates/shooter/src/entities/Breakables.ts +412 -0
- package/templates/shooter/src/entities/Enemy.ts +2095 -0
- package/templates/shooter/src/entities/FpsPlayer.ts +283 -0
- package/templates/shooter/src/entities/Rifle.ts +442 -0
- package/templates/shooter/src/entities/Target.ts +199 -83
- package/templates/shooter/src/entities/TouchControls.ts +276 -0
- package/templates/shooter/src/game.ts +75 -41
- package/templates/shooter/src/main.ts +0 -1
- package/templates/shooter/src/postPhysics.ts +36 -0
- package/templates/shooter/src/render/camera.ts +78 -74
- package/templates/shooter/src/render/decals.ts +142 -82
- package/templates/shooter/src/render/facade.ts +1373 -0
- package/templates/shooter/src/render/gunfx.ts +704 -0
- package/templates/shooter/src/render/lighting.ts +52 -38
- package/templates/shooter/src/render/loading.ts +2 -2
- package/templates/shooter/src/render/materials.ts +100 -88
- package/templates/shooter/src/render/occlusion.ts +108 -0
- package/templates/shooter/src/render/palette.ts +10 -10
- package/templates/shooter/src/render/palm.ts +455 -0
- package/templates/shooter/src/render/pooled-billboards.ts +144 -0
- package/templates/shooter/src/render/postprocessing.ts +15 -3
- package/templates/shooter/src/render/quality.ts +40 -82
- package/templates/shooter/src/render/scale.ts +148 -15
- package/templates/shooter/src/render/shapes.ts +138 -613
- package/templates/shooter/src/render/sky.ts +35 -35
- package/templates/shooter/src/render/town-schematic.ts +120 -0
- package/templates/shooter/src/render/town.ts +986 -0
- package/templates/shooter/src/render/townMaterials.ts +160 -0
- package/templates/shooter/src/render/vehicles.ts +741 -0
- package/templates/shooter/src/render/worldEnvironment.ts +52 -1
- package/templates/shooter/src/scenes/Play.ts +727 -650
- package/templates/shooter/src/state.ts +26 -41
- package/templates/shooter/src/style.css +207 -11
- package/templates/shooter/src/surfaces.ts +131 -0
- package/templates/shooter/src/ui/App.tsx +9 -10
- package/templates/shooter/src/ui/Crosshair.tsx +25 -18
- package/templates/shooter/src/ui/GameUi.tsx +6 -9
- package/templates/shooter/src/ui/Hud.tsx +428 -39
- package/templates/shooter/src/ui/Minimap.tsx +164 -0
- package/templates/shooter/src/ui/TouchOverlay.tsx +54 -0
- package/templates/shooter/src/ui/main.tsx +2 -3
- package/templates/shooter/threenative.config.ts +14 -1
- package/templates/shooter/vite.config.ts +4 -1
- package/templates/starter/AGENTS.md +20 -20
- package/templates/starter/CLAUDE.md +20 -20
- package/templates/starter/native-playtests/render-chain.playtest.json +13 -45
- package/templates/starter/package.json +15 -9
- package/templates/starter/playtests/play.playtest.json +2 -1
- package/templates/starter/playtests/{restart.playtest.json → production-readiness.playtest.json} +17 -12
- package/templates/starter/playtests/survives.playtest.json +1 -1
- package/templates/starter/src/conventions.ts +6 -3
- package/templates/starter/src/entities/Goal.ts +35 -42
- package/templates/starter/src/entities/Player.ts +99 -16
- package/templates/starter/src/render/arena.ts +131 -0
- package/templates/starter/src/render/lighting.ts +27 -44
- package/templates/starter/src/render/materials.ts +164 -35
- package/templates/starter/src/render/palette.ts +16 -16
- package/templates/starter/src/render/postprocessing.ts +6 -7
- package/templates/starter/src/render/quality.ts +43 -101
- package/templates/starter/src/render/shapes.ts +1 -1
- package/templates/starter/src/render/sky.ts +39 -45
- package/templates/starter/src/render/touch-controls.ts +3 -1
- package/templates/starter/src/render/worldEnvironment.ts +52 -1
- package/templates/starter/src/scenes/Play.ts +82 -81
- package/templates/starter/src/ui/Hud.tsx +2 -2
- package/templates/starter/src/ui/Menu.tsx +5 -1
- package/templates/starter/threenative.config.ts +14 -1
- package/templates/starter/vite.config.ts +4 -1
- package/templates/tower-defense/.codex/config.toml +19 -0
- package/templates/tower-defense/.cursor/mcp.json +32 -0
- package/templates/tower-defense/.gemini/settings.json +32 -0
- package/templates/tower-defense/.mcp.json +32 -0
- package/templates/tower-defense/.vscode/mcp.json +36 -0
- package/templates/tower-defense/.zed/settings.json +36 -0
- package/templates/{defense → tower-defense}/AGENTS.md +16 -14
- package/templates/{defense → tower-defense}/CLAUDE.md +16 -14
- package/templates/tower-defense/README.md +16 -0
- package/templates/tower-defense/index.html +12 -0
- package/templates/tower-defense/kit.json +7 -0
- package/templates/tower-defense/opencode.json +41 -0
- package/templates/tower-defense/package.json +56 -0
- package/templates/tower-defense/playtests/arsenal.playtest.json +214 -0
- package/templates/tower-defense/playtests/defeat.playtest.json +85 -0
- package/templates/tower-defense/playtests/look.playtest.json +130 -0
- package/templates/tower-defense/playtests/performance.playtest.json +77 -0
- package/templates/tower-defense/playtests/placement.playtest.json +115 -0
- package/templates/{defense → tower-defense}/playtests/pointer-placement.playtest.json +21 -49
- package/templates/tower-defense/playtests/strike.playtest.json +98 -0
- package/templates/tower-defense/playtests/survives.playtest.json +85 -0
- package/templates/tower-defense/playtests/target-priority.playtest.json +177 -0
- package/templates/tower-defense/playtests/upgrade-recycle.playtest.json +124 -0
- package/templates/tower-defense/playtests/victory.playtest.json +810 -0
- package/templates/tower-defense/src/balance.ts +187 -0
- package/templates/tower-defense/src/board/Board.ts +100 -0
- package/templates/tower-defense/src/board/Route.ts +84 -0
- package/templates/tower-defense/src/camera-rig.ts +62 -0
- package/templates/tower-defense/src/economy.ts +64 -0
- package/templates/tower-defense/src/effects/Effects.ts +284 -0
- package/templates/tower-defense/src/enemies/Enemy.ts +198 -0
- package/templates/tower-defense/src/game.ts +72 -0
- package/templates/tower-defense/src/intents.ts +21 -0
- package/templates/tower-defense/src/main.ts +15 -0
- package/templates/tower-defense/src/physics.ts +6 -0
- package/templates/tower-defense/src/render/camera.ts +38 -0
- package/templates/tower-defense/src/render/lighting.ts +42 -0
- package/templates/{defense → tower-defense}/src/render/loading.ts +3 -3
- package/templates/tower-defense/src/render/materials.ts +103 -0
- package/templates/tower-defense/src/render/palette.ts +58 -0
- package/templates/tower-defense/src/render/postprocessing.ts +46 -0
- package/templates/tower-defense/src/render/quality.ts +128 -0
- package/templates/tower-defense/src/render/shapes.ts +550 -0
- package/templates/tower-defense/src/render/sky.ts +27 -0
- package/templates/tower-defense/src/render/worldEnvironment.ts +809 -0
- package/templates/tower-defense/src/scenes/Battle.ts +539 -0
- package/templates/{shooter → tower-defense}/src/scenes/Boot.ts +3 -3
- package/templates/tower-defense/src/state.ts +88 -0
- package/templates/tower-defense/src/style.css +137 -0
- package/templates/tower-defense/src/towers/Tower.ts +230 -0
- package/templates/tower-defense/src/towers/targeting.ts +97 -0
- package/templates/tower-defense/src/ui/Hud.tsx +270 -0
- package/templates/tower-defense/src/ui/Menu.tsx +134 -0
- package/templates/tower-defense/src/ui/icons.tsx +54 -0
- package/templates/tower-defense/src/ui/main.tsx +14 -0
- package/templates/tower-defense/src/waves.ts +90 -0
- package/templates/{defense → tower-defense}/threenative.config.ts +14 -1
- package/templates/tower-defense/vite.config.ts +41 -0
- package/agent-files/.claude/skills/file-engine-bug/SKILL.md +0 -59
- package/agent-files/.claude/skills/prd-creator/SKILL.md +0 -40
- package/agent-files/.claude/skills/threenative-assets/SKILL.md +0 -45
- package/agent-files/.claude/skills/threenative-capabilities/SKILL.md +0 -27
- package/agent-files/.claude/skills/threenative-context/SKILL.md +0 -57
- package/agent-files/.claude/skills/threenative-performance/SKILL.md +0 -42
- package/agent-files/.claude/skills/threenative-playtest/SKILL.md +0 -44
- package/agent-files/.claude/skills/threenative-ui/SKILL.md +0 -20
- package/agent-files/.claude/skills/threenative-visuals/SKILL.md +0 -41
- package/agent-files/AGENT-ROLES.md +0 -26
- package/templates/action-rpg/src/render/shapes.ts +0 -269
- package/templates/action-rpg/src/render/types.ts +0 -3
- package/templates/action-rpg/src/scenes/Boot.ts +0 -12
- package/templates/defense/README.md +0 -10
- package/templates/defense/kit.json +0 -7
- package/templates/defense/playtests/leak.playtest.json +0 -23
- package/templates/defense/playtests/placement.playtest.json +0 -40
- package/templates/defense/playtests/scan.playtest.json +0 -20
- package/templates/defense/playtests/survive.playtest.json +0 -45
- package/templates/defense/playtests/survives.playtest.json +0 -36
- package/templates/defense/src/attackers/Attacker.ts +0 -120
- package/templates/defense/src/board/Route.ts +0 -94
- package/templates/defense/src/conventions.ts +0 -18
- package/templates/defense/src/economy.ts +0 -36
- package/templates/defense/src/entities/Player.ts +0 -52
- package/templates/defense/src/game.ts +0 -51
- package/templates/defense/src/physics.ts +0 -13
- package/templates/defense/src/placement/Buildable.ts +0 -49
- package/templates/defense/src/render/camera.ts +0 -16
- package/templates/defense/src/render/lighting.ts +0 -37
- package/templates/defense/src/render/materials.ts +0 -22
- package/templates/defense/src/render/palette.ts +0 -28
- package/templates/defense/src/render/quality.ts +0 -170
- package/templates/defense/src/render/shapes.ts +0 -246
- package/templates/defense/src/render/sky.ts +0 -45
- package/templates/defense/src/scenes/Boot.ts +0 -12
- package/templates/defense/src/scenes/Defense.ts +0 -231
- package/templates/defense/src/state.ts +0 -56
- package/templates/defense/src/towers/Tower.ts +0 -95
- package/templates/defense/src/towers/targeting.ts +0 -53
- package/templates/defense/src/ui/Hud.tsx +0 -62
- package/templates/defense/src/ui/Menu.tsx +0 -7
- package/templates/defense/src/waves.ts +0 -40
- package/templates/minimal/playtests/atmosphere.playtest.json +0 -32
- package/templates/minimal/src/render/hud.ts +0 -69
- package/templates/platformer/playtests/avoidance.playtest.json +0 -28
- package/templates/platformer/playtests/chase.playtest.json +0 -21
- package/templates/platformer/playtests/collision-layers.playtest.json +0 -36
- package/templates/platformer/playtests/oneway.playtest.json +0 -44
- package/templates/platformer/playtests/patrol.playtest.json +0 -13
- package/templates/platformer/playtests/physics.playtest.json +0 -14
- package/templates/platformer/playtests/stomp-rise.playtest.json +0 -55
- package/templates/platformer/playtests/terminal-loop-fail.playtest.json +0 -41
- package/templates/platformer/playtests/terminal-loop-win.playtest.json +0 -52
- package/templates/platformer/src/conventions.ts +0 -18
- package/templates/platformer/src/entities/Character.ts +0 -286
- package/templates/platformer/src/entities/Chaser.ts +0 -118
- package/templates/platformer/src/entities/Patrol.ts +0 -93
- package/templates/platformer/src/level/Platform.ts +0 -46
- package/templates/platformer/src/render/rig.ts +0 -60
- package/templates/platformer/src/render/terrain.ts +0 -31
- package/templates/platformer/src/scenes/Level.ts +0 -241
- package/templates/puzzle/README.md +0 -43
- package/templates/puzzle/playtests/carry.playtest.json +0 -71
- package/templates/puzzle/playtests/swing.playtest.json +0 -38
- package/templates/puzzle/src/conventions.ts +0 -25
- package/templates/puzzle/src/entities/Ball.ts +0 -70
- package/templates/puzzle/src/entities/Gripper.ts +0 -69
- package/templates/puzzle/src/entities/Pendulum.ts +0 -89
- package/templates/puzzle/src/physics.ts +0 -19
- package/templates/puzzle/src/room.ts +0 -83
- package/templates/puzzle/src/scenes/Boot.ts +0 -12
- package/templates/puzzle/src/scenes/Puzzle.ts +0 -158
- package/templates/shooter/playtests/combat.playtest.json +0 -48
- package/templates/shooter/playtests/fail.playtest.json +0 -31
- package/templates/shooter/playtests/per-item.playtest.json +0 -59
- package/templates/shooter/playtests/shooter.playtest.json +0 -99
- package/templates/shooter/playtests/touch-controls.playtest.json +0 -42
- package/templates/shooter/playtests/weapon-cycle.playtest.json +0 -164
- package/templates/shooter/src/combat/damage.ts +0 -40
- package/templates/shooter/src/conventions.ts +0 -51
- package/templates/shooter/src/entities/Pickup.ts +0 -70
- package/templates/shooter/src/entities/Player.ts +0 -310
- package/templates/shooter/src/level/SpawnPoints.ts +0 -28
- package/templates/shooter/src/playtest-events.ts +0 -15
- package/templates/shooter/src/render/touch-controls.ts +0 -305
- package/templates/shooter/src/render/types.ts +0 -3
- package/templates/shooter/src/render/ui.css +0 -330
- package/templates/shooter/src/render/ui.ts +0 -75
- package/templates/shooter/src/render/vfx.ts +0 -215
- package/templates/shooter/src/ui/Menu.tsx +0 -41
- package/templates/shooter/src/waves.ts +0 -50
- package/templates/shooter/src/weapons/Hitscan.ts +0 -42
- package/templates/shooter/src/weapons/Projectile.ts +0 -92
- package/templates/shooter/src/weapons/Viewmodel.ts +0 -275
- package/templates/starter/playtests/assets.playtest.json +0 -65
- package/templates/starter/playtests/buffer.playtest.json +0 -79
- package/templates/starter/playtests/cloth.playtest.json +0 -113
- package/templates/starter/playtests/coyote.playtest.json +0 -87
- package/templates/starter/playtests/gameover.playtest.json +0 -66
- package/templates/starter/playtests/goal.playtest.json +0 -67
- package/templates/starter/playtests/look.playtest.json +0 -175
- package/templates/starter/playtests/models-baseline.png +0 -0
- package/templates/starter/playtests/models.playtest.json +0 -74
- package/templates/starter/playtests/monitoring.playtest.json +0 -72
- package/templates/starter/playtests/odometer.playtest.json +0 -54
- package/templates/starter/playtests/seed.playtest.json +0 -54
- package/templates/starter/playtests/textures-baseline.png +0 -0
- package/templates/starter/playtests/textures.playtest.json +0 -74
- package/templates/starter/playtests/touch-controls.playtest.json +0 -42
- package/templates/starter/playtests/zoom-pinch.playtest.json +0 -86
- package/templates/starter/playtests/zoom-wheel.playtest.json +0 -59
- package/templates/starter/src/render/coast.ts +0 -199
- package/templates/starter/src/render/hero.ts +0 -88
- package/templates/starter/src/render/implicitSurface.ts +0 -199
- package/templates/starter/src/render/kuwahara.ts +0 -174
- package/templates/starter/src/render/outline.ts +0 -141
- package/templates/starter/src/render/painterly.ts +0 -86
- package/templates/starter/src/render/rockRidge.ts +0 -198
- package/templates/starter/src/render/rockRidge.worker.ts +0 -77
- package/templates/starter/src/render/scenery.ts +0 -40
- package/templates/starter/src/render/textureLifetime.ts +0 -53
- package/templates/starter/src/render/water.ts +0 -38
- package/templates/starter/src/render/watercolor.ts +0 -85
- /package/templates/{starter → puzzle}/playtests/boot-failure.playtest.json +0 -0
- /package/templates/{starter → puzzle}/playtests/pause.playtest.json +0 -0
- /package/templates/{defense → rts}/.codex/config.toml +0 -0
- /package/templates/{defense → rts}/.cursor/mcp.json +0 -0
- /package/templates/{defense → rts}/.gemini/settings.json +0 -0
- /package/templates/{defense → rts}/.mcp.json +0 -0
- /package/templates/{defense → rts}/.vscode/mcp.json +0 -0
- /package/templates/{defense → rts}/.zed/settings.json +0 -0
- /package/templates/{defense → rts}/index.html +0 -0
- /package/templates/{defense → rts}/opencode.json +0 -0
- /package/templates/{defense → rts}/src/main.ts +0 -0
- /package/templates/{defense → rts}/src/ui/main.tsx +0 -0
- /package/templates/{defense → tower-defense}/public/favicon.svg +0 -0
- /package/templates/{defense → tower-defense}/src/playtest-events.ts +0 -0
- /package/templates/{defense → tower-defense}/src/ui/App.tsx +0 -0
- /package/templates/{defense → tower-defense}/src/ui/GameUi.tsx +0 -0
- /package/templates/{defense → tower-defense}/tailwind.config.ts +0 -0
- /package/templates/{defense → tower-defense}/tsconfig.json +0 -0
|
@@ -0,0 +1,2095 @@
|
|
|
1
|
+
import {
|
|
2
|
+
GroundSnap,
|
|
3
|
+
type ICtx,
|
|
4
|
+
type IStrideReport,
|
|
5
|
+
SkeletalMesh3D,
|
|
6
|
+
attachToBone,
|
|
7
|
+
boneContact,
|
|
8
|
+
measureThreePose,
|
|
9
|
+
normaliseToMetres,
|
|
10
|
+
} from "@threenative/core";
|
|
11
|
+
import { CharacterBody3D, CollisionShape3D, type IPhysicsContext } from "@threenative/physics";
|
|
12
|
+
import {
|
|
13
|
+
type AnimationClip,
|
|
14
|
+
Box3,
|
|
15
|
+
BoxGeometry,
|
|
16
|
+
Color,
|
|
17
|
+
Group,
|
|
18
|
+
MathUtils,
|
|
19
|
+
Mesh,
|
|
20
|
+
MeshBasicMaterial,
|
|
21
|
+
MeshStandardMaterial,
|
|
22
|
+
type Object3D,
|
|
23
|
+
Quaternion,
|
|
24
|
+
Vector3,
|
|
25
|
+
} from "three";
|
|
26
|
+
import { scale } from "../render/scale.js";
|
|
27
|
+
import type { TownCollider } from "../render/town.js";
|
|
28
|
+
import type { GameState } from "../state.js";
|
|
29
|
+
|
|
30
|
+
type GameCtx = ICtx<GameState, IPhysicsContext>;
|
|
31
|
+
|
|
32
|
+
/** One solid box in the world: nav, grounding and hit tests all read these. */
|
|
33
|
+
export type BoxCollider = TownCollider;
|
|
34
|
+
|
|
35
|
+
export type EnemyPhase = "patrol" | "suspicious" | "engage" | "search" | "return" | "dead";
|
|
36
|
+
|
|
37
|
+
const MAX_HEALTH = 36;
|
|
38
|
+
const WALK_SPEED = 2.4;
|
|
39
|
+
/**
|
|
40
|
+
* Chase pace, in metres per second.
|
|
41
|
+
*
|
|
42
|
+
* It was 3.6, bounded to 2.75 by a rig that had no run cycle: the only locomotion clip was a
|
|
43
|
+
* walk, so a 3.6 m/s chase drove its playback rate to 2.75 against a ceiling of 3 — a walk cycle
|
|
44
|
+
* on fast-forward, which is the "moving faster than he is animated" read. The engine's stride
|
|
45
|
+
* convention now holds `Rifle_Walk` against the ground, so the pace is bounded by the soldier.
|
|
46
|
+
*/
|
|
47
|
+
const CHASE_SPEED = 3.4;
|
|
48
|
+
const HEAR_RANGE = 26;
|
|
49
|
+
const VIEW_RANGE = 30;
|
|
50
|
+
const VIEW_HALF_ANGLE = MathUtils.degToRad(46);
|
|
51
|
+
const ENGAGE_RANGE = 13;
|
|
52
|
+
const BURST_ROUNDS = 3;
|
|
53
|
+
const BURST_SPACING = 0.11;
|
|
54
|
+
const BURST_COOLDOWN = 3.2;
|
|
55
|
+
const ROUND_DAMAGE = 9;
|
|
56
|
+
const RESPAWN_SECONDS = 4.5;
|
|
57
|
+
const AGENT_RADIUS = 0.48;
|
|
58
|
+
const NAV_CELL = 0.7;
|
|
59
|
+
const NAV_MIN = -16;
|
|
60
|
+
const NAV_MAX = 16;
|
|
61
|
+
const NAV_REPLAN_SECONDS = 0.4;
|
|
62
|
+
/** Seconds between first sighting and the first round, so the player is not shot on sight. */
|
|
63
|
+
const REACTION_SECONDS = 0.45;
|
|
64
|
+
const SPAWN_GRACE_SECONDS = 2.5;
|
|
65
|
+
/**
|
|
66
|
+
* Animation blending, tuned by eye rather than by what typechecks.
|
|
67
|
+
*
|
|
68
|
+
* `LOCOMOTION_FADE` at 0.05 s is three frames, which pops; a quarter second reads as weight
|
|
69
|
+
* shifting between feet. `LOCOMOTION_HOLD` stops the rig re-deciding every frame when crouch
|
|
70
|
+
* state sits on a threshold, and `STILL_BEFORE_IDLE` stops one blocked step reading as a stop.
|
|
71
|
+
*/
|
|
72
|
+
const LOCOMOTION_FADE = 0.26;
|
|
73
|
+
const LOCOMOTION_HOLD_SECONDS = 0.3;
|
|
74
|
+
const STILL_BEFORE_IDLE_SECONDS = 0.16;
|
|
75
|
+
/**
|
|
76
|
+
* Ground speed below which the rate is meaningless and a footstep is not a footstep.
|
|
77
|
+
*
|
|
78
|
+
* The playback rate itself is the engine's business: `SkeletalMesh3D` holds the clip against
|
|
79
|
+
* the ground the body actually covered, off the root's own world motion, so a game never has to
|
|
80
|
+
* measure a stride and re-time an action itself. This floor only decides when the squad has
|
|
81
|
+
* stopped so far that the question is meaningless.
|
|
82
|
+
*/
|
|
83
|
+
const MOVING_SPEED_FLOOR = 0.3;
|
|
84
|
+
/**
|
|
85
|
+
* Ground speed `Rifle_Crouch_Walk` carries at rate 1, in metres per second, measured off this
|
|
86
|
+
* asset's own feet. It is the slowest locomotion clip the game has, so it is the one whose
|
|
87
|
+
* playback rate the engine's stride convention has to push hardest when a body changes gait.
|
|
88
|
+
*/
|
|
89
|
+
const CROUCH_PACE = 0.81;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* How a body gets up to walking pace and back down again, in metres per second squared.
|
|
93
|
+
*
|
|
94
|
+
* Travel used to be `speed * dt` with no ramp at all. Traced over 359 frames of patrol, the
|
|
95
|
+
* soldier was either standing at exactly 0 or travelling at exactly 2.400 m/s, and the change
|
|
96
|
+
* between the two happened inside one frame. That single step is most of what reads as a
|
|
97
|
+
* machine: nothing with legs reaches full pace instantly, and nothing with legs stops dead.
|
|
98
|
+
* 6.5 m/s² is about 0.37 s to walking pace, which is roughly two steps.
|
|
99
|
+
*/
|
|
100
|
+
const WALK_ACCEL = 6.5;
|
|
101
|
+
const WALK_DECEL = 9;
|
|
102
|
+
/**
|
|
103
|
+
* Steering and facing, in radians per second and radians per second squared.
|
|
104
|
+
*
|
|
105
|
+
* The old turn was `MathUtils.clamp(delta, -dt * 7, dt * 7)`: a bang-bang servo that sat at
|
|
106
|
+
* exactly zero angular velocity, saturated at the cap for two or three frames at a waypoint,
|
|
107
|
+
* and stopped dead. The same 359-frame trace held the heading bit-exactly constant on 343
|
|
108
|
+
* frames and ran at 5–10 rad/s on the other 16. Carrying angular velocity and accelerating it
|
|
109
|
+
* toward what the error asks for gives a turn that starts, peaks and settles.
|
|
110
|
+
*
|
|
111
|
+
* Travel direction is steered as well as facing, so a corner is a curve through the waypoint
|
|
112
|
+
* rather than a vertex — the A* grid is 0.7 m, and turning hard at every grid point is what
|
|
113
|
+
* made the route read as faceted. `#segmentClear` already keeps the corridor wide enough for
|
|
114
|
+
* the body, and cornering sheds speed, so the curve stays inside what the search cleared.
|
|
115
|
+
*
|
|
116
|
+
* Steering is deliberately quicker than facing. `enemy-reaches-walkway` is the constraint: the
|
|
117
|
+
* soldier has about eight seconds to cover twenty-five metres, and the first tuning — steering
|
|
118
|
+
* at 3.1 rad/s with cornering cutting to a third of pace — cost roughly a metre and a half per
|
|
119
|
+
* ninety-degree corner and he arrived too late, twice, against a baseline that passed twice.
|
|
120
|
+
* At 4.6 rad/s a right-angle takes about a third of a second, which is what a walking person
|
|
121
|
+
* takes, and the visible easing is all still there: peak turn rate stays under half of the old
|
|
122
|
+
* clamp's, and the turn still spins up and settles instead of switching on and off.
|
|
123
|
+
*/
|
|
124
|
+
const STEER_RATE_MAX = 4.6;
|
|
125
|
+
const STEER_ACCEL = 26;
|
|
126
|
+
const STEER_SETTLE = 0.11;
|
|
127
|
+
const FACE_RATE_MAX = 4.4;
|
|
128
|
+
const FACE_ACCEL = 26;
|
|
129
|
+
const FACE_SETTLE = 0.12;
|
|
130
|
+
/**
|
|
131
|
+
* Pace below which a coast is over, in metres per second.
|
|
132
|
+
*
|
|
133
|
+
* Two centimetres a second is under a millimetre a frame: past this the body is standing, and
|
|
134
|
+
* carrying the remainder only keeps the walk clip alive on a soldier who has stopped.
|
|
135
|
+
*/
|
|
136
|
+
const COAST_FLOOR = 0.02;
|
|
137
|
+
/** Heading error, in radians, at which cornering has taken all the speed off it can. */
|
|
138
|
+
const CORNER_FULL = 2.2;
|
|
139
|
+
/** Slowest a corner may be taken, as a fraction of the requested speed. */
|
|
140
|
+
const CORNER_FLOOR = 0.55;
|
|
141
|
+
/** Metres from the end of the route over which he eases off instead of stopping on the mark. */
|
|
142
|
+
const ARRIVE_DISTANCE = 1.6;
|
|
143
|
+
const ARRIVE_FLOOR = 0.3;
|
|
144
|
+
/**
|
|
145
|
+
* How far a soldier's facing may deviate from where his feet are going, in radians.
|
|
146
|
+
*
|
|
147
|
+
* `#engage` aimed the body at the player and then `#step` immediately overwrote that with the
|
|
148
|
+
* travel heading, so a flanking soldier turned his back and jogged. Facing the player outright
|
|
149
|
+
* is not the answer either: this rig has no strafe cycle, so a body moving sideways under a
|
|
150
|
+
* forward walk clip moonwalks. 0.75 rad is as far as the feet can be wrong before that shows.
|
|
151
|
+
*/
|
|
152
|
+
const AIM_LEAD_MAX = 0.75;
|
|
153
|
+
/** Reactions and deaths are sharp events, but three frames is still a pop. */
|
|
154
|
+
const REACTION_FADE = 0.12;
|
|
155
|
+
const DEATH_FADE = 0.14;
|
|
156
|
+
const FIRE_FADE = 0.1;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The clips this rig plays, by the name the AI states them under.
|
|
160
|
+
*
|
|
161
|
+
* `SkeletalMesh3D` is given the whole list as `requiredClips`, so a template shipped against an
|
|
162
|
+
* asset that lost one of them throws at load with the missing name rather than quietly playing
|
|
163
|
+
* nothing. Every name is a real clip on `assets/mannequin-combat.glb`; the pack also carries
|
|
164
|
+
* `Sprint_Loop`, `Jump_*`, `Roll` and the pistol aim variants, which this game has no state for.
|
|
165
|
+
*/
|
|
166
|
+
export const ENEMY_CLIPS = [
|
|
167
|
+
"Rifle_Idle",
|
|
168
|
+
"Rifle_Walk",
|
|
169
|
+
"Rifle_Crouch_To_Idle",
|
|
170
|
+
"Rifle_Crouch_Walk",
|
|
171
|
+
"Rifle_Shoot",
|
|
172
|
+
"Rifle_Hit",
|
|
173
|
+
"Death01",
|
|
174
|
+
] as const;
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Rifle rotation in `hand_r` space, degrees XYZ, per rifle clip: the barrel points at `hand_l`
|
|
178
|
+
* and the sights at the sky. Measured off the retargeted clips
|
|
179
|
+
* (hand_l in the hand_r frame, twelve samples a clip; the spread never exceeds 6 cm, so one
|
|
180
|
+
* rotation per clip holds the whole cycle). Clips not listed keep the last hold.
|
|
181
|
+
*/
|
|
182
|
+
const RIFLE_HOLD: Readonly<Record<string, readonly [number, number, number]>> = {
|
|
183
|
+
Rifle_Idle: [-80.1, -13.4, -148.4],
|
|
184
|
+
Rifle_Walk: [-80.2, -21.4, -158.1],
|
|
185
|
+
Rifle_Crouch_Walk: [-72.2, -17.9, -161.2],
|
|
186
|
+
Rifle_Crouch_To_Idle: [-72.6, -16.3, -147.6],
|
|
187
|
+
Rifle_Shoot: [-83.4, -20.1, -170],
|
|
188
|
+
Rifle_Hit: [-70.4, -27.7, -113.4],
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* The tint every soldier wears, in the template's own dark grey.
|
|
193
|
+
*
|
|
194
|
+
* The mannequin ships near-white, and the player's first-person hands are white gloves at the
|
|
195
|
+
* bottom of the same frame. Two white figures in one image, one of which the player is looking
|
|
196
|
+
* through and one of which he is shooting, is the readability problem the whole crate-blue /
|
|
197
|
+
* orange-plate palette is built to avoid.
|
|
198
|
+
*/
|
|
199
|
+
const ENEMY_TINT = 0x4a4f57;
|
|
200
|
+
|
|
201
|
+
const ROUTE: readonly Vector3[] = [
|
|
202
|
+
new Vector3(-4.5, 0, -9.5),
|
|
203
|
+
new Vector3(-11.5, 0, -13.0),
|
|
204
|
+
new Vector3(-1.0, 0, -15.0),
|
|
205
|
+
new Vector3(4.5, 0, -11.0),
|
|
206
|
+
new Vector3(11.6, 0, -8.6),
|
|
207
|
+
new Vector3(1.5, 0, -6.5),
|
|
208
|
+
new Vector3(-6.0, 0, -4.5),
|
|
209
|
+
];
|
|
210
|
+
const ROUTE_START = ROUTE[0] ?? new Vector3();
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Uniform world scale of an object, read straight off its world matrix.
|
|
214
|
+
*
|
|
215
|
+
* `Object3D.getWorldScale` decomposes the whole matrix and allocates a `Vector3`; the length of
|
|
216
|
+
* the first basis column is the same number for the uniform scales this rig uses, and this runs
|
|
217
|
+
* per soldier per frame.
|
|
218
|
+
*/
|
|
219
|
+
/**
|
|
220
|
+
* Where a soldier's frame goes, accumulated across the squad.
|
|
221
|
+
*
|
|
222
|
+
* A section timer inside the per-soldier update, because "enemies cost 13 ms" is not something
|
|
223
|
+
* anyone can act on. Peaks are per frame and reset by `beginSquadFrame`, so what comes out is
|
|
224
|
+
* the worst single frame each stage has ever cost across all five soldiers together.
|
|
225
|
+
*/
|
|
226
|
+
/**
|
|
227
|
+
* Seconds between line-of-sight raycasts for one soldier.
|
|
228
|
+
*
|
|
229
|
+
* The cheap half of `#canSee` — range and view cone — still runs every frame, so a soldier turning
|
|
230
|
+
* away or stepping out of range loses sight instantly. Only the raycast is rationed, because it is
|
|
231
|
+
* a `raycastAll` against every solid in the town and five soldiers doing that at 60 Hz measured at
|
|
232
|
+
* 15.9 ms of a 16.3 ms frame: the entire mid-round hitch, in one call.
|
|
233
|
+
*
|
|
234
|
+
* A tenth of a second of staleness on "can he see me" is invisible next to `REACTION_SECONDS`,
|
|
235
|
+
* which already holds fire for far longer than this after a soldier first spots the player.
|
|
236
|
+
*/
|
|
237
|
+
const LOS_INTERVAL_SECONDS = 0;
|
|
238
|
+
/**
|
|
239
|
+
* Shortest gap between two grid searches for one soldier when only the goal has drifted.
|
|
240
|
+
*
|
|
241
|
+
* `NAV_REPLAN_SECONDS` (0.4) is the gap for a route that has become obstructed. Reusing it for
|
|
242
|
+
* goal drift is too coarse to navigate with: `enemy-reaches-walkway` caught a soldier failing to
|
|
243
|
+
* work his way under the deck because he could not re-plan often enough to follow the target
|
|
244
|
+
* around it. A tenth of a second still turns a per-frame search into one every six frames, which
|
|
245
|
+
* is where nearly all of the saving was.
|
|
246
|
+
*
|
|
247
|
+
* Settled at 0.15 s, about nine frames. The scenario's flakiness turned out to be a stale sight
|
|
248
|
+
* answer rather than a stale route — see `#canSee` — so this can be rationed properly: a route
|
|
249
|
+
* re-aimed six times a second still tracks a running player, and the search stops being a
|
|
250
|
+
* per-frame cost.
|
|
251
|
+
*/
|
|
252
|
+
const GOAL_REPLAN_SECONDS = 0.15;
|
|
253
|
+
/** Spreads the squad's raycasts across frames, so five soldiers never all test on the same one. */
|
|
254
|
+
const losStagger = 0;
|
|
255
|
+
|
|
256
|
+
/** Blocked-cell bitmaps per collider set, so the squad pays for the nav grid once. */
|
|
257
|
+
const NAV_GRIDS = new WeakMap<object, Map<string, Uint8Array>>();
|
|
258
|
+
|
|
259
|
+
// Scratch for the per-frame sight test; five soldiers × three vectors × 60 Hz is real garbage.
|
|
260
|
+
const scratchGoal = new Vector3();
|
|
261
|
+
const scratchTo = new Vector3();
|
|
262
|
+
const scratchFacing = new Vector3();
|
|
263
|
+
const scratchFlat = new Vector3();
|
|
264
|
+
|
|
265
|
+
/** Shortest signed rotation from one yaw to another, in radians. */
|
|
266
|
+
function angleDelta(from: number, to: number): number {
|
|
267
|
+
return Math.atan2(Math.sin(to - from), Math.cos(to - from));
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* A yaw that carries its own angular velocity, so a turn eases in as well as out.
|
|
272
|
+
*
|
|
273
|
+
* Clamping the per-frame step — what `#turn` did — gives a body that is either not turning at
|
|
274
|
+
* all or turning at exactly its maximum. There is no third state, and both edges are a step
|
|
275
|
+
* change in angular velocity. Accelerating the rate toward `delta / settle` instead means the
|
|
276
|
+
* turn spins up over a few frames, tops out at `maxRate`, and eases onto the target. Never
|
|
277
|
+
* stepping past the remaining delta keeps it from ringing on a small correction.
|
|
278
|
+
*/
|
|
279
|
+
class EasedYaw {
|
|
280
|
+
value: number;
|
|
281
|
+
#rate = 0;
|
|
282
|
+
readonly #maxRate: number;
|
|
283
|
+
readonly #accel: number;
|
|
284
|
+
readonly #settle: number;
|
|
285
|
+
|
|
286
|
+
constructor(maxRate: number, accel: number, settle: number, value = 0) {
|
|
287
|
+
this.#maxRate = maxRate;
|
|
288
|
+
this.#accel = accel;
|
|
289
|
+
this.#settle = settle;
|
|
290
|
+
this.value = value;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** Jump to a yaw with no turn at all: a respawn, or a body placed by a scenario. */
|
|
294
|
+
set(value: number): void {
|
|
295
|
+
this.value = value;
|
|
296
|
+
this.#rate = 0;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** `scale` trims the ceiling for a slower turn — looking around rather than reorienting. */
|
|
300
|
+
step(target: number, dt: number, scale = 1): number {
|
|
301
|
+
const delta = angleDelta(this.value, target);
|
|
302
|
+
const ceiling = this.#maxRate * scale;
|
|
303
|
+
const wanted = MathUtils.clamp(delta / this.#settle, -ceiling, ceiling);
|
|
304
|
+
this.#rate += MathUtils.clamp(wanted - this.#rate, -this.#accel * dt, this.#accel * dt);
|
|
305
|
+
const magnitude = Math.abs(delta);
|
|
306
|
+
this.value += MathUtils.clamp(this.#rate * dt, -magnitude, magnitude);
|
|
307
|
+
return this.value;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** Drive the yaw directly — a scanning sweep — while keeping `rate` honest for the carriage. */
|
|
311
|
+
spin(radiansPerSecond: number, dt: number): number {
|
|
312
|
+
this.#rate = radiansPerSecond;
|
|
313
|
+
this.value += radiansPerSecond * dt;
|
|
314
|
+
return this.value;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* The rifle every soldier carries, built from five boxes.
|
|
320
|
+
*
|
|
321
|
+
* ## Why it is written rather than downloaded
|
|
322
|
+
*
|
|
323
|
+
* The old rig carried a rigged AK-47 with retargeted Mixamo clips, which meant a second skinned
|
|
324
|
+
* asset per soldier, a `Grip_Bone` to align against, a magazine bone to read a muzzle from, and
|
|
325
|
+
* a 90-line pose table translating one weapon's animation into another's. The mannequin has no
|
|
326
|
+
* weapon socket and no rifle clips, so all of that was solving a problem the template no longer
|
|
327
|
+
* has — and shipping ~1 MB of AK geometry into every scaffolded project to do it.
|
|
328
|
+
*
|
|
329
|
+
* Five boxes read as a rifle at the distances a soldier is ever seen from, cost twelve triangles,
|
|
330
|
+
* and normalise to `scale.rifleLength` like any other prop. The grip is the holder's own origin,
|
|
331
|
+
* so the right hand lands on it by construction and there is nothing to align afterwards.
|
|
332
|
+
*
|
|
333
|
+
* ## Layout, in metres, along the barrel (+z is the muzzle)
|
|
334
|
+
*
|
|
335
|
+
* receiver 0.42 long, centred at z 0.06 the body and the sight line
|
|
336
|
+
* barrel 0.30 long, centred at z 0.40 thin, forward
|
|
337
|
+
* magazine 0.14 long, below at z 0.02 the tell that reads as "rifle" from the front
|
|
338
|
+
* stock 0.24 long, behind at z -0.26 what puts the shoulder line in the right place
|
|
339
|
+
* grip 0.10 long, below at z -0.04 the block the fist closes around
|
|
340
|
+
*
|
|
341
|
+
* ## The hold
|
|
342
|
+
*
|
|
343
|
+
* `hand_r`'s own axes run *down the arm*, so the rifle's barrel (+Z) has to be turned toward the
|
|
344
|
+
* left hand and its sights toward the sky in that bone's space. Where that is depends on the
|
|
345
|
+
* clip, so `RIFLE_HOLD` carries one rotation per rifle clip and `#holdRifle` applies it on every
|
|
346
|
+
* clip change; `rightHandContact` and `leftHandContact` (engine `boneContact`) prove both fists
|
|
347
|
+
* reach the gun.
|
|
348
|
+
*/
|
|
349
|
+
function buildRifle(): Group {
|
|
350
|
+
const rifle = new Group();
|
|
351
|
+
rifle.name = "enemy-rifle";
|
|
352
|
+
const part = (
|
|
353
|
+
name: string,
|
|
354
|
+
size: readonly [number, number, number],
|
|
355
|
+
at: readonly [number, number, number],
|
|
356
|
+
): void => {
|
|
357
|
+
const mesh = new Mesh(new BoxGeometry(size[0], size[1], size[2]), rifleMaterial());
|
|
358
|
+
mesh.name = name;
|
|
359
|
+
mesh.position.set(at[0], at[1], at[2]);
|
|
360
|
+
mesh.castShadow = false;
|
|
361
|
+
mesh.receiveShadow = false;
|
|
362
|
+
rifle.add(mesh);
|
|
363
|
+
};
|
|
364
|
+
part("receiver", [0.05, 0.09, 0.42], [0, 0, 0.06]);
|
|
365
|
+
part("barrel", [0.03, 0.03, 0.3], [0, 0.01, 0.4]);
|
|
366
|
+
part("magazine", [0.04, 0.14, 0.06], [0, -0.1, 0.02]);
|
|
367
|
+
part("stock", [0.04, 0.08, 0.24], [0, -0.01, -0.26]);
|
|
368
|
+
part("grip", [0.04, 0.1, 0.05], [0, -0.08, -0.04]);
|
|
369
|
+
return rifle;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** One shared material: the rifle never changes colour, so five meshes cost one pipeline. */
|
|
373
|
+
let rifleSurface: MeshStandardMaterial | undefined;
|
|
374
|
+
function rifleMaterial(): MeshStandardMaterial {
|
|
375
|
+
rifleSurface ??= new MeshStandardMaterial({ color: 0x2a2e33, roughness: 0.55, metalness: 0.5 });
|
|
376
|
+
return rifleSurface;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export type EnemyHooks = {
|
|
380
|
+
/** True when nothing solid blocks the segment. */
|
|
381
|
+
readonly lineOfSight: (from: Vector3, to: Vector3) => boolean;
|
|
382
|
+
readonly damagePlayer: (amount: number) => void;
|
|
383
|
+
readonly onMuzzleFlash: (at: Vector3, direction: Vector3, distance: number) => void;
|
|
384
|
+
/**
|
|
385
|
+
* A foot planted while actually moving — at most twice per walk cycle, read off
|
|
386
|
+
* the locomotion action's phase so steps stay glued to the animation.
|
|
387
|
+
*/
|
|
388
|
+
readonly onFootstep?: (at: Vector3) => void;
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
/** Raised-deck footprints this soldier may be routed beneath. */
|
|
392
|
+
export type DeckFootprint = {
|
|
393
|
+
readonly minX: number;
|
|
394
|
+
readonly maxX: number;
|
|
395
|
+
readonly minZ: number;
|
|
396
|
+
readonly maxZ: number;
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
export type EnemyOptions = {
|
|
400
|
+
/** Patrol loop to walk and return to; defaults to the single range route. */
|
|
401
|
+
readonly route?: readonly Vector3[];
|
|
402
|
+
/** Ground rectangle the navigation grid covers; defaults to the old yard. */
|
|
403
|
+
readonly navBounds?: { readonly min: number; readonly max: number };
|
|
404
|
+
/** Raised deck footprints, reported by `underDeck` so a scenario can see routing under them. */
|
|
405
|
+
readonly decks?: readonly DeckFootprint[];
|
|
406
|
+
/**
|
|
407
|
+
* Scenario-placed sentry: stands at route[0] and never walks it. Hearing and vision
|
|
408
|
+
* are disconnected, so rounds landing near him cannot turn the rest of the shot into
|
|
409
|
+
* pursuit; a killing round still plays the full death. Shooting scenarios need a
|
|
410
|
+
* target whose hit window does not depend on where a patrol happens to be.
|
|
411
|
+
*/
|
|
412
|
+
readonly frozen?: boolean;
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
/** First bone whose name matches, for a model that renames the bones this rig relies on. */
|
|
416
|
+
function findBone(root: Object3D, pattern: RegExp): Object3D | undefined {
|
|
417
|
+
let found: Object3D | undefined;
|
|
418
|
+
root.traverse((object) => {
|
|
419
|
+
if (found === undefined && pattern.test(object.name)) found = object;
|
|
420
|
+
});
|
|
421
|
+
return found;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Give one mesh its own material instances, cloned off whatever it was assigned.
|
|
426
|
+
*
|
|
427
|
+
* `SkeletalMesh3D` clones the skeleton per soldier (three's own `SkeletonUtils.clone`), but its
|
|
428
|
+
* own doc comment says the quiet part: "geometries and materials … are reused by reference." All
|
|
429
|
+
* five soldiers' meshes point at the exact same `MeshStandardMaterial` objects the GLTF parsed
|
|
430
|
+
* once. Tinting it is harmless — every soldier wants the same colour — but `#setOpacity` is not:
|
|
431
|
+
* it writes `opacity` on that shared object to fade one corpse in or out, so for the ~0.35 s a
|
|
432
|
+
* kill is respawning, every *other* soldier's mesh reads that same falling-then-rising opacity
|
|
433
|
+
* and flickers transparent with him. That is the intermittent "enemy texture doesn't load" —
|
|
434
|
+
* a live soldier going half-invisible because a dead one two lanes over is fading. Cloning here,
|
|
435
|
+
* once per mesh at construction, is what makes a soldier's blend state his own.
|
|
436
|
+
*/
|
|
437
|
+
function ownMaterial(mesh: Mesh): void {
|
|
438
|
+
mesh.material = Array.isArray(mesh.material)
|
|
439
|
+
? mesh.material.map((one) => one.clone())
|
|
440
|
+
: (mesh.material as MeshStandardMaterial).clone();
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Recolour one body material in place.
|
|
445
|
+
*
|
|
446
|
+
* `color` is written rather than `set`, and `needsUpdate` is left alone: the diffuse colour is a
|
|
447
|
+
* uniform the pipeline already carries, so a soldier joining the squad costs no shader compile.
|
|
448
|
+
*/
|
|
449
|
+
function tintEnemyMaterial(material: MeshStandardMaterial | MeshStandardMaterial[]): void {
|
|
450
|
+
for (const one of Array.isArray(material) ? material : [material]) {
|
|
451
|
+
one.color.setHex(ENEMY_TINT);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
export class Enemy {
|
|
456
|
+
readonly group = new Group();
|
|
457
|
+
readonly hitbox: Mesh;
|
|
458
|
+
health = MAX_HEALTH;
|
|
459
|
+
phase: EnemyPhase = "patrol";
|
|
460
|
+
wounded = false;
|
|
461
|
+
/**
|
|
462
|
+
* Combat voice, assigned by the scene and throttled there (one shout at a time
|
|
463
|
+
* across the squad). Optional so tests and headless rigs run mute.
|
|
464
|
+
*/
|
|
465
|
+
voice:
|
|
466
|
+
| {
|
|
467
|
+
readonly spot: (at: Vector3) => void;
|
|
468
|
+
readonly chase: (at: Vector3) => void;
|
|
469
|
+
readonly pain: (at: Vector3) => void;
|
|
470
|
+
readonly death: (at: Vector3) => void;
|
|
471
|
+
}
|
|
472
|
+
| undefined;
|
|
473
|
+
/**
|
|
474
|
+
* Whether the body is snapped down so its lowest posed point rests on the deck.
|
|
475
|
+
*
|
|
476
|
+
* On is right for a soldier walking a range: the planted foot has to touch the floor and
|
|
477
|
+
* the corpse has to lie on it. Turn it off for anything that is legitimately airborne —
|
|
478
|
+
* a fall, a ragdoll driven by physics, a vault, a scripted drop — otherwise this pins the
|
|
479
|
+
* body to the ground and eats the motion. `footClearance` keeps reporting the real height
|
|
480
|
+
* either way, so a scenario can still see where the body actually is.
|
|
481
|
+
*/
|
|
482
|
+
groundSnap = true;
|
|
483
|
+
/**
|
|
484
|
+
* The rig, driven by its own clips.
|
|
485
|
+
*
|
|
486
|
+
* `SkeletalMesh3D` is the whole body layer: it clones the skeleton once per soldier, normalises
|
|
487
|
+
* the clone, and mounts the clips with `requiredClips` so a missing one throws by name at load.
|
|
488
|
+
* Its stride convention holds the current clip's playback rate against the ground the body
|
|
489
|
+
* actually covered, measured off the root's world motion — which is what this file used to do
|
|
490
|
+
* itself, with ninety lines of stride sampling and its own rate clamps.
|
|
491
|
+
*/
|
|
492
|
+
#character: SkeletalMesh3D;
|
|
493
|
+
/** Keeps the lowest posed point of the body on the deck, off a cached skin envelope. */
|
|
494
|
+
#ground: GroundSnap;
|
|
495
|
+
#clips: ReadonlySet<string>;
|
|
496
|
+
#clipDurations = new Map<string, number>();
|
|
497
|
+
#routeIndex = 0;
|
|
498
|
+
#target = new Vector3();
|
|
499
|
+
#lastSeen = new Vector3();
|
|
500
|
+
#alertTimer = 0;
|
|
501
|
+
#burstLeft = 0;
|
|
502
|
+
#burstTimer = 0;
|
|
503
|
+
#cooldown = 0;
|
|
504
|
+
#strafe = 1;
|
|
505
|
+
#strafeTimer = 0;
|
|
506
|
+
#deadFor = 0;
|
|
507
|
+
#fade = 1;
|
|
508
|
+
/**
|
|
509
|
+
* Sticky lowest body opacity this soldier has ever carried, so a scenario watching a
|
|
510
|
+
* *different* soldier's respawn cycle can prove this one never dipped — the regression
|
|
511
|
+
* `ownMaterial` fixes wrote every soldier's opacity to whichever one was fading.
|
|
512
|
+
*/
|
|
513
|
+
#opacityFloor = 1;
|
|
514
|
+
#bodyClearance: number | null = null;
|
|
515
|
+
#footClearance: number | null = null;
|
|
516
|
+
#deathObserved = false;
|
|
517
|
+
/**
|
|
518
|
+
* Sticky record of the last death: which clip ran and how many frames it advanced. Sticky
|
|
519
|
+
* because the corpse is gone 4.5 s later, so a scenario sampling after the respawn would
|
|
520
|
+
* otherwise see a live soldier and be unable to tell a played death from a frozen one.
|
|
521
|
+
*/
|
|
522
|
+
#deathClip: string | null = null;
|
|
523
|
+
#deathClipFrames = 0;
|
|
524
|
+
/** Travel direction of the round that last connected, so the body falls away from the shooter. */
|
|
525
|
+
#lastHitDirection: Vector3 | null = null;
|
|
526
|
+
#hips: Object3D | undefined;
|
|
527
|
+
/** Hip world position at the instant of death, so the fall direction can be measured. */
|
|
528
|
+
#deathHipStart: Vector3 | null = null;
|
|
529
|
+
/** Sticky fall measurement: the corpse is gone before a scenario can read a live value. */
|
|
530
|
+
#deathFallMeasured: number | null = null;
|
|
531
|
+
/** True while the last locomotion frame was a crouch, so standing up can play its transition. */
|
|
532
|
+
#crouchMoving = false;
|
|
533
|
+
/** Seconds left of the crouch-to-stand transition, during which no locomotion clip overrides it. */
|
|
534
|
+
#standUp = 0;
|
|
535
|
+
/** Seconds left of being under fire. Degrades aim and keeps the soldier low. */
|
|
536
|
+
#suppressed = 0;
|
|
537
|
+
#suppressedPeak = 0;
|
|
538
|
+
/**
|
|
539
|
+
* Frames each clip has actually advanced this session. The rig ships twenty clips; without a
|
|
540
|
+
* per-clip count nothing catches four of them quietly going unused again.
|
|
541
|
+
*/
|
|
542
|
+
#clipFrames = new Map<string, number>();
|
|
543
|
+
/** Worst `boneContact` metres either hand has been from the rifle, per clip, sampled every 4th frame. */
|
|
544
|
+
#contactPeak = new Map<string, number>();
|
|
545
|
+
/** Locomotion clip currently committed to, and how long before another switch is allowed. */
|
|
546
|
+
#locomotion = "";
|
|
547
|
+
#locomotionHold = 0;
|
|
548
|
+
/** Walk-cycle phase bookkeeping for the footstep hook: which half-cycle last planted a foot. */
|
|
549
|
+
#lastStepClip = "";
|
|
550
|
+
#lastStepHalf = -1;
|
|
551
|
+
/** Clips by name, so a footstep can be read off the action the engine is playing. */
|
|
552
|
+
#clipsByName = new Map<string, AnimationClip>();
|
|
553
|
+
/** Where the body stood at the top of this frame, so ground speed is measured, not assumed. */
|
|
554
|
+
#frameStart = new Vector3();
|
|
555
|
+
#groundSpeed = 0;
|
|
556
|
+
/**
|
|
557
|
+
* Ground pace he is actually carrying, in metres per second. Ramped toward whatever the
|
|
558
|
+
* current behaviour asked for rather than adopted outright — see `WALK_ACCEL`.
|
|
559
|
+
*/
|
|
560
|
+
#pace = 0;
|
|
561
|
+
/** True when a movement branch ran `#step` this frame; if none did, he coasts to a stop. */
|
|
562
|
+
#stepped = false;
|
|
563
|
+
/** Direction he is travelling, and which way the body is pointed. Both eased, both stateful. */
|
|
564
|
+
#heading = new EasedYaw(STEER_RATE_MAX, STEER_ACCEL, STEER_SETTLE);
|
|
565
|
+
#facing = new EasedYaw(FACE_RATE_MAX, FACE_ACCEL, FACE_SETTLE);
|
|
566
|
+
/**
|
|
567
|
+
* This soldier's own pace, as a multiplier. Five men walking at exactly 2.400 m/s in step
|
|
568
|
+
* with each other is a tell no amount of animation work can cover. Seeded, so a replay of
|
|
569
|
+
* the same run puts every soldier in the same place.
|
|
570
|
+
*/
|
|
571
|
+
#gait = 1;
|
|
572
|
+
/**
|
|
573
|
+
* Seconds the flinch still owns the pose.
|
|
574
|
+
*
|
|
575
|
+
* Without it `Rifle_Hit` was set by `hurt` and overwritten by `#playLocomotion` on the very
|
|
576
|
+
* next frame, because the locomotion branch re-asserts its clip every frame and `#play` only
|
|
577
|
+
* declines when the clip it is asked for is already current. Measured over two 45 s runs and
|
|
578
|
+
* 678 samples: the flinch was never the current clip on a single one. A soldier who does not
|
|
579
|
+
* flinch when hit is the loudest "this is a state machine" cue in the game, and it was one line.
|
|
580
|
+
*/
|
|
581
|
+
#reactionHold = 0;
|
|
582
|
+
/** Seconds the body has been continuously still, so one blocked frame is not a stop. */
|
|
583
|
+
#stillFor = 0;
|
|
584
|
+
#lastHitMultiplier = 1;
|
|
585
|
+
#modelHeightMeasured: number = scale.humanHeight;
|
|
586
|
+
#hitboxWidth: number = scale.shoulderWidth;
|
|
587
|
+
#hitboxHeight: number = scale.humanHeight;
|
|
588
|
+
#hitboxDepth: number = scale.bodyDepth;
|
|
589
|
+
#crown: Object3D | undefined;
|
|
590
|
+
#head: Object3D | undefined;
|
|
591
|
+
#leftKnee: Object3D | undefined;
|
|
592
|
+
#rightKnee: Object3D | undefined;
|
|
593
|
+
#colliders: readonly BoxCollider[];
|
|
594
|
+
#bodyMeshes: Object3D[] = [];
|
|
595
|
+
#poseBones: Object3D[] = [];
|
|
596
|
+
#bodyProxy: Object3D | undefined;
|
|
597
|
+
#body: CharacterBody3D | undefined;
|
|
598
|
+
#weapon: Object3D | undefined;
|
|
599
|
+
#weaponModel: Object3D | undefined;
|
|
600
|
+
#weaponDetached = false;
|
|
601
|
+
#weaponSettled = false;
|
|
602
|
+
#weaponVelocity = new Vector3();
|
|
603
|
+
#rifleLocalMinZ = -scale.rifleLength * 0.35;
|
|
604
|
+
#rifleLocalMaxZ = scale.rifleLength * 0.65;
|
|
605
|
+
#rightHand: Object3D | undefined;
|
|
606
|
+
#leftHand: Object3D | undefined;
|
|
607
|
+
#weaponNodes: string[] = [];
|
|
608
|
+
#renderedRifleLength: number | null = null;
|
|
609
|
+
/** Seconds left before it may fire after first seeing the player — it is not a turret. */
|
|
610
|
+
#reaction = 0;
|
|
611
|
+
/** Cached A* route. Dynamic combat goals are replanned without changing direction every frame. */
|
|
612
|
+
#path: Vector3[] = [];
|
|
613
|
+
#pathIndex = 0;
|
|
614
|
+
#pathGoal = new Vector3();
|
|
615
|
+
#replanIn = 0;
|
|
616
|
+
/** Countdown for goal-drift replans, kept apart from the obstruction cooldown. */
|
|
617
|
+
#goalReplanIn = 0;
|
|
618
|
+
#searchAtGoal = 0;
|
|
619
|
+
#patrolPause = 0;
|
|
620
|
+
#previousGroundSpeed = 0;
|
|
621
|
+
#decelPeak = 0;
|
|
622
|
+
/** Set on the frames a hard stop is honest — walking into a wall, dying, being placed. */
|
|
623
|
+
#decelExempt = false;
|
|
624
|
+
#spawnGrace = SPAWN_GRACE_SECONDS;
|
|
625
|
+
#route: readonly Vector3[] = ROUTE;
|
|
626
|
+
#navMin = NAV_MIN;
|
|
627
|
+
#navMax = NAV_MAX;
|
|
628
|
+
#decks: readonly DeckFootprint[] = [];
|
|
629
|
+
#frozen = false;
|
|
630
|
+
|
|
631
|
+
constructor(
|
|
632
|
+
ctx: GameCtx,
|
|
633
|
+
model: Object3D,
|
|
634
|
+
clips: readonly AnimationClip[],
|
|
635
|
+
colliders: readonly BoxCollider[],
|
|
636
|
+
options: EnemyOptions = {},
|
|
637
|
+
) {
|
|
638
|
+
this.#colliders = colliders;
|
|
639
|
+
this.#route = options.route ?? ROUTE;
|
|
640
|
+
this.#navMin = options.navBounds?.min ?? NAV_MIN;
|
|
641
|
+
this.#navMax = options.navBounds?.max ?? NAV_MAX;
|
|
642
|
+
this.#decks = options.decks ?? [];
|
|
643
|
+
this.#frozen = options.frozen ?? false;
|
|
644
|
+
this.#pathGoal.copy(this.#route[0] ?? ROUTE_START);
|
|
645
|
+
this.#target.copy(this.#route[0] ?? ROUTE_START);
|
|
646
|
+
model.removeFromParent();
|
|
647
|
+
model.position.set(0, 0, 0);
|
|
648
|
+
model.rotation.set(0, 0, 0);
|
|
649
|
+
model.scale.setScalar(1);
|
|
650
|
+
model.updateWorldMatrix(false, true);
|
|
651
|
+
// Bone names are read off the *source*, because `SkeletalMesh3D` clones it and remaps the
|
|
652
|
+
// requested measurement joint onto its own copy.
|
|
653
|
+
const sourceHead = model.getObjectByName("Head") ?? findBone(model, /head/i);
|
|
654
|
+
this.#clips = new Set(clips.map((clip) => clip.name));
|
|
655
|
+
this.#clipDurations = new Map(clips.map((clip) => [clip.name, clip.duration]));
|
|
656
|
+
this.#clipsByName = new Map(clips.map((clip) => [clip.name, clip]));
|
|
657
|
+
// `Head` is the base of the skull on this skeleton, not the crown, so 1.545 m at that joint
|
|
658
|
+
// is the 1.8 m figure the rest of the game is measured against — the same convention
|
|
659
|
+
// `minimal` opens every project with (see its `src/conventions.ts`).
|
|
660
|
+
this.#character = new SkeletalMesh3D({
|
|
661
|
+
clips,
|
|
662
|
+
requiredClips: [...ENEMY_CLIPS],
|
|
663
|
+
// Stride is measured from the *body's* motion, not the figure's: `group` is what `#step`
|
|
664
|
+
// moves, and it is what `GroundSnap` corrects.
|
|
665
|
+
strideRoot: this.group,
|
|
666
|
+
size: { axis: "height", metres: 1.545, top: sourceHead ?? undefined },
|
|
667
|
+
source: model,
|
|
668
|
+
});
|
|
669
|
+
const figure = this.#character.root;
|
|
670
|
+
this.#crown = figure.getObjectByName("Head") ?? findBone(figure, /head/i);
|
|
671
|
+
this.#head = this.#crown;
|
|
672
|
+
// The mannequin's knees are its calves: `calf_l` / `calf_r`.
|
|
673
|
+
this.#leftKnee = figure.getObjectByName("calf_l") ?? findBone(figure, /left.*knee|left.*leg/i);
|
|
674
|
+
this.#rightKnee =
|
|
675
|
+
figure.getObjectByName("calf_r") ?? findBone(figure, /right.*knee|right.*leg/i);
|
|
676
|
+
this.#hips = figure.getObjectByName("pelvis") ?? findBone(figure, /hips/i);
|
|
677
|
+
figure.traverse((object) => {
|
|
678
|
+
if (/pelvis|hips|thigh|upleg|calf|leg|foot|ball|toe|head/i.test(object.name))
|
|
679
|
+
this.#poseBones.push(object);
|
|
680
|
+
const mesh = object as Mesh;
|
|
681
|
+
if (mesh.isMesh === true) {
|
|
682
|
+
this.#bodyMeshes.push(mesh);
|
|
683
|
+
mesh.castShadow = false;
|
|
684
|
+
mesh.receiveShadow = false;
|
|
685
|
+
// Own material instances before touching them: see `ownMaterial`.
|
|
686
|
+
ownMaterial(mesh);
|
|
687
|
+
tintEnemyMaterial(mesh.material as MeshStandardMaterial | MeshStandardMaterial[]);
|
|
688
|
+
}
|
|
689
|
+
});
|
|
690
|
+
this.group.add(figure);
|
|
691
|
+
this.#ground = new GroundSnap(figure, { enabled: false, meshes: this.#bodyMeshes });
|
|
692
|
+
this.#equip(figure);
|
|
693
|
+
// Before the first draw, not on the first death. `#respawn` also calls this, but `#respawn`
|
|
694
|
+
// only ever runs off the death timer, so a soldier that has never died would otherwise reach
|
|
695
|
+
// his own death still carrying the blend state the asset shipped with — which is both a
|
|
696
|
+
// mid-fight pipeline compile and, once `#setOpacity` stopped flipping the flag, a fade that
|
|
697
|
+
// does not render at all because opacity is ignored on an opaque material.
|
|
698
|
+
this.#fixBlendState();
|
|
699
|
+
this.group.name = "enemy";
|
|
700
|
+
this.group.position.copy(this.#route[0] ?? ROUTE_START);
|
|
701
|
+
const firstWaypoint = this.#route[1];
|
|
702
|
+
if (firstWaypoint !== undefined) {
|
|
703
|
+
this.#target.copy(firstWaypoint);
|
|
704
|
+
this.#routeIndex = 1;
|
|
705
|
+
this.group.rotation.y = Math.atan2(
|
|
706
|
+
this.#target.x - this.group.position.x,
|
|
707
|
+
this.#target.z - this.group.position.z,
|
|
708
|
+
);
|
|
709
|
+
}
|
|
710
|
+
this.group.rotation.y = Math.atan2(
|
|
711
|
+
this.#target.x - this.group.position.x,
|
|
712
|
+
this.#target.z - this.group.position.z,
|
|
713
|
+
);
|
|
714
|
+
this.#heading.set(this.group.rotation.y);
|
|
715
|
+
this.#facing.set(this.group.rotation.y);
|
|
716
|
+
// Seeded per soldier: 0.94–1.06 is enough to break the squad out of lockstep and still
|
|
717
|
+
// leaves a walking soldier well clear of the walk/jog switch.
|
|
718
|
+
this.#gait = ctx.random.range(0.94, 1.06);
|
|
719
|
+
|
|
720
|
+
// Skinned meshes are the slow path for picking, so the rifle traces a plain
|
|
721
|
+
// box proxy that follows the body. Invisible, but still raycastable.
|
|
722
|
+
this.#modelHeightMeasured = this.modelHeight || scale.humanHeight;
|
|
723
|
+
// Width and depth are declared sizes, not measurements. A whole-body AABB is not a
|
|
724
|
+
// hitbox: in the bind pose this rig measures 1.11 m across because the arms are out in a
|
|
725
|
+
// T, and over a walk cycle it measures 1.13 m deep because the stride reaches fore and
|
|
726
|
+
// aft. Both would make a man a barn door to shoot at. Height stays measured, from the `Head`
|
|
727
|
+
// joint, which is the base of the skull: the skull itself stands one head above it, so the box
|
|
728
|
+
// adds that. Without it every round aimed above 1.545 m passed over a 1.80 m soldier.
|
|
729
|
+
this.#hitboxWidth = scale.shoulderWidth;
|
|
730
|
+
this.#hitboxHeight = this.#modelHeightMeasured + scale.headRadius * 2;
|
|
731
|
+
this.#hitboxDepth = scale.bodyDepth;
|
|
732
|
+
this.hitbox = new Mesh(
|
|
733
|
+
new BoxGeometry(this.#hitboxWidth, this.#hitboxHeight, this.#hitboxDepth),
|
|
734
|
+
new MeshBasicMaterial({ visible: false }),
|
|
735
|
+
);
|
|
736
|
+
this.hitbox.position.y = this.#hitboxHeight / 2;
|
|
737
|
+
this.hitbox.userData.enemy = this;
|
|
738
|
+
this.group.add(this.hitbox);
|
|
739
|
+
|
|
740
|
+
const bodyProxy = new Group();
|
|
741
|
+
bodyProxy.name = "enemy-body";
|
|
742
|
+
ctx.add(bodyProxy);
|
|
743
|
+
this.#bodyProxy = bodyProxy;
|
|
744
|
+
this.#body = new CharacterBody3D({
|
|
745
|
+
physics: ctx.physics,
|
|
746
|
+
object: bodyProxy,
|
|
747
|
+
entity: "enemy-body",
|
|
748
|
+
shape: CollisionShape3D.box(this.#hitboxWidth, this.#hitboxHeight, this.#hitboxDepth),
|
|
749
|
+
gravity: 0,
|
|
750
|
+
collisionLayer: 2,
|
|
751
|
+
collisionMask: 1,
|
|
752
|
+
});
|
|
753
|
+
this.#syncCollisionBody();
|
|
754
|
+
|
|
755
|
+
this.#play("Rifle_Walk");
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
get alive(): boolean {
|
|
759
|
+
return this.phase !== "dead";
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
/**
|
|
763
|
+
* What the playtest bridge reads off a registered entity, and what `assert.animation[]` bounds.
|
|
764
|
+
*
|
|
765
|
+
* The harness already has a first-class assertion for the thing this file used to measure by
|
|
766
|
+
* hand — `maxFootSlide` over `|feet - ground| / ground`, and `strideSynced` to prove the
|
|
767
|
+
* convention is actually applied rather than bypassed. Publishing the engine's own stride
|
|
768
|
+
* report is what makes those scenarios possible; without it a scenario has to re-derive the
|
|
769
|
+
* same ratio from game-side numbers and can only agree with itself.
|
|
770
|
+
*/
|
|
771
|
+
get animation(): {
|
|
772
|
+
current: string;
|
|
773
|
+
advancedFrames: number;
|
|
774
|
+
finished: boolean;
|
|
775
|
+
stride: IStrideReport;
|
|
776
|
+
} {
|
|
777
|
+
return {
|
|
778
|
+
advancedFrames: this.#character.advancedFrames,
|
|
779
|
+
current: this.#character.current ?? "",
|
|
780
|
+
finished: this.#character.finished,
|
|
781
|
+
stride: this.#character.stride,
|
|
782
|
+
};
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
/** Chest height, used as the eye and muzzle origin. */
|
|
786
|
+
get chest(): Vector3 {
|
|
787
|
+
return new Vector3(
|
|
788
|
+
this.group.position.x,
|
|
789
|
+
this.group.position.y + this.bodyHeight * 0.8,
|
|
790
|
+
this.group.position.z,
|
|
791
|
+
);
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
/**
|
|
795
|
+
* Rendered height, boots to head-top, measured off the skeleton.
|
|
796
|
+
*
|
|
797
|
+
* A `Box3` over a skinned mesh reports the *bind pose* transformed by the world matrix,
|
|
798
|
+
* not the posed body — which is precisely how a 2.68 m soldier stood beside a 1.66 m
|
|
799
|
+
* player without any gate noticing. The head-top bone is posed, so it tells the truth, and
|
|
800
|
+
* `getWorldPosition` refreshes the bone chain itself rather than trusting a matrix written
|
|
801
|
+
* by the last frame.
|
|
802
|
+
*/
|
|
803
|
+
get modelHeight(): number {
|
|
804
|
+
const crown =
|
|
805
|
+
this.#crown ??
|
|
806
|
+
findBone(this.group, /headtop|head_end|head.*end/i) ??
|
|
807
|
+
findBone(this.group, /head/i);
|
|
808
|
+
this.#crown = crown;
|
|
809
|
+
if (crown === undefined) return 0;
|
|
810
|
+
return crown.getWorldPosition(new Vector3()).y - this.group.position.y;
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
get bodyBase(): number {
|
|
814
|
+
return this.group.position.y;
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
get bodyHeight(): number {
|
|
818
|
+
return this.modelHeight || this.#modelHeightMeasured;
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
get headZoneMinY(): number {
|
|
822
|
+
const head = this.#head?.getWorldPosition(new Vector3());
|
|
823
|
+
return (head?.y ?? this.bodyBase + this.bodyHeight) - scale.headRadius;
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
get legZoneMaxY(): number {
|
|
827
|
+
const left = this.#leftKnee?.getWorldPosition(new Vector3()).y;
|
|
828
|
+
const right = this.#rightKnee?.getWorldPosition(new Vector3()).y;
|
|
829
|
+
const knees = [left, right].filter((value): value is number => value !== undefined);
|
|
830
|
+
return knees.length > 0
|
|
831
|
+
? Math.max(...knees)
|
|
832
|
+
: this.bodyBase + this.bodyHeight * scale.legZoneFraction;
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
/**
|
|
836
|
+
* Put the rifle in the enemy's right hand.
|
|
837
|
+
*
|
|
838
|
+
* The mannequin's clips pose a bare figure, so the arms are not already holding anything and
|
|
839
|
+
* the rifle has to be welded to `hand_r` by hand. The grip block sits at the rifle's own
|
|
840
|
+
* origin (see `buildRifle`), so attaching the rifle itself to the bone puts the fist around
|
|
841
|
+
* the grip with no follow-up alignment — which is the whole reason the rifle is five boxes
|
|
842
|
+
* rather than a model with a `Grip_Bone` in it.
|
|
843
|
+
*/
|
|
844
|
+
#equip(model: Object3D): void {
|
|
845
|
+
const hand = model.getObjectByName("hand_r") ?? findBone(model, /right.*hand|hand.*r$|hand_r/i);
|
|
846
|
+
this.#rightHand = hand;
|
|
847
|
+
this.#leftHand =
|
|
848
|
+
model.getObjectByName("hand_l") ?? findBone(model, /left.*hand|hand.*l$|hand_l/i);
|
|
849
|
+
|
|
850
|
+
const rifle = buildRifle();
|
|
851
|
+
this.#weaponModel = rifle;
|
|
852
|
+
this.#weaponNodes.push("receiver", "barrel", "magazine", "stock", "grip");
|
|
853
|
+
const bounds = new Box3().setFromObject(rifle);
|
|
854
|
+
this.#rifleLocalMinZ = bounds.min.z;
|
|
855
|
+
this.#rifleLocalMaxZ = bounds.max.z;
|
|
856
|
+
|
|
857
|
+
if (hand === undefined) {
|
|
858
|
+
// No hand bone: hang it beside the hip rather than dropping it. A soldier without a
|
|
859
|
+
// visible arm is already a broken rig; a rifle at his side still reads.
|
|
860
|
+
rifle.position.set(0.16, 1.0, 0.06);
|
|
861
|
+
this.group.add(rifle);
|
|
862
|
+
this.#weapon = rifle;
|
|
863
|
+
normaliseToMetres(rifle, { axis: "longest", metres: scale.rifleLength });
|
|
864
|
+
this.#renderedRifleLength = this.#measureRenderedWeapon(rifle);
|
|
865
|
+
return;
|
|
866
|
+
}
|
|
867
|
+
// The engine's attachment keeps the rifle's authored world scale under the hand bone;
|
|
868
|
+
// the measured re-normalisation below still has the final word on length.
|
|
869
|
+
attachToBone(model, hand.name, rifle);
|
|
870
|
+
this.#weapon = rifle;
|
|
871
|
+
normaliseToMetres(rifle, { axis: "longest", metres: scale.rifleLength });
|
|
872
|
+
this.#holdRifle("Rifle_Idle");
|
|
873
|
+
this.#renderedRifleLength = this.#measureRenderedWeapon(rifle);
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
#detachWeapon(ctx: GameCtx): void {
|
|
877
|
+
const holder = this.#weapon;
|
|
878
|
+
if (holder === undefined) return;
|
|
879
|
+
ctx.scene.attach(holder);
|
|
880
|
+
this.#weaponDetached = true;
|
|
881
|
+
this.#weaponSettled = false;
|
|
882
|
+
this.#weaponVelocity
|
|
883
|
+
.set(0.45, 1.4, -0.25)
|
|
884
|
+
.applyAxisAngle(new Vector3(0, 1, 0), this.group.rotation.y);
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
#updateDetachedWeapon(dt: number, deckY: number): void {
|
|
888
|
+
const holder = this.#weapon;
|
|
889
|
+
if (holder === undefined || !this.#weaponDetached || this.#weaponSettled) return;
|
|
890
|
+
this.#weaponVelocity.y -= 9.81 * dt;
|
|
891
|
+
holder.position.addScaledVector(this.#weaponVelocity, dt);
|
|
892
|
+
holder.rotation.x += dt * 2.7;
|
|
893
|
+
holder.rotation.z += dt * 1.9;
|
|
894
|
+
holder.updateWorldMatrix(false, true);
|
|
895
|
+
const minimum = new Box3().setFromObject(holder).min.y;
|
|
896
|
+
if (minimum < deckY) {
|
|
897
|
+
holder.position.y += deckY - minimum;
|
|
898
|
+
this.#weaponVelocity.set(0, 0, 0);
|
|
899
|
+
this.#weaponSettled = true;
|
|
900
|
+
}
|
|
901
|
+
holder.updateWorldMatrix(false, true);
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
#reattachWeapon(): void {
|
|
905
|
+
const holder = this.#weapon;
|
|
906
|
+
if (holder === undefined || this.#rightHand === undefined) return;
|
|
907
|
+
this.#rightHand.add(holder);
|
|
908
|
+
this.#weaponDetached = false;
|
|
909
|
+
this.#weaponSettled = false;
|
|
910
|
+
this.#weaponVelocity.set(0, 0, 0);
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
#measureRenderedWeapon(weapon: Object3D): number {
|
|
914
|
+
weapon.updateWorldMatrix(true, true);
|
|
915
|
+
const size = new Box3().setFromObject(weapon).getSize(new Vector3());
|
|
916
|
+
return Math.max(size.x, size.y, size.z);
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
#syncCollisionBody(): void {
|
|
920
|
+
const proxy = this.#bodyProxy;
|
|
921
|
+
const body = this.#body;
|
|
922
|
+
if (proxy === undefined || body === undefined) return;
|
|
923
|
+
proxy.position.set(
|
|
924
|
+
this.group.position.x,
|
|
925
|
+
this.group.position.y + this.#hitboxHeight / 2,
|
|
926
|
+
this.group.position.z,
|
|
927
|
+
);
|
|
928
|
+
body.teleport(proxy.position);
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
/**
|
|
932
|
+
* Fade a soldier in or out without changing his pipeline.
|
|
933
|
+
*
|
|
934
|
+
* `transparent` and `depthWrite` are blend and depth state, not shader uniforms, so flipping
|
|
935
|
+
* either one makes WebGPU compile a *new* render pipeline for every material it touches — and a
|
|
936
|
+
* soldier is a skinned mesh with several. This used to set `transparent = alpha < 0.999` and
|
|
937
|
+
* `depthWrite = alpha > 0.5`, which meant the first death in a round compiled a fresh pipeline
|
|
938
|
+
* per material on the frame the corpse started fading. Measured as a single ~180 ms frame,
|
|
939
|
+
* mid-round, entirely outside game logic: `outsideGame` peaked at 177-186 ms with a firefight in
|
|
940
|
+
* the scenario and 28.6 ms without one, on identical movement.
|
|
941
|
+
*
|
|
942
|
+
* So the state is fixed at construction and never moves. Only `opacity` changes, which is a
|
|
943
|
+
* uniform the existing pipeline already reads. `needsUpdate` is deliberately not set: it forces
|
|
944
|
+
* the material to be re-evaluated, and there is nothing left to re-evaluate.
|
|
945
|
+
*
|
|
946
|
+
* Keeping `depthWrite` on for a fading corpse is the deliberate half of this. It costs a little
|
|
947
|
+
* correctness on a half-faded body seen through another one, and it buys never compiling
|
|
948
|
+
* mid-fight — and a corpse fading on the ground is not what anyone is looking at.
|
|
949
|
+
*/
|
|
950
|
+
#setOpacity(alpha: number): void {
|
|
951
|
+
const objects = [
|
|
952
|
+
...this.#bodyMeshes,
|
|
953
|
+
...(this.#weaponModel === undefined ? [] : [this.#weaponModel]),
|
|
954
|
+
];
|
|
955
|
+
for (const object of objects) {
|
|
956
|
+
object.traverse((child) => {
|
|
957
|
+
const mesh = child as Mesh;
|
|
958
|
+
if (mesh.isMesh !== true) return;
|
|
959
|
+
const materials = Array.isArray(mesh.material) ? mesh.material : [mesh.material];
|
|
960
|
+
for (const material of materials) {
|
|
961
|
+
material.opacity = alpha;
|
|
962
|
+
}
|
|
963
|
+
});
|
|
964
|
+
}
|
|
965
|
+
this.#fade = alpha;
|
|
966
|
+
if (alpha < this.#opacityFloor) this.#opacityFloor = alpha;
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
/**
|
|
970
|
+
* Put every material into its final blend state once, before the soldier is ever drawn.
|
|
971
|
+
*
|
|
972
|
+
* Called at construction so the transparent pipeline is compiled during loading, where a long
|
|
973
|
+
* frame is part of the loading screen, rather than on the frame someone dies.
|
|
974
|
+
*/
|
|
975
|
+
#fixBlendState(): void {
|
|
976
|
+
const objects = [
|
|
977
|
+
...this.#bodyMeshes,
|
|
978
|
+
...(this.#weaponModel === undefined ? [] : [this.#weaponModel]),
|
|
979
|
+
];
|
|
980
|
+
for (const object of objects) {
|
|
981
|
+
object.traverse((child) => {
|
|
982
|
+
const mesh = child as Mesh;
|
|
983
|
+
if (mesh.isMesh !== true) return;
|
|
984
|
+
const materials = Array.isArray(mesh.material) ? mesh.material : [mesh.material];
|
|
985
|
+
for (const material of materials) {
|
|
986
|
+
material.transparent = true;
|
|
987
|
+
material.depthWrite = true;
|
|
988
|
+
material.needsUpdate = true;
|
|
989
|
+
}
|
|
990
|
+
});
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
/** Muzzle point in world space: the weapon tip when equipped, the chest otherwise. */
|
|
995
|
+
muzzle(): Vector3 {
|
|
996
|
+
const weapon = this.#weapon;
|
|
997
|
+
if (weapon === undefined) return this.chest;
|
|
998
|
+
const tip = new Vector3(0, 0, this.#rifleLocalMaxZ);
|
|
999
|
+
return weapon.localToWorld(tip);
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
/**
|
|
1003
|
+
* `override` is what makes the flinch survive.
|
|
1004
|
+
*
|
|
1005
|
+
* Every branch of the state machine re-asserts its own clip every frame, so a one-shot pose set
|
|
1006
|
+
* from outside the machine — the flinch, which is the only one — was replaced on the next frame
|
|
1007
|
+
* by whichever branch ran. Guarding the two call sites was not enough: the spawn-grace branch
|
|
1008
|
+
* plays `Rifle_Idle` directly rather than through `#playLocomotion`, and the burst plays
|
|
1009
|
+
* `Rifle_Shoot` on every round. One refusal here covers all of them, and death passes
|
|
1010
|
+
* `override` because a corpse outranks a flinch.
|
|
1011
|
+
*/
|
|
1012
|
+
#play(name: string, fade = 0.18, mode: "loop" | "once" = "loop", override = false): void {
|
|
1013
|
+
if (!this.#clips.has(name)) return;
|
|
1014
|
+
if (!override && this.#reactionHold > 0) return;
|
|
1015
|
+
if (this.#character.current === name) return;
|
|
1016
|
+
this.#character.play(name, { fade, mode });
|
|
1017
|
+
this.#holdRifle(name);
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
#holdRifle(clip: string): void {
|
|
1021
|
+
const hold = RIFLE_HOLD[clip];
|
|
1022
|
+
if (hold !== undefined) {
|
|
1023
|
+
this.#weaponModel?.rotation.set(
|
|
1024
|
+
...(hold.map(MathUtils.degToRad) as [number, number, number]),
|
|
1025
|
+
);
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
/**
|
|
1030
|
+
* Pick the locomotion clip from what the body is actually doing.
|
|
1031
|
+
*
|
|
1032
|
+
* Two things this fixes beyond using more of the rig: the walk cycle no longer plays while
|
|
1033
|
+
* the soldier is standing still against a blocked path, and standing up out of a crouch runs
|
|
1034
|
+
* its authored transition instead of popping straight to idle.
|
|
1035
|
+
*
|
|
1036
|
+
* `armed` is the one state the locomotion set cannot express. A planted soldier on `Rifle_Idle`
|
|
1037
|
+
* has both arms at his sides while his rifle is welded to one of them, which reads as a man
|
|
1038
|
+
* carrying a rifle in his teeth; `Rifle_Idle` is a retargeted rifle-ready pose with both hands on
|
|
1039
|
+
* the gun.
|
|
1040
|
+
*/
|
|
1041
|
+
#playLocomotion(
|
|
1042
|
+
moving: boolean,
|
|
1043
|
+
crouched: boolean,
|
|
1044
|
+
dt: number,
|
|
1045
|
+
poseLocked = false,
|
|
1046
|
+
armed = false,
|
|
1047
|
+
): void {
|
|
1048
|
+
// Ground speed is measured from where the body actually got to this frame, not from the
|
|
1049
|
+
// speed constant it was asked for: a blocked step, a corner, or a slow turn all cut it.
|
|
1050
|
+
this.#groundSpeed =
|
|
1051
|
+
dt > 0
|
|
1052
|
+
? Math.hypot(
|
|
1053
|
+
this.group.position.x - this.#frameStart.x,
|
|
1054
|
+
this.group.position.z - this.#frameStart.z,
|
|
1055
|
+
) / dt
|
|
1056
|
+
: 0;
|
|
1057
|
+
// How hard the body braked this frame, in metres per second squared.
|
|
1058
|
+
//
|
|
1059
|
+
// A soldier sheds pace at WALK_DECEL and no faster. Anything above it means the body was
|
|
1060
|
+
// teleported to rest rather than walked to it, which is what a patrol pause used to do:
|
|
1061
|
+
// `#brake` decays `#pace`, `#pace` is read only by `#step`, and the pause called neither —
|
|
1062
|
+
// so ground speed went 2.31 to 0 between two frames while the legs were mid-stride.
|
|
1063
|
+
// Exempt frames are the stops that are honest: a wall, a death, a scenario placement.
|
|
1064
|
+
if (dt > 0 && !this.#decelExempt) {
|
|
1065
|
+
const braking = (this.#previousGroundSpeed - this.#groundSpeed) / dt;
|
|
1066
|
+
if (braking > this.#decelPeak) this.#decelPeak = braking;
|
|
1067
|
+
}
|
|
1068
|
+
this.#previousGroundSpeed = this.#groundSpeed;
|
|
1069
|
+
this.#decelExempt = false;
|
|
1070
|
+
// One frame against a wall, or one frame of a replan, is not a stop. Without this the
|
|
1071
|
+
// walk clip strobes against idle whenever the path is briefly blocked.
|
|
1072
|
+
this.#stillFor = moving ? 0 : this.#stillFor + dt;
|
|
1073
|
+
// The burst clip and the flinch own the pose while they last, but the body keeps reporting the
|
|
1074
|
+
// ground speed it is actually making. Returning before the measurement above froze
|
|
1075
|
+
// `#groundSpeed` at whatever it was when the burst started, which drove `#updateFootsteps`
|
|
1076
|
+
// into playing footfalls for a soldier standing still.
|
|
1077
|
+
if (poseLocked || this.#reactionHold > 0) return;
|
|
1078
|
+
const travelling = this.#stillFor < STILL_BEFORE_IDLE_SECONDS;
|
|
1079
|
+
|
|
1080
|
+
let wanted: string;
|
|
1081
|
+
if (travelling) {
|
|
1082
|
+
// Crouch state changes on the frame he is suppressed or wounded, but the body takes
|
|
1083
|
+
// `WALK_DECEL` to shed the pace it was carrying. Dropping onto the crouch clip during
|
|
1084
|
+
// that window puts a 0.807 m/s creep under a body still making walking pace, which reads
|
|
1085
|
+
// as foot slide — so the clip waits for the legs.
|
|
1086
|
+
const creeping = this.#groundSpeed <= CROUCH_PACE * 1.02;
|
|
1087
|
+
wanted =
|
|
1088
|
+
crouched && creeping && this.#clips.has("Rifle_Crouch_Walk")
|
|
1089
|
+
? "Rifle_Crouch_Walk"
|
|
1090
|
+
: "Rifle_Walk";
|
|
1091
|
+
} else if (this.#crouchMoving && this.#clips.has("Rifle_Crouch_To_Idle")) {
|
|
1092
|
+
// Standing up runs its authored transition, and owns the pose until it finishes.
|
|
1093
|
+
this.#crouchMoving = false;
|
|
1094
|
+
this.#standUp = this.#clipDurations.get("Rifle_Crouch_To_Idle") ?? 0.5;
|
|
1095
|
+
this.#locomotion = "Rifle_Crouch_To_Idle";
|
|
1096
|
+
this.#locomotionHold = this.#standUp;
|
|
1097
|
+
this.#play("Rifle_Crouch_To_Idle", LOCOMOTION_FADE, "once");
|
|
1098
|
+
return;
|
|
1099
|
+
} else {
|
|
1100
|
+
if (this.#standUp > 0) return;
|
|
1101
|
+
wanted = "Rifle_Idle";
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
// Commit to a locomotion clip for a beat. Crouch state can flicker as suppression decays
|
|
1105
|
+
// or a burst starts, and a rig that re-blends every frame reads as a twitch, not a soldier.
|
|
1106
|
+
if (wanted !== this.#locomotion && this.#locomotionHold > 0) return;
|
|
1107
|
+
if (wanted !== this.#locomotion) this.#locomotionHold = LOCOMOTION_HOLD_SECONDS;
|
|
1108
|
+
this.#locomotion = wanted;
|
|
1109
|
+
this.#crouchMoving = wanted === "Rifle_Crouch_Walk";
|
|
1110
|
+
this.#standUp = travelling ? 0 : this.#standUp;
|
|
1111
|
+
this.#play(wanted, LOCOMOTION_FADE);
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
/**
|
|
1115
|
+
* How far the corpse travelled along the killing round's direction, in metres, measured on
|
|
1116
|
+
* the hips and flattened to the deck. Positive means he fell away from the shooter, which is
|
|
1117
|
+
* true of both death clips when they are mapped the right way round.
|
|
1118
|
+
*/
|
|
1119
|
+
#deathFallDot(): number | null {
|
|
1120
|
+
const start = this.#deathHipStart;
|
|
1121
|
+
const round = this.#lastHitDirection;
|
|
1122
|
+
if (start === null || round === null || this.#hips === undefined) return null;
|
|
1123
|
+
const now = this.#hips.getWorldPosition(new Vector3());
|
|
1124
|
+
const travel = new Vector3(now.x - start.x, 0, now.z - start.z);
|
|
1125
|
+
const heading = new Vector3(round.x, 0, round.z);
|
|
1126
|
+
if (heading.lengthSq() < 1e-6) return null;
|
|
1127
|
+
return travel.dot(heading.normalize());
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
#countClipFrame(): void {
|
|
1131
|
+
const current = this.#character.current;
|
|
1132
|
+
if (current === undefined) return;
|
|
1133
|
+
const frames = (this.#clipFrames.get(current) ?? 0) + 1;
|
|
1134
|
+
this.#clipFrames.set(current, frames);
|
|
1135
|
+
// Not the first frames: a clip that has only just started is still a crossfade from the last.
|
|
1136
|
+
if (frames < 12 || frames % 4 !== 0 || this.#weaponDetached) return;
|
|
1137
|
+
const worst = Math.max(this.#contact("hand_r") ?? 0, this.#contact("hand_l") ?? 0);
|
|
1138
|
+
this.#contactPeak.set(current, Math.max(this.#contactPeak.get(current) ?? 0, worst));
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
/**
|
|
1142
|
+
* Fire `hooks.onFootstep` each time the locomotion action crosses a half-cycle
|
|
1143
|
+
* boundary — one planted foot per half-cycle, so a re-timed walk keeps its steps
|
|
1144
|
+
* in step with the animation rather than with a timer. A clip switch resets the
|
|
1145
|
+
* phase so the first frame of a new clip cannot read as a plant.
|
|
1146
|
+
*/
|
|
1147
|
+
#updateFootsteps(hooks: EnemyHooks): void {
|
|
1148
|
+
if (hooks.onFootstep === undefined || this.#groundSpeed < MOVING_SPEED_FLOOR) return;
|
|
1149
|
+
const clip = this.#clipsByName.get(this.#locomotion);
|
|
1150
|
+
if (clip === undefined || clip.duration <= 0) return;
|
|
1151
|
+
const action = this.#character.mixer.existingAction(clip);
|
|
1152
|
+
if (action === null || action === undefined) return;
|
|
1153
|
+
const half = Math.floor(((action.time / clip.duration) % 1) * 2);
|
|
1154
|
+
if (this.#locomotion !== this.#lastStepClip) {
|
|
1155
|
+
this.#lastStepClip = this.#locomotion;
|
|
1156
|
+
this.#lastStepHalf = half;
|
|
1157
|
+
return;
|
|
1158
|
+
}
|
|
1159
|
+
if (half === this.#lastStepHalf) return;
|
|
1160
|
+
this.#lastStepHalf = half;
|
|
1161
|
+
hooks.onFootstep(this.group.position);
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
/**
|
|
1165
|
+
* The one clip a killed soldier plays.
|
|
1166
|
+
*
|
|
1167
|
+
* The old rig carried three retargeted Mixamo deaths and chose between them by the direction
|
|
1168
|
+
* the killing round travelled. The mannequin ships one (`Death01`), so the choice is gone and
|
|
1169
|
+
* only the fall direction is still measured: the corpse must travel away from the shooter
|
|
1170
|
+
* whichever way it was hit, and that is what `deathFallDot` scores.
|
|
1171
|
+
*/
|
|
1172
|
+
#deathClipFor(): string {
|
|
1173
|
+
return "Death01";
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
#occupied(x: number, z: number, padding: number): boolean {
|
|
1177
|
+
for (const box of this.#colliders) {
|
|
1178
|
+
// The raised deck is overhead, not a wall. Keep its supports and the lower range solids
|
|
1179
|
+
// in the navigation map, but let a soldier route through the open space underneath it.
|
|
1180
|
+
if (box.min[1] > scale.humanHeight + scale.ankleHeight * 6) continue;
|
|
1181
|
+
if (
|
|
1182
|
+
x > box.min[0] - padding &&
|
|
1183
|
+
x < box.max[0] + padding &&
|
|
1184
|
+
z > box.min[2] - padding &&
|
|
1185
|
+
z < box.max[2] + padding &&
|
|
1186
|
+
box.max[1] > 0.5
|
|
1187
|
+
) {
|
|
1188
|
+
return true;
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
return x < this.#navMin || x > this.#navMax || z < this.#navMin || z > this.#navMax;
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
/** True when the soldier stands in any raised deck's footprint — routing beneath it. */
|
|
1195
|
+
#underDeck(): boolean {
|
|
1196
|
+
const x = this.group.position.x;
|
|
1197
|
+
const z = this.group.position.z;
|
|
1198
|
+
for (const deck of this.#decks) {
|
|
1199
|
+
if (x > deck.minX && x < deck.maxX && z > deck.minZ && z < deck.maxZ) {
|
|
1200
|
+
return true;
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
return false;
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
/**
|
|
1207
|
+
* The navigation grid's blocked cells, built once and shared by the whole squad.
|
|
1208
|
+
*
|
|
1209
|
+
* `#occupied` is a linear scan of every town collider, and A* asked it for up to three cells per
|
|
1210
|
+
* neighbour across a 46x46 grid — per search, per soldier. It was the single hottest function in
|
|
1211
|
+
* the game (197 ms of self time in a 3.7-minute trace) and it was recomputing one static answer
|
|
1212
|
+
* over and over: the town's colliders never move, so a cell that is blocked on the first frame is
|
|
1213
|
+
* blocked on the last. The bitmap is keyed on the collider array itself, so the five soldiers
|
|
1214
|
+
* that share `town.colliders` share one grid, and a scene that rebuilds its town gets a new one.
|
|
1215
|
+
*
|
|
1216
|
+
* Only cell-centre queries use this. `#segmentClear` interpolates arbitrary points between cells
|
|
1217
|
+
* and still runs the exact test, so no route changes shape.
|
|
1218
|
+
*/
|
|
1219
|
+
#navGrid(width: number): Uint8Array {
|
|
1220
|
+
let byBounds = NAV_GRIDS.get(this.#colliders);
|
|
1221
|
+
if (byBounds === undefined) {
|
|
1222
|
+
byBounds = new Map();
|
|
1223
|
+
NAV_GRIDS.set(this.#colliders, byBounds);
|
|
1224
|
+
}
|
|
1225
|
+
const boundsKey = `${this.#navMin}|${this.#navMax}|${width}`;
|
|
1226
|
+
const cached = byBounds.get(boundsKey);
|
|
1227
|
+
if (cached !== undefined) return cached;
|
|
1228
|
+
const grid = new Uint8Array(width * width);
|
|
1229
|
+
for (let z = 0; z < width; z += 1) {
|
|
1230
|
+
for (let x = 0; x < width; x += 1) {
|
|
1231
|
+
const worldX = this.#navMin + x * NAV_CELL;
|
|
1232
|
+
const worldZ = this.#navMin + z * NAV_CELL;
|
|
1233
|
+
grid[z * width + x] = this.#occupied(worldX, worldZ, AGENT_RADIUS + 0.16) ? 1 : 0;
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
byBounds.set(boundsKey, grid);
|
|
1237
|
+
return grid;
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
#blocked(x: number, z: number): boolean {
|
|
1241
|
+
return this.#occupied(x, z, AGENT_RADIUS);
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
#navBlocked(x: number, z: number): boolean {
|
|
1245
|
+
// Grid nodes need slack: a mathematically tangent route clips a corner after interpolation.
|
|
1246
|
+
return this.#occupied(x, z, AGENT_RADIUS + 0.16);
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
/** True when the whole body-width corridor is clear, not merely its end point. */
|
|
1250
|
+
#segmentClear(from: Vector3, to: Vector3): boolean {
|
|
1251
|
+
const distance = from.distanceTo(to);
|
|
1252
|
+
const samples = Math.max(1, Math.ceil(distance / (NAV_CELL * 0.45)));
|
|
1253
|
+
for (let index = 1; index <= samples; index += 1) {
|
|
1254
|
+
const t = index / samples;
|
|
1255
|
+
if (this.#navBlocked(MathUtils.lerp(from.x, to.x, t), MathUtils.lerp(from.z, to.z, t))) {
|
|
1256
|
+
return false;
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
return true;
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
/** Build a deterministic 8-way A* route and then remove grid points visible from each other. */
|
|
1263
|
+
#findPath(goalX: number, goalZ: number): Vector3[] {
|
|
1264
|
+
const navMin = this.#navMin;
|
|
1265
|
+
const navMax = this.#navMax;
|
|
1266
|
+
const width = Math.floor((navMax - navMin) / NAV_CELL) + 1;
|
|
1267
|
+
const toCell = (value: number): number =>
|
|
1268
|
+
MathUtils.clamp(Math.round((value - navMin) / NAV_CELL), 0, width - 1);
|
|
1269
|
+
const toWorld = (cell: number): number => navMin + cell * NAV_CELL;
|
|
1270
|
+
const key = (x: number, z: number): number => z * width + x;
|
|
1271
|
+
const grid = this.#navGrid(width);
|
|
1272
|
+
/** Cell-centre blocked test: one array read instead of a scan of every collider. */
|
|
1273
|
+
const cellBlocked = (x: number, z: number): boolean => grid[z * width + x] === 1;
|
|
1274
|
+
const sx = toCell(this.group.position.x);
|
|
1275
|
+
const sz = toCell(this.group.position.z);
|
|
1276
|
+
let gx = toCell(goalX);
|
|
1277
|
+
let gz = toCell(goalZ);
|
|
1278
|
+
const requestedGoalBlocked = this.#navBlocked(goalX, goalZ);
|
|
1279
|
+
|
|
1280
|
+
// A requested point may sit within the body's clearance margin. Pick the nearest usable cell.
|
|
1281
|
+
if (cellBlocked(gx, gz)) {
|
|
1282
|
+
let replacement: [number, number] | undefined;
|
|
1283
|
+
for (let radius = 1; radius < width && replacement === undefined; radius += 1) {
|
|
1284
|
+
for (let dz = -radius; dz <= radius && replacement === undefined; dz += 1) {
|
|
1285
|
+
for (let dx = -radius; dx <= radius; dx += 1) {
|
|
1286
|
+
if (Math.max(Math.abs(dx), Math.abs(dz)) !== radius) continue;
|
|
1287
|
+
const x = gx + dx;
|
|
1288
|
+
const z = gz + dz;
|
|
1289
|
+
if (x >= 0 && z >= 0 && x < width && z < width && !cellBlocked(x, z)) {
|
|
1290
|
+
replacement = [x, z];
|
|
1291
|
+
break;
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
if (replacement === undefined) return [];
|
|
1297
|
+
[gx, gz] = replacement;
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
const start = key(sx, sz);
|
|
1301
|
+
const goal = key(gx, gz);
|
|
1302
|
+
const open = new Set<number>([start]);
|
|
1303
|
+
const cameFrom = new Map<number, number>();
|
|
1304
|
+
const gScore = new Map<number, number>([[start, 0]]);
|
|
1305
|
+
const fScore = new Map<number, number>([[start, Math.hypot(gx - sx, gz - sz)]]);
|
|
1306
|
+
const neighbours: readonly (readonly [number, number, number])[] = [
|
|
1307
|
+
[-1, 0, 1],
|
|
1308
|
+
[1, 0, 1],
|
|
1309
|
+
[0, -1, 1],
|
|
1310
|
+
[0, 1, 1],
|
|
1311
|
+
[-1, -1, Math.SQRT2],
|
|
1312
|
+
[1, -1, Math.SQRT2],
|
|
1313
|
+
[-1, 1, Math.SQRT2],
|
|
1314
|
+
[1, 1, Math.SQRT2],
|
|
1315
|
+
];
|
|
1316
|
+
|
|
1317
|
+
while (open.size > 0) {
|
|
1318
|
+
let current = -1;
|
|
1319
|
+
let best = Number.POSITIVE_INFINITY;
|
|
1320
|
+
for (const candidate of open) {
|
|
1321
|
+
const score = fScore.get(candidate) ?? Number.POSITIVE_INFINITY;
|
|
1322
|
+
if (score < best) {
|
|
1323
|
+
best = score;
|
|
1324
|
+
current = candidate;
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
if (current === goal) break;
|
|
1328
|
+
open.delete(current);
|
|
1329
|
+
const cx = current % width;
|
|
1330
|
+
const cz = Math.floor(current / width);
|
|
1331
|
+
for (const [dx, dz, cost] of neighbours) {
|
|
1332
|
+
const nx = cx + dx;
|
|
1333
|
+
const nz = cz + dz;
|
|
1334
|
+
if (nx < 0 || nz < 0 || nx >= width || nz >= width) continue;
|
|
1335
|
+
if (cellBlocked(nx, nz)) continue;
|
|
1336
|
+
// Do not squeeze diagonally between two touching solids.
|
|
1337
|
+
if (dx !== 0 && dz !== 0 && (cellBlocked(cx + dx, cz) || cellBlocked(cx, cz + dz)))
|
|
1338
|
+
continue;
|
|
1339
|
+
const next = key(nx, nz);
|
|
1340
|
+
const tentative = (gScore.get(current) ?? Number.POSITIVE_INFINITY) + cost;
|
|
1341
|
+
if (tentative >= (gScore.get(next) ?? Number.POSITIVE_INFINITY)) continue;
|
|
1342
|
+
cameFrom.set(next, current);
|
|
1343
|
+
gScore.set(next, tentative);
|
|
1344
|
+
fScore.set(next, tentative + Math.hypot(gx - nx, gz - nz));
|
|
1345
|
+
open.add(next);
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
if (start !== goal && !cameFrom.has(goal)) return [];
|
|
1349
|
+
|
|
1350
|
+
const raw: Vector3[] = [];
|
|
1351
|
+
let cursor = goal;
|
|
1352
|
+
while (cursor !== start) {
|
|
1353
|
+
raw.push(new Vector3(toWorld(cursor % width), 0, toWorld(Math.floor(cursor / width))));
|
|
1354
|
+
const previous = cameFrom.get(cursor);
|
|
1355
|
+
if (previous === undefined) return [];
|
|
1356
|
+
cursor = previous;
|
|
1357
|
+
}
|
|
1358
|
+
raw.reverse();
|
|
1359
|
+
// Preserve clearance when the requested destination itself is inside an inflated obstacle.
|
|
1360
|
+
if (!requestedGoalBlocked) raw.push(new Vector3(goalX, 0, goalZ));
|
|
1361
|
+
|
|
1362
|
+
const smooth: Vector3[] = [];
|
|
1363
|
+
let anchor = new Vector3(this.group.position.x, 0, this.group.position.z);
|
|
1364
|
+
for (let index = 0; index < raw.length; ) {
|
|
1365
|
+
let furthest = index;
|
|
1366
|
+
while (
|
|
1367
|
+
furthest + 1 < raw.length &&
|
|
1368
|
+
this.#segmentClear(anchor, raw[furthest + 1] as Vector3)
|
|
1369
|
+
) {
|
|
1370
|
+
furthest += 1;
|
|
1371
|
+
}
|
|
1372
|
+
const waypoint = (raw[furthest] as Vector3).clone();
|
|
1373
|
+
smooth.push(waypoint);
|
|
1374
|
+
anchor = waypoint;
|
|
1375
|
+
index = furthest + 1;
|
|
1376
|
+
}
|
|
1377
|
+
return smooth;
|
|
1378
|
+
}
|
|
1379
|
+
|
|
1380
|
+
/** Detection is an event: commit a route now instead of waiting for a movement branch. */
|
|
1381
|
+
#beginPursuit(target: Vector3): void {
|
|
1382
|
+
this.#path = this.#findPath(target.x, target.z);
|
|
1383
|
+
this.#pathIndex = 0;
|
|
1384
|
+
this.#pathGoal.set(target.x, 0, target.z);
|
|
1385
|
+
this.#replanIn = NAV_REPLAN_SECONDS;
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
/** Shed pace at the walking deceleration. Used by every branch that is not travelling. */
|
|
1389
|
+
#brake(dt: number): void {
|
|
1390
|
+
this.#pace = Math.max(0, this.#pace - WALK_DECEL * dt);
|
|
1391
|
+
}
|
|
1392
|
+
|
|
1393
|
+
/**
|
|
1394
|
+
* Carry the last of his pace into the stop instead of dropping it in one frame.
|
|
1395
|
+
*
|
|
1396
|
+
* `#brake` only decays `#pace`, and `#pace` is read nowhere except `#step`. So every branch
|
|
1397
|
+
* that stopped by braking without stepping — a patrol pause, a consumed path, a waypoint
|
|
1398
|
+
* reached — took the body from walking pace to standing still between two frames while the
|
|
1399
|
+
* legs were still mid-stride. Measured on patrol before this existed: ground speed went
|
|
1400
|
+
* 2.31 -> 0 with no intermediate frame, twice in 2.6 s, and the locomotion clip snapped to
|
|
1401
|
+
* idle underneath a body that had already teleported to rest. It reads as a stutter, and it is
|
|
1402
|
+
* the same foot-slide defect a clip played too fast causes on the other end of the range.
|
|
1403
|
+
*
|
|
1404
|
+
* Returns whether he is still under way, so the caller can keep the walk clip on until the
|
|
1405
|
+
* body has actually finished moving rather than the frame the decision was made.
|
|
1406
|
+
*/
|
|
1407
|
+
#coast(dt: number): boolean {
|
|
1408
|
+
// Coasting owns the pace for this frame. Without claiming the step, the tail brake at the
|
|
1409
|
+
// end of `update` — which exists for the branches that neither step nor coast, a burst or a
|
|
1410
|
+
// soldier standing and listening — decays it a second time, and the body stops at twice the
|
|
1411
|
+
// deceleration it was authored with. Measured: 18.0 m/s squared against a WALK_DECEL of 9.
|
|
1412
|
+
this.#stepped = true;
|
|
1413
|
+
this.#brake(dt);
|
|
1414
|
+
if (this.#pace <= COAST_FLOOR) {
|
|
1415
|
+
this.#pace = 0;
|
|
1416
|
+
return false;
|
|
1417
|
+
}
|
|
1418
|
+
const heading = this.#heading.value;
|
|
1419
|
+
const travel = this.#pace * dt;
|
|
1420
|
+
const nextX = this.group.position.x + Math.sin(heading) * travel;
|
|
1421
|
+
const nextZ = this.group.position.z + Math.cos(heading) * travel;
|
|
1422
|
+
// A body that walks into something does not coast through it.
|
|
1423
|
+
if (this.#blocked(nextX, nextZ)) {
|
|
1424
|
+
this.#pace = 0;
|
|
1425
|
+
this.#decelExempt = true;
|
|
1426
|
+
return false;
|
|
1427
|
+
}
|
|
1428
|
+
this.group.position.set(nextX, this.group.position.y, nextZ);
|
|
1429
|
+
return true;
|
|
1430
|
+
}
|
|
1431
|
+
|
|
1432
|
+
/** Follow a cached route, replanning when a moving goal changes or the corridor becomes blocked. */
|
|
1433
|
+
/**
|
|
1434
|
+
* Returns true while he is walking the route — including the first few frames of the ramp,
|
|
1435
|
+
* where the body has barely moved. The old test was `travel > speed * dt * 0.05`, which is
|
|
1436
|
+
* false for the whole acceleration, so the walk clip would not start until he was already
|
|
1437
|
+
* gliding. What the caller wants to know is whether he is under way, not whether this
|
|
1438
|
+
* particular frame cleared a distance threshold.
|
|
1439
|
+
*
|
|
1440
|
+
* `faceTravel` is off for combat, where `#engage` owns the facing so the rifle can stay on
|
|
1441
|
+
* the player while the feet go somewhere else.
|
|
1442
|
+
*/
|
|
1443
|
+
#step(dt: number, toX: number, toZ: number, speed: number, faceTravel = true): boolean {
|
|
1444
|
+
this.#stepped = true;
|
|
1445
|
+
this.#replanIn -= dt;
|
|
1446
|
+
this.#goalReplanIn -= dt;
|
|
1447
|
+
const goalMoved = Math.hypot(toX - this.#pathGoal.x, toZ - this.#pathGoal.z) > 0.8;
|
|
1448
|
+
const currentWaypoint = this.#path[this.#pathIndex];
|
|
1449
|
+
const routeObstructed =
|
|
1450
|
+
this.#replanIn <= 0 &&
|
|
1451
|
+
currentWaypoint !== undefined &&
|
|
1452
|
+
!this.#segmentClear(this.group.position, currentWaypoint);
|
|
1453
|
+
// A grid search is not a per-frame operation. `goalMoved` used to bypass the replan cooldown
|
|
1454
|
+
// entirely, so a soldier chasing a moving player re-planned his whole route on almost every
|
|
1455
|
+
// frame — measured at 16.7 ms across the squad, the largest single cost left in the game.
|
|
1456
|
+
// Having no path at all is still replanned immediately, because a soldier with nowhere to go
|
|
1457
|
+
// stands still and that is visible; a route four-tenths of a second out of date is not.
|
|
1458
|
+
// A grid search is not a per-frame operation. `goalMoved` used to bypass the replan cooldown
|
|
1459
|
+
// entirely, so a soldier chasing a moving player re-planned his whole route on almost every
|
|
1460
|
+
// frame — measured at 16.7 ms across the squad, the largest single cost left in the game.
|
|
1461
|
+
// Having no path at all is still replanned immediately, because a soldier with nowhere to go
|
|
1462
|
+
// stands still and that is visible; a route four-tenths of a second out of date is not.
|
|
1463
|
+
//
|
|
1464
|
+
// A squad-wide budget of one search per frame was tried on top of this and reverted: it holds
|
|
1465
|
+
// the worst frame down further, but a soldier pressed against geometry then has to win a race
|
|
1466
|
+
// for the token before he can re-route, and `enemy-reaches-walkway` caught him failing to.
|
|
1467
|
+
// The cooldown alone is the part that is safe to keep.
|
|
1468
|
+
const mustReplan = this.#path.length === 0;
|
|
1469
|
+
const goalDrifted = goalMoved && this.#goalReplanIn <= 0;
|
|
1470
|
+
if (goalDrifted) this.#goalReplanIn = GOAL_REPLAN_SECONDS;
|
|
1471
|
+
if (mustReplan || goalDrifted || routeObstructed) {
|
|
1472
|
+
this.#beginPursuit(scratchGoal.set(toX, 0, toZ));
|
|
1473
|
+
}
|
|
1474
|
+
let waypoint = this.#path[this.#pathIndex];
|
|
1475
|
+
while (waypoint !== undefined && this.group.position.distanceTo(waypoint) < 0.32) {
|
|
1476
|
+
this.#pathIndex += 1;
|
|
1477
|
+
waypoint = this.#path[this.#pathIndex];
|
|
1478
|
+
}
|
|
1479
|
+
if (waypoint === undefined) return this.#coast(dt);
|
|
1480
|
+
const dx = waypoint.x - this.group.position.x;
|
|
1481
|
+
const dz = waypoint.z - this.group.position.z;
|
|
1482
|
+
const distance = Math.hypot(dx, dz);
|
|
1483
|
+
if (distance < 1e-3) return this.#coast(dt);
|
|
1484
|
+
|
|
1485
|
+
// Steer the direction of travel rather than snapping it to the bearing of the next grid
|
|
1486
|
+
// point. This is what turns the 0.7 m A* polyline into a path a body could have walked.
|
|
1487
|
+
const bearing = Math.atan2(dx, dz);
|
|
1488
|
+
const heading = this.#heading.step(bearing, dt);
|
|
1489
|
+
const error = Math.abs(angleDelta(heading, bearing));
|
|
1490
|
+
// Nobody walks a corner at full pace. This also keeps the arc tight: the harder he has to
|
|
1491
|
+
// turn, the less ground he covers while turning, so the curve stays in the cleared corridor.
|
|
1492
|
+
const cornering = MathUtils.clamp(1 - error / CORNER_FULL, CORNER_FLOOR, 1);
|
|
1493
|
+
// Ease off onto the last waypoint instead of stopping on the mark.
|
|
1494
|
+
const arriving =
|
|
1495
|
+
this.#pathIndex >= this.#path.length - 1
|
|
1496
|
+
? MathUtils.clamp(distance / ARRIVE_DISTANCE, ARRIVE_FLOOR, 1)
|
|
1497
|
+
: 1;
|
|
1498
|
+
const wanted = speed * this.#gait * cornering * arriving;
|
|
1499
|
+
const gained = MathUtils.clamp(wanted - this.#pace, -WALK_DECEL * dt, WALK_ACCEL * dt);
|
|
1500
|
+
this.#pace += gained;
|
|
1501
|
+
|
|
1502
|
+
const travel = Math.min(distance, this.#pace * dt);
|
|
1503
|
+
const nextX = this.group.position.x + Math.sin(heading) * travel;
|
|
1504
|
+
const nextZ = this.group.position.z + Math.cos(heading) * travel;
|
|
1505
|
+
if (this.#blocked(nextX, nextZ)) {
|
|
1506
|
+
this.#path = [];
|
|
1507
|
+
this.#replanIn = 0;
|
|
1508
|
+
// He walked into something. A body that hits a wall does not keep its momentum.
|
|
1509
|
+
this.#pace *= 0.3;
|
|
1510
|
+
this.#decelExempt = true;
|
|
1511
|
+
return false;
|
|
1512
|
+
}
|
|
1513
|
+
this.group.position.set(nextX, this.group.position.y, nextZ);
|
|
1514
|
+
if (faceTravel) this.group.rotation.y = this.#facing.step(heading, dt);
|
|
1515
|
+
return true;
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
/**
|
|
1519
|
+
* Can he see the player right now?
|
|
1520
|
+
*
|
|
1521
|
+
* Every frame, with no caching. Rationing this to one answer every few frames was tried and
|
|
1522
|
+
* reverted: `enemy-reaches-walkway` went from reliable to a coin flip, because a soldier acting
|
|
1523
|
+
* on a sight answer a tenth of a second stale takes a different route and does not arrive.
|
|
1524
|
+
* Vision is what the whole AI branches on, and stale input to a state machine is not a
|
|
1525
|
+
* shortcut — it is a different state machine.
|
|
1526
|
+
*
|
|
1527
|
+
* It is affordable because the sight line itself got cheap: the range and cone rejects here
|
|
1528
|
+
* cost three dot products, and `lineOfSight` answers most calls from a box test without
|
|
1529
|
+
* touching a raycast at all. See the two-stage test in `Play`.
|
|
1530
|
+
*/
|
|
1531
|
+
#canSee(eye: Vector3, hooks: EnemyHooks): boolean {
|
|
1532
|
+
const chest = this.chest;
|
|
1533
|
+
scratchTo.subVectors(eye, chest);
|
|
1534
|
+
if (scratchTo.length() > VIEW_RANGE) return false;
|
|
1535
|
+
scratchFacing.set(Math.sin(this.group.rotation.y), 0, Math.cos(this.group.rotation.y));
|
|
1536
|
+
scratchFlat.set(scratchTo.x, 0, scratchTo.z).normalize();
|
|
1537
|
+
if (scratchFacing.dot(scratchFlat) < Math.cos(VIEW_HALF_ANGLE)) return false;
|
|
1538
|
+
return hooks.lineOfSight(chest, eye);
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
hearShot(shooter: Vector3): void {
|
|
1542
|
+
if (!this.alive || this.#frozen) return;
|
|
1543
|
+
if (shooter.distanceTo(this.group.position) > HEAR_RANGE) return;
|
|
1544
|
+
this.#lastSeen.copy(shooter);
|
|
1545
|
+
this.#beginPursuit(shooter);
|
|
1546
|
+
if (this.phase === "patrol" || this.phase === "return") {
|
|
1547
|
+
this.phase = "suspicious";
|
|
1548
|
+
this.#alertTimer = 0;
|
|
1549
|
+
// Only the men pulled off a calm route call it out; anyone already
|
|
1550
|
+
// fighting has said his piece.
|
|
1551
|
+
this.voice?.chase(this.group.position);
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
|
|
1555
|
+
/** Returns the score the shot earned: 300 for the kill, 100 for the first wound. */
|
|
1556
|
+
hurt(ctx: GameCtx, amount: number): number {
|
|
1557
|
+
if (!this.alive) return 0;
|
|
1558
|
+
this.health -= amount;
|
|
1559
|
+
let earned = 0;
|
|
1560
|
+
if (!this.wounded) {
|
|
1561
|
+
this.wounded = true;
|
|
1562
|
+
earned = 100;
|
|
1563
|
+
}
|
|
1564
|
+
if (this.health <= 0) {
|
|
1565
|
+
this.health = 0;
|
|
1566
|
+
this.phase = "dead";
|
|
1567
|
+
this.#decelExempt = true;
|
|
1568
|
+
this.voice?.death(this.group.position);
|
|
1569
|
+
this.#deadFor = 0;
|
|
1570
|
+
this.#deathObserved = true;
|
|
1571
|
+
this.#bodyClearance = null;
|
|
1572
|
+
this.#footClearance = null;
|
|
1573
|
+
this.#deathClipFrames = 0;
|
|
1574
|
+
this.#deathFallMeasured = null;
|
|
1575
|
+
this.#deathHipStart = this.#hips?.getWorldPosition(new Vector3()) ?? null;
|
|
1576
|
+
const clip = this.#deathClipFor();
|
|
1577
|
+
this.#deathClip = this.#clips.has(clip) ? clip : null;
|
|
1578
|
+
this.#play(clip, DEATH_FADE, "once", true);
|
|
1579
|
+
this.#detachWeapon(ctx);
|
|
1580
|
+
ctx.after(RESPAWN_SECONDS, () => this.#respawn());
|
|
1581
|
+
return earned + 300;
|
|
1582
|
+
}
|
|
1583
|
+
// Surviving a round costs him his composure for a couple of seconds, not just health.
|
|
1584
|
+
this.#suppressed = Math.max(this.#suppressed, 2.4);
|
|
1585
|
+
this.#suppressedPeak = Math.max(this.#suppressedPeak, this.#suppressed);
|
|
1586
|
+
this.voice?.pain(this.group.position);
|
|
1587
|
+
// "once", and held: the clip is a one-shot flinch, and the hold is what stops locomotion
|
|
1588
|
+
// reclaiming the rig before a single frame of it has been drawn.
|
|
1589
|
+
const flinch = "Rifle_Hit";
|
|
1590
|
+
this.#reactionHold = Math.min(this.#clipDurations.get(flinch) ?? 0.4, 0.45);
|
|
1591
|
+
this.#play(flinch, REACTION_FADE, "once", true);
|
|
1592
|
+
// A frozen sentry flinches at the impact but holds his ground: engaging here
|
|
1593
|
+
// would walk him out of a scenario-placed spawn on the first non-killing round.
|
|
1594
|
+
if (!this.#frozen && this.phase !== "engage") this.phase = "engage";
|
|
1595
|
+
return earned;
|
|
1596
|
+
}
|
|
1597
|
+
|
|
1598
|
+
/** `shotDirection` is the round's travel direction, used to pick which way the body falls. */
|
|
1599
|
+
recordHit(multiplier: number, shotDirection?: Vector3): void {
|
|
1600
|
+
this.#lastHitMultiplier = multiplier;
|
|
1601
|
+
if (shotDirection !== undefined) {
|
|
1602
|
+
this.#lastHitDirection = shotDirection.clone().normalize();
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
#respawn(): void {
|
|
1607
|
+
this.health = MAX_HEALTH;
|
|
1608
|
+
this.wounded = false;
|
|
1609
|
+
this.phase = "patrol";
|
|
1610
|
+
this.#suppressed = 0;
|
|
1611
|
+
this.#standUp = 0;
|
|
1612
|
+
this.#reactionHold = 0;
|
|
1613
|
+
this.#crouchMoving = false;
|
|
1614
|
+
this.#locomotion = "";
|
|
1615
|
+
this.#locomotionHold = 0;
|
|
1616
|
+
this.#stillFor = 0;
|
|
1617
|
+
this.#lastHitDirection = null;
|
|
1618
|
+
this.#reattachWeapon();
|
|
1619
|
+
this.#bodyClearance = null;
|
|
1620
|
+
this.#footClearance = null;
|
|
1621
|
+
// Blend state first, and only here: it is what costs a pipeline, so it is set once per
|
|
1622
|
+
// soldier and never again. The fade below is a uniform on the pipeline this just built.
|
|
1623
|
+
this.#fixBlendState();
|
|
1624
|
+
this.#setOpacity(0);
|
|
1625
|
+
this.#reaction = 0;
|
|
1626
|
+
this.#burstLeft = 0;
|
|
1627
|
+
this.#cooldown = 0;
|
|
1628
|
+
this.#path = [];
|
|
1629
|
+
this.#pathIndex = 0;
|
|
1630
|
+
this.#pathGoal.copy(this.#route[0] ?? ROUTE_START);
|
|
1631
|
+
this.#replanIn = 0;
|
|
1632
|
+
this.#searchAtGoal = 0;
|
|
1633
|
+
this.#patrolPause = 0;
|
|
1634
|
+
this.#spawnGrace = SPAWN_GRACE_SECONDS;
|
|
1635
|
+
this.#routeIndex = 0;
|
|
1636
|
+
this.group.position.copy(this.#route[0] ?? ROUTE_START);
|
|
1637
|
+
const firstWaypoint = this.#route[1];
|
|
1638
|
+
if (firstWaypoint !== undefined) {
|
|
1639
|
+
this.#target.copy(firstWaypoint);
|
|
1640
|
+
this.#routeIndex = 1;
|
|
1641
|
+
}
|
|
1642
|
+
this.group.rotation.set(
|
|
1643
|
+
0,
|
|
1644
|
+
Math.atan2(this.#target.x - this.group.position.x, this.#target.z - this.group.position.z),
|
|
1645
|
+
0,
|
|
1646
|
+
);
|
|
1647
|
+
// A fresh body starts still and pointed down its route, with no turn or pace carried over
|
|
1648
|
+
// from the one that died — otherwise he respawns already leaning out of a corner.
|
|
1649
|
+
this.#heading.set(this.group.rotation.y);
|
|
1650
|
+
this.#facing.set(this.group.rotation.y);
|
|
1651
|
+
this.#pace = 0;
|
|
1652
|
+
this.#play("Rifle_Walk", LOCOMOTION_FADE, "loop", true);
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
update(ctx: GameCtx, dt: number, playerEye: Vector3, deckY: number, hooks: EnemyHooks): void {
|
|
1656
|
+
this.#frameStart.copy(this.group.position);
|
|
1657
|
+
if (this.phase === "dead") {
|
|
1658
|
+
this.#deadFor += dt;
|
|
1659
|
+
this.#character.update(dt);
|
|
1660
|
+
this.#countClipFrame();
|
|
1661
|
+
this.#deathClipFrames = Math.max(this.#deathClipFrames, this.#character.advancedFrames);
|
|
1662
|
+
const fall = this.#deathFallDot();
|
|
1663
|
+
if (fall !== null && (this.#deathFallMeasured === null || fall > this.#deathFallMeasured)) {
|
|
1664
|
+
this.#deathFallMeasured = fall;
|
|
1665
|
+
}
|
|
1666
|
+
this.#updateDetachedWeapon(dt, deckY);
|
|
1667
|
+
this.#groundToDeck(deckY, dt);
|
|
1668
|
+
this.#syncCollisionBody();
|
|
1669
|
+
if (this.#deadFor > RESPAWN_SECONDS - 0.35) {
|
|
1670
|
+
this.#setOpacity(MathUtils.clamp((RESPAWN_SECONDS - this.#deadFor) / 0.35, 0, 1));
|
|
1671
|
+
}
|
|
1672
|
+
return;
|
|
1673
|
+
}
|
|
1674
|
+
this.#spawnGrace = Math.max(0, this.#spawnGrace - dt);
|
|
1675
|
+
this.#reactionHold = Math.max(0, this.#reactionHold - dt);
|
|
1676
|
+
const sees = !this.#frozen && this.#spawnGrace <= 0 && this.#canSee(playerEye, hooks);
|
|
1677
|
+
if (sees) {
|
|
1678
|
+
// Entering combat from anywhere else starts the reaction clock, so the player gets a
|
|
1679
|
+
// moment to react rather than taking a burst the instant they step into the open.
|
|
1680
|
+
if (this.phase !== "engage") {
|
|
1681
|
+
this.#reaction = REACTION_SECONDS;
|
|
1682
|
+
this.#beginPursuit(playerEye);
|
|
1683
|
+
this.voice?.spot(this.group.position);
|
|
1684
|
+
}
|
|
1685
|
+
this.#lastSeen.copy(playerEye);
|
|
1686
|
+
this.phase = "engage";
|
|
1687
|
+
this.#alertTimer = 0;
|
|
1688
|
+
}
|
|
1689
|
+
|
|
1690
|
+
this.#stepped = false;
|
|
1691
|
+
switch (this.phase) {
|
|
1692
|
+
case "patrol": {
|
|
1693
|
+
if (this.#spawnGrace > 0) {
|
|
1694
|
+
this.#play("Rifle_Idle");
|
|
1695
|
+
break;
|
|
1696
|
+
}
|
|
1697
|
+
if (this.#patrolPause > 0) {
|
|
1698
|
+
this.#patrolPause -= dt;
|
|
1699
|
+
// Coasting, not stopping: he sheds the pace he was carrying over WALK_DECEL and the
|
|
1700
|
+
// legs stay under him until the body has actually come to rest.
|
|
1701
|
+
this.#playLocomotion(this.#coast(dt), false, dt);
|
|
1702
|
+
break;
|
|
1703
|
+
}
|
|
1704
|
+
this.#playLocomotion(this.#step(dt, this.#target.x, this.#target.z, WALK_SPEED), false, dt);
|
|
1705
|
+
if (this.group.position.distanceTo(this.#target) < 0.9) {
|
|
1706
|
+
const routeLength = this.#route.length;
|
|
1707
|
+
if (routeLength === 0) break;
|
|
1708
|
+
this.#routeIndex = (this.#routeIndex + 1) % routeLength;
|
|
1709
|
+
const nextWaypoint = this.#route[this.#routeIndex];
|
|
1710
|
+
if (nextWaypoint !== undefined) this.#target.copy(nextWaypoint);
|
|
1711
|
+
// A patrol that pauses for exactly 0.45 s at every corner reads as a machine.
|
|
1712
|
+
this.#patrolPause = 0.35 + ctx.random() * 1.1;
|
|
1713
|
+
}
|
|
1714
|
+
break;
|
|
1715
|
+
}
|
|
1716
|
+
case "suspicious": {
|
|
1717
|
+
// Heard something: turn toward it, then go looking.
|
|
1718
|
+
this.#alertTimer += dt;
|
|
1719
|
+
const wanted = Math.atan2(
|
|
1720
|
+
this.#lastSeen.x - this.group.position.x,
|
|
1721
|
+
this.#lastSeen.z - this.group.position.z,
|
|
1722
|
+
);
|
|
1723
|
+
// Slower than a combat turn: he is placing a sound, not swinging onto a target.
|
|
1724
|
+
this.group.rotation.y = this.#facing.step(wanted, dt, 0.55);
|
|
1725
|
+
// Something is wrong and he does not know what: stay low while he works it out.
|
|
1726
|
+
this.#playLocomotion(false, true, dt);
|
|
1727
|
+
if (this.#alertTimer > 0.8) this.phase = "search";
|
|
1728
|
+
break;
|
|
1729
|
+
}
|
|
1730
|
+
case "engage": {
|
|
1731
|
+
this.#engage(ctx, dt, playerEye, hooks, sees);
|
|
1732
|
+
break;
|
|
1733
|
+
}
|
|
1734
|
+
case "search": {
|
|
1735
|
+
this.#alertTimer += dt;
|
|
1736
|
+
if (this.group.position.distanceTo(this.#lastSeen) >= 1.6 && this.#alertTimer <= 7) {
|
|
1737
|
+
// Closing on a position someone was just shooting from: move low and quick.
|
|
1738
|
+
this.#playLocomotion(
|
|
1739
|
+
this.#step(dt, this.#lastSeen.x, this.#lastSeen.z, CHASE_SPEED * 0.85, true),
|
|
1740
|
+
true,
|
|
1741
|
+
dt,
|
|
1742
|
+
);
|
|
1743
|
+
} else {
|
|
1744
|
+
// Search the last known area before giving up; do not instantly snap back to patrol.
|
|
1745
|
+
this.#searchAtGoal += dt;
|
|
1746
|
+
// Through the eased yaw rather than straight onto the transform, so the sweep is
|
|
1747
|
+
// reported as angular velocity and the shoulders and head read it like any other turn.
|
|
1748
|
+
this.group.rotation.y = this.#facing.spin(this.#strafe > 0 ? 1.2 : -1.2, dt);
|
|
1749
|
+
this.#playLocomotion(false, true, dt);
|
|
1750
|
+
}
|
|
1751
|
+
if (this.#searchAtGoal > 2.4 || this.#alertTimer > 9.5) {
|
|
1752
|
+
this.phase = "return";
|
|
1753
|
+
this.#alertTimer = 0;
|
|
1754
|
+
this.#searchAtGoal = 0;
|
|
1755
|
+
this.#path = [];
|
|
1756
|
+
}
|
|
1757
|
+
break;
|
|
1758
|
+
}
|
|
1759
|
+
case "return": {
|
|
1760
|
+
const home = this.#route[this.#routeIndex] ?? this.#route[0] ?? this.group.position;
|
|
1761
|
+
this.#playLocomotion(this.#step(dt, home.x, home.z, WALK_SPEED), false, dt);
|
|
1762
|
+
if (this.group.position.distanceTo(home) < 1.0) this.phase = "patrol";
|
|
1763
|
+
break;
|
|
1764
|
+
}
|
|
1765
|
+
}
|
|
1766
|
+
// A branch that never called `#step` — a patrol pause, a burst, standing and listening —
|
|
1767
|
+
// is a soldier coming to a halt, not one who was never moving. Coast the pace down.
|
|
1768
|
+
if (!this.#stepped) this.#brake(dt);
|
|
1769
|
+
this.#standUp = Math.max(0, this.#standUp - dt);
|
|
1770
|
+
this.#suppressed = Math.max(0, this.#suppressed - dt);
|
|
1771
|
+
this.#locomotionHold = Math.max(0, this.#locomotionHold - dt);
|
|
1772
|
+
this.#character.update(dt);
|
|
1773
|
+
this.#countClipFrame();
|
|
1774
|
+
this.#updateFootsteps(hooks);
|
|
1775
|
+
this.#groundToDeck(deckY, dt);
|
|
1776
|
+
this.#syncCollisionBody();
|
|
1777
|
+
if (this.#fade < 1) {
|
|
1778
|
+
this.#setOpacity(MathUtils.clamp(this.#fade + dt / 0.35, 0, 1));
|
|
1779
|
+
}
|
|
1780
|
+
}
|
|
1781
|
+
|
|
1782
|
+
/** Keep the lowest posed body point on the requested deck, and report the real clearance. */
|
|
1783
|
+
#groundToDeck(deckY: number, dt: number): void {
|
|
1784
|
+
// `GroundSnap` is the engine's own render grounding: a skin envelope calibrated once, so a
|
|
1785
|
+
// frame loop never runs the precise per-vertex bounds path, plus the clearance it measured.
|
|
1786
|
+
// `groundSnap` off is a range, not a mute — the measurement and both numbers below stay
|
|
1787
|
+
// truthful, which is what a scenario asserts against.
|
|
1788
|
+
this.#ground.enabled = this.groundSnap;
|
|
1789
|
+
this.#ground.apply(this.group, deckY, dt);
|
|
1790
|
+
const clearance = this.#ground.clearance;
|
|
1791
|
+
this.#footClearance = clearance === null ? null : Math.max(0, clearance);
|
|
1792
|
+
this.#bodyClearance = clearance === null ? null : Math.abs(clearance);
|
|
1793
|
+
}
|
|
1794
|
+
|
|
1795
|
+
#engage(ctx: GameCtx, dt: number, playerEye: Vector3, hooks: EnemyHooks, sees: boolean): void {
|
|
1796
|
+
const chest = this.chest;
|
|
1797
|
+
const knownTarget = sees ? playerEye : this.#lastSeen;
|
|
1798
|
+
const aim = Math.atan2(knownTarget.x - chest.x, knownTarget.z - chest.z);
|
|
1799
|
+
const flatDistance = Math.hypot(playerEye.x - chest.x, playerEye.z - chest.z);
|
|
1800
|
+
|
|
1801
|
+
this.#strafeTimer -= dt;
|
|
1802
|
+
if (this.#strafeTimer <= 0) {
|
|
1803
|
+
// A metronome flank is the loudest tell that this is a state machine. Vary it.
|
|
1804
|
+
this.#strafeTimer = 0.9 + ctx.random() * 1.4;
|
|
1805
|
+
this.#strafe = -this.#strafe;
|
|
1806
|
+
}
|
|
1807
|
+
|
|
1808
|
+
// Stay low when hurt or when rounds are landing near him, and plant to shoot: nobody
|
|
1809
|
+
// sprints through their own burst. This is most of what separates a soldier from a turret
|
|
1810
|
+
// that happens to be walking.
|
|
1811
|
+
const firing = this.#burstLeft > 0;
|
|
1812
|
+
const crouched = this.wounded || this.#suppressed > 0;
|
|
1813
|
+
// Planted, not creeping. At 0.18 he still made 0.65 m/s under a firing clip that has both
|
|
1814
|
+
// feet nailed down — 16 frames of visible slide per engagement, measured. `#step` still runs,
|
|
1815
|
+
// so he decelerates into the plant over ~70 ms rather than stopping on the frame.
|
|
1816
|
+
const settle = firing ? 0 : crouched ? 0.72 : 1;
|
|
1817
|
+
let moved = false;
|
|
1818
|
+
|
|
1819
|
+
if (this.#reaction > 0) {
|
|
1820
|
+
// Detection means pursuit immediately; tactical spacing begins only after reacting.
|
|
1821
|
+
moved = this.#step(dt, knownTarget.x, knownTarget.z, CHASE_SPEED * settle, false);
|
|
1822
|
+
} else {
|
|
1823
|
+
// Every later combat route is still derived from the player: close distance when far,
|
|
1824
|
+
// back off when rushed, and flank rather than running straight into the muzzle.
|
|
1825
|
+
const away = new Vector3(
|
|
1826
|
+
this.group.position.x - knownTarget.x,
|
|
1827
|
+
0,
|
|
1828
|
+
this.group.position.z - knownTarget.z,
|
|
1829
|
+
);
|
|
1830
|
+
if (away.lengthSq() < 1e-4) away.set(0, 0, 1);
|
|
1831
|
+
away.normalize();
|
|
1832
|
+
const desiredRange = flatDistance > ENGAGE_RANGE ? 10.5 : 9;
|
|
1833
|
+
const lateral = new Vector3(away.z, 0, -away.x).multiplyScalar(this.#strafe * 2.6);
|
|
1834
|
+
const combatGoal = new Vector3(knownTarget.x, 0, knownTarget.z)
|
|
1835
|
+
.addScaledVector(away, desiredRange)
|
|
1836
|
+
.add(lateral);
|
|
1837
|
+
moved = this.#step(
|
|
1838
|
+
dt,
|
|
1839
|
+
combatGoal.x,
|
|
1840
|
+
combatGoal.z,
|
|
1841
|
+
(flatDistance > ENGAGE_RANGE ? CHASE_SPEED : WALK_SPEED) * settle,
|
|
1842
|
+
false,
|
|
1843
|
+
);
|
|
1844
|
+
}
|
|
1845
|
+
// Facing is the engage branch's, not `#step`'s. Standing, he squares up on the player;
|
|
1846
|
+
// moving, he keeps the rifle as far round as the feet can carry without moonwalking, which
|
|
1847
|
+
// is a flanker who never takes his weapon off you rather than one who turns his back.
|
|
1848
|
+
const carry = moved
|
|
1849
|
+
? this.#heading.value +
|
|
1850
|
+
MathUtils.clamp(angleDelta(this.#heading.value, aim), -AIM_LEAD_MAX, AIM_LEAD_MAX)
|
|
1851
|
+
: aim;
|
|
1852
|
+
this.group.rotation.y = this.#facing.step(carry, dt, 1.25);
|
|
1853
|
+
// The firing clip owns the pose for as long as the burst lasts; locomotion resumes after.
|
|
1854
|
+
this.#playLocomotion(moved, crouched, dt, firing, sees);
|
|
1855
|
+
|
|
1856
|
+
this.#cooldown -= dt;
|
|
1857
|
+
this.#burstTimer -= dt;
|
|
1858
|
+
this.#reaction -= dt;
|
|
1859
|
+
if (this.#burstLeft > 0) {
|
|
1860
|
+
if (this.#burstTimer <= 0) {
|
|
1861
|
+
this.#burstLeft -= 1;
|
|
1862
|
+
this.#burstTimer = BURST_SPACING;
|
|
1863
|
+
// A round only reaches the player if the shot is actually clear. Firing through a
|
|
1864
|
+
// barricade was the loudest tell that this was a timer and not a soldier.
|
|
1865
|
+
const clear = hooks.lineOfSight(chest, playerEye);
|
|
1866
|
+
// A round that connects costs the full 9; the ones that go wide do not.
|
|
1867
|
+
// Seeded, so a replay of the same run takes the same damage.
|
|
1868
|
+
// Taking rounds spoils a shooter's aim. Without this he shoots exactly as well while
|
|
1869
|
+
// being hit as he does unopposed, which reads as a machine no matter how he moves.
|
|
1870
|
+
const composure = this.#suppressed > 0 ? 0.45 : 1;
|
|
1871
|
+
const accuracy = MathUtils.clamp((0.75 - flatDistance * 0.035) * composure, 0.05, 0.75);
|
|
1872
|
+
const muzzle = this.muzzle();
|
|
1873
|
+
const shotDirection = playerEye.clone().sub(muzzle).normalize();
|
|
1874
|
+
const missDirection = shotDirection.clone();
|
|
1875
|
+
if (ctx.random() >= accuracy) {
|
|
1876
|
+
missDirection.x += (ctx.random() - 0.5) * 0.16;
|
|
1877
|
+
missDirection.y += (ctx.random() - 0.5) * 0.1;
|
|
1878
|
+
missDirection.normalize();
|
|
1879
|
+
}
|
|
1880
|
+
if (clear && shotDirection.angleTo(missDirection) < 0.02) hooks.damagePlayer(ROUND_DAMAGE);
|
|
1881
|
+
hooks.onMuzzleFlash(muzzle, missDirection, playerEye.distanceTo(muzzle));
|
|
1882
|
+
// `#play` declines while a flinch is held, so a soldier hit mid-burst keeps the reaction
|
|
1883
|
+
// rather than having it stomped by the very next round 110 ms later.
|
|
1884
|
+
this.#play("Rifle_Shoot", FIRE_FADE);
|
|
1885
|
+
if (this.#burstLeft === 0) {
|
|
1886
|
+
// Break contact for an irregular beat, longer when he is rattled. A fixed 3.2 s
|
|
1887
|
+
// gap between bursts is learnable within two engagements.
|
|
1888
|
+
this.#cooldown =
|
|
1889
|
+
BURST_COOLDOWN * (0.7 + ctx.random() * 0.6) + (this.#suppressed > 0 ? 0.9 : 0);
|
|
1890
|
+
}
|
|
1891
|
+
}
|
|
1892
|
+
} else if (sees && this.#cooldown <= 0 && this.#reaction <= 0) {
|
|
1893
|
+
// Bursts of two to four, not always three.
|
|
1894
|
+
this.#burstLeft = BURST_ROUNDS + Math.round((ctx.random() - 0.5) * 2);
|
|
1895
|
+
this.#burstTimer = 0;
|
|
1896
|
+
}
|
|
1897
|
+
|
|
1898
|
+
if (!sees) {
|
|
1899
|
+
this.#burstLeft = 0;
|
|
1900
|
+
this.#alertTimer += dt;
|
|
1901
|
+
if (this.#alertTimer > 0.9) {
|
|
1902
|
+
this.phase = "search";
|
|
1903
|
+
this.#alertTimer = 0;
|
|
1904
|
+
this.#searchAtGoal = 0;
|
|
1905
|
+
this.#beginPursuit(this.#lastSeen);
|
|
1906
|
+
}
|
|
1907
|
+
}
|
|
1908
|
+
}
|
|
1909
|
+
|
|
1910
|
+
debug(): {
|
|
1911
|
+
health: number;
|
|
1912
|
+
phase: EnemyPhase;
|
|
1913
|
+
position: number[];
|
|
1914
|
+
deadFor: number;
|
|
1915
|
+
armed: boolean;
|
|
1916
|
+
/**
|
|
1917
|
+
* This soldier's own body opacity — 1 unless he is mid-respawn-fade. Materials are cloned
|
|
1918
|
+
* per soldier (see `ownMaterial`) precisely so this number is his alone: before that fix, one
|
|
1919
|
+
* soldier's fade wrote every soldier's shared material and this field would have moved for
|
|
1920
|
+
* all five at once.
|
|
1921
|
+
*/
|
|
1922
|
+
materialOpacity: number;
|
|
1923
|
+
/** Lowest `materialOpacity` this soldier has ever reported. See `#opacityFloor`. */
|
|
1924
|
+
materialOpacityFloor: number;
|
|
1925
|
+
reaction: number;
|
|
1926
|
+
bodyClearance: number | null;
|
|
1927
|
+
footClearance: number | null;
|
|
1928
|
+
modelHeight: number;
|
|
1929
|
+
hitboxHeight: number;
|
|
1930
|
+
headZoneMinY: number;
|
|
1931
|
+
legZoneMaxY: number;
|
|
1932
|
+
underWalkway: boolean;
|
|
1933
|
+
deathObserved: boolean;
|
|
1934
|
+
wounded: boolean;
|
|
1935
|
+
suppressedPeak: number;
|
|
1936
|
+
crouchClipFrames: number;
|
|
1937
|
+
clipsPlayed: string[];
|
|
1938
|
+
groundSnap: boolean;
|
|
1939
|
+
deathFallDot: number | null;
|
|
1940
|
+
crouching: boolean;
|
|
1941
|
+
suppressed: number;
|
|
1942
|
+
animation: string | null;
|
|
1943
|
+
decelPeakMs2: number;
|
|
1944
|
+
groundSpeed: number;
|
|
1945
|
+
locomotionRate: number;
|
|
1946
|
+
clipGroundSpeed: number;
|
|
1947
|
+
/**
|
|
1948
|
+
* Head joint height above the body's own origin, in metres, as the clip currently poses
|
|
1949
|
+
* it. This is the number that fails on a body folded at the waist or collapsed into its
|
|
1950
|
+
* own chest: 1.545 m is the authored height of the `Head` joint, and a rig that is playing
|
|
1951
|
+
* the wrong clip, bound to the wrong skeleton or wound about the wrong axis reads far
|
|
1952
|
+
* below it. A scenario asserts a floor on it, so a broken body cannot be a green run.
|
|
1953
|
+
*/
|
|
1954
|
+
headHeight: number;
|
|
1955
|
+
strideSynced: boolean;
|
|
1956
|
+
deathClip: string | null;
|
|
1957
|
+
deathClipFrames: number;
|
|
1958
|
+
clips: string[];
|
|
1959
|
+
lastHitMultiplier: number;
|
|
1960
|
+
navigation: { goal: number[]; next: number[] | null; remaining: number };
|
|
1961
|
+
rifleForward: number[] | null;
|
|
1962
|
+
rifleForwardDot: number | null;
|
|
1963
|
+
clipMarkerDownDot: number | null;
|
|
1964
|
+
rifleLength: number | null;
|
|
1965
|
+
rightHandToGrip: number | null;
|
|
1966
|
+
leftHandToRifle: number | null;
|
|
1967
|
+
rightHandContact: number | null;
|
|
1968
|
+
leftHandContact: number | null;
|
|
1969
|
+
handContactPeak: Record<string, number>;
|
|
1970
|
+
weaponNodes: string[];
|
|
1971
|
+
bodyJoints: Record<string, number[]>;
|
|
1972
|
+
} {
|
|
1973
|
+
const weaponPose =
|
|
1974
|
+
this.#weapon === undefined || this.#weaponModel === undefined
|
|
1975
|
+
? null
|
|
1976
|
+
: measureThreePose(this.#weapon, { bounds: false });
|
|
1977
|
+
const rifleForward = weaponPose === null ? null : new Vector3().fromArray(weaponPose.axes.z);
|
|
1978
|
+
const enemyForward = new Vector3(0, 0, 1)
|
|
1979
|
+
.applyQuaternion(this.group.getWorldQuaternion(this.group.quaternion.clone()))
|
|
1980
|
+
.normalize();
|
|
1981
|
+
// The grip is the rifle's own origin, so the hand's distance to it is the whole check:
|
|
1982
|
+
// a soldier holding his rifle anywhere else is a bug, and there is no bone to compare with.
|
|
1983
|
+
const gripPosition = this.#weapon?.getWorldPosition(new Vector3()) ?? null;
|
|
1984
|
+
const rightHandPosition = this.#rightHand?.getWorldPosition(new Vector3()) ?? null;
|
|
1985
|
+
const magazinePosition =
|
|
1986
|
+
this.#weaponModel?.getObjectByName("magazine")?.getWorldPosition(new Vector3()) ?? null;
|
|
1987
|
+
const leftHandPosition = this.#leftHand?.getWorldPosition(new Vector3()) ?? null;
|
|
1988
|
+
const rifleStart = this.#weapon?.localToWorld(new Vector3(0, 0, this.#rifleLocalMinZ)) ?? null;
|
|
1989
|
+
const rifleEnd = this.#weapon?.localToWorld(new Vector3(0, 0, this.#rifleLocalMaxZ)) ?? null;
|
|
1990
|
+
const bodyJoints: Record<string, number[]> = {};
|
|
1991
|
+
for (const bone of this.#poseBones) {
|
|
1992
|
+
bodyJoints[bone.name] = [...measureThreePose(bone, { bounds: false }).position];
|
|
1993
|
+
}
|
|
1994
|
+
const stride = this.#character.stride;
|
|
1995
|
+
return {
|
|
1996
|
+
health: this.health,
|
|
1997
|
+
phase: this.phase,
|
|
1998
|
+
position: this.group.position.toArray(),
|
|
1999
|
+
deadFor: this.#deadFor,
|
|
2000
|
+
armed: this.#weapon !== undefined,
|
|
2001
|
+
materialOpacity: this.#fade,
|
|
2002
|
+
materialOpacityFloor: this.#opacityFloor,
|
|
2003
|
+
reaction: this.#reaction,
|
|
2004
|
+
bodyClearance: this.#bodyClearance,
|
|
2005
|
+
footClearance: this.#footClearance,
|
|
2006
|
+
modelHeight: this.modelHeight,
|
|
2007
|
+
hitboxHeight: this.#hitboxHeight,
|
|
2008
|
+
headZoneMinY: this.headZoneMinY,
|
|
2009
|
+
legZoneMaxY: this.legZoneMaxY,
|
|
2010
|
+
underWalkway: this.#underDeck(),
|
|
2011
|
+
deathObserved: this.#deathObserved,
|
|
2012
|
+
wounded: this.wounded,
|
|
2013
|
+
suppressedPeak: this.#suppressedPeak,
|
|
2014
|
+
crouchClipFrames: this.#clipFrames.get("Rifle_Crouch_Walk") ?? 0,
|
|
2015
|
+
// Every clip the rig has actually run, so an unused animation is visible to a scenario.
|
|
2016
|
+
clipsPlayed: [...this.#clipFrames.keys()].sort(),
|
|
2017
|
+
groundSnap: this.groundSnap,
|
|
2018
|
+
// Whichever death clip ran, the body must travel the way the round did — i.e. away from
|
|
2019
|
+
// the shooter. One number that catches DeathFront and DeathBack being swapped.
|
|
2020
|
+
deathFallDot: this.#deathFallMeasured,
|
|
2021
|
+
crouching: this.#crouchMoving,
|
|
2022
|
+
suppressed: this.#suppressed,
|
|
2023
|
+
animation: this.#character.current ?? null,
|
|
2024
|
+
// Locomotion honesty, straight off the engine's own stride convention: how fast the body
|
|
2025
|
+
// is going, how fast the clip is being played to match, and how far the clip carries the
|
|
2026
|
+
// body at rate 1. `strideSynced` is the honest half — false means the rate is 1 because
|
|
2027
|
+
// the game (or a scenario) turned the convention off, not because the feet are matching.
|
|
2028
|
+
decelPeakMs2: this.#decelPeak,
|
|
2029
|
+
groundSpeed: this.#groundSpeed,
|
|
2030
|
+
locomotionRate: stride.rate,
|
|
2031
|
+
clipGroundSpeed: stride.clipGroundSpeed,
|
|
2032
|
+
headHeight:
|
|
2033
|
+
this.#head === undefined
|
|
2034
|
+
? 0
|
|
2035
|
+
: this.#head.getWorldPosition(new Vector3()).y - this.group.position.y,
|
|
2036
|
+
strideSynced: stride.synced,
|
|
2037
|
+
deathClip: this.#deathClip,
|
|
2038
|
+
deathClipFrames: this.#deathClipFrames,
|
|
2039
|
+
clips: [...this.#clips].sort(),
|
|
2040
|
+
lastHitMultiplier: this.#lastHitMultiplier,
|
|
2041
|
+
navigation: {
|
|
2042
|
+
goal: this.#pathGoal.toArray(),
|
|
2043
|
+
next: this.#path[this.#pathIndex]?.toArray() ?? null,
|
|
2044
|
+
remaining: Math.max(0, this.#path.length - this.#pathIndex),
|
|
2045
|
+
},
|
|
2046
|
+
rifleForward: rifleForward?.toArray() ?? null,
|
|
2047
|
+
rifleForwardDot: rifleForward?.dot(enemyForward) ?? null,
|
|
2048
|
+
clipMarkerDownDot:
|
|
2049
|
+
magazinePosition === null || gripPosition === null
|
|
2050
|
+
? null
|
|
2051
|
+
: magazinePosition
|
|
2052
|
+
.clone()
|
|
2053
|
+
.sub(gripPosition)
|
|
2054
|
+
.normalize()
|
|
2055
|
+
.dot(new Vector3(0, -1, 0)),
|
|
2056
|
+
rifleLength: weaponPose === null ? null : this.#renderedRifleLength,
|
|
2057
|
+
rightHandToGrip:
|
|
2058
|
+
rightHandPosition === null || gripPosition === null
|
|
2059
|
+
? null
|
|
2060
|
+
: rightHandPosition.distanceTo(gripPosition),
|
|
2061
|
+
leftHandToRifle:
|
|
2062
|
+
leftHandPosition === null || rifleStart === null || rifleEnd === null
|
|
2063
|
+
? null
|
|
2064
|
+
: this.#distanceToSegment(leftHandPosition, rifleStart, rifleEnd),
|
|
2065
|
+
// Engine `boneContact`: nearest rifle vertex to the hand joint, so a fist in the air reads
|
|
2066
|
+
// as a distance rather than as a screenshot.
|
|
2067
|
+
rightHandContact: this.#contact("hand_r"),
|
|
2068
|
+
leftHandContact: this.#contact("hand_l"),
|
|
2069
|
+
handContactPeak: Object.fromEntries(this.#contactPeak),
|
|
2070
|
+
weaponNodes: this.#weaponNodes,
|
|
2071
|
+
bodyJoints,
|
|
2072
|
+
};
|
|
2073
|
+
}
|
|
2074
|
+
|
|
2075
|
+
#contact(bone: string): number | null {
|
|
2076
|
+
return this.#weaponModel === undefined
|
|
2077
|
+
? null
|
|
2078
|
+
: boneContact(this.group, bone, this.#weaponModel).distance;
|
|
2079
|
+
}
|
|
2080
|
+
|
|
2081
|
+
#distanceToSegment(point: Vector3, start: Vector3, end: Vector3): number {
|
|
2082
|
+
const segment = end.clone().sub(start);
|
|
2083
|
+
const lengthSquared = segment.lengthSq();
|
|
2084
|
+
if (lengthSquared === 0) return point.distanceTo(start);
|
|
2085
|
+
const t = MathUtils.clamp(point.clone().sub(start).dot(segment) / lengthSquared, 0, 1);
|
|
2086
|
+
return point.distanceTo(start.addScaledVector(segment, t));
|
|
2087
|
+
}
|
|
2088
|
+
|
|
2089
|
+
dispose(): void {
|
|
2090
|
+
this.#character.dispose();
|
|
2091
|
+
this.#body?.dispose();
|
|
2092
|
+
this.#bodyProxy?.removeFromParent();
|
|
2093
|
+
this.group.removeFromParent();
|
|
2094
|
+
}
|
|
2095
|
+
}
|