Animal Health System - UQcsse3200/2024-studio-1 GitHub Wiki

Animal Health System

Description

The Animal Health System in our game provides a comprehensive framework for managing Non-Player Character (NPC) health, death animations, and health bar visualization. This system enhances the game's visual feedback and contributes to a more immersive player experience.

Components

1. CombatStatsComponent

The CombatStatsComponent is the core component that manages the health and combat-related statistics for both NPCs and the player.

Key Features:

  • Manages health, max health, and attack values
  • Handles damage calculations and health modifications
  • Triggers events when an entity dies

2. NPCDeathHandler

The NPCDeathHandler component manages the death process for NPCs.

WhatsAppVideo2024-10-15at12 29 33_d905d613-ezgif com-crop (1)

Key Features:

  • Plays a death animation when the NPC's health reaches zero
  • Disables physics, AI, hitbox, and collider components upon death
  • Removes the entity from the game after the death animation completes
  • Triggers events for player score and coin collection

3. NPCHealthBarComponent

The NPCHealthBarComponent renders a visual health bar above NPCs.

WhatsAppVideo2024-10-15at12 29 33_d905d613-ezgif com-crop

Key Features:

  • Displays current health as a percentage of maximum health
  • Uses color-coding (red for empty, green for filled) for intuitive visualization

Logic and Working

  1. Health Management:

    • The CombatStatsComponent manages the NPC's health.
    • When health reaches zero, it triggers the "died" event.
  2. Death Handling:

    • The NPCDeathHandler listens for the "died" event.
    • Upon death, it plays the death animation, disables physics and AI, and schedules the entity's removal.
    • It also triggers events for player score and coin collection.
  3. Health Bar Visualization:

    • The NPCHealthBarComponent continuously renders the health bar above the NPC.
    • It calculates the health percentage and draws a proportional green bar over a red background.

UML Diagrams

Class Diagram

classDiagram
    class Component {
        +create()
        +update()
        +dispose()
    }
    class CombatStatsComponent {
        -int health
        -int maxHealth
        -int baseAttack
        +getHealth()
        +setHealth(int)
        +hit(CombatStatsComponent)
    }
    class NPCDeathHandler {
        -boolean isDead
        -Entity target
        -int npcStrength
        +onDeath()
    }
    class NPCHealthBarComponent {
        -ShapeRenderer shapeRenderer
        +draw(SpriteBatch)
    }
    class Entity {
        +addComponent(Component)
        +getComponent(Class)
    }
    Component <|-- CombatStatsComponent
    Component <|-- NPCDeathHandler
    Component <|-- NPCHealthBarComponent
    Entity "1" *-- "many" Component : has

Sequence Diagram

sequenceDiagram
    participant Player
    participant NPC
    participant CombatStats
    participant DeathHandler
    participant HealthBar
    
    Player->>NPC: Attacks
    NPC->>CombatStats: Receive damage
    CombatStats->>CombatStats: Update health
    CombatStats->>HealthBar: Update display
    alt Health <= 0
        CombatStats->>DeathHandler: Trigger "died" event
        DeathHandler->>NPC: Play death animation
        DeathHandler->>NPC: Disable components
        DeathHandler->>Player: Update score/coins
        DeathHandler->>NPC: Remove entity (after delay)
    end

Integration Guide

To integrate these components into an NPC entity:

  1. Add the components to your NPC entity creation method:
Entity createNPC(Entity player, int strength) {
    return new Entity()
        .addComponent(new CombatStatsComponent(100, 10))  // health, attack
        .addComponent(new NPCDeathHandler(player, strength))
        .addComponent(new NPCHealthBarComponent())
        // ... other components
}
  1. Ensure your NPC entity has an AnimationRenderComponent with a "death" animation for the death sequence to work properly.

  2. Make sure to call the draw() method of NPCHealthBarComponent in your rendering loop.

Design Considerations

  • Modularity: The separation of combat stats, death handling, and health bar rendering into distinct components allows for easy reuse and maintenance.
  • Performance: The use of ShapeRenderer for health bars is efficient for rendering multiple NPCs.
  • Extensibility: The current design allows for easy addition of new features, such as different types of status effects or more complex health systems.

Recent Changes and Improvements

  1. Enhanced Death Handling: The NPCDeathHandler now disables multiple components (AI, physics, hitbox, collider) when the NPC dies, ensuring comprehensive cleanup.

  2. Coin Collection Integration: Upon NPC death, a "collectCoin" event is triggered with the NPC's strength as the parameter, allowing for dynamic reward systems.

  3. Centralized Entity Disposal: Entity removal now uses the GameAreaService, providing a more centralized and consistent approach to entity management.

  4. Improved Health Bar Rendering: The NPCHealthBarComponent now uses a more efficient rendering approach, improving performance for games with many NPCs.

These changes have resulted in a more robust, performant, and visually appealing system for managing NPC health and providing feedback to the player. The revised architecture allows for easier expansion and maintenance of the Animal Health System, aligning well with our goals of creating an immersive and responsive game environment.


⬅ Back to Animal Overview