// Fill out your copyright notice in the Description page of Project Settings. #include "TeamsGameMode.h" #include "Blaster/GameState/BlasterGameState.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(UGameplayStatics::GetGameState(this)) : BGameState; ABlasterPlayerState* BPState = Exiting->GetPlayerState(); 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(UGameplayStatics::GetGameState(this)) : BGameState; if (BGameState == nullptr) return; for (auto PState : BGameState->PlayerArray) { AssignToTeam(PState); } } void ATeamsGameMode::AssignToTeam(APlayerState* PlayerState) { BGameState = (BGameState == nullptr) ? Cast(UGameplayStatics::GetGameState(this)) : BGameState; if (PlayerState == nullptr) return; ABlasterPlayerState* BPlayerState = Cast(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* VictimPState = Victim->GetPlayerState(); if (AttackerPState == nullptr || VictimPState == nullptr) return BaseDamage; if (VictimPState == AttackerPState) return BaseDamage; if (AttackerPState->GetTeam() == VictimPState->GetTeam() && !FriendlyFire) return 0.f; return BaseDamage; }