# virgs/tetris architecture

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

- Interactive diagram: https://gitdiagram.com/virgs/tetris
- Repository: https://github.com/virgs/tetris

## Overview

##### Project Type & Purpose
- This is a browser-based Tetris game built with TypeScript and the Phaser game framework. It leverages a reactive programming style and includes modular game components.
- The README clearly indicates it is an example Tetris game, and the file structure (with assets, scenes, and actors) reinforces this focus on game logic and rendering.

##### Analyzing the File Structure
- Top-level files like index.html, package.json, webpack.config.js, tsconfig.json, and tslint.json show that the project is compiled (using Webpack/TypeScript) and eventually deployed as a browser game.
- The assets folder holds images (like “hole.bmp” and “wall.bmp”) and sounds (game-specific audio files), which are critical for the game’s visual and audio feedback.
- The src directory is where the main application logic resides. It’s subdivided into:
- actors/ — contains game entities (e.g., tetramino behaviors, stack management).
- scenes/ — holds the scene controllers (e.g., splash-scene.ts for the introductory screen and game-controller.ts for in-game control).
- message-manager/ — indicates a subsystem for passing messages or events among different game parts.
- Other key files (e.g., game.ts, input-manager.ts, command.ts) imply that the game relies on a clear separation of responsibilities (handling game logic, user input, and command orchestration).
- The presence of service-worker.js and sw-toolbox.js suggests the game supports offline caching or PWA features.

##### Key Components
a. Main Game Engine & Entry Point:
- index.html: Loads the compiled bundle (build/bundle.js).
- bundle.js: Contains the game bootstrapping code from Webpack, which initializes the game engine.

b. Game Logic & Scenes:
- game.ts: Likely the main game controller tying everything together.
- scenes/ (splash-scene.ts, game-controller.ts): Represents distinct UI flows and game states (startup, game play, etc.).

c. Actors Module:
- actors/falling-tetramino.ts, tetramino-factory.ts, tetramino-stack.ts: Handle game objects, creation of pieces, and stacking behavior.

d. Input & Messaging:
- input-manager.ts: Manages user input (keyboard/touch) and directs it to the relevant game logic.
- message-manager/ (message-manager.ts, messages.ts): Facilitates passing messages/events among modules (e.g., notifying when pieces attach, clearing lines, etc.).

e. Assets & Presentation:
- assets/ directory: Contains images, fonts, and sounds. Represented as an external resource that the game loads dynamically.
- styles/ directory: CSS and LESS files used for styling any UI overlays (or game menus) displayed within the browser.

f. Service & Build Infrastructure:
- service-worker.js and sw-toolbox.js: Enable caching and offline capabilities.
- Configuration files (webpack.config.js, tsconfig.json, tslint.json): Indicate the build system and code quality tools that support development.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation/Client Interface
    subgraph "Presentation/Client Interface"
        A1["index.html"]
        A2["assets/"]
        A3["styles/"]
    end

    %% Game Engine Core
    subgraph "Game Engine Core"
        B1["src/game.ts"]
        B2["build/bundle.js"]
    end

    %% Modules
    subgraph "Modules"
        subgraph "Scenes Module"
            C1["src/scenes/splash-scene.ts"]
            C2["src/scenes/game-controller.ts"]
        end
        subgraph "Actors Module"
            D1["src/actors/falling-tetramino.ts"]
            D2["src/actors/tetramino-factory.ts"]
            D3["src/actors/tetramino-stack.ts"]
        end
        E1["src/input-manager.ts"]
        subgraph "Message Manager"
            F1["src/message-manager/message-manager.ts"]
            F2["src/message-manager/messages.ts"]
        end
    end

    %% Service Worker & Offline Support
    subgraph "Service Worker & Offline Support"
        G1["src/service-worker.js"]
        G2["sw-toolbox.js"]
    end

    %% Build/Configuration Infrastructure
    subgraph "Build/Configuration Infrastructure"
        H1["package.json"]
        H2["webpack.config.js"]
        H3["tsconfig.json"]
        H4["tslint.json"]
    end

    %% Connections between layers
    %% Browser loads the game bundle and provides assets and styling
    A1 -- "loads" --> B2
    A2 -- "provides-assets" --> B1
    A3 -- "styles" --> A1

    %% Build process: webpack builds the bundle from game source
    H2 -- "builds" --> B2
    H1 --- H2
    H3 --- H2
    H4 --- H2

    %% Game Engine Core connects to Modules
    B1 -- "initializes" --> C1
    B1 -- "initializes" --> C2
    B1 -- "creates" --> D2
    B1 -- "processes" --> E1
    B1 -- "receives" --> F1

    %% Modules inter-communication
    E1 -- "sends-input" --> B1
    D1 -- "sends-event" --> F1
    D2 -- "sends-event" --> F1
    D3 -- "sends-event" --> F1
    F1 -- "notifies" --> C1
    F1 -- "notifies" --> C2

    %% Service Worker integration
    A1 -- "caches" --> G1
    A1 -- "caches" --> G2

    %% Assigning classes for color coding
    class A1,A2,A3 presentation
    class B1,B2,C1,C2,D1,D2,D3,E1,F1,F2 gameLogic
    class G1,G2,H1,H2,H3,H4 infra

click A1 "https://github.com/virgs/tetris/blob/master/index.html"
click A2 "https://github.com/virgs/tetris/tree/master/assets/"
click A3 "https://github.com/virgs/tetris/tree/master/styles/"
click B1 "https://github.com/virgs/tetris/blob/master/src/game.ts"
click B2 "https://github.com/virgs/tetris/blob/master/build/bundle.js"
click C1 "https://github.com/virgs/tetris/blob/master/src/scenes/splash-scene.ts"
click C2 "https://github.com/virgs/tetris/blob/master/src/scenes/game-controller.ts"
click D1 "https://github.com/virgs/tetris/blob/master/src/actors/falling-tetramino.ts"
click D2 "https://github.com/virgs/tetris/blob/master/src/actors/tetramino-factory.ts"
click D3 "https://github.com/virgs/tetris/blob/master/src/actors/tetramino-stack.ts"
click E1 "https://github.com/virgs/tetris/blob/master/src/input-manager.ts"
click F1 "https://github.com/virgs/tetris/blob/master/src/message-manager/message-manager.ts"
click F2 "https://github.com/virgs/tetris/blob/master/src/message-manager/messages.ts"
click G1 "https://github.com/virgs/tetris/blob/master/src/service-worker.js"
click G2 "https://github.com/virgs/tetris/blob/master/sw-toolbox.js"
click H1 "https://github.com/virgs/tetris/blob/master/package.json"
click H2 "https://github.com/virgs/tetris/blob/master/webpack.config.js"
click H3 "https://github.com/virgs/tetris/blob/master/tsconfig.json"
click H4 "https://github.com/virgs/tetris/blob/master/tslint.json"

classDef presentation fill:#ADD8E6,stroke:#000,stroke-width:1px;
classDef gameLogic fill:#90EE90,stroke:#000,stroke-width:1px;
classDef infra fill:#FFFF99,stroke:#000,stroke-width:1px;
```
