blaster/Source/Blaster/GameMode/BlasterGameMode.cpp

111 lines
2.9 KiB
C++
Raw Normal View History

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"
#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();
}
void ABlasterGameMode::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
if (MatchState == MatchState::WaitingToStart)
{
CountDownTime = WarmupTime - GetWorld()->GetTimeSeconds() + LevelStartingTime;
2022-05-10 12:55:02 +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)
{
CountDownTime = WarmupTime + MatchTime - GetWorld()->GetTimeSeconds() + LevelStartingTime;
if (CountDownTime <= 0.f)
{
SetMatchState(MatchState::Cooldown);
}
}
2022-05-10 13:29:00 +00:00
else if (MatchState == MatchState::Cooldown)
{
2022-05-10 17:05:04 +00:00
CountDownTime = CooldownTime + WarmupTime + MatchTime - GetWorld()->GetTimeSeconds() + LevelStartingTime;
2022-05-10 14:03:35 +00:00
if (CountDownTime <= 0.f)
{
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();
for (FConstPlayerControllerIterator Iterator = GetWorld()->GetPlayerControllerIterator(); Iterator; ++Iterator)
{
ABlasterPlayerController* PlayerController = Cast<ABlasterPlayerController>(*Iterator);
if (PlayerController)
{
PlayerController->OnMatchStateSet(MatchState);
}
}
}
2022-05-07 17:10:32 +00:00
void ABlasterGameMode::PlayerEliminated(ABlasterCharacter* EliminatedCharacter, ABlasterPlayerController* VictimController,
ABlasterPlayerController* AttackerController)
2022-05-07 15:11:53 +00:00
{
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)
{
AttackerPlayerState->IncreaseScore(1.f);
}
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);
const int32 Selection = FMath::RandRange(0, PlayerStarts.Num() - 1);
RestartPlayerAtPlayerStart(EliminatedController, PlayerStarts[Selection]);
}
2022-05-07 15:11:53 +00:00
}