# tomasis7/snake-game-2d-typescript-oop architecture

> Architecture diagram of the GitHub repository tomasis7/snake-game-2d-typescript-oop, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tomasis7/snake-game-2d-typescript-oop
- Repository: https://github.com/tomasis7/snake-game-2d-typescript-oop

## Overview

##### Project type and purpose
- This is a single-page, front-end browser game built in TypeScript using the p5.js canvas library. There is no server or database backend—everything runs in the user’s browser. The build tool is Vite, producing a bundled JS file and static HTML/assets.

##### Main components of the system
a. Development environment
- Node.js/npm – package management and script runner (npm run dev, npm run build).
- Vite dev server – serves the app locally, watches TS files, hot-reloads.
- TypeScript compiler – compiles `.ts` to `.js`.
b. Build artifact
- bundle.js (in `/build` or `/dist`) – the compiled and minified game code plus p5.js.
- index.html – the shell that loads bundle.js and bootstraps the canvas.
- assets folder – fonts, images, sounds, music.
c. Runtime (Browser)
- HTML5 Canvas – drawing surface managed by p5.js.
- Game engine modules (in `/src`):
– Entry point: `main.ts` → initializes `Game`.
– Game loop: `game.ts` → orchestrates screens, input, entity updates.
– Screens/UI: `startmenu.ts`, `gamescreen.ts`, `interactionscreen.ts`, `gameOverScreen.ts`.
– Entities: `player.ts`, `ghost.ts`, `plant.ts`, `star.ts`, `heart.ts`, `block.ts`, `winBlock.ts`, `tetrisBlocks.ts`.
– Managers/factories: `collisionmanager.ts`, `scoreManager.ts`, `levelfactory.ts`, `countdown.ts`.
d. External libraries
- p5.js and p5.sound (for audio).
- Type definitions from `global.d.ts`.

##### Relationships and data flow
- Dev tools → (TS compile/Vite) → Build artifact
- Browser loads index.html → bundle.js + assets
- p5.js runtime calls `main.setup()` → instantiates `Game`
- User input (keyboard/mouse) → event handlers in `game.ts` → affects entity properties

##### Architectural patterns and design principles
- Modular code: each game concept (screen, entity, manager) in its own file
- Game loop / event-driven architecture (update + render per frame)
- Factory pattern (`levelfactory`) for dynamic level composition
- Manager classes for collision and scoring concerns
- Separation of concerns: UI screens vs. game logic vs. asset management

##### Capturing essential details
- Asset retrieval (e.g., `loadSound()` in `global.d.ts`)
- How the level factory injects entities into the gameboard
- Collision manager linking Entities to Game logic
- ScoreManager feeding back into UI screens (e.g., game-over / win screen)
- That all modules live in the browser—no server calls

