# platzhersh/pacman-canvas architecture

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

- Interactive diagram: https://gitdiagram.com/platzhersh/pacman-canvas
- Repository: https://github.com/platzhersh/pacman-canvas

## Overview

##### Main Components

a. Frontend Layer:
- Main HTML5 Canvas component where the game renders
- CSS styling components (pacman-canvas.css, style.css)

b. Game Engine Layer:
- Core Game Logic (pacman-canvas.js)
- Entity Components:
- Pacman (js/pacman/pacman.js)
- Ghosts (js/pacman/ghost.js)
- Virtual Joystick (js/virtualjoystick.js)

c. Resource Management Layer:
- Audio Assets (mp3 and wav directories)
- Visual Assets (img directory)
- Fonts (fonts directory)
- Data (data/map.json)

d. Backend Services:
- Express.js Server (server.js)
- Database Handler (data/db-handler.php)

##### Component Relationships

- User Input → Game Engine
- Game Engine → Canvas Rendering
- Game Engine ↔ Resource Management
- Game Engine ↔ Backend Services (for highscores)

##### Architectural Patterns to Highlight

- Client-side game loop architecture
- Asset loading and caching system (cache.manifest)
- Event-driven input handling
- State management for game entities

##### Implementation Details to Note

Create sections showing:
- Game state management
- Ghost AI pathfinding
- Collision detection system
- Score tracking and validation

##### Server Layer (Express, Database)

- Component types
- Data flow directions
- State transitions
- Asset loading paths

Remember to indicate that this is a transitional architecture, as mentioned in the disclaimer about migration to TypeScript and React in the future.

This system design should help developers understand both the current architecture and potential areas for the planned migration to React and TypeScript.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph UI
        Canvas["HTML5 Canvas"]:::frontend
        Controls["Game Controls"]:::frontend
        Styles["CSS Styles"]:::frontend
        click Styles "https://github.com/platzhersh/pacman-canvas/blob/master/pacman-canvas.css"
    end

    subgraph GameEngine
        CoreLogic["Core Game Logic"]:::engine
        click CoreLogic "https://github.com/platzhersh/pacman-canvas/blob/master/pacman-canvas.js"
        
        subgraph Entities
            Pacman["Pacman Entity"]:::entity
            Ghost["Ghost Entity"]:::entity
            VJoystick["Virtual Joystick"]:::entity
            click Pacman "https://github.com/platzhersh/pacman-canvas/blob/master/js/pacman/pacman.js"
            click Ghost "https://github.com/platzhersh/pacman-canvas/blob/master/js/pacman/ghost.js"
            click VJoystick "https://github.com/platzhersh/pacman-canvas/blob/master/js/virtualjoystick.js"
        end

        GameState["Game State Manager"]:::engine
        CollisionSystem["Collision Detection"]:::engine
        PathFinding["Ghost AI Pathfinding"]:::engine
    end

    subgraph Resources
        Assets["Asset Manager"]:::resource
        subgraph AssetTypes
            Audio["Audio Files"]:::asset
            Images["Image Files"]:::asset
            Fonts["Font Files"]:::asset
            MapData["Map Data"]:::asset
            click Audio "https://github.com/platzhersh/pacman-canvas/tree/master/mp3/"
            click Images "https://github.com/platzhersh/pacman-canvas/tree/master/img/"
            click Fonts "https://github.com/platzhersh/pacman-canvas/tree/master/fonts/"
            click MapData "https://github.com/platzhersh/pacman-canvas/blob/master/data/map.json"
        end
        Cache["Application Cache"]:::resource
        click Cache "https://github.com/platzhersh/pacman-canvas/blob/master/cache.manifest"
    end

    subgraph Backend
        Server["Express Server"]:::backend
        Database["Database Handler"]:::backend
        BuildSystem["Webpack Build"]:::backend
        click Server "https://github.com/platzhersh/pacman-canvas/blob/master/server.js"
        click Database "https://github.com/platzhersh/pacman-canvas/blob/master/data/db-handler.php"
        click BuildSystem "https://github.com/platzhersh/pacman-canvas/blob/master/webpack.config.js"
    end

    %% Relationships
    Controls --> CoreLogic
    CoreLogic --> Canvas
    CoreLogic <--> GameState
    CoreLogic <--> CollisionSystem
    CoreLogic <--> PathFinding
    CoreLogic <--> Entities
    CoreLogic <--> Assets
    Assets <--> AssetTypes
    Assets <--> Cache
    CoreLogic <--> Server
    Server <--> Database

    %% Styles
    classDef frontend fill:#3498db,stroke:#2980b9,color:white
    classDef engine fill:#2ecc71,stroke:#27ae60,color:white
    classDef entity fill:#27ae60,stroke:#219a52,color:white
    classDef resource fill:#f1c40f,stroke:#f39c12,color:black
    classDef asset fill:#f39c12,stroke:#e67e22,color:black
    classDef backend fill:#e74c3c,stroke:#c0392b,color:white

    %% Legend
    subgraph Legend
        Frontend["Frontend Component"]:::frontend
        Engine["Game Engine"]:::engine
        Resource["Resource Manager"]:::resource
        BackendService["Backend Service"]:::backend
    end
```
