Implement crouch and prone stances
This commit is contained in:
@@ -31,6 +31,7 @@ AAgrarianGameCharacter::AAgrarianGameCharacter()
|
||||
bUseControllerRotationRoll = false;
|
||||
|
||||
// Configure character movement
|
||||
GetCharacterMovement()->NavAgentProps.bCanCrouch = true;
|
||||
GetCharacterMovement()->bOrientRotationToMovement = true;
|
||||
GetCharacterMovement()->RotationRate = FRotator(0.0f, 500.0f, 0.0f);
|
||||
|
||||
@@ -39,6 +40,7 @@ AAgrarianGameCharacter::AAgrarianGameCharacter()
|
||||
GetCharacterMovement()->JumpZVelocity = 500.f;
|
||||
GetCharacterMovement()->AirControl = 0.35f;
|
||||
GetCharacterMovement()->MaxWalkSpeed = WalkSpeed;
|
||||
GetCharacterMovement()->MaxWalkSpeedCrouched = WalkSpeed * CrouchSpeedMultiplier;
|
||||
GetCharacterMovement()->MinAnalogWalkSpeed = 20.f;
|
||||
GetCharacterMovement()->BrakingDecelerationWalking = 2000.f;
|
||||
GetCharacterMovement()->BrakingDecelerationFalling = 1500.0f;
|
||||
@@ -95,6 +97,7 @@ void AAgrarianGameCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty
|
||||
{
|
||||
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
|
||||
DOREPLIFETIME(AAgrarianGameCharacter, bWantsToSprint);
|
||||
DOREPLIFETIME(AAgrarianGameCharacter, bIsProne);
|
||||
DOREPLIFETIME(AAgrarianGameCharacter, AgeYears);
|
||||
DOREPLIFETIME(AAgrarianGameCharacter, PhysicalConditionMultiplier);
|
||||
DOREPLIFETIME(AAgrarianGameCharacter, StrengthMultiplier);
|
||||
@@ -130,6 +133,16 @@ void AAgrarianGameCharacter::SetupPlayerInputComponent(UInputComponent* PlayerIn
|
||||
EnhancedInputComponent->BindAction(SprintAction, ETriggerEvent::Canceled, this, &AAgrarianGameCharacter::StopSprint);
|
||||
}
|
||||
|
||||
if (CrouchAction)
|
||||
{
|
||||
EnhancedInputComponent->BindAction(CrouchAction, ETriggerEvent::Started, this, &AAgrarianGameCharacter::ToggleCrouchStance);
|
||||
}
|
||||
|
||||
if (ProneAction)
|
||||
{
|
||||
EnhancedInputComponent->BindAction(ProneAction, ETriggerEvent::Started, this, &AAgrarianGameCharacter::ToggleProneStance);
|
||||
}
|
||||
|
||||
if (ToggleCameraAction)
|
||||
{
|
||||
EnhancedInputComponent->BindAction(ToggleCameraAction, ETriggerEvent::Started, this, &AAgrarianGameCharacter::ToggleCameraPerspective);
|
||||
@@ -166,6 +179,11 @@ void AAgrarianGameCharacter::Interact()
|
||||
|
||||
void AAgrarianGameCharacter::StartSprint()
|
||||
{
|
||||
if (bIsProne || bIsCrouched)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
SetWantsToSprint(true);
|
||||
}
|
||||
|
||||
@@ -174,6 +192,32 @@ void AAgrarianGameCharacter::StopSprint()
|
||||
SetWantsToSprint(false);
|
||||
}
|
||||
|
||||
void AAgrarianGameCharacter::ToggleCrouchStance()
|
||||
{
|
||||
if (bIsProne)
|
||||
{
|
||||
SetProne(false);
|
||||
return;
|
||||
}
|
||||
|
||||
SetWantsToSprint(false);
|
||||
if (bIsCrouched)
|
||||
{
|
||||
UnCrouch();
|
||||
}
|
||||
else
|
||||
{
|
||||
Crouch();
|
||||
}
|
||||
|
||||
ApplyMovementSpeed();
|
||||
}
|
||||
|
||||
void AAgrarianGameCharacter::ToggleProneStance()
|
||||
{
|
||||
SetProne(!bIsProne);
|
||||
}
|
||||
|
||||
void AAgrarianGameCharacter::ToggleCameraPerspective()
|
||||
{
|
||||
SetFirstPersonCamera(!bFirstPersonCamera);
|
||||
@@ -235,6 +279,8 @@ bool AAgrarianGameCharacter::CanSprint() const
|
||||
{
|
||||
return SurvivalComponent
|
||||
&& SurvivalComponent->IsAlive()
|
||||
&& !bIsProne
|
||||
&& !bIsCrouched
|
||||
&& SurvivalComponent->Survival.Stamina > MinSprintStamina;
|
||||
}
|
||||
|
||||
@@ -253,6 +299,29 @@ float AAgrarianGameCharacter::GetCurrentMovementSpeedMultiplier() const
|
||||
return CalculateMovementSpeedMultiplier();
|
||||
}
|
||||
|
||||
void AAgrarianGameCharacter::SetProne(bool bNewProne)
|
||||
{
|
||||
if (bIsProne == bNewProne)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bIsProne = bNewProne;
|
||||
|
||||
if (bIsProne)
|
||||
{
|
||||
SetWantsToSprint(false);
|
||||
UnCrouch();
|
||||
}
|
||||
|
||||
ApplyMovementSpeed();
|
||||
|
||||
if (!HasAuthority())
|
||||
{
|
||||
ServerSetProne(bIsProne);
|
||||
}
|
||||
}
|
||||
|
||||
void AAgrarianGameCharacter::SetTerrainMovementMultiplier(float NewTerrainMovementMultiplier)
|
||||
{
|
||||
TerrainMovementMultiplier = FMath::Clamp(NewTerrainMovementMultiplier, 0.25f, 1.25f);
|
||||
@@ -264,7 +333,9 @@ void AAgrarianGameCharacter::ApplyMovementSpeed()
|
||||
if (UCharacterMovementComponent* MovementComponent = GetCharacterMovement())
|
||||
{
|
||||
const float BaseSpeed = IsSprinting() ? SprintSpeed : WalkSpeed;
|
||||
MovementComponent->MaxWalkSpeed = FMath::Max(0.0f, BaseSpeed * CalculateMovementSpeedMultiplier());
|
||||
const float FinalSpeed = FMath::Max(0.0f, BaseSpeed * CalculateMovementSpeedMultiplier());
|
||||
MovementComponent->MaxWalkSpeed = FinalSpeed;
|
||||
MovementComponent->MaxWalkSpeedCrouched = FinalSpeed;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -278,6 +349,7 @@ float AAgrarianGameCharacter::CalculateMovementSpeedMultiplier() const
|
||||
* TraitMultiplier
|
||||
* CalculateSurvivalMovementMultiplier()
|
||||
* CalculateCarryWeightMovementMultiplier()
|
||||
* CalculateStanceMovementMultiplier()
|
||||
* FMath::Clamp(TerrainMovementMultiplier, 0.25f, 1.25f),
|
||||
0.15f,
|
||||
1.35f);
|
||||
@@ -353,11 +425,36 @@ float AAgrarianGameCharacter::CalculateCarryWeightMovementMultiplier() const
|
||||
CurrentCarryWeight);
|
||||
}
|
||||
|
||||
float AAgrarianGameCharacter::CalculateStanceMovementMultiplier() const
|
||||
{
|
||||
if (bIsProne)
|
||||
{
|
||||
return FMath::Clamp(ProneSpeedMultiplier, 0.05f, 1.0f);
|
||||
}
|
||||
|
||||
if (bIsCrouched)
|
||||
{
|
||||
return FMath::Clamp(CrouchSpeedMultiplier, 0.1f, 1.0f);
|
||||
}
|
||||
|
||||
return 1.0f;
|
||||
}
|
||||
|
||||
void AAgrarianGameCharacter::OnRep_SprintState()
|
||||
{
|
||||
ApplyMovementSpeed();
|
||||
}
|
||||
|
||||
void AAgrarianGameCharacter::OnRep_ProneState()
|
||||
{
|
||||
if (bIsProne)
|
||||
{
|
||||
UnCrouch();
|
||||
}
|
||||
|
||||
ApplyMovementSpeed();
|
||||
}
|
||||
|
||||
void AAgrarianGameCharacter::DoMove(float Right, float Forward)
|
||||
{
|
||||
if (GetController() != nullptr)
|
||||
@@ -448,6 +545,11 @@ void AAgrarianGameCharacter::ServerSetWantsToSprint_Implementation(bool bNewWant
|
||||
SetWantsToSprint(bNewWantsToSprint);
|
||||
}
|
||||
|
||||
void AAgrarianGameCharacter::ServerSetProne_Implementation(bool bNewProne)
|
||||
{
|
||||
SetProne(bNewProne);
|
||||
}
|
||||
|
||||
void AAgrarianGameCharacter::ServerInteract_Implementation(AActor* TargetActor)
|
||||
{
|
||||
if (!TargetActor || !TargetActor->GetClass()->ImplementsInterface(UAgrarianInteractable::StaticClass()))
|
||||
|
||||
@@ -77,6 +77,14 @@ protected:
|
||||
UPROPERTY(EditAnywhere, Category="Input")
|
||||
UInputAction* SprintAction;
|
||||
|
||||
/** Toggle crouch movement stance. */
|
||||
UPROPERTY(EditAnywhere, Category="Input")
|
||||
UInputAction* CrouchAction;
|
||||
|
||||
/** Toggle prone movement stance. */
|
||||
UPROPERTY(EditAnywhere, Category="Input")
|
||||
UInputAction* ProneAction;
|
||||
|
||||
/** Toggle between third-person and first-person camera views. */
|
||||
UPROPERTY(EditAnywhere, Category="Input")
|
||||
UInputAction* ToggleCameraAction;
|
||||
@@ -101,6 +109,14 @@ protected:
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Agrarian|Movement", meta = (ClampMin = "0", ClampMax = "100"))
|
||||
float MinSprintStamina = 5.0f;
|
||||
|
||||
/** Movement multiplier while crouched. */
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Agrarian|Movement|Stance", meta = (ClampMin = "0.1", ClampMax = "1"))
|
||||
float CrouchSpeedMultiplier = 0.55f;
|
||||
|
||||
/** Movement multiplier while prone. */
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Agrarian|Movement|Stance", meta = (ClampMin = "0.05", ClampMax = "1"))
|
||||
float ProneSpeedMultiplier = 0.25f;
|
||||
|
||||
/** Age hook used by movement until full lifecycle/aging systems own it. */
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Replicated, Category="Agrarian|Movement|Modifiers", meta = (ClampMin = "0"))
|
||||
float AgeYears = 25.0f;
|
||||
@@ -145,6 +161,10 @@ protected:
|
||||
UPROPERTY(ReplicatedUsing = OnRep_SprintState, VisibleAnywhere, BlueprintReadOnly, Category="Agrarian|Movement", meta = (AllowPrivateAccess = "true"))
|
||||
bool bWantsToSprint = false;
|
||||
|
||||
/** Replicated prone stance for low crawl movement. */
|
||||
UPROPERTY(ReplicatedUsing = OnRep_ProneState, VisibleAnywhere, BlueprintReadOnly, Category="Agrarian|Movement|Stance", meta = (AllowPrivateAccess = "true"))
|
||||
bool bIsProne = false;
|
||||
|
||||
public:
|
||||
|
||||
/** Constructor */
|
||||
@@ -175,6 +195,12 @@ protected:
|
||||
/** Called when sprint input is released. */
|
||||
void StopSprint();
|
||||
|
||||
/** Called when crouch input is pressed. */
|
||||
void ToggleCrouchStance();
|
||||
|
||||
/** Called when prone input is pressed. */
|
||||
void ToggleProneStance();
|
||||
|
||||
/** Called for camera perspective toggle input */
|
||||
void ToggleCameraPerspective();
|
||||
|
||||
@@ -196,10 +222,14 @@ protected:
|
||||
float CalculateAgeMovementMultiplier() const;
|
||||
float CalculateSurvivalMovementMultiplier() const;
|
||||
float CalculateCarryWeightMovementMultiplier() const;
|
||||
float CalculateStanceMovementMultiplier() const;
|
||||
|
||||
UFUNCTION()
|
||||
void OnRep_SprintState();
|
||||
|
||||
UFUNCTION()
|
||||
void OnRep_ProneState();
|
||||
|
||||
public:
|
||||
|
||||
/** Handles move inputs from either controls or UI interfaces */
|
||||
@@ -230,6 +260,12 @@ public:
|
||||
UFUNCTION(BlueprintPure, Category="Agrarian|Movement")
|
||||
bool IsSprinting() const;
|
||||
|
||||
UFUNCTION(BlueprintPure, Category="Agrarian|Movement|Stance")
|
||||
bool IsProne() const { return bIsProne; }
|
||||
|
||||
UFUNCTION(BlueprintCallable, Category="Agrarian|Movement|Stance")
|
||||
void SetProne(bool bNewProne);
|
||||
|
||||
UFUNCTION(BlueprintPure, Category="Agrarian|Movement")
|
||||
float GetCurrentCarryWeight() const;
|
||||
|
||||
@@ -247,6 +283,10 @@ public:
|
||||
UFUNCTION(Server, Reliable)
|
||||
void ServerSetWantsToSprint(bool bNewWantsToSprint);
|
||||
|
||||
/** Server-authoritative prone stance update. */
|
||||
UFUNCTION(Server, Reliable)
|
||||
void ServerSetProne(bool bNewProne);
|
||||
|
||||
public:
|
||||
|
||||
/** Returns CameraBoom subobject **/
|
||||
|
||||
Reference in New Issue
Block a user