// Fill out your copyright notice in the Description page of Project Settings. #include "StatusSystem.h" #include #include "Blueprint/UserWidget.h" #include "Components/Image.h" #include "Components/TextBlock.h" // Sets default values for this component's properties UStatusSystem::UStatusSystem() { // 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; static ConstructorHelpers::FClassFinder HUDStatusIndicatorsWidgetClassFinder(TEXT("/Game/Blueprints/Status_UI/StatusIndicator")); HUDStatusIndicatorsWidget = HUDStatusIndicatorsWidgetClassFinder.Class; static ConstructorHelpers::FClassFinder HUDStatusIconWidgetClassFinder(TEXT("/Game/Blueprints/Status_UI/StatusIcon")); HUDStatusIconWidget = HUDStatusIconWidgetClassFinder.Class; } // Called when the game starts void UStatusSystem::BeginPlay() { Super::BeginPlay(); HUDStatusIndicators = CreateWidget(GetWorld(), HUDStatusIndicatorsWidget); HUDStatusIndicators->AddToViewport(); StatusIconsBox = Cast(HUDStatusIndicators->GetWidgetFromName(TEXT("StatusIndicators"))); } // Called every frame void UStatusSystem::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) { Super::TickComponent(DeltaTime, TickType, ThisTickFunction); } void UStatusSystem::AddStatusEffect(UStatusEffect* StatusEffect, const float DurationMultiplier, const bool Invoke) { FActiveStatusEffect NewStatusEffect; NewStatusEffect.StatusEffect = StatusEffect; NewStatusEffect.TimeInitiated = UGameplayStatics::GetRealTimeSeconds(GetWorld()); NewStatusEffect.TimeTillExpiry = UGameplayStatics::GetRealTimeSeconds(GetWorld()) + StatusEffect->BaseDuration * DurationMultiplier; NewStatusEffect.StatusIcon = CreateWidget(GetWorld(), HUDStatusIconWidget); UImage* StatusIconImage = Cast(NewStatusEffect.StatusIcon->GetWidgetFromName(TEXT("StatusIconImage"))); StatusIconImage->SetBrushFromTexture(StatusEffect->Icon); StatusIconsBox->AddChild(NewStatusEffect.StatusIcon); if (Invoke) NewStatusEffect.StatusEffect->Invoke(GetOwner(), NewStatusEffect.TimeTillExpiry); ActiveStatusEffects.Add(NewStatusEffect); } void UStatusSystem::RemoveStatusEffect(UStatusEffect* StatusEffect) { if (ActiveStatusEffects.Contains(FActiveStatusEffect{0, 0, StatusEffect, nullptr})) { ActiveStatusEffects.Remove(FActiveStatusEffect{0, 0, StatusEffect, nullptr}); } else { UE_LOG(LogTemp, Warning, TEXT("StatusEffect not found in ActiveStatusEffects")); } } FActiveStatusEffect UStatusSystem::GetActiveStatusEffect(UStatusEffect* StatusEffect) { if (ActiveStatusEffects.Contains(FActiveStatusEffect{0, 0, StatusEffect, nullptr})) { return ActiveStatusEffects[ActiveStatusEffects.Find(FActiveStatusEffect{0, 0, StatusEffect, nullptr})]; } UE_LOG(LogTemp, Warning, TEXT("StatusEffect not found in ActiveStatusEffects")); return FActiveStatusEffect(); }