// Fill out your copyright notice in the Description page of Project Settings.


#include "AIControlHub.h"

#include "EnemyCharacter.h"


// Sets default values
AAIControlHub::AAIControlHub()
{
	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
}

// Called when the game starts or when spawned
void AAIControlHub::BeginPlay()
{
	Super::BeginPlay();

	for (AEnemyCharacter* EnemyActor : EnemyActors)
	{
		EnemyActor->SubscribeToGroupAIEvents(this);
	}
}

// Called every frame
void AAIControlHub::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

void AAIControlHub::IncreaseAlertLevel()
{
	AlertLevel = FMath::Clamp(AlertLevel + 1, 0, 3);
	OnAlertLevelChanged();
}

void AAIControlHub::DecreaseAlertLevel()
{
	AlertLevel = FMath::Clamp(AlertLevel - 1, 0, 3);
	OnAlertLevelChanged();
}

void AAIControlHub::SetAlertLevel(int NewAlertLevel)
{
	AlertLevel = FMath::Clamp(NewAlertLevel, 0, 3);
	OnAlertLevelChanged();
}

int AAIControlHub::GetAlertLevel() const
{
	return AlertLevel;
}

void AAIControlHub::OnAlertLevelChanged()
{
	AlertLevelEvent.Broadcast(AlertLevel);
	if (AlertLevel == 2)
	{
		SetPlayerLastKnownLocation();
		HuntPlayerEvent.Broadcast(PlayerLastKnownLocation);
	}
}

void AAIControlHub::SetPlayerLastKnownLocation(FVector Location)
{
	if (Location == FVector(0, 0, 0))
	{
		PlayerLastKnownLocation = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation();
	}
	else
	{
		PlayerLastKnownLocation = Location;
	}
}

void AAIControlHub::RemoveEnemyActor(AEnemyCharacter* EnemyActor)
{
	EnemyActors.Remove(EnemyActor);
}
