blaster/Source/Blaster/GameMode/BlasterGameMode.cpp

115 lines
3.2 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "BlasterGameMode.h"
#include "Blaster/Character/BlasterCharacter.h"
#include "Blaster/GameState/BlasterGameState.h"
#include "Blaster/PlayerController/BlasterPlayerController.h"
#include "Blaster/PlayerState/BlasterPlayerState.h"
#include "GameFramework/PlayerStart.h"
#include "Kismet/GameplayStatics.h"
namespace MatchState
{
const FName Cooldown = FName("Cooldown");
}
ABlasterGameMode::ABlasterGameMode()
{
bDelayedStart = true;
}
void ABlasterGameMode::BeginPlay()
{
Super::BeginPlay();
LevelStartingTime = GetWorld()->GetTimeSeconds();
}
void ABlasterGameMode::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (MatchState == MatchState::WaitingToStart)
{
CountdownTime = WarmupTime - GetWorld()->GetTimeSeconds(); // + LevelStartingTime;
if (CountdownTime <= 0.f)
{
StartMatch();
}
}
else if (MatchState == MatchState::InProgress)
{
CountdownTime = WarmupTime + MatchTime - GetWorld()->GetTimeSeconds(); // + LevelStartingTime;
if (CountdownTime <= 0.f)
{
SetMatchState(MatchState::Cooldown);
}
}
else if (MatchState == MatchState::Cooldown)
{
CountdownTime = WarmupTime + MatchTime + CooldownTime - GetWorld()->GetTimeSeconds(); // + LevelStartingTime;
if (CountdownTime <= 0.f)
{
RestartGame();
}
}
}
void ABlasterGameMode::OnMatchStateSet()
{
Super::OnMatchStateSet();
for (FConstPlayerControllerIterator It = GetWorld()->GetPlayerControllerIterator(); It; ++It)
{
ABlasterPlayerController* BlasterPlayer = Cast<ABlasterPlayerController>(*It);
if (BlasterPlayer)
{
BlasterPlayer->OnMatchStateSet(MatchState);
}
}
}
void ABlasterGameMode::PlayerEliminated(class ABlasterCharacter* EliminatedCharacter, class ABlasterPlayerController* VictimController, ABlasterPlayerController* AttackerController)
{
if (AttackerController == nullptr || AttackerController->PlayerState == nullptr) return;
if (VictimController == nullptr || VictimController->PlayerState == nullptr) return;
ABlasterPlayerState* AttackerPlayerState = AttackerController ? Cast<ABlasterPlayerState>(AttackerController->PlayerState) : nullptr;
ABlasterPlayerState* VictimPlayerState = VictimController ? Cast<ABlasterPlayerState>(VictimController->PlayerState) : nullptr;
ABlasterGameState* BlasterGameState = GetGameState<ABlasterGameState>();
if (AttackerPlayerState && AttackerPlayerState != VictimPlayerState && BlasterGameState)
{
AttackerPlayerState->IncreaseScore(1.f);
BlasterGameState->UpdateTopScore(AttackerPlayerState);
}
if (VictimPlayerState)
{
VictimPlayerState->IncreaseDefeats(1);
}
if (EliminatedCharacter)
{
EliminatedCharacter->Eliminated();
}
}
void ABlasterGameMode::RequestRespawn(ACharacter* EliminatedCharacter, AController* EliminatedController)
{
if (EliminatedCharacter)
{
EliminatedCharacter->Reset();
EliminatedCharacter->Destroy();
}
if (EliminatedController)
{
TArray<AActor*> PlayerStarts;
UGameplayStatics::GetAllActorsOfClass(this, APlayerStart::StaticClass(), PlayerStarts);
int32 Selection = FMath::RandRange(0, PlayerStarts.Num() - 1);
RestartPlayerAtPlayerStart(EliminatedController, PlayerStarts[Selection]);
}
}