From 283b53624240e573040ab842ece2e92a75373d00 Mon Sep 17 00:00:00 2001 From: baz Date: Thu, 4 Jan 2024 20:31:44 +0000 Subject: [PATCH] Add Crouch functionality --- Source/Nakatomi/NakatomiCMC.cpp | 21 +++++++++++---- Source/Nakatomi/NakatomiCMC.h | 6 +++++ Source/Nakatomi/PlayerCharacter.cpp | 40 +++++++++++++++++++++++++---- Source/Nakatomi/PlayerCharacter.h | 7 +++++ 4 files changed, 64 insertions(+), 10 deletions(-) diff --git a/Source/Nakatomi/NakatomiCMC.cpp b/Source/Nakatomi/NakatomiCMC.cpp index 7da097f..ab8e374 100644 --- a/Source/Nakatomi/NakatomiCMC.cpp +++ b/Source/Nakatomi/NakatomiCMC.cpp @@ -5,6 +5,11 @@ #include +UNakatomiCMC::UNakatomiCMC(): Safe_bWantsToSprint(false) +{ + NavAgentProps.bCanCrouch = true; +} + // Checks if we can combine the NewMove with the current move to save on data. // If all the data in a saved move is identical, if true, we cam tell the server to run the existing move instead. bool UNakatomiCMC::FSavedMove_Nakatomi::CanCombineWith(const FSavedMovePtr& NewMove, ACharacter* InCharacter, @@ -83,10 +88,6 @@ FNetworkPredictionData_Client* UNakatomiCMC::GetPredictionData_Client() const return ClientPredictionData; } -UNakatomiCMC::UNakatomiCMC(): Safe_bWantsToSprint(false) -{ -} - void UNakatomiCMC::UpdateFromCompressedFlags(uint8 Flags) { Super::UpdateFromCompressedFlags(Flags); @@ -119,4 +120,14 @@ void UNakatomiCMC::EnableSprint() void UNakatomiCMC::DisableSprint() { Safe_bWantsToSprint = false; -} \ No newline at end of file +} + +void UNakatomiCMC::EnableCrouch() +{ + bWantsToCrouch = true; +} + +void UNakatomiCMC::DisableCrouch() +{ + bWantsToCrouch = false; +} diff --git a/Source/Nakatomi/NakatomiCMC.h b/Source/Nakatomi/NakatomiCMC.h index 16f41cd..689975e 100644 --- a/Source/Nakatomi/NakatomiCMC.h +++ b/Source/Nakatomi/NakatomiCMC.h @@ -57,4 +57,10 @@ public: UFUNCTION(BlueprintCallable) void DisableSprint(); + + UFUNCTION(BlueprintCallable) + void EnableCrouch(); + + UFUNCTION(BlueprintCallable) + void DisableCrouch(); }; diff --git a/Source/Nakatomi/PlayerCharacter.cpp b/Source/Nakatomi/PlayerCharacter.cpp index ba09290..e17a04f 100644 --- a/Source/Nakatomi/PlayerCharacter.cpp +++ b/Source/Nakatomi/PlayerCharacter.cpp @@ -187,6 +187,12 @@ void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputCom { Input->BindAction(PauseAction, ETriggerEvent::Completed, this, &APlayerCharacter::PauseCallback); } + + if (CrouchAction) + { + Input->BindAction(CrouchAction, ETriggerEvent::Started, this, &APlayerCharacter::BeginCrouchCallback); + Input->BindAction(CrouchAction, ETriggerEvent::Completed, this, &APlayerCharacter::EndCrouchCallback); + } } } @@ -268,17 +274,21 @@ void APlayerCharacter::QuitCallback(const FInputActionInstance& Instance) void APlayerCharacter::SetSprintingCallback(const FInputActionInstance& Instance) { - if (NakatomiCMC) + UNakatomiCMC* cmc = GetCharacterMovementComponent(); + + if (cmc) { - NakatomiCMC->EnableSprint(); + cmc->EnableSprint(); } } void APlayerCharacter::SetWalkingCallback(const FInputActionInstance& Instance) { - if (NakatomiCMC) + UNakatomiCMC* cmc = GetCharacterMovementComponent(); + + if (cmc) { - NakatomiCMC->DisableSprint(); + cmc->DisableSprint(); } } @@ -536,6 +546,26 @@ void APlayerCharacter::PauseCallback(const FInputActionInstance& Instance) } } +void APlayerCharacter::BeginCrouchCallback(const FInputActionInstance& Instance) +{ + UNakatomiCMC* cmc = GetCharacterMovementComponent(); + + if (cmc) + { + cmc->EnableCrouch(); + } +} + +void APlayerCharacter::EndCrouchCallback(const FInputActionInstance& Instance) +{ + UNakatomiCMC* cmc = GetCharacterMovementComponent(); + + if (cmc) + { + cmc->DisableCrouch(); + } +} + void APlayerCharacter::OnFire() { if (!IsFiring || CurrentWeapon->GetAmmoCount() == 0) @@ -684,4 +714,4 @@ AThrowable* APlayerCharacter::ThrowThrowable() } return nullptr; -} +} \ No newline at end of file diff --git a/Source/Nakatomi/PlayerCharacter.h b/Source/Nakatomi/PlayerCharacter.h index 7507789..5f75c86 100644 --- a/Source/Nakatomi/PlayerCharacter.h +++ b/Source/Nakatomi/PlayerCharacter.h @@ -64,6 +64,9 @@ public: UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UInputAction* AimDownSightsAction; + + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + UInputAction* CrouchAction; UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) TSoftObjectPtr InputMappingContext; @@ -174,6 +177,10 @@ public: void EndAimDownSightsCallback(const FInputActionInstance& Instance); void PauseCallback(const FInputActionInstance& Instance); + + void BeginCrouchCallback(const FInputActionInstance& Instance); + + void EndCrouchCallback(const FInputActionInstance& Instance); virtual void OnFire() override;