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
|
|
|
{
|
2022-05-28 20:21:03 +00:00
|
|
|
TArray<ABlasterPlayerState*> PlayersCurrentlyInTheLead;
|
|
|
|
for (auto LeadPlayer : BlasterGameState->TopScoringPlayers)
|
|
|
|
{
|
|
|
|
PlayersCurrentlyInTheLead.Add(LeadPlayer);
|
|
|
|
}
|
2022-05-29 09:59:15 +00:00
|
|
|
|
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-28 20:21:03 +00:00
|
|
|
if (BlasterGameState->TopScoringPlayers.Contains(AttackerPlayerState))
|
|
|
|
{
|
|
|
|
ABlasterCharacter* Leader = Cast<ABlasterCharacter>(AttackerPlayerState->GetPawn());
|
|
|
|
if (Leader)
|
|
|
|
{
|
|
|
|
Leader->MulticastGainedTheLead();
|
|
|
|
}
|
|
|
|
}
|
2022-05-29 09:59:15 +00:00
|
|
|
|
2022-05-28 20:21:03 +00:00
|
|
|
for (int32 i = 0; i < PlayersCurrentlyInTheLead.Num(); i++)
|
|
|
|
{
|
|
|
|
if (!BlasterGameState->TopScoringPlayers.Contains(PlayersCurrentlyInTheLead[i]))
|
|
|
|
{
|
|
|
|
ABlasterCharacter* Loser = Cast<ABlasterCharacter>(PlayersCurrentlyInTheLead[i]->GetPawn());
|
|
|
|
if (Loser)
|
|
|
|
{
|
|
|
|
Loser->MulticastLostTheLead();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
{
|
2022-05-28 14:26:06 +00:00
|
|
|
EliminatedCharacter->Eliminated(false);
|
2022-05-07 17:10:32 +00:00
|
|
|
}
|
2022-05-29 10:42:42 +00:00
|
|
|
|
|
|
|
for (FConstPlayerControllerIterator It = GetWorld()->GetPlayerControllerIterator(); It; ++It)
|
|
|
|
{
|
|
|
|
ABlasterPlayerController* BlasterPlayer = Cast<ABlasterPlayerController>(*It);
|
|
|
|
if (BlasterPlayer && AttackerPlayerState && VictimPlayerState)
|
|
|
|
{
|
|
|
|
BlasterPlayer->BroadcastElim(AttackerPlayerState, VictimPlayerState);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
2022-05-28 14:26:06 +00:00
|
|
|
|
|
|
|
void ABlasterGameMode::PlayerLeftGame(ABlasterPlayerState* PlayerLeaving)
|
|
|
|
{
|
|
|
|
if (PlayerLeaving == nullptr) return;
|
|
|
|
ABlasterGameState* BlasterGameState = GetGameState<ABlasterGameState>();
|
|
|
|
if (BlasterGameState && BlasterGameState->TopScoringPlayers.Contains(PlayerLeaving))
|
|
|
|
{
|
|
|
|
BlasterGameState->TopScoringPlayers.Remove(PlayerLeaving);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ABlasterCharacter* CharacterLeaving = Cast<ABlasterCharacter>(PlayerLeaving->GetPawn()))
|
|
|
|
{
|
|
|
|
CharacterLeaving->Eliminated(true);
|
|
|
|
}
|
|
|
|
}
|