This repository has been archived on 2025-04-18. You can view files and clone it, but cannot push or open issues or pull requests.
LegumeMix/Source/LegumeMix/Public/Player/LMHealthComponent.h

106 lines
3.3 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "LMHealthComponent.generated.h"
class ULMHitBox;
UCLASS(Blueprintable, ClassGroup=(Legumix), meta=(BlueprintSpawnableComponent))
class LEGUMEMIX_API ULMHealthComponent : public UActorComponent
{
GENERATED_BODY()
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnHealthChangedSignature, float, Health, float, Damage);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnDeathSignature);
public:
ULMHealthComponent();
virtual void BeginPlay() override;
void Kill();
public:
FORCEINLINE float GetHealth() const { return Health; }
/**
* Applies an amount of damage to the component.
* @param Damage The damage to apply the Health to.
*/
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category=Legumix)
void TakeDamage(float Damage);
/**
* Called automatically when the component hits zero health.
*/
UFUNCTION(BlueprintCallable, BlueprintImplementableEvent, Category=Legumix)
void OnKill();
/**
* Register all hitboxes associated with this component owner.
*/
UFUNCTION(BlueprintCallable, Category=Legumix, CallInEditor)
void RegisterHitBoxes();
/**
* Called automatically when a hitbox is hit.
* @param HitBox The hitbox that was hit.
* @param Damage The damage received by the hitbox.
*/
UFUNCTION()
void HitBoxHit(ULMHitBox* HitBox, float Damage);
/**
* Applies the amount of damage received to the component.
*
* Called automatically when one of this component's LMHitBox is hit.
* @param Damage The damage received.
*/
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category=Legumix)
void OnHit(float Damage);
/**
* Adds an amount of health to the character.
* @param AddedHealth The amount of health to add.
* @param NoLimits If sets, ignore the component's MaxHealth value.
*/
UFUNCTION(BlueprintCallable, Category=Legumix, meta=(Keywords = "Refill, Health"))
void AddHealth(const float AddedHealth, const bool NoLimits = false) { Health = FMath::Clamp(Health + AddedHealth, 0.0f, NoLimits ? INT_MAX : MaxHealth); }
/**
* Completely refill the health of the component.
*/
UFUNCTION(BlueprintCallable, Category=Legumix, meta=(Keywords = "Refill, Health"))
void FillHealth() { Health = MaxHealth; }
public:
/** Delegate called when this component receives damages. */
UPROPERTY(BlueprintAssignable, Category=Legumix)
FOnHealthChangedSignature OnHealthChanged;
/** Delegate called when this component is killed, aka. Health reaches 0.*/
UPROPERTY(BlueprintAssignable, Category=Legumix)
FOnDeathSignature OnDeath;
private:
/**
* If set, the Health will be initialized to MaxHealth.
*/
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Legumix, meta=(AllowPrivateAccess=true))
bool bMaxHealthOnStart = true;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Legumix, meta=(AllowPrivateAccess=true, ClampMin=0, UIMin=0))
float Health = 100.f;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Legumix, meta=(AllowPrivateAccess=true, ClampMin=0, UIMin=0, UIMax=1000))
float MaxHealth = 100.f;
/**
* A list of all associated LMHitboxes.
*/
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Legumix, meta=(AllowPrivateAccess=true))
TArray<TObjectPtr<ULMHitBox>> HitBoxes;
};