125 lines
4.2 KiB
C++
125 lines
4.2 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
#pragma once
|
|
|
|
#include "CoreMinimal.h"
|
|
#include "LMTeam.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);
|
|
DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FOnHitSignature, float, Damage, const FVector&, Origin, const FHitResult&, HitInfo);
|
|
|
|
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.
|
|
* @param Origin The origin of the damage.
|
|
* @param HitInfo Information about the ray that hit the hitbox.
|
|
*/
|
|
UFUNCTION()
|
|
void HitBoxHit(ULMHitBox* HitBox, float Damage, const FVector& Origin, const FHitResult& HitInfo);
|
|
|
|
/**
|
|
* 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 OnDamageReceived(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); }
|
|
|
|
/**
|
|
* Event automatically called when the character receives damages from an attack source.
|
|
* @param Damage The damage received.
|
|
* @param Origin The origin of the damage.
|
|
* @param HitInfo The collisions informations.
|
|
*/
|
|
UPROPERTY(BlueprintCallable, BlueprintAssignable, Category=Legumix)
|
|
FOnHitSignature OnHit;
|
|
|
|
/**
|
|
* Completely refill the health of the component.
|
|
*/
|
|
UFUNCTION(BlueprintCallable, Category=Legumix, meta=(Keywords = "Refill, Health"))
|
|
void FillHealth() { Health = MaxHealth; }
|
|
|
|
UFUNCTION(BlueprintCallable, Category=Legumix, meta=(Keywords = "Team"))
|
|
bool CanBeHurtByTeam(const ETeam AttackingTeam) const { return Team != AttackingTeam; }
|
|
|
|
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;
|
|
|
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Legumix, meta=(AllowPrivateAccess=true))
|
|
ETeam Team = ETeam::ET_Enemy;
|
|
|
|
/**
|
|
* A list of all associated LMHitboxes.
|
|
*/
|
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Legumix, meta=(AllowPrivateAccess=true))
|
|
TArray<TObjectPtr<ULMHitBox>> HitBoxes;
|
|
};
|