Skip to content

Combat Stat Component

Krish edited this page Sep 2, 2026 · 5 revisions

Usage:

Component used to store information related to combat such as health, attack, movementSpeed and attackSpeed. Any entities which engages it's combat should have an instance of this class registered. This class can be extended for more specific combat needs.

Fields:

  • health : current health, minimum bound 0
  • maxHealth: the upper boud for health which can be set via construction or (via setMaxHealth)
  • baseAttack : base attack damage
  • movementSpeed : movement speed (in Sprint 1 only used by player using overloaded constructor)
  • attackSpeed : attack Speed multiplier (for attack cooldown, currently related to player only)

Constructors:

  • CombatStatsComponent(int health, int baseAttack) : used by enemies; movementSpeed/attackSpeed default to 0
  • CombatStatsComponent(int health, int baseAttack, float movementSpeed, float attackSpeed) : used by the player

Methods:

getter and setter methods for all fields. Additional methods:

  • isDead : return true if health = 0.
  • setHealth(int health): clamps to [0, maxHealth]; triggers updateHealth, and entityDied if health transitions to 0 from some positive health
  • Add more health, movementSpeed and attackSpeed methods: can be used to increase current health by items/charms. E.g addHealth(20) if player current health is 70, increases it to 90
  • takeDamage(int damage) / takeDamage(int damage, Entity attacker): reduces health and triggers a hit reaction
  • hit(CombatStatComponent attacker) : applies attacker's baseAttack as damage to the entity calling this event. E.g enemyStat.hit(playerStat) reduces enemy's health by player's current base attack.

Events Triggered:

  • updateHealth, updateBaseAttack, updateMovementSpeed, updateMaxHealth, updateAttackSpeed events are triggered when stat of the player changes and used by Player Stat Display.
  • entityDied thie event triggered when health field reaches zero.

Example Usage:

// Enemy without movementSpeed/attackSpeed needed
Entity enemy = new Entity()
    .addComponent(new CombatStatsComponent(50, 10));

// Player has now movementSpeed and attackSpeed
Entity player = new Entity()
    .addComponent(new CombatStatsComponent(
        stats.health, stats.baseAttack, stats.movementSpeed, stats.attackSpeed));

// Reading/modifying stats (e.g. a charm boosting movement speed)
CombatStatsComponent playerStats = player.getComponent(CombatStatsComponent.class);
playerStats.addMovementSpeed(1.5f); // e.g. speed charm buff

// Applying damage from an attacker
enemyStats.hit(playerStats); // reduces enemy health by player's baseAttack

Testing

Junit : All of the methods develop by player team has their Junit tests in CombatStatComponentTest.java Example: shouldSetGetHealth: health clamps correctly at both bounds shouldCheckIsDead: isDead() reflects health == 0

Manual Testing: Player health and Strength(attack speed) updates when player deal with enemies in game and base attack increases when player pick up the charm using E keys. This can be seen at the top left corner where player stats displays.

UML Diagram

classDiagram
    class Component {
        <abstract>
    }

    class CombatStatsComponent {
        -int health
        -int maxHealth
        -int baseAttack
        -float movementSpeed
        -float attackSpeed
        +CombatStatsComponent(health, baseAttack)
        +CombatStatsComponent(health, baseAttack, movementSpeed, attackSpeed)
        +isDead() Boolean
        +getHealth() int
        +getMaxHealth() int
        +setMaxHealth(health) void
        +setHealth(health) void
        +addHealth(health) void
        +getBaseAttack() int
        +setBaseAttack(attack) void
        +addBaseAttack(attack) void
        +getMovementSpeed() float
        +setMovementSpeed(newSpeed) void
        +addMovementSpeed(speed) void
        +getAttackSpeed() float
        +setAttackSpeed(newSpeed) void
        +addAttackSpeed(speed) void
        +takeDamage(damage) void
        +takeDamage(damage, attacker) void
        +hit(attacker) void
        +applyHitReaction(attacker) void
    }

    Component <|-- CombatStatsComponent
Loading

Table of Contents

Home

Test Plan

Team 2

Features

Components
Player
Items and Inventory
Enemies
Interactable NPCs
Cutscenes and Dialogue
Music
Bosses
Rooms
Weapons
Abilities & Spells
Game Screens and Areas
Dungeons

Game Engine

Getting Started

Entities and Components

Service Locator

Loading Resources

Logging

Unit Testing

Debug Terminal

Input Handling

UI

Animations

Audio

AI

Physics

Terrain

Concurrency & Threading

Settings

Troubleshooting

MacOS Setup Guide

Clone this wiki locally