// Fill out your copyright notice in the Description page of Project Settings.
#include "TankGameModeBase.h"
#include "ToonTanks/Pawns/PawnTank.h"
#include "TimerManager.h"
#include "ToonTanks/Pawns/PawnTurret.h"
#include "GameFramework/PlayerController.h"
#include "ToonTanks/PlayerControllers/PlayerControllerBase.h"
#include "Engine/World.h"
#include "Kismet/GameplayStatics.h"
void ATankGameModeBase::BeginPlay()
{
Super::BeginPlay();
HandleGameStart();
}
void ATankGameModeBase::ActorDied(AActor* DeadActor)
{
if (DeadActor == PlayerTank) {
PlayerTank->HandleDestruction();
HandleGameOver(false);
if (PlayerControllerRef) {
PlayerControllerRef->SetPlayerEnabledState(false);
}
}
else {
if (APawnTurret* DestroyedTurret=Cast<APawnTurret>(DeadActor)) {
DestroyedTurret->HandleDestruction();
if (--TargetTurrets == 0) {
HandleGameOver(true);
}
}
}
}
int32 ATankGameModeBase::GetTargetTurretCount()
{
TArray<AActor*> TurretActors;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), APawnTurret::StaticClass(), TurretActors);
return TurretActors.Num();
}
void ATankGameModeBase::HandleGameStart()
{
PlayerControllerRef = Cast<APlayerControllerBase>(UGameplayStatics::GetPlayerController(this, 0));
PlayerTank = Cast<APawnTank>(UGameplayStatics::GetPlayerPawn(this, 0));
TargetTurrets = GetTargetTurretCount();
GameStart();
}
void ATankGameModeBase::HandleGameOver(bool PlayerWon)
{
GameOver(PlayerWon);
}