Class Overview

Summary: Represents the player character, handling health, UI updates, and combat mechanics. Manages enemy interactions, buff systems (shield, healing, damage), and controls the flow of combat through a turn-based system.

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

healthText
Public

Type: TMP_Text

Displays the player's health in the UI using TextMeshPro.

enemyHealthText
Public

Type: TMP_Text

Displays the current enemy's health in the UI.

enemy1, enemy2
Public

Type: Enemy

References to enemy objects in the scene. enemy1 is activated first.

currentEnemy
Private

Type: Enemy

The currently active enemy being targeted.

damageMultiplier
Public

Type: float

Multiplier for damage calculation. Can be boosted to 3x with damage buff.

baseDamage
Public

Type: int

Base attack damage before multipliers (default: 10).

shieldActive
Private

Type: bool

Tracks if the shield buff is currently active.

damageBuffActive
Private

Type: bool

Tracks if the damage buff is currently active (3x multiplier).

Methods

Start()
Public
Initializes the player, setting up UI and activating the first enemy.

Unity's initialization method. Sets up button listeners, initializes enemies, and prepares the combat system.

TakeDamage(int damage)
Public
Reduces player health when taking damage.
damage Amount of damage received from enemy attacks

Decreases health and updates UI. If health reaches 0, player dies and UI is disabled.

AttackButtonClicked()
Private
Handles the attack action, dealing damage to the enemy.

Calculates damage using baseDamage and damageMultiplier, applies it to current enemy, and triggers enemy retaliation.

ToggleShield()
Private
Toggles shield activation.

Activates or deactivates the shield buff. When active, should reduce incoming damage (implementation needed).

ActivateHealingBuff()
Private
Restores health using a healing buff.

Heals the player by 20 health points (up to maximum of 100). Updates health UI immediately.

ActivateDamageBuff()
Private
Toggles damage buff activation.

Sets damageMultiplier to 3.0 when active, or 1.0 when inactive. Greatly increases attack power.

OnEnemyDefeated()
Public
Handles enemy defeat and determines next actions.

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()
Inherits from
Player Class

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 Class
Player Class

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

1. Combat Start
Initialize enemies and UI
2. Player Turn
Choose: Attack, Shield, Heal, or Damage Buff
3. Enemy Retaliation
Enemy counter-attacks (if alive)
4. Check Conditions
Enemy defeated? Switch or win
5. Repeat or End
Continue combat or declare victory