|
|
|
|
| #include "LyraRangedWeaponInstance.h"
|
| #include "NativeGameplayTags.h"
|
| #include "GameFramework/Pawn.h"
|
| #include "GameFramework/CharacterMovementComponent.h"
|
| #include "Camera/LyraCameraComponent.h"
|
| #include "Physics/PhysicalMaterialWithTags.h"
|
| #include "Weapons/LyraWeaponInstance.h"
|
|
|
| #include UE_INLINE_GENERATED_CPP_BY_NAME(LyraRangedWeaponInstance)
|
|
|
| UE_DEFINE_GAMEPLAY_TAG_STATIC(TAG_Lyra_Weapon_SteadyAimingCamera, "Lyra.Weapon.SteadyAimingCamera");
|
|
|
| ULyraRangedWeaponInstance::ULyraRangedWeaponInstance(const FObjectInitializer& ObjectInitializer)
|
| : Super(ObjectInitializer)
|
| {
|
| HeatToHeatPerShotCurve.EditorCurveData.AddKey(0.0f, 1.0f);
|
| HeatToCoolDownPerSecondCurve.EditorCurveData.AddKey(0.0f, 2.0f);
|
| }
|
|
|
| void ULyraRangedWeaponInstance::PostLoad()
|
| {
|
| Super::PostLoad();
|
|
|
| #if WITH_EDITOR
|
| UpdateDebugVisualization();
|
| #endif
|
| }
|
|
|
| #if WITH_EDITOR
|
| void ULyraRangedWeaponInstance::PostEditChangeProperty(struct FPropertyChangedEvent& PropertyChangedEvent)
|
| {
|
| Super::PostEditChangeProperty(PropertyChangedEvent);
|
| UpdateDebugVisualization();
|
| }
|
|
|
| void ULyraRangedWeaponInstance::UpdateDebugVisualization()
|
| {
|
| ComputeHeatRange( Debug_MinHeat, Debug_MaxHeat);
|
| ComputeSpreadRange( Debug_MinSpreadAngle, Debug_MaxSpreadAngle);
|
| Debug_CurrentHeat = CurrentHeat;
|
| Debug_CurrentSpreadAngle = CurrentSpreadAngle;
|
| Debug_CurrentSpreadAngleMultiplier = CurrentSpreadAngleMultiplier;
|
| }
|
| #endif
|
|
|
| void ULyraRangedWeaponInstance::OnEquipped()
|
| {
|
| Super::OnEquipped();
|
|
|
|
|
| float MinHeatRange;
|
| float MaxHeatRange;
|
| ComputeHeatRange( MinHeatRange, MaxHeatRange);
|
| CurrentHeat = (MinHeatRange + MaxHeatRange) * 0.5f;
|
|
|
|
|
| CurrentSpreadAngle = HeatToSpreadCurve.GetRichCurveConst()->Eval(CurrentHeat);
|
|
|
|
|
| CurrentSpreadAngleMultiplier = 1.0f;
|
| StandingStillMultiplier = 1.0f;
|
| JumpFallMultiplier = 1.0f;
|
| CrouchingMultiplier = 1.0f;
|
| }
|
|
|
| void ULyraRangedWeaponInstance::OnUnequipped()
|
| {
|
| Super::OnUnequipped();
|
| }
|
|
|
| void ULyraRangedWeaponInstance::Tick(float DeltaSeconds)
|
| {
|
| APawn* Pawn = GetPawn();
|
| check(Pawn != nullptr);
|
|
|
| const bool bMinSpread = UpdateSpread(DeltaSeconds);
|
| const bool bMinMultipliers = UpdateMultipliers(DeltaSeconds);
|
|
|
| bHasFirstShotAccuracy = bAllowFirstShotAccuracy && bMinMultipliers && bMinSpread;
|
|
|
| #if WITH_EDITOR
|
| UpdateDebugVisualization();
|
| #endif
|
| }
|
|
|
| void ULyraRangedWeaponInstance::ComputeHeatRange(float& MinHeat, float& MaxHeat)
|
| {
|
| float Min1;
|
| float Max1;
|
| HeatToHeatPerShotCurve.GetRichCurveConst()->GetTimeRange( Min1, Max1);
|
|
|
| float Min2;
|
| float Max2;
|
| HeatToCoolDownPerSecondCurve.GetRichCurveConst()->GetTimeRange( Min2, Max2);
|
|
|
| float Min3;
|
| float Max3;
|
| HeatToSpreadCurve.GetRichCurveConst()->GetTimeRange( Min3, Max3);
|
|
|
| MinHeat = FMath::Min(FMath::Min(Min1, Min2), Min3);
|
| MaxHeat = FMath::Max(FMath::Max(Max1, Max2), Max3);
|
| }
|
|
|
| void ULyraRangedWeaponInstance::ComputeSpreadRange(float& MinSpread, float& MaxSpread)
|
| {
|
| HeatToSpreadCurve.GetRichCurveConst()->GetValueRange( MinSpread, MaxSpread);
|
| }
|
|
|
| void ULyraRangedWeaponInstance::AddSpread()
|
| {
|
|
|
| const float HeatPerShot = HeatToHeatPerShotCurve.GetRichCurveConst()->Eval(CurrentHeat);
|
| CurrentHeat = ClampHeat(CurrentHeat + HeatPerShot);
|
|
|
|
|
| CurrentSpreadAngle = HeatToSpreadCurve.GetRichCurveConst()->Eval(CurrentHeat);
|
|
|
| #if WITH_EDITOR
|
| UpdateDebugVisualization();
|
| #endif
|
| }
|
|
|
| float ULyraRangedWeaponInstance::GetDistanceAttenuation(float Distance, const FGameplayTagContainer* SourceTags, const FGameplayTagContainer* TargetTags) const
|
| {
|
| const FRichCurve* Curve = DistanceDamageFalloff.GetRichCurveConst();
|
| return Curve->HasAnyData() ? Curve->Eval(Distance) : 1.0f;
|
| }
|
|
|
| float ULyraRangedWeaponInstance::GetPhysicalMaterialAttenuation(const UPhysicalMaterial* PhysicalMaterial, const FGameplayTagContainer* SourceTags, const FGameplayTagContainer* TargetTags) const
|
| {
|
| float CombinedMultiplier = 1.0f;
|
| if (const UPhysicalMaterialWithTags* PhysMatWithTags = Cast<const UPhysicalMaterialWithTags>(PhysicalMaterial))
|
| {
|
| for (const FGameplayTag MaterialTag : PhysMatWithTags->Tags)
|
| {
|
| if (const float* pTagMultiplier = MaterialDamageMultiplier.Find(MaterialTag))
|
| {
|
| CombinedMultiplier *= *pTagMultiplier;
|
| }
|
| }
|
| }
|
|
|
| return CombinedMultiplier;
|
| }
|
|
|
| bool ULyraRangedWeaponInstance::UpdateSpread(float DeltaSeconds)
|
| {
|
| const float TimeSinceFired = GetWorld()->TimeSince(LastFireTime);
|
|
|
| if (TimeSinceFired > SpreadRecoveryCooldownDelay)
|
| {
|
| const float CooldownRate = HeatToCoolDownPerSecondCurve.GetRichCurveConst()->Eval(CurrentHeat);
|
| CurrentHeat = ClampHeat(CurrentHeat - (CooldownRate * DeltaSeconds));
|
| CurrentSpreadAngle = HeatToSpreadCurve.GetRichCurveConst()->Eval(CurrentHeat);
|
| }
|
|
|
| float MinSpread;
|
| float MaxSpread;
|
| ComputeSpreadRange( MinSpread, MaxSpread);
|
|
|
| return FMath::IsNearlyEqual(CurrentSpreadAngle, MinSpread, KINDA_SMALL_NUMBER);
|
| }
|
|
|
| bool ULyraRangedWeaponInstance::UpdateMultipliers(float DeltaSeconds)
|
| {
|
| const float MultiplierNearlyEqualThreshold = 0.05f;
|
|
|
| APawn* Pawn = GetPawn();
|
| check(Pawn != nullptr);
|
| UCharacterMovementComponent* CharMovementComp = Cast<UCharacterMovementComponent>(Pawn->GetMovementComponent());
|
|
|
|
|
| const float PawnSpeed = Pawn->GetVelocity().Size();
|
| const float MovementTargetValue = FMath::GetMappedRangeValueClamped(
|
| FVector2D(StandingStillSpeedThreshold, StandingStillSpeedThreshold + StandingStillToMovingSpeedRange),
|
| FVector2D(SpreadAngleMultiplier_StandingStill, 1.0f),
|
| PawnSpeed);
|
| StandingStillMultiplier = FMath::FInterpTo(StandingStillMultiplier, MovementTargetValue, DeltaSeconds, TransitionRate_StandingStill);
|
| const bool bStandingStillMultiplierAtMin = FMath::IsNearlyEqual(StandingStillMultiplier, SpreadAngleMultiplier_StandingStill, SpreadAngleMultiplier_StandingStill*0.1f);
|
|
|
|
|
| const bool bIsCrouching = (CharMovementComp != nullptr) && CharMovementComp->IsCrouching();
|
| const float CrouchingTargetValue = bIsCrouching ? SpreadAngleMultiplier_Crouching : 1.0f;
|
| CrouchingMultiplier = FMath::FInterpTo(CrouchingMultiplier, CrouchingTargetValue, DeltaSeconds, TransitionRate_Crouching);
|
| const bool bCrouchingMultiplierAtTarget = FMath::IsNearlyEqual(CrouchingMultiplier, CrouchingTargetValue, MultiplierNearlyEqualThreshold);
|
|
|
|
|
| const bool bIsJumpingOrFalling = (CharMovementComp != nullptr) && CharMovementComp->IsFalling();
|
| const float JumpFallTargetValue = bIsJumpingOrFalling ? SpreadAngleMultiplier_JumpingOrFalling : 1.0f;
|
| JumpFallMultiplier = FMath::FInterpTo(JumpFallMultiplier, JumpFallTargetValue, DeltaSeconds, TransitionRate_JumpingOrFalling);
|
| const bool bJumpFallMultiplerIs1 = FMath::IsNearlyEqual(JumpFallMultiplier, 1.0f, MultiplierNearlyEqualThreshold);
|
|
|
|
|
| float AimingAlpha = 0.0f;
|
| if (const ULyraCameraComponent* CameraComponent = ULyraCameraComponent::FindCameraComponent(Pawn))
|
| {
|
| float TopCameraWeight;
|
| FGameplayTag TopCameraTag;
|
| CameraComponent->GetBlendInfo( TopCameraWeight, TopCameraTag);
|
|
|
| AimingAlpha = (TopCameraTag == TAG_Lyra_Weapon_SteadyAimingCamera) ? TopCameraWeight : 0.0f;
|
| }
|
| const float AimingMultiplier = FMath::GetMappedRangeValueClamped(
|
| FVector2D(0.0f, 1.0f),
|
| FVector2D(1.0f, SpreadAngleMultiplier_Aiming),
|
| AimingAlpha);
|
| const bool bAimingMultiplierAtTarget = FMath::IsNearlyEqual(AimingMultiplier, SpreadAngleMultiplier_Aiming, KINDA_SMALL_NUMBER);
|
|
|
|
|
| const float CombinedMultiplier = AimingMultiplier * StandingStillMultiplier * CrouchingMultiplier * JumpFallMultiplier;
|
| CurrentSpreadAngleMultiplier = CombinedMultiplier;
|
|
|
|
|
| return bStandingStillMultiplierAtMin && bCrouchingMultiplierAtTarget && bJumpFallMultiplerIs1 && bAimingMultiplierAtTarget;
|
| }
|
|
|
|
|