Connect movement speed modifiers
This commit is contained in:
@@ -67,6 +67,8 @@ void AAgrarianGameCharacter::Tick(float DeltaSeconds)
|
||||
{
|
||||
Super::Tick(DeltaSeconds);
|
||||
|
||||
ApplyMovementSpeed();
|
||||
|
||||
if (!HasAuthority() || !bWantsToSprint)
|
||||
{
|
||||
return;
|
||||
@@ -80,7 +82,8 @@ void AAgrarianGameCharacter::Tick(float DeltaSeconds)
|
||||
|
||||
if (GetVelocity().SizeSquared2D() > KINDA_SMALL_NUMBER && SurvivalComponent)
|
||||
{
|
||||
SurvivalComponent->SpendStamina(SprintStaminaCostPerSecond * DeltaSeconds);
|
||||
const float EffectiveEndurance = FMath::Max(0.25f, EnduranceMultiplier);
|
||||
SurvivalComponent->SpendStamina((SprintStaminaCostPerSecond / EffectiveEndurance) * DeltaSeconds);
|
||||
if (!CanSprint())
|
||||
{
|
||||
SetWantsToSprint(false);
|
||||
@@ -92,6 +95,11 @@ void AAgrarianGameCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty
|
||||
{
|
||||
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
|
||||
DOREPLIFETIME(AAgrarianGameCharacter, bWantsToSprint);
|
||||
DOREPLIFETIME(AAgrarianGameCharacter, AgeYears);
|
||||
DOREPLIFETIME(AAgrarianGameCharacter, PhysicalConditionMultiplier);
|
||||
DOREPLIFETIME(AAgrarianGameCharacter, StrengthMultiplier);
|
||||
DOREPLIFETIME(AAgrarianGameCharacter, EnduranceMultiplier);
|
||||
DOREPLIFETIME(AAgrarianGameCharacter, TerrainMovementMultiplier);
|
||||
}
|
||||
|
||||
void AAgrarianGameCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
|
||||
@@ -235,14 +243,116 @@ bool AAgrarianGameCharacter::IsSprinting() const
|
||||
return bWantsToSprint && CanSprint();
|
||||
}
|
||||
|
||||
float AAgrarianGameCharacter::GetCurrentCarryWeight() const
|
||||
{
|
||||
return InventoryComponent ? InventoryComponent->GetTotalWeight() : 0.0f;
|
||||
}
|
||||
|
||||
float AAgrarianGameCharacter::GetCurrentMovementSpeedMultiplier() const
|
||||
{
|
||||
return CalculateMovementSpeedMultiplier();
|
||||
}
|
||||
|
||||
void AAgrarianGameCharacter::SetTerrainMovementMultiplier(float NewTerrainMovementMultiplier)
|
||||
{
|
||||
TerrainMovementMultiplier = FMath::Clamp(NewTerrainMovementMultiplier, 0.25f, 1.25f);
|
||||
ApplyMovementSpeed();
|
||||
}
|
||||
|
||||
void AAgrarianGameCharacter::ApplyMovementSpeed()
|
||||
{
|
||||
if (UCharacterMovementComponent* MovementComponent = GetCharacterMovement())
|
||||
{
|
||||
MovementComponent->MaxWalkSpeed = IsSprinting() ? SprintSpeed : WalkSpeed;
|
||||
const float BaseSpeed = IsSprinting() ? SprintSpeed : WalkSpeed;
|
||||
MovementComponent->MaxWalkSpeed = FMath::Max(0.0f, BaseSpeed * CalculateMovementSpeedMultiplier());
|
||||
}
|
||||
}
|
||||
|
||||
float AAgrarianGameCharacter::CalculateMovementSpeedMultiplier() const
|
||||
{
|
||||
const float TraitMultiplier = FMath::Clamp(PhysicalConditionMultiplier, 0.25f, 1.25f)
|
||||
* FMath::Clamp(EnduranceMultiplier, 0.25f, 2.0f);
|
||||
|
||||
return FMath::Clamp(
|
||||
CalculateAgeMovementMultiplier()
|
||||
* TraitMultiplier
|
||||
* CalculateSurvivalMovementMultiplier()
|
||||
* CalculateCarryWeightMovementMultiplier()
|
||||
* FMath::Clamp(TerrainMovementMultiplier, 0.25f, 1.25f),
|
||||
0.15f,
|
||||
1.35f);
|
||||
}
|
||||
|
||||
float AAgrarianGameCharacter::CalculateAgeMovementMultiplier() const
|
||||
{
|
||||
if (AgeYears < 13.0f)
|
||||
{
|
||||
return 0.65f;
|
||||
}
|
||||
|
||||
if (AgeYears < 18.0f)
|
||||
{
|
||||
return FMath::GetMappedRangeValueClamped(FVector2D(13.0f, 18.0f), FVector2D(0.8f, 1.0f), AgeYears);
|
||||
}
|
||||
|
||||
if (AgeYears <= 50.0f)
|
||||
{
|
||||
return 1.0f;
|
||||
}
|
||||
|
||||
if (AgeYears <= 70.0f)
|
||||
{
|
||||
return FMath::GetMappedRangeValueClamped(FVector2D(50.0f, 70.0f), FVector2D(1.0f, 0.75f), AgeYears);
|
||||
}
|
||||
|
||||
return 0.65f;
|
||||
}
|
||||
|
||||
float AAgrarianGameCharacter::CalculateSurvivalMovementMultiplier() const
|
||||
{
|
||||
if (!SurvivalComponent || !SurvivalComponent->IsAlive())
|
||||
{
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
const FAgrarianSurvivalSnapshot& Survival = SurvivalComponent->Survival;
|
||||
const float HungerMultiplier = Survival.Hunger >= 50.0f
|
||||
? 1.0f
|
||||
: FMath::GetMappedRangeValueClamped(FVector2D(0.0f, 50.0f), FVector2D(0.85f, 1.0f), Survival.Hunger);
|
||||
const float ThirstMultiplier = Survival.Thirst >= 50.0f
|
||||
? 1.0f
|
||||
: FMath::GetMappedRangeValueClamped(FVector2D(0.0f, 50.0f), FVector2D(0.75f, 1.0f), Survival.Thirst);
|
||||
const float InjuryMultiplier = FMath::GetMappedRangeValueClamped(
|
||||
FVector2D(0.0f, 100.0f),
|
||||
FVector2D(1.0f, 0.5f),
|
||||
Survival.InjurySeverity);
|
||||
|
||||
return HungerMultiplier * ThirstMultiplier * InjuryMultiplier;
|
||||
}
|
||||
|
||||
float AAgrarianGameCharacter::CalculateCarryWeightMovementMultiplier() const
|
||||
{
|
||||
const float Strength = FMath::Clamp(StrengthMultiplier, 0.25f, 2.0f);
|
||||
const float EffectiveComfortWeight = ComfortableCarryWeight * Strength;
|
||||
const float EffectiveHeavyWeight = HeavyCarryWeight * Strength;
|
||||
const float CurrentCarryWeight = GetCurrentCarryWeight();
|
||||
|
||||
if (CurrentCarryWeight <= EffectiveComfortWeight || EffectiveHeavyWeight <= EffectiveComfortWeight)
|
||||
{
|
||||
return 1.0f;
|
||||
}
|
||||
|
||||
if (CurrentCarryWeight >= EffectiveHeavyWeight)
|
||||
{
|
||||
return 0.45f;
|
||||
}
|
||||
|
||||
return FMath::GetMappedRangeValueClamped(
|
||||
FVector2D(EffectiveComfortWeight, EffectiveHeavyWeight),
|
||||
FVector2D(1.0f, 0.65f),
|
||||
CurrentCarryWeight);
|
||||
}
|
||||
|
||||
void AAgrarianGameCharacter::OnRep_SprintState()
|
||||
{
|
||||
ApplyMovementSpeed();
|
||||
|
||||
Reference in New Issue
Block a user