blaster/Source/Blaster/GameMode/TeamsGameMode.cpp

106 lines
3.1 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "TeamsGameMode.h"
#include "Blaster/GameState/BlasterGameState.h"
#include "Blaster/PlayerController/BlasterPlayerController.h"
#include "Kismet/GameplayStatics.h"
ATeamsGameMode::ATeamsGameMode()
{
bTeamsMatch = true;
}
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);
}
}
}
float ATeamsGameMode::CalculateDamage(AController* Attacker, AController* Victim, float BaseDamage)
{
ABlasterPlayerState* AttackerPState = Attacker->GetPlayerState<ABlasterPlayerState>();
ABlasterPlayerState* VictimPState = Victim->GetPlayerState<ABlasterPlayerState>();
if (AttackerPState == nullptr || VictimPState == nullptr) return BaseDamage;
if (VictimPState == AttackerPState) return BaseDamage;
if (AttackerPState->GetTeam() == VictimPState->GetTeam() && !FriendlyFire) return 0.f;
return BaseDamage;
}
void ATeamsGameMode::PlayerEliminated(ABlasterCharacter* EliminatedCharacter, ABlasterPlayerController* VictimController,
ABlasterPlayerController* AttackerController)
{
Super::PlayerEliminated(EliminatedCharacter, VictimController, AttackerController);
BGameState = (BGameState == nullptr) ? Cast<ABlasterGameState>(UGameplayStatics::GetGameState(this)) : BGameState;
const ABlasterPlayerState* AttackerPlayerState = AttackerController ? AttackerController->GetPlayerState<ABlasterPlayerState>() : nullptr;
if (BGameState && AttackerPlayerState)
{
if (AttackerPlayerState->GetTeam() == ETeam::ET_BlueTeam)
{
BGameState->BlueTeamScores();
}
if (AttackerPlayerState->GetTeam() == ETeam::ET_RedTeam)
{
BGameState->RedTeamScores();
}
}
}