Class Overview
The Player class is the core of the player-controlled character in a Unity combat system. It extends the base Character class and adds specific functionality for:
- Health management and UI updates
- Combat mechanics (attacks, damage calculation)
- Enemy targeting and switching
- Buff system (shield, healing, damage boosts)
- Game state management (victory/defeat conditions)
Properties & Fields
Type: TMP_Text
Displays the player's health in the UI using TextMeshPro.
Type: TMP_Text
Displays the current enemy's health in the UI.
Type: Enemy
References to enemy objects in the scene. enemy1 is activated first.
Type: Enemy
The currently active enemy being targeted.
Type: float
Multiplier for damage calculation. Can be boosted to 3x with damage buff.
Type: int
Base attack damage before multipliers (default: 10).
Type: bool
Tracks if the shield buff is currently active.
Type: bool
Tracks if the damage buff is currently active (3x multiplier).
Methods
Unity's initialization method. Sets up button listeners, initializes enemies, and prepares the combat system.
Decreases health and updates UI. If health reaches 0, player dies and UI is disabled.
Calculates damage using baseDamage and damageMultiplier, applies it to current enemy, and triggers enemy retaliation.
Activates or deactivates the shield buff. When active, should reduce incoming damage (implementation needed).
Heals the player by 20 health points (up to maximum of 100). Updates health UI immediately.
Sets damageMultiplier to 3.0 when active, or 1.0 when inactive. Greatly increases attack power.
When enemy1 is defeated, activates enemy2. When enemy2 is defeated, declares victory.
Inheritance Hierarchy
Character
Abstract Base Class
Properties:
- health (protected)
- maxHealth (protected)
- Health (public readonly)
- IsDead (public readonly)
Methods:
- TakeDamage(int)
- Heal(int)
- Die()
- ResetCharacter()
Player : Character
Concrete Implementation
New Properties:
- healthText (TMP_Text)
- enemyHealthText (TMP_Text)
- damageMultiplier (float)
- baseDamage (int)
- currentEnemy (Enemy)
Overrides & Extends:
- new TakeDamage(int) - Adds UI updates
- Adds combat system methods
- Adds buff system methods
- Adds enemy management
Enemy also inherits from Character
Combat interaction through Player class
Inheritance Notes
Method Hiding
Player uses new void TakeDamage() to hide the base implementation and add UI updates.
Virtual Methods
Character's TakeDamage() and Die() are virtual for easy overriding.
Protected Members
health and maxHealth are protected for derived class access.
Abstract vs Concrete
Character is designed as a base class; Player adds concrete combat implementation.
Example Usage
Here's how to use the Player class in a GameManager or other controller:
using UnityEngine; // 1. Define the base Character class (abstract) public abstract class Character : MonoBehaviour { protected int health = 100; public int Health => health; public virtual void TakeDamage(int damage) { health -= damage; Debug.Log($"Base: Took {damage} damage"); } }
// 2. Player inherits from Character public class Player : Character { // Hides base method with new implementation public new void TakeDamage(int damage) { Debug.Log("Player: Updating UI before taking damage"); // Can still call base method if needed base.TakeDamage(damage); // Player-specific logic Debug.Log("Player: Updating health display"); } public void SpecialAttack() { // Has access to protected 'health' field Debug.Log($"Player health: {health}"); } }
// 3. Usage example public class GameManager : MonoBehaviour { void Start() { // Create player (inherits from Character) Player player = new Player(); // Can access Character properties int playerHealth = player.Health; // From Character // Calls Player's TakeDamage (hides Character's) player.TakeDamage(20); // Player-specific method player.SpecialAttack(); // Polymorphism example Character characterRef = player; // Upcasting characterRef.TakeDamage(10); // Calls Character's version! // Downcasting back to Player if (characterRef is Player p) { p.SpecialAttack(); // Now we can use Player methods } } }
using UnityEngine; using TMPro; public class GameController : MonoBehaviour { public Player player; public Enemy enemy1, enemy2; public TMP_Text gameStatusText; void Start() { // Set up player references player.enemy1 = enemy1; player.enemy2 = enemy2; // Initialize UI player.UpdateUI(); player.UpdateEnemyUI(); // Simulate game events StartCoroutine(GameSequence()); } IEnumerator GameSequence() { // Player attacks first enemy yield return new WaitForSeconds(1); player.AttackButtonClicked(); // Player activates shield yield return new WaitForSeconds(0.5); player.ToggleShield(); // Player uses healing buff when low health yield return new WaitForSeconds(1); player.ActivateHealingBuff(); // Activate damage buff for powerful attack player.ActivateDamageBuff(); yield return new WaitForSeconds(0.5); player.AttackButtonClicked(); } }
Combat Flow
Player Combat Sequence
Initialize enemies and UI
Choose: Attack, Shield, Heal, or Damage Buff
Enemy counter-attacks (if alive)
Enemy defeated? Switch or win
Continue combat or declare victory