// Copyright (c) 2026, LuQ Studios. All Rights Reserved.

#include "MyStandStillObjective.h"

UMyStandStillObjective::UMyStandStillObjective(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	// Tick is only called when this is set.
	bWantsTick = true;
}

EPulseObjectiveCompletionStatus UMyStandStillObjective::InitObjective_Implementation(const FPulseObjectiveHandle& Handle, const FGameplayTag& InQuestTag)
{
	// The target progress is the number of seconds. The objective completes by itself when the progress reaches it.
	if (HasAuthority())
		SetTargetProgress(FMath::Max(FMath::CeilToInt(Seconds), 1));

	return Super::InitObjective_Implementation(Handle, InQuestTag);
}

void UMyStandStillObjective::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	// Only the authority changes progress.
	if (!HasAuthority() || IsCompleted())
		return;

	TArray<AActor*> avatars;
	GetQuestAvatars(avatars);

	bool bAllStill = avatars.Num() > 0;
	for (const AActor* avatar : avatars)
	{
		if (avatar->GetVelocity().Size2D() > MaxSpeed)
		{
			bAllStill = false;
			break;
		}
	}

	StillTime = bAllStill ? StillTime + DeltaTime : 0.f;
	SetProgress(FMath::FloorToInt(StillTime));
}
