AzureAbyss/Source/the_twilight_abyss/TurnBasedCombatV2/StatusEffect.cpp

53 lines
2.2 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)
{
return;
}
void UStatusEffect::OnStatusEffectAdd()
{
ATurnBaseCombatV2* CombatSystem = Cast<ATurnBaseCombatV2>(GetWorld()->GetGameState());
OnPlayerTurnDelegateHandle = CombatSystem->OnPlayerTurn.AddUObject(this, &UStatusEffect::OnPlayerTurn);
OnEnemyTurnDelegateHandle = CombatSystem->OnEnemyTurn.AddUObject(this, &UStatusEffect::OnEnemyTurn);
}
void UStatusEffect::CheckForExpiry(const float TimeOfExpiry, AActor* Character)
{
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()))));
}