// 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(Character->GetComponentByClass(UStatusSystem::StaticClass())); if (StatusSystem->GetActiveStatusEffect(this).StatusIcon == nullptr) return; StatusSystem->GetActiveStatusEffect(this).StatusIcon->RemoveFromParent(); StatusSystem->RemoveStatusEffect(this); ATurnBaseCombatV2* CombatSystem = Cast(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(GetWorld()->GetGameState()); OnPlayerTurnDelegateHandle = CombatSystem->OnPlayerTurn.AddUObject(this, &UStatusEffect::OnPlayerTurn); OnEnemyTurnDelegateHandle = CombatSystem->OnEnemyTurn.AddUObject(this, &UStatusEffect::OnEnemyTurn); } void UStatusEffect::TickDown(AActor* Character) { UStatusSystem* StatusSystem = Cast(Character->GetComponentByClass(UStatusSystem::StaticClass())); UTextBlock* StatusText = Cast(StatusSystem->GetActiveStatusEffect(this).StatusIcon->GetWidgetFromName(TEXT("DurationText"))); StatusText->SetText(FText::FromString(FString::FromInt(BaseDuration))); } void UStatusEffect::CheckForExpiry(const float TimeOfExpiry, AActor* Character) { if (TimeOfExpiry <= UGameplayStatics::GetRealTimeSeconds(GetWorld())) OnExpiry(Character); UStatusSystem* StatusSystem = Cast(Character->GetComponentByClass(UStatusSystem::StaticClass())); if (StatusSystem->GetActiveStatusEffect(this).StatusIcon == nullptr) return; UTextBlock* StatusText = Cast(StatusSystem->GetActiveStatusEffect(this).StatusIcon->GetWidgetFromName(TEXT("DurationText"))); StatusText->SetText(FText::FromString(FString::FromInt(TimeOfExpiry - UGameplayStatics::GetRealTimeSeconds(GetWorld())))); }