68 lines
1.7 KiB
C++
68 lines
1.7 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "TeamsGameMode.h"
|
|
|
|
#include "Blaster/GameState/BlasterGameState.h"
|
|
#include "Kismet/GameplayStatics.h"
|
|
|
|
void ATeamsGameMode::PostLogin(APlayerController* NewPlayer)
|
|
{
|
|
Super::PostLogin(NewPlayer);
|
|
|
|
AssignToTeam(NewPlayer->PlayerState.Get());
|
|
}
|
|
|
|
void ATeamsGameMode::Logout(AController* Exiting)
|
|
{
|
|
Super::Logout(Exiting);
|
|
|
|
BGameState = (BGameState == nullptr) ? Cast<ABlasterGameState>(UGameplayStatics::GetGameState(this)) : BGameState;
|
|
ABlasterPlayerState* BPState = Exiting->GetPlayerState<ABlasterPlayerState>();
|
|
if (BGameState && BPState)
|
|
{
|
|
if (BGameState->RedTeam.Contains(BPState))
|
|
{
|
|
BGameState->RedTeam.Remove(BPState);
|
|
}
|
|
if (BGameState->BlueTeam.Contains(BPState))
|
|
{
|
|
BGameState->BlueTeam.Remove(BPState);
|
|
}
|
|
}
|
|
}
|
|
|
|
void ATeamsGameMode::HandleMatchHasStarted()
|
|
{
|
|
Super::HandleMatchHasStarted();
|
|
|
|
BGameState = (BGameState == nullptr) ? Cast<ABlasterGameState>(UGameplayStatics::GetGameState(this)) : BGameState;
|
|
if (BGameState == nullptr) return;
|
|
|
|
for (auto PState : BGameState->PlayerArray)
|
|
{
|
|
AssignToTeam(PState);
|
|
}
|
|
}
|
|
|
|
void ATeamsGameMode::AssignToTeam(APlayerState* PlayerState)
|
|
{
|
|
BGameState = (BGameState == nullptr) ? Cast<ABlasterGameState>(UGameplayStatics::GetGameState(this)) : BGameState;
|
|
if (PlayerState == nullptr) return;
|
|
|
|
ABlasterPlayerState* BPlayerState = Cast<ABlasterPlayerState>(PlayerState);
|
|
if (BPlayerState && BPlayerState->GetTeam() == ETeam::ET_NoTeam)
|
|
{
|
|
if (BGameState->BlueTeam.Num() >= BGameState->RedTeam.Num())
|
|
{
|
|
BGameState->RedTeam.AddUnique(BPlayerState);
|
|
BPlayerState->SetTeam(ETeam::ET_RedTeam);
|
|
}
|
|
else
|
|
{
|
|
BGameState->BlueTeam.AddUnique(BPlayerState);
|
|
BPlayerState->SetTeam(ETeam::ET_BlueTeam);
|
|
}
|
|
}
|
|
}
|