File size: 4,151 Bytes
1e67697 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | // Copyright Epic Games, Inc. All Rights Reserved.
#include "LyraCharacterMovementComponent.h"
#include "AbilitySystemComponent.h"
#include "AbilitySystemGlobals.h"
#include "Components/CapsuleComponent.h"
#include "Engine/World.h"
#include "GameFramework/Character.h"
#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraCharacterMovementComponent)
UE_DEFINE_GAMEPLAY_TAG(TAG_Gameplay_MovementStopped, "Gameplay.MovementStopped");
namespace LyraCharacter
{
static float GroundTraceDistance = 100000.0f;
FAutoConsoleVariableRef CVar_GroundTraceDistance(TEXT("LyraCharacter.GroundTraceDistance"), GroundTraceDistance, TEXT("Distance to trace down when generating ground information."), ECVF_Cheat);
};
ULyraCharacterMovementComponent::ULyraCharacterMovementComponent(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
}
void ULyraCharacterMovementComponent::SimulateMovement(float DeltaTime)
{
if (bHasReplicatedAcceleration)
{
// Preserve our replicated acceleration
const FVector OriginalAcceleration = Acceleration;
Super::SimulateMovement(DeltaTime);
Acceleration = OriginalAcceleration;
}
else
{
Super::SimulateMovement(DeltaTime);
}
}
bool ULyraCharacterMovementComponent::CanAttemptJump() const
{
// Same as UCharacterMovementComponent's implementation but without the crouch check
return IsJumpAllowed() &&
(IsMovingOnGround() || IsFalling()); // Falling included for double-jump and non-zero jump hold time, but validated by character.
}
void ULyraCharacterMovementComponent::InitializeComponent()
{
Super::InitializeComponent();
}
const FLyraCharacterGroundInfo& ULyraCharacterMovementComponent::GetGroundInfo()
{
if (!CharacterOwner || (GFrameCounter == CachedGroundInfo.LastUpdateFrame))
{
return CachedGroundInfo;
}
if (MovementMode == MOVE_Walking)
{
CachedGroundInfo.GroundHitResult = CurrentFloor.HitResult;
CachedGroundInfo.GroundDistance = 0.0f;
}
else
{
const UCapsuleComponent* CapsuleComp = CharacterOwner->GetCapsuleComponent();
check(CapsuleComp);
const float CapsuleHalfHeight = CapsuleComp->GetUnscaledCapsuleHalfHeight();
const ECollisionChannel CollisionChannel = (UpdatedComponent ? UpdatedComponent->GetCollisionObjectType() : ECC_Pawn);
const FVector TraceStart(GetActorLocation());
const FVector TraceEnd(TraceStart.X, TraceStart.Y, (TraceStart.Z - LyraCharacter::GroundTraceDistance - CapsuleHalfHeight));
FCollisionQueryParams QueryParams(SCENE_QUERY_STAT(LyraCharacterMovementComponent_GetGroundInfo), false, CharacterOwner);
FCollisionResponseParams ResponseParam;
InitCollisionParams(QueryParams, ResponseParam);
FHitResult HitResult;
GetWorld()->LineTraceSingleByChannel(HitResult, TraceStart, TraceEnd, CollisionChannel, QueryParams, ResponseParam);
CachedGroundInfo.GroundHitResult = HitResult;
CachedGroundInfo.GroundDistance = LyraCharacter::GroundTraceDistance;
if (MovementMode == MOVE_NavWalking)
{
CachedGroundInfo.GroundDistance = 0.0f;
}
else if (HitResult.bBlockingHit)
{
CachedGroundInfo.GroundDistance = FMath::Max((HitResult.Distance - CapsuleHalfHeight), 0.0f);
}
}
CachedGroundInfo.LastUpdateFrame = GFrameCounter;
return CachedGroundInfo;
}
void ULyraCharacterMovementComponent::SetReplicatedAcceleration(const FVector& InAcceleration)
{
bHasReplicatedAcceleration = true;
Acceleration = InAcceleration;
}
FRotator ULyraCharacterMovementComponent::GetDeltaRotation(float DeltaTime) const
{
if (UAbilitySystemComponent* ASC = UAbilitySystemGlobals::GetAbilitySystemComponentFromActor(GetOwner()))
{
if (ASC->HasMatchingGameplayTag(TAG_Gameplay_MovementStopped))
{
return FRotator(0,0,0);
}
}
return Super::GetDeltaRotation(DeltaTime);
}
float ULyraCharacterMovementComponent::GetMaxSpeed() const
{
if (UAbilitySystemComponent* ASC = UAbilitySystemGlobals::GetAbilitySystemComponentFromActor(GetOwner()))
{
if (ASC->HasMatchingGameplayTag(TAG_Gameplay_MovementStopped))
{
return 0;
}
}
return Super::GetMaxSpeed();
}
|