Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- moreeffectiveC++
- 선택정렬
- 언리얼엔진구조체
- stl
- 람다사용정렬
- 알고리즘
- 람다
- C++
- UE4 커스텀로그
- 약참조
- 스마트포인터
- BFS
- 자료구조
- unorder_map
- 프로그래머스
- enumasByue
- 크리티컬섹션
- UE_LOG
- C++최적화
- dataasset
- 강참조
- 데이터애셋
- 정렬
- map
- UELOG
- UML관련
- 델리게이트
- 언리얼가비지컬렉터
- 애셋로드
- 정렬알고리즘
Archives
- Today
- Total
기억을 위한 기록들
[UE CPP] 체력관리 컴포넌트 (HealthManageComponent) 본문
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "HealthManageComponent.generated.h"
DECLARE_MULTICAST_DELEGATE_OneParam(FUpdateHealthPoint, int);
DECLARE_MULTICAST_DELEGATE_OneParam(FAddtiveHealthPoint, int);
DECLARE_MULTICAST_DELEGATE_OneParam(FMinusHealthPoint, int);
DECLARE_MULTICAST_DELEGATE(FDeathDelegate);
class FHealthPoint
{
public:
FHealthPoint()
{
MaxHealth = TNumericLimits< uint16 >::Max();
CurHealth = MaxHealth;
}
FHealthPoint(const int newMaxHealth)
: MaxHealth(newMaxHealth),
CurHealth(newMaxHealth)
{}
FHealthPoint(const FHealthPoint& rhs)
: MaxHealth(rhs.MaxHealth),
CurHealth(rhs.CurHealth)
{}
void InitData(const int newMaxHealth)
{
MaxHealth = newMaxHealth;
CurHealth = newMaxHealth;
}
FORCEINLINE void ResetHP()
{
CurHealth = MaxHealth;
}
void AddHP(const int additiveValue)
{
if (MaxHealth < CurHealth + additiveValue)
{
CurHealth = MaxHealth;
}
else
{
CurHealth += additiveValue;
}
}
void MinHP(const int taragetValue)
{
if (CurHealth - taragetValue < MinHealth)
{
CurHealth = MinHealth;
}
else
{
CurHealth -= CurHealth;
}
}
FORCEINLINE bool IsZero() const {
return CurHealth == 0;
}
uint16 GetMaxHP() const { return MaxHealth; }
int GetCurrentHP() const { return CurHealth; }
private:
uint16 MaxHealth = 0;
int CurHealth = 0;
static const int MinHealth = 0;
};
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class STUDYCPP_API UHealthManageComponent : public UActorComponent
{
GENERATED_BODY()
public:
UHealthManageComponent();
protected:
virtual void BeginPlay() override;
UFUNCTION(BlueprintCallable, Category = "Manage")
void InitData(const int maxHP);
void InitData(const FHealthPoint& newHPData);
UFUNCTION(BlueprintCallable, Category="Manage")
void AddHP(const int addtiveHP);
UFUNCTION(BlueprintCallable, Category = "Manage")
void MinHP(const int minusHP);
UFUNCTION(BlueprintCallable, Category = "Manage")
void ResetHP();
UFUNCTION(BlueprintPure, Category = "Manage")
bool IsDead() const;
UFUNCTION(BlueprintPure, Category = "Manage")
int GetCurrentHP() const;
public:
FUpdateHealthPoint& OnUpdateHealthDelegate() {return UpdateHealthDelegate;}
FAddtiveHealthPoint& OnAddHealthDelegate() {return AddtiveHealthDelegate;}
FMinusHealthPoint& OnMinusHealthDelegate() {return MinusHealthDelegate;}
FDeathDelegate& OnDeathDelegate() {return DeathUpdateDelegate;}
private:
FUpdateHealthPoint UpdateHealthDelegate;
FAddtiveHealthPoint AddtiveHealthDelegate;
FMinusHealthPoint MinusHealthDelegate;
FDeathDelegate DeathUpdateDelegate;
TUniquePtr<FHealthPoint> mHealthPoint;
};
#include "HealthManageComponent.h"
UHealthManageComponent::UHealthManageComponent()
{
PrimaryComponentTick.bCanEverTick = false;
mHealthPoint = MakeUnique<FHealthPoint>();
}
// Called when the game starts
void UHealthManageComponent::BeginPlay()
{
Super::BeginPlay();
// 외부 데이터를 통해 hp 세팅
InitData(1000);
}
void UHealthManageComponent::InitData(const int maxHP)
{
mHealthPoint->InitData(maxHP);
UpdateHealthDelegate.Broadcast(GetCurrentHP());
}
void UHealthManageComponent::InitData(const FHealthPoint& newHPData)
{
mHealthPoint.Reset();
mHealthPoint = MakeUnique<FHealthPoint>(newHPData);
UpdateHealthDelegate.Broadcast(GetCurrentHP());
}
void UHealthManageComponent::AddHP(const int addtiveHP)
{
mHealthPoint->AddHP(addtiveHP);
UpdateHealthDelegate.Broadcast(GetCurrentHP());
AddtiveHealthDelegate.Broadcast(GetCurrentHP());
}
void UHealthManageComponent::MinHP(const int minusHP)
{
mHealthPoint->MinHP(minusHP);
UpdateHealthDelegate.Broadcast(GetCurrentHP());
MinusHealthDelegate.Broadcast(GetCurrentHP());
if (true == IsDead())
{
if (DeathUpdateDelegate.IsBound())
{
DeathUpdateDelegate.Broadcast();
}
}
}
void UHealthManageComponent::ResetHP()
{
mHealthPoint->ResetHP();
UpdateHealthDelegate.Broadcast(GetCurrentHP());
}
bool UHealthManageComponent::IsDead() const
{
return mHealthPoint->IsZero();
}
int UHealthManageComponent::GetCurrentHP() const
{
return mHealthPoint->GetCurrentHP();
}
'UnrealEngine > UnrealEngine C++ 관련' 카테고리의 다른 글
언리얼엔진 UI UMG 및 C++ 최적화에 관하여 (0) | 2024.07.21 |
---|---|
[UE-CPP]std::tie 함수와 언리얼에서의 tie (0) | 2023.07.23 |
[UE-CPP] FStruct를 TMap/TSet의 Key로 사용하고 싶다면?(with GetTypeHash) (0) | 2023.07.09 |
[UE-CPP] 부동소수점 비교시 (0) | 2023.05.14 |
[UE-C++] 특정 액터가 블루프린트 상속받은지 확인하는 방법 (0) | 2023.03.02 |