// Project Lab - NHTV Igad

#include "UnrealProject.h"
#include "KOTHPlayerController.h"
#include "NetworkPlayer.h"
#include "KOTHHUD.h"
#include "KOTHGameState.h"

void AKOTHPlayerController::BeginPlay()
{
	Super::BeginPlay();
}

void AKOTHPlayerController::Tick(float DeltaSeconds)
{
	Super::Tick(DeltaSeconds);

	AKOTHGameState* gs = GetGameState<AKOTHGameState>();
	if(gs && m_kothhud)
	{
		m_kothhud->Update(gs->kothTeams);
	}
}

void AKOTHPlayerController::m_OnPawnChanged()
{
	Super::m_OnPawnChanged();

	class ANetworkPlayer* character = Cast<ANetworkPlayer>(GetPawn());
	if(character)
	{
		if(IsLocalController())
		{
			if(!m_kothhud)
			{
				m_kothhud = CreateWidget<UKOTHHUD>(GetWorld(), kothHUD);
				if(m_kothhud)
				{
					m_kothhud->AddToViewport(2);
				}
			}
		}
	}

	if(m_kothhud)
		m_kothhud->SetVisibility(character ? ESlateVisibility::Visible : ESlateVisibility::Hidden);
}
