# iercann/enari-engine architecture

> Architecture diagram of the GitHub repository iercann/enari-engine, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/iercann/enari-engine
- Repository: https://github.com/iercann/enari-engine

## Overview

##### Main Components
- Game Engine Core – Representing the heart of the project (see files like src/Game.ts). This is where the game loop and central orchestration happen.
- Input Management – Found in src/Input (InputManager.ts, KeyBinding.ts), handling user inputs (WASD, mouse clicks, etc.).
- Controllers – In src/Controller (CarController.ts, PlayerController.ts, and generic Controller.ts) which process game object behaviors based on input and game state.
- Core Domain Objects – Under src/Core, containing classes such as Actor, GameObject, Pawn, Player, and various utility math classes (Vector, Quaternion, etc.) that represent game entities and support their behaviors.
- Physics Engine – Located in src/Physics, including colliders (Cube, Sphere, Ground, Trimesh) and physics integration with Ammo.js. This module is responsible for collision detection and physical simulations.
- Rendering & Visualization – Consisting of various submodules under src/View:
- Mesh and Asset Loading (files in src/View/Mesh and related Draco decoding support)
- Renderer Components (in src/View/Renderer: different renderers like CubeRenderer, GroundRenderer, FPSRenderer, ThirdPersonRenderer, etc.)
- Camera Management (src/View/CameraManager with FPS and TPS camera implementations)
- Lighting and Shader management (src/View/Shader, SceneLighting, SkyLight, etc.)
- Particle systems (src/View/Particle/ParticleManager.ts)
- Audio (src/View/Audio/AudioManager.ts)
- User Interface & Debug – Any UI-specific elements like the debug UI (src/View/DebugUI.ts) to adjust settings and visualization tools.
- Public Assets Repository – The public directory holds 3D model files (.glb, .blend, etc.), audio assets, and other static resources like maps, which are loaded at runtime.

##### Relationships and Data Flow
- The central Game Engine (Game.ts) acts as the coordinator:
- It integrates the Input Manager, Controllers, and Core Game Objects.
- It manages the update cycle by periodically triggering updates (via the PeriodicUpdater in Core/PeriodicUpdater.ts) for both the physics engine and the rendering system.
- Input Management passes user actions to the Controllers (e.g., PlayerController for movement or CarController for vehicles).
- Controllers update the state of Game Objects (through the Core/Actor, Pawn, etc.), which in turn update their visual representations in the Rendering module.
- The Physics Engine receives updated state from game objects, calculates collisions and dynamics (with support from the Collider types), then passes physics updates back to the Core.
- The Rendering module:
- Uses Mesh loaders to interpret 3D assets.
- Integrates with the Camera Managers (switching between FPS/TPS views).
- Applies lighting, shadowing, and shader effects.
- The Audio system (AudioManager) and Particle system (ParticleManager) work in parallel to provide immersive effects triggered by in-game events.
- There’s a clear separation between visualization (src/View) and game logic (src/Core, src/Controller, src/Input), making it a modular design.

##### Important Architectural Patterns
- Modular Design: Each subsystem (Input, Physics, Rendering, etc.) is in its dedicated directory, facilitating loose coupling and easier maintenance.
- MVC-Like Separation: The game’s domain logic (Models and Controllers) is separated from the view/rendering components, similar to an MVC pattern.
- Abstraction and Extension: Interfaces in src/Interface, and the abstraction of colliders (Cube, Sphere, Trimesh) suggest extensibility for various game objects.

##### Technologies and Frameworks
- Three.js for rendering and 3D model handling.
- Ammo.js (with mention of a migration possibility to Rapier.js) for physics simulation.
- Relevant libraries like Tweakpane for UI debugging.
- Draco decoder (in src/View/Mesh/draco) for optimized 3D asset compression.

##### Final Touches
- Provide directional cues that clearly represent dataflows and dependencies among modules.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Game Logic Layer"
        IM("Input Manager"):::input
        C("Controllers"):::controller
        GEC("Game Engine Core"):::gameCore
        CDO("Core Domain Objects"):::coreData
        PE("Physics Engine"):::physics
    end

    subgraph "Rendering & Visualization Layer"
        ML("Mesh & Asset Loading"):::rendering
        RC("Renderer Components"):::rendering
        CM("Camera Management"):::rendering
        LS("Lighting/Shader Management"):::rendering
        PS("Particle System"):::rendering
        AM("Audio Manager"):::rendering
    end

    UI("UI & Debug"):::ui
    PA("Public Assets"):::assets

    %% Relationships in Game Logic
    IM -->|"input_events"| C
    C -->|"update_commands"| GEC
    GEC -->|"state_update"| CDO
    CDO -->|"physics_simulation"| PE
    CDO -->|"render_update"| RC
    GEC ---|"update_cycle"| PE

    %% Relationships between Game Engine and Rendering
    GEC ---|"update_cycle"| ML
    ML -->|"asset_loading"| PA

    %% Internal connections within Rendering Layer
    RC -->|"camera_link"| CM
    CM -->|"lighting_link"| LS
    LS -->|"particle_trigger"| PS
    PS -->|"audio_trigger"| AM

    %% UI receives debug data from Game Engine
    GEC -->|"debug_data"| UI

    %% Click Events for Component Mapping
    click GEC "https://github.com/iercann/enari-engine/blob/main/src/Game.ts"
    click IM "https://github.com/iercann/enari-engine/tree/main/src/Input"
    click C "https://github.com/iercann/enari-engine/tree/main/src/Controller"
    click CDO "https://github.com/iercann/enari-engine/tree/main/src/Core"
    click PE "https://github.com/iercann/enari-engine/tree/main/src/Physics"
    click ML "https://github.com/iercann/enari-engine/tree/main/src/View/Mesh"
    click RC "https://github.com/iercann/enari-engine/tree/main/src/View/Renderer"
    click CM "https://github.com/iercann/enari-engine/tree/main/src/View/CameraManager"
    click LS "https://github.com/iercann/enari-engine/tree/main/src/View/Shader"
    click PS "https://github.com/iercann/enari-engine/blob/main/src/View/Particle/ParticleManager.ts"
    click AM "https://github.com/iercann/enari-engine/blob/main/src/View/Audio/AudioManager.ts"
    click UI "https://github.com/iercann/enari-engine/blob/main/src/View/DebugUI.ts"
    click PA "https://github.com/iercann/enari-engine/tree/main/public"

    %% Class Definitions for Colour Coding
    classDef gameCore fill:#ffcc00,stroke:#333,stroke-width:2px;
    classDef input fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef controller fill:#ccffcc,stroke:#333,stroke-width:2px;
    classDef coreData fill:#ffddcc,stroke:#333,stroke-width:2px;
    classDef physics fill:#f9c2ff,stroke:#333,stroke-width:2px;
    classDef rendering fill:#d1e7dd,stroke:#333,stroke-width:2px;
    classDef ui fill:#f8d7da,stroke:#333,stroke-width:2px;
    classDef assets fill:#e2e3e5,stroke:#333,stroke-width:2px;

    class GEC gameCore;
    class IM input;
    class C controller;
    class CDO coreData;
    class PE physics;
    class ML,RC,CM,LS,PS,AM rendering;
    class UI ui;
    class PA assets;
```
