Update Game Mode for Basic Player Spawning

This commit is contained in:
Philip W 2024-01-09 04:53:28 +00:00
parent 1169adc721
commit 3345a4d27d
23 changed files with 314 additions and 18 deletions

BIN
CorruptedMemory/Content/BP_CMGameMode.uasset (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:5a1f6f15d2bcd70e7fde7988ed7890d1048dcb13dbd3aa3d53da77443b0cd6bc
size 30219

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:185f092ca45fc44e8c25644e92e32ff0b5bcd6727ba98871a52bdb67e21d00e4
size 925096

View File

@ -386,6 +386,10 @@ void UCMGameInstance::OnRequestToGetLobbyPlayersComplete(FHttpRequestPtr Request
const auto JsonIsReady = Player->AsObject()->GetBoolField("isReady"); const auto JsonIsReady = Player->AsObject()->GetBoolField("isReady");
PlayersInLobby.Add(FPlayerInLobby(JsonUsername, JsonIsOwner, JsonIsReady)); PlayersInLobby.Add(FPlayerInLobby(JsonUsername, JsonIsOwner, JsonIsReady));
} }
PlayerIndex = PlayersInLobby.IndexOfByPredicate([this](const FPlayerInLobby& Player)
{
return Player.Username == AccountUsername;
});
OnGetPlayersInLobbyComplete.Broadcast(true); OnGetPlayersInLobbyComplete.Broadcast(true);
} }
else else

View File

@ -74,9 +74,11 @@ public:
FOnServerClose OnServerClose; FOnServerClose OnServerClose;
UPROPERTY(BlueprintReadOnly, Category = "Account") UPROPERTY(BlueprintReadOnly, Category = "Account")
FString AccountUsername; FString AccountUsername = "Bruh";
UPROPERTY(BlueprintReadOnly, Category = "Account") UPROPERTY(BlueprintReadOnly, Category = "Account")
FString JwtToken; FString JwtToken;
UPROPERTY(BlueprintReadOnly, Category = "Account")
int32 PlayerIndex = 0;
UPROPERTY(BlueprintReadOnly, Category = "Lobby") UPROPERTY(BlueprintReadOnly, Category = "Lobby")
FString ServerFQDN = "cm.api.philipwhite.dev"; FString ServerFQDN = "cm.api.philipwhite.dev";

View File

@ -0,0 +1,10 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "CM_GameState.h"
#include "CMGameInstance.h"
#include "CorruptedMemoryGameMode.h"
#include "GameFramework/PlayerState.h"

View File

@ -0,0 +1,20 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameState.h"
#include "CM_GameState.generated.h"
/**
*
*/
UCLASS()
class CORRUPTEDMEMORY_API ACM_GameState : public AGameStateBase
{
GENERATED_BODY()
private:
UPROPERTY()
APlayerState* PlayerState;
};

View File

@ -0,0 +1,16 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "CM_PlayerController.h"
#include "CorruptedMemoryGameMode.h"
void ACM_PlayerController::BeginPlay()
{
Super::BeginPlay();
FString MapName = GetWorld()->GetMapName();
MapName.RemoveFromStart(GetWorld()->StreamingLevelsPrefix);
if (MapName == "ClientEntry") return;
if (!GetWorld()->GetAuthGameMode()) return;
Cast<ACorruptedMemoryGameMode>(GetWorld()->GetAuthGameMode())->ServerSpawnPlayer(this);
}

View File

@ -0,0 +1,19 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "CM_PlayerController.generated.h"
/**
*
*/
UCLASS()
class CORRUPTEDMEMORY_API ACM_PlayerController : public APlayerController
{
GENERATED_BODY()
protected:
virtual void BeginPlay() override;
};

View File

@ -0,0 +1,24 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "CM_PlayerState.h"
#include "CMGameInstance.h"
#include "CM_GameState.h"
#include "CorruptedMemoryCharacter.h"
void ACM_PlayerState::ClientPlayerJoin_Implementation()
{
if (GetWorld()->GetAuthGameMode()) return;
const UCMGameInstance* GameInstance = Cast<UCMGameInstance>(GetWorld()->GetGameInstance());
if (GetPlayerName() == GameInstance->AccountUsername) return;
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::White, FString::Printf(TEXT("Player %s Joined %s"), *GetPlayerName(), *FString::FromInt(GameInstance->PlayerIndex)));
if (!IsValid(Cast<ACorruptedMemoryCharacter>(GetWorld()->GetFirstPlayerController()->GetCharacter())))
{
UE_LOG(LogTemp, Warning, TEXT("Invalid Character Queueing Server Request"));
bServerRequestUserInformation = true;
return;
}
Cast<ACorruptedMemoryCharacter>(GetWorld()->GetFirstPlayerController()->GetCharacter())->ServerPlayerJoin(GameInstance->AccountUsername, GameInstance->PlayerIndex, GetUniqueId());
}

View File

@ -0,0 +1,23 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerState.h"
#include "CM_PlayerState.generated.h"
/**
*
*/
UCLASS()
class CORRUPTEDMEMORY_API ACM_PlayerState : public APlayerState
{
GENERATED_BODY()
public:
UFUNCTION(Client, Reliable)
void ClientPlayerJoin();
UPROPERTY(BlueprintReadOnly)
bool bServerRequestUserInformation = false;
};

