blaster/Source/Blaster/GameMode/BlasterGameMode.cpp

89 lines
2.4 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "BlasterGameMode.h"
#include "Blaster/Character/BlasterCharacter.h"
#include "Blaster/PlayerController/BlasterPlayerController.h"
#include "Blaster/PlayerState/BlasterPlayerState.h"
#include "GameFramework/PlayerStart.h"
#include "Kismet/GameplayStatics.h"
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;
if (CountDownTime < 0.f)
{
StartMatch();
}
}
}
void ABlasterGameMode::OnMatchStateSet()
{
Super::OnMatchStateSet();
for (FConstPlayerControllerIterator Iterator = GetWorld()->GetPlayerControllerIterator(); Iterator; ++Iterator)
{
ABlasterPlayerController* PlayerController = Cast<ABlasterPlayerController>(*Iterator);
if (PlayerController)
{
PlayerController->OnMatchStateSet(MatchState);
}
}
}
void ABlasterGameMode::PlayerEliminated(ABlasterCharacter* EliminatedCharacter, ABlasterPlayerController* VictimController,
ABlasterPlayerController* AttackerController)
{
ABlasterPlayerState* AttackerPlayerState = AttackerController ? Cast<ABlasterPlayerState>(AttackerController->PlayerState) : nullptr;
ABlasterPlayerState* VictimPlayerState = VictimController ? Cast<ABlasterPlayerState>(VictimController->PlayerState) : nullptr;
if (AttackerPlayerState && AttackerPlayerState != VictimPlayerState)
{
AttackerPlayerState->IncreaseScore(1.f);
}
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);
const int32 Selection = FMath::RandRange(0, PlayerStarts.Num() - 1);
RestartPlayerAtPlayerStart(EliminatedController, PlayerStarts[Selection]);
}
}