From 2515ce4e9c48ac941e87a578164310043e176b03 Mon Sep 17 00:00:00 2001 From: PHILIP WHITE Date: Tue, 9 Jan 2024 09:25:22 +0000 Subject: [PATCH] Update Player Character for Base Scanning Function --- .../Content/Levels/ClientEntry.umap | 2 +- CorruptedMemory/Content/Levels/Main.umap | 2 +- CorruptedMemory/Content/UI/BP_GameUI.uasset | 3 + .../CorruptedMemory/CM_PlayerController.cpp | 6 ++ .../CorruptedMemory/CM_PlayerController.h | 4 ++ .../CorruptedMemoryCharacter.cpp | 66 +++++++++++++++++-- .../CorruptedMemoryCharacter.h | 49 +++++++++++--- .../CorruptedMemoryGameMode.cpp | 33 ++++++++++ .../CorruptedMemory/CorruptedMemoryGameMode.h | 9 ++- 9 files changed, 154 insertions(+), 20 deletions(-) create mode 100644 CorruptedMemory/Content/UI/BP_GameUI.uasset diff --git a/CorruptedMemory/Content/Levels/ClientEntry.umap b/CorruptedMemory/Content/Levels/ClientEntry.umap index 0f6e7c0..b539e2d 100644 --- a/CorruptedMemory/Content/Levels/ClientEntry.umap +++ b/CorruptedMemory/Content/Levels/ClientEntry.umap @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:c3384b0ef9ac9102b4073bc0793ca6fe17e0874a220fe4bb55a21c0d2604c028 +oid sha256:682634b96166cdd01fa16451402719e5e849c8cf7e5f4e8e3511d57a5decbb13 size 42197 diff --git a/CorruptedMemory/Content/Levels/Main.umap b/CorruptedMemory/Content/Levels/Main.umap index ee00431..9ea80a1 100644 --- a/CorruptedMemory/Content/Levels/Main.umap +++ b/CorruptedMemory/Content/Levels/Main.umap @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:176aa0e3af0cb53206b368ca8493438efd106cf7275ee80c5808c70421dc255f +oid sha256:52a5259efe0a304bf08cdbf35cf509dab18fa2dae3ac9da963b6d36d4686bda2 size 901607 diff --git a/CorruptedMemory/Content/UI/BP_GameUI.uasset b/CorruptedMemory/Content/UI/BP_GameUI.uasset new file mode 100644 index 0000000..1ece487 --- /dev/null +++ b/CorruptedMemory/Content/UI/BP_GameUI.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:44c850bde4cbd5839a09e56aa98661c6ed81aae05e343a32f9462d57483e0d2e +size 167096 diff --git a/CorruptedMemory/Source/CorruptedMemory/CM_PlayerController.cpp b/CorruptedMemory/Source/CorruptedMemory/CM_PlayerController.cpp index 5b44a31..174c421 100644 --- a/CorruptedMemory/Source/CorruptedMemory/CM_PlayerController.cpp +++ b/CorruptedMemory/Source/CorruptedMemory/CM_PlayerController.cpp @@ -5,6 +5,12 @@ #include "CorruptedMemoryGameMode.h" +void ACM_PlayerController::ServerAddGameplayTagToShelf_Implementation(AActor* Shelving, const FString& GameplayTag) +{ + if (!GetWorld()->GetAuthGameMode()) return; + Cast(GetWorld()->GetAuthGameMode())->AddGameplayTagToShelf(Shelving, GameplayTag); +} + void ACM_PlayerController::BeginPlay() { Super::BeginPlay(); diff --git a/CorruptedMemory/Source/CorruptedMemory/CM_PlayerController.h b/CorruptedMemory/Source/CorruptedMemory/CM_PlayerController.h index 4babf27..1c03b70 100644 --- a/CorruptedMemory/Source/CorruptedMemory/CM_PlayerController.h +++ b/CorruptedMemory/Source/CorruptedMemory/CM_PlayerController.h @@ -14,6 +14,10 @@ class CORRUPTEDMEMORY_API ACM_PlayerController : public APlayerController { GENERATED_BODY() +public: + UFUNCTION(Server, Reliable) + void ServerAddGameplayTagToShelf(AActor* Shelving, const FString& GameplayTag); + protected: virtual void BeginPlay() override; }; diff --git a/CorruptedMemory/Source/CorruptedMemory/CorruptedMemoryCharacter.cpp b/CorruptedMemory/Source/CorruptedMemory/CorruptedMemoryCharacter.cpp index 6e26094..db1fa09 100644 --- a/CorruptedMemory/Source/CorruptedMemory/CorruptedMemoryCharacter.cpp +++ b/CorruptedMemory/Source/CorruptedMemory/CorruptedMemoryCharacter.cpp @@ -4,6 +4,8 @@ #include "CMGameInstance.h" #include "CM_GameState.h" +#include "CM_PlayerController.h" +#include "CM_Shelving.h" #include "CorruptedMemoryProjectile.h" #include "Animation/AnimInstance.h" #include "Camera/CameraComponent.h" @@ -30,13 +32,14 @@ ACorruptedMemoryCharacter::ACorruptedMemoryCharacter() FirstPersonCameraComponent->bUsePawnControlRotation = true; // Create a mesh component that will be used when being viewed from a '1st person' view (when controlling this pawn) - Mesh1P = CreateDefaultSubobject(TEXT("CharacterMesh1P")); - Mesh1P->SetOnlyOwnerSee(true); - Mesh1P->SetupAttachment(FirstPersonCameraComponent); - Mesh1P->bCastDynamicShadow = false; - Mesh1P->CastShadow = false; - //Mesh1P->SetRelativeRotation(FRotator(0.9f, -19.19f, 5.2f)); - Mesh1P->SetRelativeLocation(FVector(-30.f, 0.f, -150.f)); + // Mesh1P = CreateDefaultSubobject(TEXT("CharacterMesh1P")); + // Mesh1P->SetOnlyOwnerSee(true); + // Mesh1P->SetupAttachment(FirstPersonCameraComponent); + // Mesh1P->bCastDynamicShadow = false; + // Mesh1P->CastShadow = false; + // //Mesh1P->SetRelativeRotation(FRotator(0.9f, -19.19f, 5.2f)); + // Mesh1P->SetRelativeLocation(FVector(-30.f, 0.f, -150.f)); + bReplicates = true; } void ACorruptedMemoryCharacter::BeginPlay() @@ -79,6 +82,10 @@ void ACorruptedMemoryCharacter::SetupPlayerInputComponent(class UInputComponent* //Looking EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ACorruptedMemoryCharacter::Look); + + //Scanning + EnhancedInputComponent->BindAction(ScanAction, ETriggerEvent::Started, this, &ACorruptedMemoryCharacter::ScanStart); + EnhancedInputComponent->BindAction(ScanAction, ETriggerEvent::Canceled, this, &ACorruptedMemoryCharacter::ScanCancel); } } @@ -89,6 +96,24 @@ void ACorruptedMemoryCharacter::LogServerMessage_Implementation(const FString& M GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, Message); } +void ACorruptedMemoryCharacter::Client_EnableUserInput_Implementation() +{ + GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("EnableUserInput_Implementation")); + EnableInput(GetWorld()->GetFirstPlayerController()); + GetWorld()->GetFirstPlayerController()->SetInputMode(FInputModeGameOnly()); +} + +void ACorruptedMemoryCharacter::Client_DisableUserInput_Implementation() +{ + DisableInput(GetWorld()->GetFirstPlayerController()); + GetWorld()->GetFirstPlayerController()->SetInputMode(FInputModeUIOnly()); +} + +void ACorruptedMemoryCharacter::Client_GameStart_Implementation() +{ + OnGameStart.Broadcast(); +} + void ACorruptedMemoryCharacter::Move(const FInputActionValue& Value) { // input is a Vector2D @@ -115,6 +140,33 @@ void ACorruptedMemoryCharacter::Look(const FInputActionValue& Value) } } +void ACorruptedMemoryCharacter::ScanStart(const FInputActionValue& Value) +{ + //Line trace to see if there is a shelf in front of the player + FHitResult HitResult; + FVector StartLocation = FirstPersonCameraComponent->GetComponentLocation(); + FVector EndLocation = StartLocation + (FirstPersonCameraComponent->GetForwardVector() * 1000.f); + FCollisionQueryParams TraceParams; + TraceParams.AddIgnoredActor(this); + TraceParams.AddIgnoredActor(GetOwner()); + TraceParams.bTraceComplex = true; + TraceParams.bReturnPhysicalMaterial = false; + GetWorld()->LineTraceSingleByChannel(HitResult, StartLocation, EndLocation, ECC_Visibility, TraceParams); + + if (!IsValid(HitResult.GetActor())) return; + if (HitResult.GetActor()->Tags.Contains("StoreShelf")) + { + FString AccountName = Cast(GetWorld()->GetGameInstance())->AccountUsername; + if (Cast(HitResult.GetActor())->GameplayTags.Contains(AccountName)) return; + Cast(GetWorld()->GetFirstPlayerController())->ServerAddGameplayTagToShelf(HitResult.GetActor(), AccountName); + ServerSuccessfulScan(AccountName); + } +} + +void ACorruptedMemoryCharacter::ScanCancel(const FInputActionValue& Value) +{ +} + void ACorruptedMemoryCharacter::SetHasRifle(bool bNewHasRifle) { bHasRifle = bNewHasRifle; diff --git a/CorruptedMemory/Source/CorruptedMemory/CorruptedMemoryCharacter.h b/CorruptedMemory/Source/CorruptedMemory/CorruptedMemoryCharacter.h index 110fd5c..bc47616 100644 --- a/CorruptedMemory/Source/CorruptedMemory/CorruptedMemoryCharacter.h +++ b/CorruptedMemory/Source/CorruptedMemory/CorruptedMemoryCharacter.h @@ -6,6 +6,7 @@ #include "GameFramework/Character.h" #include "InputActionValue.h" #include "CM_PlayerState.h" +#include "CorruptedMemoryGameMode.h" #include "CorruptedMemoryCharacter.generated.h" class UInputComponent; @@ -20,9 +21,17 @@ class ACorruptedMemoryCharacter : public ACharacter { GENERATED_BODY() - /** Pawn mesh: 1st person view (arms; seen only by self) */ - UPROPERTY(VisibleDefaultsOnly, Category=Mesh) - USkeletalMeshComponent* Mesh1P; + // /** Pawn mesh: 1st person view (arms; seen only by self) */ + // UPROPERTY(VisibleDefaultsOnly, Category=Mesh) + // USkeletalMeshComponent* Mesh1P; + DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnGameStart); + + DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnGameEnd); + + UPROPERTY(BlueprintAssignable, Category = "Game") + FOnGameStart OnGameStart; + UPROPERTY(BlueprintAssignable, Category = "Game") + FOnGameEnd OnGameEnd; /** First person camera */ UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) @@ -40,7 +49,9 @@ class ACorruptedMemoryCharacter : public ACharacter UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Input, meta=(AllowPrivateAccess = "true")) class UInputAction* MoveAction; - + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Input, meta=(AllowPrivateAccess = "true")) + UInputAction* ScanAction; + public: ACorruptedMemoryCharacter(); @@ -48,7 +59,6 @@ protected: virtual void BeginPlay(); public: - /** Look Input Action */ UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true")) class UInputAction* LookAction; @@ -67,10 +77,21 @@ public: UFUNCTION(Server, Reliable) void ServerPlayerJoin(const FString& PlayerName, const int32& PlayerIndex, const FUniqueNetIdRepl& UNetID); - + UFUNCTION(Server, Reliable) + void ServerSuccessfulScan(const FString& Username); + + UFUNCTION(Client, Reliable) void LogServerMessage(const FString& Message); - + + UFUNCTION(Client, Reliable) + void Client_EnableUserInput(); + UFUNCTION(Client, Reliable) + void Client_DisableUserInput(); + + UFUNCTION(Client, Reliable) + void Client_GameStart(); + protected: /** Called for movement input */ void Move(const FInputActionValue& Value); @@ -78,6 +99,9 @@ protected: /** Called for looking input */ void Look(const FInputActionValue& Value); + void ScanStart(const FInputActionValue& Value); + void ScanCancel(const FInputActionValue& Value); + protected: // APawn interface virtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override; @@ -85,10 +109,15 @@ protected: public: /** Returns Mesh1P subobject **/ - USkeletalMeshComponent* GetMesh1P() const { return Mesh1P; } + // USkeletalMeshComponent* GetMesh1P() const { return Mesh1P; } /** Returns FirstPersonCameraComponent subobject **/ UCameraComponent* GetFirstPersonCameraComponent() const { return FirstPersonCameraComponent; } - - }; +inline void ACorruptedMemoryCharacter::ServerSuccessfulScan_Implementation(const FString& Username) +{ + if (const ACorruptedMemoryGameMode* GameMode = Cast(GetWorld()->GetAuthGameMode())) + { + GameMode->SuccessfulScan(Username); + } +} diff --git a/CorruptedMemory/Source/CorruptedMemory/CorruptedMemoryGameMode.cpp b/CorruptedMemory/Source/CorruptedMemory/CorruptedMemoryGameMode.cpp index af81cf2..90f06b1 100644 --- a/CorruptedMemory/Source/CorruptedMemory/CorruptedMemoryGameMode.cpp +++ b/CorruptedMemory/Source/CorruptedMemory/CorruptedMemoryGameMode.cpp @@ -4,6 +4,7 @@ #include "CMGameInstance.h" #include "CM_GameState.h" +#include "CM_PlayerController.h" #include "CM_PlayerState.h" #include "CorruptedMemoryCharacter.h" #include "GameFramework/GameStateBase.h" @@ -27,6 +28,36 @@ void ACorruptedMemoryGameMode::PostLogin(APlayerController* NewPlayer) { Super::PostLogin(NewPlayer); NewPlayer->GetPlayerState()->ClientPlayerJoin(); + FString MaxPlayers; + FParse::Value(FCommandLine::Get(), TEXT("maxPlayers="), MaxPlayers); + if (GetWorld()->GetGameState()->PlayerArray.Num() == FCString::Atoi(*MaxPlayers)) + { + auto World = GetWorld(); + FTimerHandle TimerHandle; + GetWorld()->GetTimerManager().SetTimer(TimerHandle, [World]() + { + for (const auto PlayerState : World->GetGameState()->PlayerArray) + { + Cast(PlayerState->GetPlayerController()->GetCharacter())->Client_GameStart(); + Cast(PlayerState->GetPlayerController()->GetCharacter())->Client_EnableUserInput(); + PlayerState->GetPlayerController()->EnableInput(PlayerState->GetPlayerController()); + PlayerState->GetPlayerController()->SetInputMode(FInputModeGameOnly()); + } + }, 5.f, false); + } +} + +void ACorruptedMemoryGameMode::SuccessfulScan(const FString& Username) const +{ + ACM_GameState* CurrentGameState = GetWorld()->GetGameState(); + const auto PlayerState = CurrentGameState->PlayerArray.FindByPredicate([Username](const APlayerState* PlayerState) { return PlayerState->GetPlayerName() == Username; }); + PlayerState->Get()->SetScore(PlayerState->Get()->GetScore() + SuccessfulScanScore); +} + +void ACorruptedMemoryGameMode::AddGameplayTagToShelf(AActor* Shelving, const FString& GameplayTag) +{ + ACM_Shelving** FoundShelf = Shelves.FindByPredicate([Shelving](const ACM_Shelving* Shelf) { return Shelf == Shelving; }); + (*FoundShelf)->GameplayTags.Add(GameplayTag); } void ACorruptedMemoryGameMode::BeginPlay() @@ -65,4 +96,6 @@ void ACorruptedMemoryGameMode::ServerSpawnPlayer_Implementation(APlayerControlle } ACharacter* SpawnedPlayerCharacter = GetWorld()->SpawnActor(PlayerCharacterClass, SpawnPoints[PlayerController->GetPlayerState()->GetPlayerId()]); PlayerController->Possess(SpawnedPlayerCharacter); + PlayerController->EnableInput(PlayerController); + PlayerController->SetInputMode(FInputModeGameOnly()); } diff --git a/CorruptedMemory/Source/CorruptedMemory/CorruptedMemoryGameMode.h b/CorruptedMemory/Source/CorruptedMemory/CorruptedMemoryGameMode.h index 3cca837..0712a57 100644 --- a/CorruptedMemory/Source/CorruptedMemory/CorruptedMemoryGameMode.h +++ b/CorruptedMemory/Source/CorruptedMemory/CorruptedMemoryGameMode.h @@ -14,11 +14,19 @@ class ACorruptedMemoryGameMode : public AGameModeBase public: ACorruptedMemoryGameMode(); + + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Game") + int SuccessfulScanScore = 10; + virtual void PostLogin(APlayerController* NewPlayer) override; UFUNCTION(Server, Reliable) void ServerSpawnPlayer(APlayerController* PlayerController); + void SuccessfulScan(const FString& Username) const; + + void AddGameplayTagToShelf(AActor* Shelving, const FString& GameplayTag); + protected: virtual void BeginPlay() override; @@ -29,5 +37,4 @@ private: TArray Shelves; TSubclassOf PlayerCharacterClass; - };