RealtimeStyleTransferRuntime/Source/LyraGame/Character/LyraCharacterMovementCompon...

142 lines
4.4 KiB
C++
Raw Permalink Normal View History

2022-05-23 18:41:30 +00:00
// Copyright Epic Games, Inc. All Rights Reserved.
#include "LyraCharacterMovementComponent.h"
2022-09-13 07:18:28 +00:00
#include "AbilitySystemComponent.h"
#include "AbilitySystemGlobals.h"
#include "CharacterMovementComponentAsync.h"
2022-05-23 18:41:30 +00:00
#include "CollisionQueryParams.h"
#include "Components/CapsuleComponent.h"
2022-09-13 07:18:28 +00:00
#include "Components/SceneComponent.h"
#include "Containers/EnumAsByte.h"
#include "CoreGlobals.h"
#include "Engine/EngineTypes.h"
#include "Engine/World.h"
#include "GameFramework/Character.h"
#include "HAL/IConsoleManager.h"
#include "Math/Vector.h"
#include "Misc/AssertionMacros.h"
2022-05-23 18:41:30 +00:00
#include "NativeGameplayTags.h"
2022-09-13 07:18:28 +00:00
#include "Stats/Stats2.h"
#include "UObject/NameTypes.h"
#include "UObject/ObjectPtr.h"
2022-05-23 18:41:30 +00:00
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();
}