refactor: tighten project structure and strengthen tooling
This commit is contained in:
+32
-403
@@ -1,414 +1,43 @@
|
||||
# Architecture Patterns
|
||||
# Implemented Architecture
|
||||
|
||||
Coding conventions are maintained in `.agent/skills/best-practices.md`.
|
||||
This document describes the code that exists today in the repository.
|
||||
|
||||
The project uses **two complementary patterns**:
|
||||
## Runtime Structure
|
||||
|
||||
- **Singleton service classes** for orchestration and side effects
|
||||
- **Declarative React components** for all 3D scene objects
|
||||
- `src/App.tsx` mounts the `Canvas`, the 3D `World`, the debug perf overlay, and the HTML crosshair overlay.
|
||||
- `src/world/World.tsx` composes the active 3D scene.
|
||||
- `src/world/Map.tsx` loads and centers the blocking map model.
|
||||
- `src/world/Lighting.tsx` owns the current ambient and directional light setup.
|
||||
- `src/world/Environment.tsx` owns the current background color.
|
||||
- `src/world/player/FPSController.tsx` provides the current player camera, pointer lock, and `ZQSD` movement.
|
||||
- `src/utils/debug/` contains debug-only tooling such as `lil-gui`, scene helpers, and the free debug camera.
|
||||
- `src/components/ui/Crosshair.tsx` is the only current HTML overlay component in use.
|
||||
|
||||
This distinction is intentional. Scene elements such as the map, lights, environment, zones, and player are implemented as **React Three Fiber components** and mounted through `<Canvas>`.
|
||||
Global systems such as gameplay flow, cinematics, audio, and debug tooling are implemented as **manager classes**.
|
||||
## Camera Modes
|
||||
|
||||
Consistency matters, but the codebase does **not** force the same lifecycle pattern on scene components and global services.
|
||||
The application currently has two camera modes:
|
||||
|
||||
---
|
||||
- `player`
|
||||
- controlled by `FPSController`
|
||||
- player height is `1.75m`
|
||||
- movement uses `ZQSD`
|
||||
- `E` is reserved for future interaction
|
||||
- `debug`
|
||||
- controlled by `DebugCameraControls`
|
||||
- enabled from the debug panel
|
||||
|
||||
## 1. Singleton Pattern for Global Managers Only
|
||||
The active mode is stored in the debug subsystem and consumed through `src/hooks/debug/useCameraMode.ts`.
|
||||
|
||||
Only cross-cutting services use the singleton pattern.
|
||||
## Debug System
|
||||
|
||||
Examples:
|
||||
- `src/utils/debug/Debug.ts` is a singleton wrapper around `lil-gui`
|
||||
- `src/utils/debug/DebugPerf.tsx` lazy-loads `r3f-perf`
|
||||
- `src/utils/debug/scene/DebugHelpers.tsx` mounts grid and axes in debug mode
|
||||
- `src/utils/debug/scene/DebugCameraControls.tsx` mounts the free camera in debug mode
|
||||
|
||||
- `GameManager`
|
||||
- `CinematicManager`
|
||||
- `AudioManager`
|
||||
- `ZoneManager`
|
||||
- `Debug`
|
||||
- `EventEmitter`
|
||||
## Current Limitations
|
||||
|
||||
These services must exist once, be accessible from anywhere, and coordinate the experience globally.
|
||||
|
||||
```ts
|
||||
// stateManager/GameManager.ts
|
||||
export class GameManager {
|
||||
private static _instance: GameManager | null = null;
|
||||
|
||||
cinematic!: CinematicManager;
|
||||
audio!: AudioManager;
|
||||
zone!: ZoneManager;
|
||||
|
||||
static getInstance(): GameManager {
|
||||
if (!GameManager._instance) {
|
||||
GameManager._instance = new GameManager();
|
||||
}
|
||||
return GameManager._instance;
|
||||
}
|
||||
|
||||
private constructor() {
|
||||
this.cinematic = CinematicManager.getInstance();
|
||||
this.audio = AudioManager.getInstance();
|
||||
this.zone = ZoneManager.getInstance();
|
||||
}
|
||||
|
||||
destroy(): void {
|
||||
this.cinematic.destroy();
|
||||
this.audio.destroy();
|
||||
this.zone.destroy();
|
||||
GameManager._instance = null;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Usage:
|
||||
|
||||
```ts
|
||||
const game = GameManager.getInstance();
|
||||
game.startMission("workshop");
|
||||
```
|
||||
|
||||
**Important:** scene objects such as `Map`, `WorkshopZone`, `Lighting`, or `Environment` are **not** singletons and must remain standard React components.
|
||||
|
||||
---
|
||||
|
||||
## 2. Scene Objects Are React Components, Not Manager Classes
|
||||
|
||||
All 3D scene objects are implemented as **declarative React components**.
|
||||
|
||||
This includes:
|
||||
|
||||
- maps
|
||||
- lights
|
||||
- environments
|
||||
- player controllers
|
||||
- zones
|
||||
- interactive props
|
||||
- postprocessing layers
|
||||
|
||||
This keeps the code aligned with the R3F runtime instead of rebuilding a parallel imperative engine.
|
||||
|
||||
Example:
|
||||
|
||||
```tsx
|
||||
// world/zones/WorkshopZone.tsx
|
||||
import { useEffect, useRef } from "react";
|
||||
import * as THREE from "three";
|
||||
import { useFrame } from "@react-three/fiber";
|
||||
import { useGLTF } from "@react-three/drei";
|
||||
|
||||
export function WorkshopZone() {
|
||||
const root = useRef<THREE.Group>(null);
|
||||
const gltf = useGLTF("/models/workshop/ebike.glb");
|
||||
const mixer = useRef<THREE.AnimationMixer | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
mixer.current = new THREE.AnimationMixer(gltf.scene);
|
||||
|
||||
return () => {
|
||||
mixer.current?.stopAllAction();
|
||||
mixer.current = null;
|
||||
};
|
||||
}, [gltf.scene]);
|
||||
|
||||
useFrame((_, delta) => {
|
||||
mixer.current?.update(delta);
|
||||
});
|
||||
|
||||
return <primitive ref={root} object={gltf.scene.clone()} />;
|
||||
}
|
||||
```
|
||||
|
||||
Per-frame values such as movement, interpolation, camera smoothing, and physics must stay in:
|
||||
|
||||
- `useRef`
|
||||
- `useFrame`
|
||||
- Rapier bodies
|
||||
- other frame-based systems
|
||||
|
||||
They must **never** go through React state.
|
||||
|
||||
---
|
||||
|
||||
## 3. Single Source of Truth for Durable Gameplay State
|
||||
|
||||
The project uses a single authoritative `GameManager` for durable gameplay state.
|
||||
|
||||
React components subscribe to that state through thin hooks.
|
||||
Other managers communicate through `GameManager`, which acts as the main gameplay orchestrator.
|
||||
|
||||
High-frequency values such as movement, camera interpolation, or physics never go through React state and stay in refs or frame-based systems.
|
||||
|
||||
```ts
|
||||
// stateManager/GameManager.ts
|
||||
type Phase = "loading" | "intro" | "exploring" | "cinematic" | "outro";
|
||||
type ZoneId = "workshop" | "powerGrid" | "farm" | null;
|
||||
|
||||
type GameSnapshot = {
|
||||
phase: Phase;
|
||||
activeZone: ZoneId;
|
||||
missionId: string | null;
|
||||
missionStep: number;
|
||||
inputLocked: boolean;
|
||||
dialogueId: string | null;
|
||||
};
|
||||
|
||||
export class GameManager {
|
||||
private static _instance: GameManager | null = null;
|
||||
private listeners = new Set<() => void>();
|
||||
|
||||
private state: GameSnapshot = {
|
||||
phase: "loading",
|
||||
activeZone: null,
|
||||
missionId: null,
|
||||
missionStep: 0,
|
||||
inputLocked: false,
|
||||
dialogueId: null,
|
||||
};
|
||||
|
||||
static getInstance(): GameManager {
|
||||
if (!GameManager._instance) {
|
||||
GameManager._instance = new GameManager();
|
||||
}
|
||||
return GameManager._instance;
|
||||
}
|
||||
|
||||
getState(): GameSnapshot {
|
||||
return this.state;
|
||||
}
|
||||
|
||||
subscribe(listener: () => void): () => void {
|
||||
this.listeners.add(listener);
|
||||
return () => this.listeners.delete(listener);
|
||||
}
|
||||
|
||||
private emit(): void {
|
||||
this.listeners.forEach((cb) => cb());
|
||||
}
|
||||
|
||||
setPhase(phase: Phase): void {
|
||||
this.state.phase = phase;
|
||||
this.emit();
|
||||
}
|
||||
|
||||
setActiveZone(zone: ZoneId): void {
|
||||
this.state.activeZone = zone;
|
||||
this.emit();
|
||||
}
|
||||
|
||||
startMission(id: string): void {
|
||||
this.state.missionId = id;
|
||||
this.state.missionStep = 0;
|
||||
this.emit();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```ts
|
||||
// hooks/useGameState.ts
|
||||
import { useEffect, useState } from "react";
|
||||
import { GameManager } from "@/stateManager/GameManager";
|
||||
|
||||
export function useGameState() {
|
||||
const game = GameManager.getInstance();
|
||||
const [state, setState] = useState(game.getState());
|
||||
|
||||
useEffect(() => {
|
||||
return game.subscribe(() => {
|
||||
setState({ ...game.getState() });
|
||||
});
|
||||
}, [game]);
|
||||
|
||||
return state;
|
||||
}
|
||||
```
|
||||
|
||||
This keeps the architecture simple:
|
||||
|
||||
- **GameManager** owns durable gameplay state
|
||||
- **other managers** handle side effects
|
||||
- **React components** render that state
|
||||
- **R3F frame systems** handle fast-changing values
|
||||
|
||||
---
|
||||
|
||||
## 4. Side Effects Stay in Specialized Managers
|
||||
|
||||
Managers other than `GameManager` should not become secondary state stores.
|
||||
|
||||
Their role is to manage side effects and specialized runtime logic, such as:
|
||||
|
||||
- GSAP timelines
|
||||
- audio playback
|
||||
- zone entry detection
|
||||
- interaction triggers
|
||||
- camera lock/unlock
|
||||
- temporary event coordination
|
||||
|
||||
They can read from `GameManager`, react to its state, or notify it of important transitions.
|
||||
|
||||
Example flow:
|
||||
|
||||
```
|
||||
Component / Hook
|
||||
↓
|
||||
GameManager.getInstance()
|
||||
├── startMission('workshop')
|
||||
├── cinematic.play('intro_workshop')
|
||||
├── audio.playAmbience('workshop')
|
||||
└── zone.setActive('workshop')
|
||||
```
|
||||
|
||||
This keeps the dependency graph understandable while avoiding duplicated durable state.
|
||||
|
||||
---
|
||||
|
||||
## 5. Memory Management — Dispose Only What You Own
|
||||
|
||||
GPU memory must be cleaned carefully.
|
||||
|
||||
However, the project does **not** blindly deep-dispose every object on unmount.
|
||||
Only resources explicitly created and owned by the current component or manager should be disposed.
|
||||
|
||||
This includes things like:
|
||||
|
||||
- custom materials
|
||||
- render targets
|
||||
- postprocessing passes
|
||||
- manually created geometries
|
||||
- manually created textures
|
||||
- temporary clones with owned resources
|
||||
|
||||
Shared or cached assets must **not** be blindly disposed.
|
||||
|
||||
```ts
|
||||
// utils/Dispose.ts
|
||||
import * as THREE from "three";
|
||||
|
||||
export class Dispose {
|
||||
static material(material: THREE.Material): void {
|
||||
for (const value of Object.values(material)) {
|
||||
if (value instanceof THREE.Texture) {
|
||||
value.dispose();
|
||||
}
|
||||
}
|
||||
material.dispose();
|
||||
}
|
||||
|
||||
static mesh(mesh: THREE.Mesh): void {
|
||||
mesh.geometry?.dispose();
|
||||
|
||||
const materials = Array.isArray(mesh.material)
|
||||
? mesh.material
|
||||
: [mesh.material];
|
||||
|
||||
for (const material of materials) {
|
||||
if (material) this.material(material);
|
||||
}
|
||||
}
|
||||
|
||||
static renderTarget(rt: THREE.WebGLRenderTarget): void {
|
||||
rt.texture.dispose();
|
||||
rt.dispose();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Example usage:
|
||||
|
||||
```ts
|
||||
useEffect(() => {
|
||||
const material = new THREE.ShaderMaterial({
|
||||
vertexShader,
|
||||
fragmentShader,
|
||||
});
|
||||
|
||||
meshRef.current.material = material;
|
||||
|
||||
return () => {
|
||||
Dispose.material(material);
|
||||
};
|
||||
}, []);
|
||||
```
|
||||
|
||||
**Rule:** disposal is ownership-based, not automatic and not blind.
|
||||
|
||||
---
|
||||
|
||||
## 6. Debug System
|
||||
|
||||
The debug panel can be activated by appending `?debug` to the URL:
|
||||
|
||||
`http://localhost:5173?debug`
|
||||
|
||||
All debug logic is centralized in `src/debug/`.
|
||||
Do not scatter debug checks across the codebase.
|
||||
|
||||
- `src/debug/Debug.ts` owns the global lil-gui singleton
|
||||
- `src/debug/DebugPerf.tsx` mounts the perf overlay
|
||||
- `src/debug/scene/*` contains debug-only R3F helpers such as free camera controls and axes/grid helpers
|
||||
|
||||
`world/` stays focused on product scene components, while `debug/` contains developer tooling.
|
||||
|
||||
```ts
|
||||
// src/debug/Debug.ts
|
||||
import GUI from "lil-gui";
|
||||
|
||||
export class Debug {
|
||||
private static _instance: Debug | null = null;
|
||||
|
||||
readonly active: boolean;
|
||||
gui: GUI | null = null;
|
||||
|
||||
static getInstance(): Debug {
|
||||
if (!Debug._instance) Debug._instance = new Debug();
|
||||
return Debug._instance;
|
||||
}
|
||||
|
||||
private constructor() {
|
||||
this.active = new URLSearchParams(window.location.search).has("debug");
|
||||
if (this.active) {
|
||||
this.gui = new GUI({ title: "La-Fabrik Debug" });
|
||||
}
|
||||
}
|
||||
|
||||
destroy(): void {
|
||||
this.gui?.destroy();
|
||||
Debug._instance = null;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Usage:
|
||||
|
||||
```ts
|
||||
const debug = Debug.getInstance();
|
||||
|
||||
if (debug.active) {
|
||||
debug.gui!.add(params, "bloomIntensity", 0, 3).name("Bloom");
|
||||
}
|
||||
```
|
||||
|
||||
Debug-only scene helpers should live outside `world/`:
|
||||
|
||||
```tsx
|
||||
// src/debug/scene/DebugHelpers.tsx
|
||||
import { Debug } from "@/debug/Debug";
|
||||
|
||||
export function DebugHelpers(): React.JSX.Element | null {
|
||||
const debug = Debug.getInstance();
|
||||
|
||||
if (!debug.active) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<gridHelper
|
||||
args={[180, 36, "#1d4ed8", "#1e293b"]}
|
||||
position={[0, 0.01, 0]}
|
||||
/>
|
||||
<axesHelper args={[10]} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
```
|
||||
- There is no gameplay state manager implemented yet.
|
||||
- There are no zone systems, missions, dialogue systems, or cinematic systems implemented yet.
|
||||
- Player movement currently uses a simple height clamp instead of real collision or ground detection.
|
||||
- The map is currently a blocking preview scene, not a full playable world.
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
# Target Architecture
|
||||
|
||||
This document describes the intended medium-term architecture for the project.
|
||||
|
||||
## Goals
|
||||
|
||||
- Keep `main` stable, `develop` as the integration branch, and `feat/*` for feature work.
|
||||
- Keep the runtime split between scene composition, gameplay systems, debug tooling, and HTML UI.
|
||||
- Keep one clear source of truth per concern.
|
||||
|
||||
## Intended Layers
|
||||
|
||||
### App Layer
|
||||
|
||||
- `App.tsx` should stay small and orchestration-oriented.
|
||||
- It should mount the canvas scene and top-level HTML overlays.
|
||||
|
||||
### World Layer
|
||||
|
||||
- `src/world/` should contain only production scene objects and scene composition.
|
||||
- Expected responsibilities:
|
||||
- world composition
|
||||
- map/environment/lighting
|
||||
- player controller
|
||||
- zones
|
||||
- post-processing used in production
|
||||
|
||||
### Debug Layer
|
||||
|
||||
- `src/utils/debug/` should contain only developer tooling.
|
||||
- Expected responsibilities:
|
||||
- `lil-gui`
|
||||
- performance overlay
|
||||
- scene helpers
|
||||
- free camera and calibration controls
|
||||
|
||||
### UI Layer
|
||||
|
||||
- `src/components/ui/` should contain HTML overlays used by the player.
|
||||
- Expected examples:
|
||||
- crosshair
|
||||
- loading screen
|
||||
- mission HUD
|
||||
- narrative overlays
|
||||
|
||||
### Gameplay Layer
|
||||
|
||||
- Gameplay state should eventually live in dedicated managers and thin hooks once those systems exist.
|
||||
- Expected future concerns:
|
||||
- missions
|
||||
- zones
|
||||
- cinematics
|
||||
- audio
|
||||
- interactions
|
||||
|
||||
## Rules
|
||||
|
||||
- `world/` should not contain debug-only tooling.
|
||||
- `debug/` should not own production gameplay systems.
|
||||
- Shared types should live close to their domain and move outward only when they gain multiple real consumers.
|
||||
- New files should only be created when they have an active runtime purpose.
|
||||
+43
-2
@@ -1,3 +1,44 @@
|
||||
# Features
|
||||
# Implemented Features
|
||||
|
||||
TODO: Documenter les fonctionnalités du jeu.
|
||||
This document lists features that are actually implemented in the current codebase.
|
||||
|
||||
## Scene Preview
|
||||
|
||||
- Fullscreen React Three Fiber scene
|
||||
- Blocking map loaded from `public/models/map/blocking/model.glb`
|
||||
- Ambient and directional lighting
|
||||
- Solid background environment color
|
||||
|
||||
## Camera Modes
|
||||
|
||||
- Player camera mode
|
||||
- eye height at `1.75m`
|
||||
- pointer lock mouse look
|
||||
- movement with `ZQSD`
|
||||
- vertical clamp to prevent falling below the map plane
|
||||
- Debug camera mode
|
||||
- free orbit camera
|
||||
- switchable from the debug panel
|
||||
|
||||
## UI
|
||||
|
||||
- Center-screen crosshair shown only in player mode
|
||||
|
||||
## Debug Tooling
|
||||
|
||||
- `?debug` query param enables the debug panel
|
||||
- `lil-gui` panel with camera mode selection
|
||||
- debug lighting controls
|
||||
- debug scene helpers
|
||||
- `r3f-perf` overlay
|
||||
|
||||
## Not Implemented Yet
|
||||
|
||||
- missions
|
||||
- interactions on `E`
|
||||
- gameplay zones
|
||||
- cinematics
|
||||
- audio systems
|
||||
- loading flow
|
||||
- minimap and mission HUD
|
||||
- collisions beyond the current simple player height clamp
|
||||
|
||||
Reference in New Issue
Block a user