# holymonkey/asteroids architecture

> Architecture diagram of the GitHub repository holymonkey/asteroids, 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/holymonkey/asteroids
- Repository: https://github.com/holymonkey/asteroids

## Overview

##### Project Type and Purpose
- This is a Unity game project – a clone of the Asteroids game.
- Its purpose is to demonstrate how game logic can be separated into a model independent of Unity’s typical MonoBehaviour framework.
- The README indicates that it’s a vanilla Unity project (without dependency injection frameworks like Zenject) and serves as a base for students to refactor problematic design aspects.

##### Analysis of the File Structure
- Top-Level Organization:
- There are typical Unity directories (Assets, Packages, ProjectSettings).
- Within Assets, you see folders like "Scenes", "Prefabs", "Sprites", and "Sources".

- Sources Directory Breakdown:
- CompositeRoot: Contains files for composition and initialization (e.g., CompositeRoot.cs, CompositionOrder.cs). This likely represents the bootstrap or entry-point that wires up the various systems/components.
- Input: Hosts classes such as ShipInput.cs, ShipInputRouter.cs, and input configurations (e.g., .inputactions). This component handles player input.
- Model: This is the core game logic layer. Subdirectories include:
- Enemies (Asteroid.cs, Nlo.cs, Enemy.cs) – managing enemy behavior.
- Simulations (e.g., BulletsSimulation, EnemiesSimulation, EnemiesSpawner, Trajectory) – governing dynamic in-game simulations.
- Other model files for ship movement, collisions (CollisionsRecords.cs), physics routing, timers, and other gameplay mechanics.
- Weapon: Manages different types of weapon implementations (Bullet.cs, DefaultGun.cs, LaserGun.cs, LaserGunRollback.cs).
- View: Contains view factories and UI-related scripts (e.g., BulletsViewFactory.cs, EnemiesViewFactory.cs, EndGameWindowView.cs, ShipDebugUIView.cs). This layer is responsible for how game state is visually represented, bridging the gap between the model and the Unity scene objects.

- Other Directories:
- Prefabs and Scenes: Represent Unity scene contents. For example, prefabs for Asteroid, Bullet, Laser, etc., and the SampleScene for gameplay.

a. Main Components:
- Composite Root/Composition Layer: Acts as the initializer and dependency configuration center.
- Input Layer: Handles player actions (keyboard, mouse, or controller inputs) and passes them to the game model.
- Model Layer: Contains the game’s core logic, including:
- Entities: Ship, Enemies (Asteroid, Nlo, etc.), and Weapons.
- Simulations: Bullet trajectories, enemy spawning, collision handling, and physics routing.
- Configuration and State Management (via files like Config.cs, Timers.cs, etc.).
- View Layer: Manages UI and visual representations (factories for spawning views, Debug UI, End Game windows, etc.).
- Unity-specific Components: Scenes, Prefabs, Input System configuration, and related project assets.

b. Relationships and Interactions:
- The Composite Root initializes and wires together the Input, Model, and View components.
- User inputs captured by the Input layer (e.g., ShipInput) are routed via a ShipInputRouter to update the Model.
- The Model processes inputs, runs simulations (e.g., enemy behaviors, bullet trajectories), and updates its state.
- Changes in the Model are reflected by the View layer, via factories (like BulletsViewFactory and EnemiesViewFactory) that instantiate or update visual prefabs.
- Additionally, communication between model components (physics events, collision records, timers) is likely coordinated through internal messaging patterns (such as PhysicsEventsBroadcaster).

c. Architectural Patterns & Design Principles:
- Separation of Concerns: Clearly delineated layers (Input, Model, View) that allow independent development and testing.
- Composition over Inheritance: The use of a CompositeRoot and non-MonoBehaviour classes illustrates a preference for composition.
- Modularity: Individual model subcomponents (Enemies, Weapons, Simulation, Physics) are isolated for easier refactoring and testing.

d. Relevant Technologies & Frameworks:
- Unity Engine: Underlying game engine (shown by folders like Assets, Scenes, Prefabs).
- Unity Input System: Evident from the Input configuration files (.inputsettings, .inputactions).
- C# Scripts: For game logic, simulation, and view management (all under Assets/Sources).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Composite Root / Initialization"
        CR["Composite Root/Initializer"]:::composite
    end

    subgraph "Input Layer"
        IN["Input System"]:::input
    end

    subgraph "Model Layer"
        GM["General Model Files"]:::model
        EN["Enemies Entities"]:::model
        SIM["Simulations"]:::model
        WP["Weapons"]:::model
    end

    subgraph "View Layer"
        VL["View & UI Factories"]:::view
    end

    subgraph "Unity Assets"
        PF["Prefabs"]:::unity
        SC["Scenes"]:::unity
        UI["Unity Input Config"]:::unity
    end

    %% Initialization arrows
    CR -->|"initializes"| IN
    CR -->|"initializes"| GM
    CR -->|"initializes"| VL

    %% Input to Model communication
    IN -->|"commands"| GM

    %% Model internal relationships
    GM -->|"organizes"| EN
    GM -->|"organizes"| SIM
    GM -->|"organizes"| WP
    EN -->|"triggers"| SIM
    SIM -->|"integrates"| WP

    %% Model to View updates
    GM -->|"updates_state"| VL

    %% View to Unity Assets communication
    VL -->|"instantiates"| PF
    VL -->|"loads"| SC

    %% Input layer configuration link
    IN -->|"configured_by"| UI

    %% Click Events
    click CR "https://github.com/holymonkey/asteroids/tree/master/Assets/Sources/CompositeRoot"
    click IN "https://github.com/holymonkey/asteroids/tree/master/Assets/Sources/Input"
    click GM "https://github.com/holymonkey/asteroids/tree/master/Assets/Sources/Model"
    click EN "https://github.com/holymonkey/asteroids/tree/master/Assets/Sources/Model/Enemies"
    click SIM "https://github.com/holymonkey/asteroids/tree/master/Assets/Sources/Model/Simulations"
    click WP "https://github.com/holymonkey/asteroids/tree/master/Assets/Sources/Model/Weapon"
    click VL "https://github.com/holymonkey/asteroids/tree/master/Assets/Sources/View"
    click PF "https://github.com/holymonkey/asteroids/tree/master/Assets/Prefabs"
    click SC "https://github.com/holymonkey/asteroids/tree/master/Assets/Scenes"
    click UI "https://github.com/holymonkey/asteroids/blob/master/Assets/InputSystem.inputsettings.asset"

    %% Styles
    classDef composite fill:#E0BBFF,stroke:#8A2BE2,stroke-width:2px;
    classDef input fill:#B3E5FC,stroke:#0288D1,stroke-width:2px;
    classDef model fill:#C8E6C9,stroke:#388E3C,stroke-width:2px;
    classDef view fill:#FFE0B2,stroke:#FB8C00,stroke-width:2px;
    classDef unity fill:#F0F4C3,stroke:#AFB42B,stroke-width:2px;
```
