ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Actor Components: Health Component Damage Calculation

C++ (Unreal)

Goal -- WPM

Ready
Exercise Algorithm Area
1class UHealthComponent : public UActorComponent
2{
3GENERATED_BODY()
4
5public:
6// Current health of the actor.
7UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Health")
8float CurrentHealth;
9
10// Maximum health of the actor.
11UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Health")
12float MaxHealth;
13
14// Armor value, reduces incoming physical damage.
15UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Defense")
16float Armor;
17
18// Resistance value, reduces incoming magical damage.
19UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Defense")
20float MagicResistance;
21
22UHealthComponent()
23{
24PrimaryComponentTick.bCanEverTick = false; // No tick needed for this logic
25CurrentHealth = 100.0f;
26MaxHealth = 100.0f;
27Armor = 10.0f;
28MagicResistance = 5.0f;
29}
30
31// Applies damage to the actor, considering armor and resistance.
32// BaseDamage: The raw damage amount before mitigation.
33// DamageType: The type of damage (e.g., Physical, Magical).
34// Returns the actual damage taken after mitigation.
35UFUNCTION(BlueprintCallable, Category = "Health")
36float TakeDamage(float BaseDamage, FString DamageType)
37{
38if (BaseDamage <= 0.0f)
39{
40return 0.0f; // No damage if base damage is zero or negative.
41}
42
43float DamageMitigation = 0.0f;
44FString LowercaseDamageType = DamageType.ToLower();
45
46if (LowercaseDamageType == "physical")
47{
48DamageMitigation = Armor;
49}
50else if (LowercaseDamageType == "magical")
51{
52DamageMitigation = MagicResistance;
53}
54// Add more damage types and their respective resistances here if needed.
55
56// Calculate final damage: BaseDamage - Mitigation. Ensure damage doesn't go below a minimum (e.g., 1).
57float FinalDamage = FMath::Max(1.0f, BaseDamage - DamageMitigation);
58
59// Apply damage to current health.
60CurrentHealth = FMath::Max(0.0f, CurrentHealth - FinalDamage);
61
62// Broadcast a delegate or event here if health changes.
63
64return FinalDamage;
65}
66
67// Helper function to heal the actor.
68UFUNCTION(BlueprintCallable, Category = "Health")
69void Heal(float Amount)
70{
71if (Amount <= 0.0f)
72{
73return; // Cannot heal with zero or negative amount.
74}
75CurrentHealth = FMath::Min(MaxHealth, CurrentHealth + Amount);
76}
77
78// Helper function to check if the actor is dead.
79UFUNCTION(BlueprintPure, Category = "Health")
80bool IsDead() const
81{
82return CurrentHealth <= 0.0f;
83}
84};
Algorithm description viewbox

Actor Components: Health Component Damage Calculation

Algorithm description:

This C++ code defines a `UHealthComponent` for Unreal Engine actors, managing their health and defense. The core functionality is the `TakeDamage` function, which calculates the final damage dealt to an actor after applying mitigation based on armor or magic resistance, depending on the damage type. It also includes helper functions for healing and checking if the actor is dead. This component is essential for any game character or object that can receive damage.

Algorithm explanation:

The `UHealthComponent` class manages an actor's health and defense. The `TakeDamage` function is the primary logic for calculating damage. It first checks if the `BaseDamage` is valid (greater than 0). It then determines the `DamageMitigation` based on the `DamageType` (case-insensitively compared to "physical" or "magical"), applying `Armor` or `MagicResistance` respectively. The `FinalDamage` is calculated by subtracting mitigation from base damage, with a minimum of 1 damage to prevent trivial negation. This `FinalDamage` is then subtracted from `CurrentHealth`, ensuring health does not drop below 0. The `Heal` function increases `CurrentHealth` up to `MaxHealth`. `IsDead` simply checks if `CurrentHealth` is 0 or less. The time complexity for `TakeDamage`, `Heal`, and `IsDead` is O(1) as they involve a fixed number of arithmetic operations and comparisons. Space complexity is O(1) as it only uses a few member variables.

Pseudocode:

Class HealthComponent:
  CurrentHealth, MaxHealth, Armor, MagicResistance

  Function TakeDamage(BaseDamage, DamageType):
    If BaseDamage <= 0:
      Return 0

    Mitigation = 0
    LowercaseDamageType = ToLowercase(DamageType)
    If LowercaseDamageType == "physical":
      Mitigation = Armor
    Else if LowercaseDamageType == "magical":
      Mitigation = MagicResistance

    FinalDamage = Max(1.0, BaseDamage - Mitigation)
    CurrentHealth = Max(0.0, CurrentHealth - FinalDamage)
    Return FinalDamage

  Function Heal(Amount):
    If Amount <= 0:
      Return
    CurrentHealth = Min(MaxHealth, CurrentHealth + Amount)

  Function IsDead():
    Return CurrentHealth <= 0