Adding animations and updates to health component
This commit is contained in:
89
Source/OpenConflict/PlayerCharacter/Components/DDIHealth.cpp
Normal file
89
Source/OpenConflict/PlayerCharacter/Components/DDIHealth.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
// Fill out your copyright notice in the Description page of Project Settings.
|
||||
|
||||
|
||||
#include "DDIHealth.h"
|
||||
|
||||
// Sets default values for this component's properties
|
||||
UDDIHealth::UDDIHealth()
|
||||
{
|
||||
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
|
||||
// off to improve performance if you don't need them.
|
||||
PrimaryComponentTick.bCanEverTick = true;
|
||||
MaxHealth = 0;
|
||||
CurrentHealth = 0;
|
||||
ClassName = ClassNames::Scout;
|
||||
HealTickTime = 0.2f;
|
||||
HealDelayTime = 1.5f;
|
||||
|
||||
}
|
||||
|
||||
// Called when the game starts
|
||||
void UDDIHealth::BeginPlay()
|
||||
{
|
||||
Super::BeginPlay();
|
||||
|
||||
FString classNameString = UEnum::GetValueAsName(ClassName).ToString();
|
||||
TArray<FString> ParsedStrings;
|
||||
|
||||
// Split the string by the comma delimiter
|
||||
classNameString.ParseIntoArray(ParsedStrings, TEXT("::"), true);
|
||||
|
||||
if (FHealthSegment* Segment = HealthSegmentTable->FindRow<FHealthSegment>(FName(ParsedStrings[1]), FString("HealthSegments")))
|
||||
{
|
||||
HealthSegments = Segment->SegmentList;
|
||||
FString output = "";
|
||||
for (int seg : Segment->SegmentList)
|
||||
output.Append(seg + ", ");
|
||||
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, "");
|
||||
}
|
||||
|
||||
for (int seg : HealthSegments)
|
||||
MaxHealth += seg;
|
||||
|
||||
CurrentHealth = MaxHealth;
|
||||
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Cyan, FString::Printf(TEXT("CurrentHealth: %d"), CurrentHealth));
|
||||
}
|
||||
|
||||
|
||||
// Called every frame
|
||||
void UDDIHealth::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
|
||||
{
|
||||
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
|
||||
|
||||
if (CurrentHealth < MaxHealth && !HealTimer.IsValid())
|
||||
{
|
||||
GetWorld()->GetTimerManager().SetTimer(HealTimer, this, &UDDIHealth::Heal, HealTickTime, true, HealDelayTime);
|
||||
int tempHealthPool = 0;
|
||||
for (int seg : HealthSegments)
|
||||
{
|
||||
if (CurrentHealth >= tempHealthPool)
|
||||
tempHealthPool += seg;
|
||||
}
|
||||
MaxHealth = tempHealthPool;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void UDDIHealth::Heal()
|
||||
{
|
||||
if (CurrentHealth >= MaxHealth)
|
||||
{
|
||||
CurrentHealth = MaxHealth;
|
||||
HealTimer.Invalidate();
|
||||
return;
|
||||
}
|
||||
|
||||
CurrentHealth += 1;
|
||||
}
|
||||
|
||||
|
||||
// Called to cause damage
|
||||
void UDDIHealth::TakeDamage(int DamageValue)
|
||||
{
|
||||
if (HealTimer.IsValid())
|
||||
HealTimer.Invalidate();
|
||||
|
||||
CurrentHealth -= DamageValue;
|
||||
|
||||
}
|
||||
74
Source/OpenConflict/PlayerCharacter/Components/DDIHealth.h
Normal file
74
Source/OpenConflict/PlayerCharacter/Components/DDIHealth.h
Normal file
@@ -0,0 +1,74 @@
|
||||
// Fill out your copyright notice in the Description page of Project Settings.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "HealthSegmentStruct.h"
|
||||
#include "Components/ActorComponent.h"
|
||||
#include "DDIHealth.generated.h"
|
||||
|
||||
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
|
||||
class OPENCONFLICT_API UDDIHealth : public UActorComponent
|
||||
{
|
||||
GENERATED_BODY()
|
||||
/*UPROPERTY and UFUNCTION declarations*/
|
||||
private:
|
||||
/*Properties*/
|
||||
|
||||
/*Functions*/
|
||||
UFUNCTION(BlueprintAuthorityOnly, BlueprintCallable, Category = "Health")
|
||||
void Heal();
|
||||
|
||||
protected:
|
||||
/*Properties*/
|
||||
UPROPERTY(BlueprintReadOnly, Category = "Health")
|
||||
int MaxHealth; //Stores the default max health of component
|
||||
UPROPERTY(BlueprintReadOnly, Category = "Health")
|
||||
int CurrentHealth; //Stores the active health of component
|
||||
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Health")
|
||||
float HealTickTime; //Time in Seconds between heal ticks
|
||||
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Health")
|
||||
float HealDelayTime; //Time in Seconds before heal ticks
|
||||
|
||||
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Stage")
|
||||
UDataTable* HealthSegmentTable;
|
||||
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Stage")
|
||||
ClassNames ClassName;
|
||||
|
||||
/*Functions*/
|
||||
UFUNCTION(BlueprintAuthorityOnly, BlueprintCallable, Category = "Health")
|
||||
void TakeDamage(int DamageValue);
|
||||
|
||||
public:
|
||||
/*Properties*/
|
||||
|
||||
/*Functions*/
|
||||
|
||||
/*C++ only declarations*/
|
||||
private:
|
||||
/*Properties*/
|
||||
|
||||
/*Functions*/
|
||||
|
||||
protected:
|
||||
/*Properties*/
|
||||
TArray<int> HealthSegments;
|
||||
|
||||
FTimerHandle HealTimer; // Called to begin healing
|
||||
// FTimerManager& TimerManager = GetWorld()->GetTimerManager();
|
||||
|
||||
/*Functions*/
|
||||
// Called when the game starts
|
||||
virtual void BeginPlay() override;
|
||||
|
||||
public:
|
||||
/*Properties*/
|
||||
|
||||
/*Functions*/
|
||||
// Sets default values for this component's properties
|
||||
UDDIHealth();
|
||||
|
||||
// Called every frame
|
||||
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user