 // Project Lab - NHTV Igad

#include "UnrealProject.h"
#include "BossBase.h"
#include "MusicPlayer.h"
#include "SpawnerBase.h"
#include "NetworkPlayer.h"
#include "KingOfTheHillGameMode.h"
#include "KOTHBossSpawner.h"


ABossBase::ABossBase()
{
	PrimaryActorTick.bCanEverTick = true;

}

void ABossBase::BeginPlay()
{
	Super::BeginPlay();

	m_usesMana = false;
	m_musicPlayer = nullptr;

	// Search for a music player in the world.
	for (TActorIterator<AMusicPlayer> actorItr(GetWorld()); actorItr; ++actorItr)
	{
		m_musicPlayer = *actorItr;
		break;
	}

	if (Role != ROLE_Authority)
		return;
}
void ABossBase::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
	Super::EndPlay(EndPlayReason);

	if (Role != ROLE_Authority)
		return;

	// Stop playing boss music.
	if (m_musicPlayer != nullptr)
		m_musicPlayer->SetInCombat(false);

}
void ABossBase::Tick(float deltaTime)
{
	Super::Tick(deltaTime);
	if (Role != ROLE_Authority)
		return;

	if((!IsPendingKill() || !IsPendingKill()) && GetTeam() < 5)
	{
		AKOTHBossSpawner* bSpawner = Cast<AKOTHBossSpawner>(m_spawn);
		AKingOfTheHillGameMode* kothMode = Cast<AKingOfTheHillGameMode>(GetWorld()->GetAuthGameMode());
		if(kothMode&&bSpawner)
			kothMode->AddScore(GetTeam(), deltaTime);
	}
}


void ABossBase::m_Engaged()
{
	// Set music to combat music.
	if (m_musicPlayer != nullptr)
		m_musicPlayer->SetInCombat(true);
}
void ABossBase::m_Disengaged()
{
	// Set music to regular music.
	if (m_musicPlayer != nullptr)
		m_musicPlayer->SetInCombat(false);
}
void ABossBase::NativeOnKilled(class ANetworkCharacter* killer, class UAbilityInfo* ability)
{
	Super::NativeOnKilled(killer, ability);
	if (Role != ROLE_Authority)
		return;
	if (m_spawn &&m_spawn->possesable)
	{
	//	m_spawn->CaptureCamp(killer->GetTeam());
		//AKOTHBossSpawner* bSpawner = Cast<AKOTHBossSpawner>(m_spawn);
		//AKingOfTheHillGameMode* kothMode = Cast<AKingOfTheHillGameMode>(GetWorld()->GetAuthGameMode());
		//if (kothMode&&bSpawner)
		//{
		/*	for (TActorIterator<ASpawnerBase>actorIt(GetWorld()); actorIt; ++actorIt)
			{

				ASpawnerBase *spawn = *actorIt;
				if(!spawn->possesable)
					spawn->SetTeam(killer->GetTeam());
			}
		}*/
	}
}