View File

@ -0,0 +1,33 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "CM_Shelving.h"
#include "Net/UnrealNetwork.h"
// Sets default values
ACM_Shelving::ACM_Shelving()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
bReplicates = true;
}
// Called when the game starts or when spawned
void ACM_Shelving::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ACM_Shelving::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ACM_Shelving::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(ACM_Shelving, GameplayTags);
}

View File

@ -0,0 +1,29 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CM_Shelving.generated.h"
UCLASS()
class CORRUPTEDMEMORY_API ACM_Shelving : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ACM_Shelving();
UPROPERTY(Replicated, BlueprintReadOnly, VisibleAnywhere)
TArray<FString> GameplayTags;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
};

View File

@ -1,6 +1,9 @@
// Copyright Epic Games, Inc. All Rights Reserved. // Copyright Epic Games, Inc. All Rights Reserved.
#include "CorruptedMemoryCharacter.h" #include "CorruptedMemoryCharacter.h"
#include "CMGameInstance.h"
#include "CM_GameState.h"
#include "CorruptedMemoryProjectile.h" #include "CorruptedMemoryProjectile.h"
#include "Animation/AnimInstance.h" #include "Animation/AnimInstance.h"
#include "Camera/CameraComponent.h" #include "Camera/CameraComponent.h"
@ -34,7 +37,6 @@ ACorruptedMemoryCharacter::ACorruptedMemoryCharacter()
Mesh1P->CastShadow = false; Mesh1P->CastShadow = false;
//Mesh1P->SetRelativeRotation(FRotator(0.9f, -19.19f, 5.2f)); //Mesh1P->SetRelativeRotation(FRotator(0.9f, -19.19f, 5.2f));
Mesh1P->SetRelativeLocation(FVector(-30.f, 0.f, -150.f)); Mesh1P->SetRelativeLocation(FVector(-30.f, 0.f, -150.f));
} }
void ACorruptedMemoryCharacter::BeginPlay() void ACorruptedMemoryCharacter::BeginPlay()
@ -51,6 +53,14 @@ void ACorruptedMemoryCharacter::BeginPlay()
} }
} }
if (GetWorld()->GetAuthGameMode()) return;
if (!IsValid(GetPlayerState<ACM_PlayerState>())) return;
if (GetPlayerState<ACM_PlayerState>()->bServerRequestUserInformation)
{
GetPlayerState<ACM_PlayerState>()->bServerRequestUserInformation = false;
const UCMGameInstance* GameInstance = Cast<UCMGameInstance>(GetWorld()->GetGameInstance());
ServerPlayerJoin(GameInstance->AccountUsername, GameInstance->PlayerIndex, GetPlayerState<ACM_PlayerState>()->GetUniqueId());
}
} }
//////////////////////////////////////////////////////////////////////////// Input //////////////////////////////////////////////////////////////////////////// Input
@ -73,6 +83,12 @@ void ACorruptedMemoryCharacter::SetupPlayerInputComponent(class UInputComponent*
} }
void ACorruptedMemoryCharacter::LogServerMessage_Implementation(const FString& Message)
{
if (GetWorld()->GetAuthGameMode()) return;
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, Message);
}
void ACorruptedMemoryCharacter::Move(const FInputActionValue& Value) void ACorruptedMemoryCharacter::Move(const FInputActionValue& Value)
{ {
// input is a Vector2D // input is a Vector2D
@ -108,3 +124,16 @@ bool ACorruptedMemoryCharacter::GetHasRifle()
{ {
return bHasRifle; return bHasRifle;
} }
void ACorruptedMemoryCharacter::ServerPlayerJoin_Implementation(const FString& PlayerName, const int32& PlayerIndex, const FUniqueNetIdRepl& UNetID)
{
if (!GetWorld()->GetAuthGameMode()) return;
if (const auto PlayerStateObjPtr = Cast<ACM_GameState>(GetWorld()->GetGameState())->PlayerArray.FindByPredicate([&](const APlayerState* Player)
{
return Player->GetUniqueId() == UNetID;
}))
{
PlayerStateObjPtr->Get()->SetPlayerName(PlayerName);
PlayerStateObjPtr->Get()->SetPlayerId(PlayerIndex);
}
}

View File

@ -5,6 +5,7 @@
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "GameFramework/Character.h" #include "GameFramework/Character.h"
#include "InputActionValue.h" #include "InputActionValue.h"
#include "CM_PlayerState.h"
#include "CorruptedMemoryCharacter.generated.h" #include "CorruptedMemoryCharacter.generated.h"
class UInputComponent; class UInputComponent;
@ -64,6 +65,12 @@ public:
UFUNCTION(BlueprintCallable, Category = Weapon) UFUNCTION(BlueprintCallable, Category = Weapon)
bool GetHasRifle(); bool GetHasRifle();
UFUNCTION(Server, Reliable)
void ServerPlayerJoin(const FString& PlayerName, const int32& PlayerIndex, const FUniqueNetIdRepl& UNetID);
UFUNCTION(Client, Reliable)
void LogServerMessage(const FString& Message);
protected: protected:
/** Called for movement input */ /** Called for movement input */
void Move(const FInputActionValue& Value); void Move(const FInputActionValue& Value);

View File

@ -1,7 +1,15 @@
// Copyright Epic Games, Inc. All Rights Reserved. // Copyright Epic Games, Inc. All Rights Reserved.
#include "CorruptedMemoryGameMode.h" #include "CorruptedMemoryGameMode.h"
#include "CMGameInstance.h"
#include "CM_GameState.h"
#include "CM_PlayerState.h"
#include "CorruptedMemoryCharacter.h" #include "CorruptedMemoryCharacter.h"
#include "GameFramework/GameStateBase.h"
#include "GameFramework/PlayerStart.h"
#include "GameFramework/PlayerState.h"
#include "Kismet/GameplayStatics.h"
#include "UObject/ConstructorHelpers.h" #include "UObject/ConstructorHelpers.h"
ACorruptedMemoryGameMode::ACorruptedMemoryGameMode() ACorruptedMemoryGameMode::ACorruptedMemoryGameMode()
@ -11,4 +19,50 @@ ACorruptedMemoryGameMode::ACorruptedMemoryGameMode()
static ConstructorHelpers::FClassFinder<APawn> PlayerPawnClassFinder(TEXT("/Game/FirstPerson/Blueprints/BP_FirstPersonCharacter")); static ConstructorHelpers::FClassFinder<APawn> PlayerPawnClassFinder(TEXT("/Game/FirstPerson/Blueprints/BP_FirstPersonCharacter"));
DefaultPawnClass = PlayerPawnClassFinder.Class; DefaultPawnClass = PlayerPawnClassFinder.Class;
static ConstructorHelpers::FClassFinder<ACharacter> PlayerCharacterClassFinder(TEXT("/Game/FirstPerson/Blueprints/BP_FirstPersonCharacter"));
PlayerCharacterClass = PlayerCharacterClassFinder.Class;
}
void ACorruptedMemoryGameMode::PostLogin(APlayerController* NewPlayer)
{
Super::PostLogin(NewPlayer);
NewPlayer->GetPlayerState<ACM_PlayerState>()->ClientPlayerJoin();
}
void ACorruptedMemoryGameMode::BeginPlay()
{
Super::BeginPlay();
FString MapName = GetWorld()->GetMapName();
MapName.RemoveFromStart(GetWorld()->StreamingLevelsPrefix);
if (MapName == "ClientEntry") return;
//Gets all PlayerStart Actors and sorts them by PlayerStartTag
TArray<AActor*> PlayerStartActors;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), APlayerStart::StaticClass(), PlayerStartActors);
PlayerStartActors.Sort([](const AActor& A, const AActor& B) { return FCString::Atoi(*Cast<APlayerStart>(&A)->PlayerStartTag.ToString()) < FCString::Atoi(*Cast<APlayerStart>(&B)->PlayerStartTag.ToString()); });
for (const AActor* PlayerStartActor : PlayerStartActors)
{
SpawnPoints.Add(PlayerStartActor->GetActorTransform());
}
//Gets all store shelves in level
TArray<AActor*> StoreShelves;
UGameplayStatics::GetAllActorsWithTag(GetWorld(), "StoreShelf", StoreShelves);
for (AActor* StoreShelf : StoreShelves)
{
Shelves.Add(Cast<ACM_Shelving>(StoreShelf));
}
Shelves[0]->GameplayTags.Add("Corrupted");
}
void ACorruptedMemoryGameMode::ServerSpawnPlayer_Implementation(APlayerController* PlayerController)
{
if (!IsValid(PlayerController)) return;
if (IsValid(PlayerController->GetPawn()))
{
PlayerController->GetPawn()->Destroy();
}
ACharacter* SpawnedPlayerCharacter = GetWorld()->SpawnActor<ACharacter>(PlayerCharacterClass, SpawnPoints[PlayerController->GetPlayerState<ACM_PlayerState>()->GetPlayerId()]);
PlayerController->Possess(SpawnedPlayerCharacter);
} }

View File

@ -3,6 +3,7 @@
#pragma once #pragma once
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "CM_Shelving.h"
#include "GameFramework/GameModeBase.h" #include "GameFramework/GameModeBase.h"
#include "CorruptedMemoryGameMode.generated.h" #include "CorruptedMemoryGameMode.generated.h"
@ -13,7 +14,20 @@ class ACorruptedMemoryGameMode : public AGameModeBase
public: public:
ACorruptedMemoryGameMode(); ACorruptedMemoryGameMode();
virtual void PostLogin(APlayerController* NewPlayer) override;
UFUNCTION(Server, Reliable)
void ServerSpawnPlayer(APlayerController* PlayerController);
protected:
virtual void BeginPlay() override;
private:
UPROPERTY(BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
TArray<FTransform> SpawnPoints;
UPROPERTY(BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
TArray<ACM_Shelving*> Shelves;
TSubclassOf<ACharacter> PlayerCharacterClass;
}; };