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-17 22:41:31 +00:00
|
|
|
#include "Blaster/GameState/BlasterGameState.h"
|
2022-05-09 14:33:48 +00:00
|
|
|
#include "Blaster/PlayerController/BlasterPlayerController.h"
|
|
|
|
#include "Blaster/PlayerState/BlasterPlayerState.h"
|
2022-05-07 18:34:40 +00:00
|
|
|
#include "GameFramework/PlayerStart.h"
|
|
|
|
#include "Kismet/GameplayStatics.h"
|
2022-05-07 17:10:32 +00:00
|
|
|
|
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-12 13:37:49 +00:00
|
|
|
void ABlasterGameMode::Tick(float DeltaTime)
|
2022-05-10 10:15:22 +00:00
|
|
|
{
|
2022-05-12 13:37:49 +00:00
|
|
|
Super::Tick(DeltaTime);
|
2022-05-10 10:15:22 +00:00
|
|
|
|
|
|
|
if (MatchState == MatchState::WaitingToStart)
|
|
|
|
{
|
2022-05-16 21:51:15 +00:00
|
|
|
CountdownTime = WarmupTime - GetWorld()->GetTimeSeconds(); // + LevelStartingTime;
|
2022-05-12 13:37:49 +00:00
|
|
|
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-16 21:51:15 +00:00
|
|
|
CountdownTime = WarmupTime + MatchTime - GetWorld()->GetTimeSeconds(); // + LevelStartingTime;
|
2022-05-12 13:37:49 +00:00
|
|
|
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-16 21:51:15 +00:00
|
|
|
CountdownTime = WarmupTime + MatchTime + CooldownTime - GetWorld()->GetTimeSeconds(); // + LevelStartingTime;
|
2022-05-12 13:37:49 +00:00
|
|
|
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-12 13:37:49 +00:00
|
|
|
for (FConstPlayerControllerIterator It = GetWorld()->GetPlayerControllerIterator(); It; ++It)
|
2022-05-10 10:42:09 +00:00
|
|
|
{
|
2022-05-12 13:37:49 +00:00
|
|
|
ABlasterPlayerController* BlasterPlayer = Cast<ABlasterPlayerController>(*It);
|
|
|
|
if (BlasterPlayer)
|
2022-05-10 10:42:09 +00:00
|
|
|
{
|
2022-05-12 13:37:49 +00:00
|
|
|
BlasterPlayer->OnMatchStateSet(MatchState);
|
2022-05-10 10:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-12 13:37:49 +00:00
|
|
|
void ABlasterGameMode::PlayerEliminated(class ABlasterCharacter* EliminatedCharacter, class ABlasterPlayerController* VictimController, ABlasterPlayerController* AttackerController)
|
2022-05-07 15:11:53 +00:00
|
|
|
{
|
2022-05-12 13:37:49 +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;
|
|
|
|
|
2022-05-17 22:41:31 +00:00
|
|
|
ABlasterGameState* BlasterGameState = GetGameState<ABlasterGameState>();
|
|
|
|
|
|
|
|
if (AttackerPlayerState && AttackerPlayerState != VictimPlayerState && BlasterGameState)
|
2022-05-09 14:33:48 +00:00
|
|
|
{
|
|
|
|
AttackerPlayerState->IncreaseScore(1.f);
|
2022-05-17 22:41:31 +00:00
|
|
|
BlasterGameState->UpdateTopScore(AttackerPlayerState);
|
2022-05-09 14:33:48 +00:00
|
|
|
}
|
2022-05-09 15:16:41 +00:00
|
|
|
if (VictimPlayerState)
|
|
|
|
{
|
|
|
|
VictimPlayerState->IncreaseDefeats(1);
|
|
|
|
}
|
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-12 13:37:49 +00:00
|
|
|
int32 Selection = FMath::RandRange(0, PlayerStarts.Num() - 1);
|
2022-05-07 18:34:40 +00:00
|
|
|
RestartPlayerAtPlayerStart(EliminatedController, PlayerStarts[Selection]);
|
|
|
|
}
|
2022-05-07 15:11:53 +00:00
|
|
|
}
|