63 lines
2.7 KiB
C++
63 lines
2.7 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "StatusEffect.h"
|
|
|
|
#include "StatusSystem.h"
|
|
#include "Kismet/GameplayStatics.h"
|
|
#include "TurnBaseCombatV2.h"
|
|
#include "Components/TextBlock.h"
|
|
|
|
void UStatusEffect::Invoke(AActor* Character, float TimeOfInit)
|
|
{
|
|
GetWorld()->GetTimerManager().SetTimer(ExpiryTimerHandle, [this, Character, TimeOfInit] { CheckForExpiry(TimeOfInit, Character); }, 1, true, 0);
|
|
}
|
|
|
|
void UStatusEffect::OnExpiry(AActor* Character)
|
|
{
|
|
GetWorld()->GetTimerManager().ClearTimer(ExpiryTimerHandle);
|
|
UStatusSystem* StatusSystem = Cast<UStatusSystem>(Character->GetComponentByClass(UStatusSystem::StaticClass()));
|
|
if (StatusSystem->GetActiveStatusEffect(this).StatusIcon == nullptr) return;
|
|
StatusSystem->GetActiveStatusEffect(this).StatusIcon->RemoveFromParent();
|
|
StatusSystem->RemoveStatusEffect(this);
|
|
ATurnBaseCombatV2* CombatSystem = Cast<ATurnBaseCombatV2>(GetWorld()->GetGameState());
|
|
CombatSystem->OnPlayerTurn.Remove(OnPlayerTurnDelegateHandle);
|
|
CombatSystem->OnEnemyTurn.Remove(OnEnemyTurnDelegateHandle);
|
|
}
|
|
|
|
void UStatusEffect::OnPlayerTurn(AActor* Character, AActor* Enemy)
|
|
{
|
|
return;
|
|
}
|
|
|
|
void UStatusEffect::OnEnemyTurn(AActor* Enemy, AActor* Character)
|
|
{
|
|
TickDown(Character);
|
|
}
|
|
|
|
void UStatusEffect::OnStatusEffectAdd(AActor* Character)
|
|
{
|
|
ATurnBaseCombatV2* CombatSystem = Cast<ATurnBaseCombatV2>(GetWorld()->GetGameState());
|
|
OnPlayerTurnDelegateHandle = CombatSystem->OnPlayerTurn.AddUObject(this, &UStatusEffect::OnPlayerTurn);
|
|
OnEnemyTurnDelegateHandle = CombatSystem->OnEnemyTurn.AddUObject(this, &UStatusEffect::OnEnemyTurn);
|
|
}
|
|
|
|
void UStatusEffect::TickDown(AActor* Character)
|
|
{
|
|
UStatusSystem* StatusSystem = Cast<UStatusSystem>(Character->GetComponentByClass(UStatusSystem::StaticClass()));
|
|
if (!IsValid(StatusSystem->GetActiveStatusEffect(this).StatusIcon)) return;
|
|
UTextBlock* StatusText = Cast<UTextBlock>(StatusSystem->GetActiveStatusEffect(this).StatusIcon->GetWidgetFromName(TEXT("DurationText")));
|
|
StatusText->SetText(FText::FromString(FString::FromInt(BaseDuration)));
|
|
}
|
|
|
|
void UStatusEffect::CheckForExpiry(const float TimeOfExpiry, AActor* Character)
|
|
{
|
|
if (!IsValid(this)) return;
|
|
if (!IsValid(Character)) return;
|
|
if (TimeOfExpiry <= UGameplayStatics::GetRealTimeSeconds(GetWorld())) OnExpiry(Character);
|
|
UStatusSystem* StatusSystem = Cast<UStatusSystem>(Character->GetComponentByClass(UStatusSystem::StaticClass()));
|
|
if (StatusSystem->GetActiveStatusEffect(this).StatusIcon == nullptr) return;
|
|
UTextBlock* StatusText = Cast<UTextBlock>(StatusSystem->GetActiveStatusEffect(this).StatusIcon->GetWidgetFromName(TEXT("DurationText")));
|
|
StatusText->SetText(FText::FromString(FString::FromInt(TimeOfExpiry - UGameplayStatics::GetRealTimeSeconds(GetWorld()))));
|
|
}
|