How dkheng/pvz-unity works
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.