Add Player Start Spot
This commit is contained in:
@@ -2,3 +2,46 @@
|
||||
|
||||
|
||||
#include "Crunch/Public/Game/CGameMode.h"
|
||||
|
||||
#include "EngineUtils.h"
|
||||
#include "GameFramework/PlayerStart.h"
|
||||
|
||||
APlayerController* ACGameMode::SpawnPlayerController(ENetRole InRemoteRole, const FString& Options)
|
||||
{
|
||||
const auto NewPlayerController{Super::SpawnPlayerController(InRemoteRole, Options)};
|
||||
const auto NewPlayerControllerTeamInterface{Cast<IGenericTeamAgentInterface>(NewPlayerController)};
|
||||
const FGenericTeamId TeamId{GetTeamIDForPlayer(NewPlayerController)};
|
||||
if (NewPlayerControllerTeamInterface)
|
||||
{
|
||||
NewPlayerControllerTeamInterface->SetGenericTeamId(TeamId);
|
||||
}
|
||||
NewPlayerController->StartSpot = FindNextStartSpotForTeam(TeamId);
|
||||
|
||||
return NewPlayerController;
|
||||
}
|
||||
|
||||
FGenericTeamId ACGameMode::GetTeamIDForPlayer(const APlayerController* PlayerController) const
|
||||
{
|
||||
static int PlayerCount{0};
|
||||
++PlayerCount;
|
||||
return FGenericTeamId(PlayerCount % 2);
|
||||
}
|
||||
|
||||
AActor* ACGameMode::FindNextStartSpotForTeam(const FGenericTeamId& TeamId) const
|
||||
{
|
||||
const auto StartSpotTag{TeamStartSpotTagMap.Find(TeamId)};
|
||||
if (!StartSpotTag)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
auto World{GetWorld()};
|
||||
for (TActorIterator<APlayerStart> It(World); It; ++It)
|
||||
{
|
||||
if (It->PlayerStartTag == *StartSpotTag)
|
||||
{
|
||||
It->PlayerStartTag = FName("Taken");
|
||||
return *It;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "GenericTeamAgentInterface.h"
|
||||
#include "GameFramework/GameModeBase.h"
|
||||
#include "CGameMode.generated.h"
|
||||
|
||||
@@ -10,4 +11,13 @@ UCLASS()
|
||||
class CRUNCH_API ACGameMode : public AGameModeBase
|
||||
{
|
||||
GENERATED_BODY()
|
||||
public:
|
||||
virtual APlayerController* SpawnPlayerController(ENetRole InRemoteRole, const FString& Options) override;
|
||||
private:
|
||||
FGenericTeamId GetTeamIDForPlayer(const APlayerController* PlayerController) const;
|
||||
|
||||
AActor* FindNextStartSpotForTeam(const FGenericTeamId& TeamId) const;
|
||||
|
||||
UPROPERTY(EditDefaultsOnly, Category="Team")
|
||||
TMap<FGenericTeamId, FName> TeamStartSpotTagMap;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user