# dkheng/pvz-unity architecture

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

- Interactive diagram: https://gitdiagram.com/dkheng/pvz-unity
- Repository: https://github.com/dkheng/pvz-unity

## Overview

##### Main Components
- Game Scene & Engine – Represented by the GameScene in Assets/Scenes. This is where Unity’s runtime engine brings the game to life.
- Game Management Module – The central “GameManagement” object that uses several scripts (GameManagement.cs, LevelController.cs, LevelData.cs, etc.) to manage the overall level flow, level selection (0–4), and game state.
- Plants Module – This module encompasses the logic and behavior for plants, with separate scripts (e.g., PeaShooterSingle.cs, Squash.cs, SunFlower.cs, etc.) along with associated assets like animations and prefabs.
- Zombies Module – Similar to the Plants module, this module includes scripts for various types of zombies (e.g., BoneZombie.cs, ChineseZombie.cs, Ghost.cs, etc.) and their animation, sprites, and behavior.
- PlantBullets Subsystem – Manages projectile behavior (e.g., StraightBullet.cs, ThrowBullet.cs, FirePea.cs) and connects to associated animations, sound effects, and prefabs.
- UI Subsystem – Encompassing the user interface, including UI scripts (e.g., DialogPanel, CrazyDave.cs), prefabs for cards and dialog panels, and the sound and sprite elements that support the UI.
- Animation and Assets Layer – The project contains rich animation assets for plants, zombies, and UI (stored under Assets/Resources/Animations), managed by Unity’s Animator as well as sprites and prefab references.
- Sound & Media – Assets for background music, effect sounds, and voice snippets gathered within the Sounds folder.
- Supporting Systems – Including Sun management, which controls sun generation via scripts (e.g., SunManagement.cs and related scripts) and other environmental effects.

##### Relationships and Interactions
- The GameManagement module dynamically loads level-specific data (from LevelData.cs and LevelController.cs) and instantiates plants and zombies based on prefab definitions found in Resources/Prefabs.
- The Sound & Media system is integrated with most modules (UI, Plants, Zombies) for audio feedback. A connecting line should show that Sound assets are used by these modules.

##### Architectural Patterns & Design Principles
- Use a modular approach to clearly separate the gameplay logic (GameManagement, LevelData) from visual representations (Animation, Sprites, Prefabs).
- Follow the model-view-controller (MVC) idea: the game logic (models) is handled in GameManagement and submodules (Plants/Zombies controllers) while the view is represented by Unity’s scene, animations, and UI, and interactions are directed via input handlers.
- Dependency injection where GameManagement instantiates and manages levels, while each subsystem (e.g., Plants, Zombies, UI) is loosely coupled.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Runtime Engine Subgraph
    subgraph "Runtime Engine"
        GS["Game Scene & Engine"]:::gameLogic
        GM["Game Management Module"]:::gameLogic
        Sun["Supporting Systems (Sun Management)"]:::support
    end

    %% Gameplay Modules Subgraph
    subgraph "Gameplay Modules"
        Plants["Plants Module"]:::gameLogic
        Zombies["Zombies Module"]:::gameLogic
        PlantBullets["PlantBullets Subsystem"]:::gameLogic
    end

    %% UI Layer Subgraph
    subgraph "User Interface"
        UI["UI Subsystem"]:::ui
    end

    %% Assets & Media Subgraph
    subgraph "Assets & Media"
        AnimAssets["Animation and Assets Layer"]:::assets
        Sound["Sound & Media"]:::assets
    end

    %% Connections
    GS -->|"loads"| GM
    GM -->|"instantiates"| Plants
    GM -->|"instantiates"| Zombies
    GM <-->|"input_events"| UI
    GM -->|"controls_sun"| Sun
    Plants -->|"triggers_projectiles"| PlantBullets

    Plants -->|"uses_assets"| AnimAssets
    Zombies -->|"uses_assets"| AnimAssets
    PlantBullets -->|"uses_anim"| AnimAssets
    UI -->|"displays_anim"| AnimAssets

    Plants -->|"audio_feedback"| Sound
    Zombies -->|"audio_feedback"| Sound
    UI -->|"audio_feedback"| Sound

    %% Click Events
    click GS "https://github.com/dkheng/pvz-unity/blob/main/Assets/Scenes/GameScene.unity"
    click GM "https://github.com/dkheng/pvz-unity/tree/main/Assets/Resources/Scripts/GameManagement"
    click Plants "https://github.com/dkheng/pvz-unity/tree/main/Assets/Resources/Scripts/Plants"
    click Zombies "https://github.com/dkheng/pvz-unity/tree/main/Assets/Resources/Scripts/Zombies"
    click PlantBullets "https://github.com/dkheng/pvz-unity/tree/main/Assets/Resources/Scripts/PlantBullets"
    click UI "https://github.com/dkheng/pvz-unity/tree/main/Assets/Resources/Scripts/UI"
    click AnimAssets "https://github.com/dkheng/pvz-unity/tree/main/Assets/Resources/Animations"
    click Sound "https://github.com/dkheng/pvz-unity/tree/main/Assets/Resources/Sounds"
    click Sun "https://github.com/dkheng/pvz-unity/tree/main/Assets/Resources/Scripts/Sun"

    %% Styles
    classDef gameLogic fill:#a3d5ff,stroke:#0b5f8a,stroke-width:2px;
    classDef assets fill:#c8e6c9,stroke:#388e3c,stroke-width:2px;
    classDef ui fill:#ffcdd2,stroke:#d32f2f,stroke-width:2px;
    classDef support fill:#ffe082,stroke:#fbc02d,stroke-width:2px;
```