##### Final advice
- High level (Dev → Build → Runtime)
- Mid level (Game loop → Screens/Entities/Managers → Canvas/Audio)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Dev Environment
    subgraph "Dev Environment"
        npm["package.json"]:::dev
        tsconfig["tsconfig.json"]:::dev
        vitecfg["vite.config.js"]:::dev
        ci[".github/workflows/main.yml"]:::dev
        modules["node_modules/"]:::dev
    end

    %% Build Output
    subgraph "Build Output"
        bundle["bundle.js"]:::build
        indexBuild["index.html"]:::build
        assets["Dist Assets"]:::assets
    end

    %% Browser Runtime
    subgraph "Browser Runtime"
        canvas["HTML5 Canvas"]:::canvas
        p5js["p5.js"]:::external
        p5sound["p5.sound"]:::external
    end

    %% Source Modules
    subgraph "Source Modules"
        subgraph "Screens" 
            startmenu["startmenu.ts"]:::screens
            gamescreen["gamescreen.ts"]:::screens
            interactionscreen["interactionscreen.ts"]:::screens
            gameOver["gameOverScreen.ts"]:::screens
        end
        subgraph "Entities"
            entity["entity.ts"]:::entities
            player["player.ts"]:::entities
            ghost["ghost.ts"]:::entities
            plant["plant.ts"]:::entities
            star["star.ts"]:::entities
            heart["heart.ts"]:::entities
            block["block.ts"]:::entities
            winBlock["winBlock.ts"]:::entities
            tetris["tetrisBlocks.ts"]:::entities
        end
        subgraph "Managers & Factories"
            collision["collisionmanager.ts"]:::managers
            score["scoreManager.ts"]:::managers
            level["levelfactory.ts"]:::managers
            countdown["countdown.ts"]:::managers
        end
        subgraph "Supporting"
            gameboard["gameboard.ts"]:::entities
            button["button.ts"]:::entities
            globald["global.d.ts"]:::entities
        end
        main["main.ts"]:::screens
        game["game.ts"]:::screens
    end

    %% Relationships
    vitecfg -->|"compile & bundle"| bundle
    tsconfig -->|“TS compile”| bundle
    npm -->|"npm run build"| bundle
    ci -->|"CI build"| bundle

    bundle -->|"load bundle.js"| canvas
    indexBuild -->|"load index.html"| canvas
    assets -->|"static assets"| canvas

    p5js -->|"calls setup()/draw()"| main
    main -->|"init Game"| game
    game -->|"manage Screens"| startmenu
    game -->|"manage Screens"| gamescreen
    game -->|"manage Screens"| interactionscreen
    game -->|"manage Screens"| gameOver

    game -->|"update Entities"| player
    game -->|"update Entities"| ghost
    game -->|"update Entities"| plant
    game -->|"update Entities"| star
    game -->|"update Entities"| heart
    game -->|"update Entities"| block
    game -->|"update Entities"| winBlock
    game -->|"update Entities"| tetris

    game -->|"use Managers"| collision
    game -->|"use Managers"| score
    game -->|"use Managers"| level
    game -->|"use Managers"| countdown

    level -->|"inject Entities"| game
    collision -->|"detect collisions"| game
    score -->|"track score"| game

    startmenu -->|"render() "| canvas
    gamescreen -->|"render() "| canvas
    interactionscreen -->|"render() "| canvas
    gameOver -->|"render() "| canvas

    game -->|"play audio"| p5sound

    %% Click Events
    click npm "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/package.json"
    click tsconfig "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/tsconfig.json"
    click vitecfg "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/vite.config.js"
    click ci "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/.github/workflows/main.yml"
    click modules "https://github.com/tomasis7/snake-game-2d-typescript-oop/tree/main/node_modules/"
    click bundle "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/build/bundle.js"
    click indexBuild "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/build/index.html"
    click assets "https://github.com/tomasis7/snake-game-2d-typescript-oop/tree/main/dist/assets/"
    click main "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/src/main.ts"
    click game "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/src/game.ts"
    click startmenu "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/src/startmenu.ts"
    click gamescreen "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/src/gamescreen.ts"
    click interactionscreen "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/src/interactionscreen.ts"
    click gameOver "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/src/gameOverScreen.ts"
    click entity "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/src/entity.ts"
    click player "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/src/player.ts"
    click ghost "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/src/ghost.ts"
    click plant "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/src/plant.ts"
    click star "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/src/star.ts"
    click heart "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/src/heart.ts"
    click block "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/src/block.ts"
    click winBlock "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/src/winBlock.ts"
    click tetris "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/src/tetrisBlocks.ts"
    click collision "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/src/collisionmanager.ts"
    click score "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/src/scoreManager.ts"
    click level "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/src/levelfactory.ts"
    click countdown "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/src/countdown.ts"
    click gameboard "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/src/gameboard.ts"
    click button "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/src/button.ts"
    click globald "https://github.com/tomasis7/snake-game-2d-typescript-oop/blob/main/global.d.ts"

    %% Styles
    classDef dev fill:#87CEFA,stroke:#000,stroke-width:1px
    classDef build fill:#FFDAB9,stroke:#000,stroke-width:1px
    classDef assets fill:#FFFF66,stroke:#000,stroke-width:1px
    classDef canvas fill:#FFA500,stroke:#000,stroke-width:1px
    classDef external fill:#FFFFFF,stroke-dasharray: 5 5,stroke:#000
    classDef screens fill:#98FB98,stroke:#000,stroke-width:1px
    classDef entities fill:#7FFFD4,stroke:#000,stroke-width:1px
    classDef managers fill:#FFD700,stroke:#000,stroke-width:1px
```
