2022-05-07 15:11:53 +00:00
|
|
|
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
|
|
|
|
|
|
#include "BlasterGameMode.h"
|
2022-05-07 17:10:32 +00:00
|
|
|
#include "Blaster/Character/BlasterCharacter.h"
|
2022-05-09 14:33:48 +00:00
|
|
|
#include "Blaster/PlayerController/BlasterPlayerController.h"
|
2022-05-07 18:34:40 +00:00
|
|
|
#include "Kismet/GameplayStatics.h"
|
2022-05-10 20:34:28 +00:00
|
|
|
#include "GameFramework/PlayerStart.h"
|
|
|
|
#include "Blaster/PlayerState/BlasterPlayerState.h"
|
2022-05-10 12:55:02 +00:00
|
|
|
|
|
|
|
namespace MatchState
|
|
|
|
{
|
|
|
|
const FName Cooldown = FName("Cooldown");
|
|
|
|
}
|
|
|
|
|
2022-05-10 10:15:22 +00:00
|
|
|
ABlasterGameMode::ABlasterGameMode()
|
|
|
|
{
|
|
|
|
bDelayedStart = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ABlasterGameMode::BeginPlay()
|
|
|
|
{
|
|
|
|
Super::BeginPlay();
|
|
|
|
|
|
|
|
LevelStartingTime = GetWorld()->GetTimeSeconds();
|
|
|
|
}
|
|
|
|
|
2022-05-10 20:34:28 +00:00
|
|
|
void ABlasterGameMode::Tick(float DeltaTime)
|
2022-05-10 10:15:22 +00:00
|
|
|
{
|
2022-05-10 20:34:28 +00:00
|
|
|
Super::Tick(DeltaTime);
|
2022-05-10 10:15:22 +00:00
|
|
|
|
|
|
|
if (MatchState == MatchState::WaitingToStart)
|
|
|
|
{
|
2022-05-10 20:34:28 +00:00
|
|
|
CountdownTime = WarmupTime - GetWorld()->GetTimeSeconds() + LevelStartingTime;
|
|
|
|
if (CountdownTime <= 0.f)
|
2022-05-10 10:15:22 +00:00
|
|
|
{
|
|
|
|
StartMatch();
|
|
|
|
}
|
|
|
|
}
|
2022-05-10 12:55:02 +00:00
|
|
|
else if (MatchState == MatchState::InProgress)
|
|
|
|
{
|
2022-05-10 20:34:28 +00:00
|
|
|
CountdownTime = WarmupTime + MatchTime - GetWorld()->GetTimeSeconds() + LevelStartingTime;
|
|
|
|
if (CountdownTime <= 0.f)
|
2022-05-10 12:55:02 +00:00
|
|
|
{
|
|
|
|
SetMatchState(MatchState::Cooldown);
|
|
|
|
}
|
|
|
|
}
|
2022-05-10 13:29:00 +00:00
|
|
|
else if (MatchState == MatchState::Cooldown)
|
|
|
|
{
|
2022-05-10 20:34:28 +00:00
|
|
|
CountdownTime = CooldownTime + WarmupTime + MatchTime - GetWorld()->GetTimeSeconds() + LevelStartingTime;
|
|
|
|
if (CountdownTime <= 0.f)
|
2022-05-10 14:03:35 +00:00
|
|
|
{
|
|
|
|
RestartGame();
|
|
|
|
}
|
2022-05-10 13:29:00 +00:00
|
|
|
}
|
2022-05-10 10:15:22 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 10:42:09 +00:00
|
|
|
void ABlasterGameMode::OnMatchStateSet()
|
|
|
|
{
|
|
|
|
Super::OnMatchStateSet();
|
|
|
|
|
2022-05-10 20:34:28 +00:00
|
|
|
for (FConstPlayerControllerIterator It = GetWorld()->GetPlayerControllerIterator(); It; ++It)
|
2022-05-10 10:42:09 +00:00
|
|
|
{
|
2022-05-10 20:34:28 +00:00
|
|
|
ABlasterPlayerController* BlasterPlayer = Cast<ABlasterPlayerController>(*It);
|
|
|
|
if (BlasterPlayer)
|
2022-05-10 10:42:09 +00:00
|
|
|
{
|
2022-05-10 20:34:28 +00:00
|
|
|
BlasterPlayer->OnMatchStateSet(MatchState);
|
2022-05-10 10:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 20:34:28 +00:00
|
|
|
void ABlasterGameMode::PlayerEliminated(class ABlasterCharacter* EliminatedCharacter, ABlasterPlayerController* VictimController,
|
2022-05-07 17:10:32 +00:00
|
|
|
ABlasterPlayerController* AttackerController)
|
2022-05-07 15:11:53 +00:00
|
|
|
{
|
2022-05-10 20:34:28 +00:00
|
|
|
if (AttackerController == nullptr || AttackerController->PlayerState == nullptr) return;
|
|
|
|
if (VictimController == nullptr || VictimController->PlayerState == nullptr) return;
|
2022-05-09 14:33:48 +00:00
|
|
|
ABlasterPlayerState* AttackerPlayerState = AttackerController ? Cast<ABlasterPlayerState>(AttackerController->PlayerState) : nullptr;
|
|
|
|
ABlasterPlayerState* VictimPlayerState = VictimController ? Cast<ABlasterPlayerState>(VictimController->PlayerState) : nullptr;
|
|
|
|
|
|
|
|
if (AttackerPlayerState && AttackerPlayerState != VictimPlayerState)
|
|
|
|
{
|
2022-05-10 20:34:28 +00:00
|
|
|
AttackerPlayerState->AddToScore(1.f);
|
2022-05-09 14:33:48 +00:00
|
|
|
}
|
2022-05-09 15:16:41 +00:00
|
|
|
if (VictimPlayerState)
|
|
|
|
{
|
2022-05-10 20:34:28 +00:00
|
|
|
VictimPlayerState->AddToDefeats(1);
|
2022-05-09 15:16:41 +00:00
|
|
|
}
|
2022-05-09 14:33:48 +00:00
|
|
|
|
2022-05-07 17:10:32 +00:00
|
|
|
if (EliminatedCharacter)
|
|
|
|
{
|
|
|
|
EliminatedCharacter->Eliminated();
|
|
|
|
}
|
2022-05-07 18:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ABlasterGameMode::RequestRespawn(ACharacter* EliminatedCharacter, AController* EliminatedController)
|
|
|
|
{
|
|
|
|
if (EliminatedCharacter)
|
|
|
|
{
|
|
|
|
EliminatedCharacter->Reset();
|
|
|
|
EliminatedCharacter->Destroy();
|
|
|
|
}
|
|
|
|
if (EliminatedController)
|
|
|
|
{
|
|
|
|
TArray<AActor*> PlayerStarts;
|
|
|
|
UGameplayStatics::GetAllActorsOfClass(this, APlayerStart::StaticClass(), PlayerStarts);
|
2022-05-10 20:34:28 +00:00
|
|
|
int32 Selection = FMath::RandRange(0, PlayerStarts.Num() - 1);
|
2022-05-07 18:34:40 +00:00
|
|
|
RestartPlayerAtPlayerStart(EliminatedController, PlayerStarts[Selection]);
|
|
|
|
}
|
2022-05-10 20:34:28 +00:00
|
|
|
}
|