# iconix/snek architecture

> Architecture diagram of the GitHub repository iconix/snek, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/iconix/snek
- Repository: https://github.com/iconix/snek

## Overview

##### Main Components

A. Core Game Engine Layer
- Game Loop Controller (from game/game.js)
- State Management (game/state.js)
- Input Handler (game/input.js)
- Render Engine (game/render.js)

B. Game Elements Layer
- Snake Entity (snake.js)
- Board Manager (board.js)
- Item System (item.js)
- Motion Controller (motion.js)

C. Technical Infrastructure
- Canvas Manager (canvas.js)
- Configuration System (config.js)
- Error Handler (error.js)

**┌─────────────────────────────────────────────┐**
- HTML5 Canvas                  │
- ┤
- Render Engine                  │
- ┤
- Game Engine                    │
- ┬──────────────┬───────────────┤
- Game State  │ Input Handler│ Game Loop     │
- ┴──────────────┴───────────────┤
- Game Elements                     │
- ┘

##### Component Relationships to Highlight

a. Data Flow:
- Input Handler → Game State → Game Loop → Render Engine
- Game Elements interact with Game State
- Canvas Manager provides rendering context to Render Engine

b. State Management:
- How game/state.js manages the overall game state
- State updates flowing to rendering system

##### Special Features

- Power-up System (Teleport and Phase abilities)
- Motion Controls for Mobile
- Wall-passing mechanics (Pacman-style)

##### Important Implementation Details

- Integration with Firebase Hosting
- Test coverage structure
- Motion control system for mobile devices
- Vanilla JS implementation (no framework)

Use these symbols:
- Rectangles: Major components
- Hexagons: External services (Firebase)
- Diamonds: Decision points in game logic

A notes section mentioning:
- Vanilla JS implementation
- HTML5 Canvas-based rendering
- Mobile-first considerations
- Testing strategy (based on extensive test coverage visible in file structure)

##### The power-up system integration

Keep the abstraction level focused on the major architectural components while showing enough detail to understand the game's unique features like teleportation and phase-through abilities.

Remember to include the testing architecture as a parallel structure, showing how each component has corresponding test coverage (as evidenced by the tests directory structure).

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Canvas
        HTML["HTML5 Canvas"]:::canvas
        CanvasManager["Canvas Manager"]:::canvas
    end

    subgraph RenderLayer
        RenderEngine["Render Engine"]:::render
    end

    subgraph GameEngineLayer
        GameLoop["Game Loop Controller"]:::core
        StateManager["State Management"]:::core
        InputHandler["Input Handler"]:::input
    end

    subgraph GameElements
        Snake["Snake Entity"]:::element
        Board["Board Manager"]:::element
        Items["Item System"]:::element
        Motion["Motion Controller"]:::input
        Direction["Direction System"]:::element
    end

    subgraph Infrastructure
        Config["Configuration System"]:::infra
        ErrorHandler["Error Handler"]:::infra
        Main["Main Application Entry"]:::infra
    end

    subgraph Testing
        GameTests["Game Engine Tests"]:::test
        SnakeTests["Snake Tests"]:::test
        BoardTests["Board Tests"]:::test
        ItemTests["Item Tests"]:::test
        CanvasTests["Canvas Tests"]:::test
        IntegrationTests["Integration Tests"]:::test
    end

    %% Relationships
    Main --> GameLoop
    GameLoop --> StateManager
    GameLoop --> RenderEngine
    InputHandler --> StateManager
    StateManager --> RenderEngine
    RenderEngine --> CanvasManager
    CanvasManager --> HTML

    Snake --> StateManager
    Board --> StateManager
    Items --> StateManager
    Motion --> InputHandler
    Direction --> Motion

    %% Testing Relationships
    GameTests -.-> GameEngineLayer
    SnakeTests -.-> Snake
    BoardTests -.-> Board
    ItemTests -.-> Items
    CanvasTests -.-> CanvasManager
    IntegrationTests -.-> GameLoop

    %% Click Events
    click GameLoop "https://github.com/iconix/snek/blob/main/src/js/game/game.js"
    click StateManager "https://github.com/iconix/snek/blob/main/src/js/game/state.js"
    click InputHandler "https://github.com/iconix/snek/blob/main/src/js/game/input.js"
    click RenderEngine "https://github.com/iconix/snek/blob/main/src/js/game/render.js"
    click Snake "https://github.com/iconix/snek/blob/main/src/js/snake.js"
    click Board "https://github.com/iconix/snek/blob/main/src/js/board.js"
    click Items "https://github.com/iconix/snek/blob/main/src/js/item.js"
    click Motion "https://github.com/iconix/snek/blob/main/src/js/motion.js"
    click CanvasManager "https://github.com/iconix/snek/blob/main/src/js/canvas.js"
    click Config "https://github.com/iconix/snek/blob/main/src/js/config.js"
    click ErrorHandler "https://github.com/iconix/snek/blob/main/src/js/error.js"
    click Direction "https://github.com/iconix/snek/blob/main/src/js/direction.js"
    click Main "https://github.com/iconix/snek/blob/main/src/js/main.js"
    click GameTests "https://github.com/iconix/snek/tree/main/tests/game/"
    click SnakeTests "https://github.com/iconix/snek/blob/main/tests/snake.test.js"
    click BoardTests "https://github.com/iconix/snek/blob/main/tests/board.test.js"
    click ItemTests "https://github.com/iconix/snek/blob/main/tests/item.test.js"
    click CanvasTests "https://github.com/iconix/snek/blob/main/tests/canvas.test.js"
    click IntegrationTests "https://github.com/iconix/snek/blob/main/tests/__integration__/game.integration.test.js"

    %% Styling
    classDef core fill:#2196F3,stroke:#1565C0,color:white
    classDef input fill:#FF9800,stroke:#F57C00,color:white
    classDef element fill:#4CAF50,stroke:#388E3C,color:white
    classDef render fill:#9C27B0,stroke:#7B1FA2,color:white
    classDef canvas fill:#673AB7,stroke:#512DA8,color:white
    classDef infra fill:#757575,stroke:#616161,color:white
    classDef test fill:#78909C,stroke:#546E7A,color:white

    %% Legend
    subgraph Legend
        CoreComponent["Core Engine"]:::core
        InputComponent["Input Systems"]:::input
        GameComponent["Game Elements"]:::element
        RenderComponent["Render Systems"]:::render
        CanvasComponent["Canvas Layer"]:::canvas
        InfraComponent["Infrastructure"]:::infra
        TestComponent["Testing"]:::test
    end
```
