Architecture Overview
PlatformerGame follows Unity’s component-based architecture with these key patterns:- Event-Driven Communication - Decoupled systems using C# events
- Component-Based Design - Single-responsibility components
- ScriptableObject Pattern - Data-driven power-up system
- Unity Input System - Modern input handling
- Physics-Based Movement - Rigidbody2D-driven player control
Event-Driven Architecture
The project uses C# events extensively to decouple systems and maintain clean separation of concerns.Event Pattern Implementation
Jump Events
Jump Events
Source: Subscriber: Benefits:
Assets/Scripts/Player/PlayerJump.cs:26Assets/Scripts/MusicManager.cs:14PlayerJumpdoesn’t need to know about audio- Audio system can be added/removed without modifying player code
- Multiple systems can respond to the same event
Coin Collection Events
Coin Collection Events
Source: Subscribers:Key Feature: Event passes the coin instance, allowing subscribers to access
Assets/Scripts/Score/Coin.cs:7coin.ValueGame State Events
Game State Events
Source: Subscribers:
Assets/Scripts/SceneLogic/FinishGame.cs:8MusicManager.cs:16- Death soundScoreSystem.cs:14- Finalize score on deathScoreSystem.cs:15- Finalize score on completion
Event Subscription Pattern
All event subscribers follow this lifecycle pattern:- Prevents memory leaks from dangling references
- Handles scene transitions correctly
- Works with object pooling if implemented
- Safer than Awake/OnDestroy for component lifecycle
Component-Based Architecture
Each script has a single, well-defined responsibility.Player Components
The player GameObject uses multiple specialized components:PlayerInput.cs
PlayerInput.cs
Location: Features:
Assets/Scripts/Player/PlayerInput.cs:4Responsibility: Horizontal movement and character orientation- Receives input via
OnMove()callback (Unity Input System) - Updates horizontal velocity only (doesn’t touch Y axis)
- Handles character flipping based on direction
- Updates walk animation parameter
PlayerJump.cs
PlayerJump.cs
Location: Advanced Features:
Assets/Scripts/Player/PlayerJump.cs:4Responsibility: Vertical movement, jumping mechanics, wall sliding- Variable jump height (hold for higher jump)
- Double jump with 0.2s delay
- Wall sliding mechanics
- Physics-based gravity calculation
- Separate
OnJumpStarted()andOnJumpFinished()callbacks
CollisionDetection.cs
CollisionDetection.cs
Location: Provides clean collision queries for
Assets/Scripts/Player/CollisionDetection.csResponsibility: Ground and wall detection for jump systemUsage:PlayerJump logic.PlayerDeath.cs
PlayerDeath.cs
Location:
Assets/Scripts/Player/PlayerDeath.csResponsibility: Death state handling and visualsSeparates death logic from movement logic for clarity.Separation Benefits
Single Responsibility:- Each component can be tested independently
- Easy to modify jump without affecting movement
- Can disable wall-slide by removing component
CollisionDetectioncould be used by enemiesPlayerInputcould control different objects
- Bug in jumping? Check
PlayerJump.cs - Input issues? Check
PlayerInput.cs - Clear file locations for each concern
Unity Input System Integration
The project uses Unity’s new Input System for modern, flexible input handling.Input Actions Configuration
Location:Assets/InputSystem/BasicInput.inputactions
Input System Features
PlayerInputComponent Integration
PlayerInputComponent Integration
Automatic Callbacks: Unity’s Convention: Method name = “On” + ActionName (e.g., “Move” →
PlayerInput component automatically calls methods on scripts:OnMove())Multiple Input Bindings
Multiple Input Bindings
Actions support multiple control schemes:
- WASD for movement
- Arrow keys for movement
- Both work simultaneously
- Easy to add gamepad support
Direct Input System Access
Direct Input System Access
For immediate queries (like ESC key):Pattern:
Device.current.control.wasPressedThisFrameInput System Advantages
- Rebindable: Change controls without code changes
- Multiple Devices: Keyboard, gamepad, touch support
- Action-Based: Think in game actions, not raw inputs
- Event-Driven: Callbacks instead of polling in Update()
- Variable Jump: Separate started/finished events enable hold mechanics
ScriptableObject Pattern
Power-ups use ScriptableObjects for data-driven, designer-friendly implementation.Abstract Base Class
Location:Assets/Scripts/ScriptableObjectsScripts/Powerup.cs:3
Apply() logic.
Concrete Implementation
Location:Assets/Scripts/ScriptableObjectsScripts/JumpBoost.cs
Collection System
Location:Assets/Scripts/ScriptableObjectsScripts/PowerUpPickUp.cs:3
Pattern Benefits
Designer-Friendly
Designer-Friendly
- Create new power-ups in Unity Editor without code
- Tune values (JumpMultiplier) in Inspector
- Reuse effects across multiple pickup instances
- No compilation needed for value changes
Data-Driven
Data-Driven
Extensible
Extensible
Add new power-up types:No changes needed to
PowerUpPickUp.cs.Coding Conventions
FromCONVENTIONS.md, the project follows standard C# conventions:
Naming Conventions
| Element | Convention | Example |
|---|---|---|
| Classes | TitleCase | PlayerInput, ScoreSystem |
| Public Fields | TitleCase | public float Speed; |
| Private Fields | lowerCase | private float horizontalDirection; |
| Methods | TitleCase | Jump(), GetJumpForce() |
| Parameters | lowerCase | target, collision |
| Constants | lowerCase | const int maxValue = 8; |
Code Style
Key Rules
- Four spaces for indentation (no tabs)
- No trailing spaces
- All fields initialized
- One public class per file
- File name matches class name
System Communication Flow
Example: Collecting a coin Key Points:- Single trigger causes multiple system reactions
- Systems don’t reference each other directly
- Events enable loose coupling
- Easy to add new subscribers without modifying existing code