215 - Teams Game Mode

This commit is contained in:
Kingsmedia 2022-05-30 16:54:13 +02:00
parent a1892bc4b7
commit 37ee057440
3 changed files with 97 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,67 @@
// 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);
}
}
}

View File

@ -0,0 +1,30 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "BlasterGameMode.h"
#include "TeamsGameMode.generated.h"
/**
*
*/
UCLASS()
class BLASTER_API ATeamsGameMode : public ABlasterGameMode
{
GENERATED_BODY()
public:
virtual void PostLogin(APlayerController* NewPlayer) override;
virtual void Logout(AController* Exiting) override;
protected:
virtual void HandleMatchHasStarted() override;
private:
UPROPERTY()
class ABlasterGameState* BGameState;
void AssignToTeam(APlayerState* PlayerState);
};