# micoloth/opentrainyard architecture

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

- Interactive diagram: https://gitdiagram.com/micoloth/opentrainyard
- Repository: https://github.com/micoloth/opentrainyard

## Overview

##### Main Components

a) Game Core Components:
- Game Engine Layer (Bevy)
- Game State Management
- Rendering System
- Audio System (though currently limited)
- Input Handler

b) Game-Specific Components:
- Board System
- Train System
- Puzzle Logic
- Simulation Engine
- Menu System
- Save/Load System

**Top Layer: User Interface**
- Menu Systems (Multiple screens)
- Title Menu
- Game Screen
- Levels Menu
- Solutions Menu
- Credits Menu

**Middle Layer: Game Logic**
- Board Management
- Train Simulation
- Puzzle Logic
- State Management

**Bottom Layer: Core Systems**
- Bevy Engine Integration
- Asset Management
- Data Persistence
- Audio System

##### Key Relationships to Highlight

- Interaction between the board system and train simulator
- Asset loading paths from the assets directory to the respective systems
- Save/load data flow between the game state and storage

##### Technical Details

- Bevy ECS (Entity Component System) architecture
- Web build support (indicated by index.html and web assets)
- Cross-platform compilation paths
- Data saving/loading mechanisms

##### Implementation Notes

- Rectangles: Core systems
- Rounded rectangles: Game features
- Circles: State containers
- Diamonds: Decision points/game logic

##### Platform-specific build processes

This architecture should reflect the Rust/Bevy-specific patterns while maintaining clarity about the game's puzzle-solving nature and cross-platform capabilities.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Style Definitions
    classDef core fill:#2196F3,stroke:#1565C0,color:white
    classDef game fill:#4CAF50,stroke:#388E3C,color:white
    classDef ui fill:#FF9800,stroke:#F57C00,color:white
    classDef asset fill:#9C27B0,stroke:#7B1FA2,color:white
    classDef data fill:#FFEB3B,stroke:#FBC02D,color:black
    
    %% Core Systems Layer
    subgraph CoreSystems
        BevyEngine["Bevy Game Engine"]
        GameCore["Game Core"]:::core
        AudioSystem["Audio System"]:::core
        InputHandler["Input Handler"]:::core
        AssetMgmt["Asset Management"]:::asset
    end
    
    click GameCore "https://github.com/micoloth/opentrainyard/blob/main/src/main.rs"
    click AudioSystem "https://github.com/micoloth/opentrainyard/blob/main/src/audio.rs"
    click AssetMgmt "https://github.com/micoloth/opentrainyard/tree/main/assets/"

    %% Asset Management Subgraph
    subgraph Assets
        Textures["Textures"]:::asset
        Fonts["Fonts"]:::asset
        Audio["Audio Files"]:::asset
        PuzzleDB["Puzzle Database"]:::asset
    end

    click Textures "https://github.com/micoloth/opentrainyard/tree/main/assets/textures/"
    click Fonts "https://github.com/micoloth/opentrainyard/tree/main/assets/fonts/"
    click Audio "https://github.com/micoloth/opentrainyard/tree/main/assets/audio/"
    click PuzzleDB "https://github.com/micoloth/opentrainyard/blob/main/assets/all_puzzles_clean_ordered.json"

    %% Game Logic Layer
    subgraph GameLogic
        BoardSystem["Board System"]:::game
        TrainSystem["Train System"]:::game
        PuzzleLogic["Puzzle Logic"]:::game
        SimEngine["Simulation Engine"]:::game
        TileSystem["Tile System"]:::game
        Utils["Utility Functions"]:::game
    end

    click BoardSystem "https://github.com/micoloth/opentrainyard/blob/main/src/board.rs"
    click TrainSystem "https://github.com/micoloth/opentrainyard/blob/main/src/train.rs"
    click PuzzleLogic "https://github.com/micoloth/opentrainyard/blob/main/src/logic.rs"
    click SimEngine "https://github.com/micoloth/opentrainyard/blob/main/src/simulator.rs"
    click TileSystem "https://github.com/micoloth/opentrainyard/blob/main/src/tile.rs"
    click Utils "https://github.com/micoloth/opentrainyard/blob/main/src/utils.rs"

    %% UI Layer
    subgraph MenuSystem
        TitleMenu["Title Menu"]:::ui
        GameScreen["Game Screen"]:::ui
        LevelsMenu["Levels Menu"]:::ui
        SolutionsMenu["Solutions Menu"]:::ui
        CreditsMenu["Credits Menu"]:::ui
        MenuUtils["Menu Utilities"]:::ui
    end

    click TitleMenu "https://github.com/micoloth/opentrainyard/blob/main/src/menu_title.rs"
    click GameScreen "https://github.com/micoloth/opentrainyard/blob/main/src/menu_game_screen.rs"
    click LevelsMenu "https://github.com/micoloth/opentrainyard/blob/main/src/menu_levels.rs"
    click SolutionsMenu "https://github.com/micoloth/opentrainyard/blob/main/src/menu_solutions.rs"
    click CreditsMenu "https://github.com/micoloth/opentrainyard/blob/main/src/menu_credits.rs"
    click MenuUtils "https://github.com/micoloth/opentrainyard/blob/main/src/menu_utils.rs"

    %% Data Management
    subgraph DataManagement
        SaveLoad["Save/Load System"]:::data
        LoadingSystem["Loading System"]:::data
    end

    click SaveLoad "https://github.com/micoloth/opentrainyard/blob/main/src/data_saving.rs"
    click LoadingSystem "https://github.com/micoloth/opentrainyard/blob/main/src/loading.rs"

    %% Platform Builds
    subgraph Platforms
        WebBuild["Web Build"]
        MacBuild["MacOS Build"]
        WinBuild["Windows Build"]
    end

    click WebBuild "https://github.com/micoloth/opentrainyard/tree/main/build/web/"
    click MacBuild "https://github.com/micoloth/opentrainyard/tree/main/build/macos/"
    click WinBuild "https://github.com/micoloth/opentrainyard/tree/main/build/windows/"

    %% Relationships
    BevyEngine --> GameCore
    GameCore --> BoardSystem
    GameCore --> TrainSystem
    GameCore --> AudioSystem
    GameCore --> InputHandler
    
    AssetMgmt --> Textures
    AssetMgmt --> Fonts
    AssetMgmt --> Audio
    AssetMgmt --> PuzzleDB
    
    BoardSystem --> TileSystem
    TrainSystem --> SimEngine
    PuzzleLogic --> SimEngine
    
    MenuUtils --> TitleMenu
    MenuUtils --> GameScreen
    MenuUtils --> LevelsMenu
    MenuUtils --> SolutionsMenu
    MenuUtils --> CreditsMenu
    
    SaveLoad --> GameCore
    LoadingSystem --> AssetMgmt
    
    GameCore --> WebBuild
    GameCore --> MacBuild
    GameCore --> WinBuild
```
