108 lines
3.7 KiB
C++
108 lines
3.7 KiB
C++
// Copyright Epic Games, Inc. All Rights Reserved.
|
||
|
||
#include "Locomotion/PHYSLSMovementBridgeComponent.h"
|
||
|
||
#include UE_INLINE_GENERATED_CPP_BY_NAME(PHYSLSMovementBridgeComponent)
|
||
|
||
#include "Characters/PHYCharacterBase.h"
|
||
#include "Characters/PHYCharacterSettings.h"
|
||
#include "Components/SLSCharacterMovementComponent.h"
|
||
#include "GameFramework/Character.h"
|
||
#include "PHYConfigSettings.h"
|
||
#include "SLSIntegrationComponent.h"
|
||
|
||
UPHYSLSMovementBridgeComponent::UPHYSLSMovementBridgeComponent()
|
||
{
|
||
PrimaryComponentTick.bCanEverTick = false;
|
||
SetIsReplicatedByDefault(false);
|
||
}
|
||
|
||
bool UPHYSLSMovementBridgeComponent::InitializeSLSMovementBridge(ACharacter* InCharacter, UActorComponent* InGenericIntegrationComponent, USLSIntegrationComponent* InSLSIntegrationComponent)
|
||
{
|
||
bInitialized = false;
|
||
OwnerCharacter = InCharacter;
|
||
GenericIntegrationComponent = InGenericIntegrationComponent;
|
||
SLSIntegrationComponent = InSLSIntegrationComponent;
|
||
SLSCharacterMovementComponent = nullptr;
|
||
|
||
const UPHYLocomotionSettings* LocomotionSettings = GetDefault<UPHYLocomotionSettings>();
|
||
if (!LocomotionSettings || !LocomotionSettings->bUseSmoothLocomotionSystem || !OwnerCharacter)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
SLSCharacterMovementComponent = Cast<USLSCharacterMovementComponent>(OwnerCharacter->GetCharacterMovement());
|
||
if (!SLSCharacterMovementComponent)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
if (!SLSIntegrationComponent)
|
||
{
|
||
SLSIntegrationComponent = OwnerCharacter->FindComponentByClass<USLSIntegrationComponent>();
|
||
}
|
||
|
||
const UPHYCharacterSettings* CharacterSettings = GetDefault<UPHYCharacterSettings>();
|
||
|
||
// 项目侧保留显式初始化,SLSIntegrationComponent 只作为运动层集中集成入口。
|
||
SLSCharacterMovementComponent->Config.InitComponentType = ESLSComponentInitType::ManualInit;
|
||
SLSCharacterMovementComponent->Config.MovementType = ESLSMovementType::Jog;
|
||
SLSCharacterMovementComponent->Config.RotationMode = ESLSRotationMode::VelocityDirection;
|
||
SLSCharacterMovementComponent->Config.bEnableSprintInLookingDirectionRotationMode = true;
|
||
|
||
SLSCharacterMovementComponent->ApplyBaseSLSCharacterMovementSettings();
|
||
|
||
if (CharacterSettings)
|
||
{
|
||
SLSCharacterMovementComponent->MaxWalkSpeed = CharacterSettings->DefaultMaxWalkSpeed;
|
||
SLSCharacterMovementComponent->MaxAcceleration = CharacterSettings->DefaultMaxAcceleration;
|
||
}
|
||
|
||
SLSCharacterMovementComponent->InitializeMovementComponent(SLSCharacterMovementComponent->Config);
|
||
bInitialized = true;
|
||
return true;
|
||
}
|
||
|
||
bool UPHYSLSMovementBridgeComponent::HandleMoveInput(const FVector2D MoveInput)
|
||
{
|
||
if (!EnsureSLSMovementBridgeReady())
|
||
{
|
||
return false;
|
||
}
|
||
|
||
const FRotator YawRotation(0.0f, OwnerCharacter->GetControlRotation().Yaw, 0.0f);
|
||
const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
|
||
const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
|
||
const FVector WorldIntent = (ForwardDirection * MoveInput.Y) + (RightDirection * MoveInput.X);
|
||
|
||
if (APHYCharacterBase* PHYCharacter = Cast<APHYCharacterBase>(OwnerCharacter))
|
||
{
|
||
PHYCharacter->SetMovementIntent(WorldIntent);
|
||
}
|
||
|
||
SLSCharacterMovementComponent->Input_OnMove(MoveInput);
|
||
return true;
|
||
}
|
||
|
||
bool UPHYSLSMovementBridgeComponent::HandleLookInput(const FVector2D LookInput)
|
||
{
|
||
if (!EnsureSLSMovementBridgeReady())
|
||
{
|
||
return false;
|
||
}
|
||
|
||
SLSCharacterMovementComponent->Input_OnLook(LookInput);
|
||
return true;
|
||
}
|
||
|
||
bool UPHYSLSMovementBridgeComponent::EnsureSLSMovementBridgeReady()
|
||
{
|
||
if (bInitialized && OwnerCharacter && SLSCharacterMovementComponent)
|
||
{
|
||
return true;
|
||
}
|
||
|
||
ACharacter* Character = OwnerCharacter ? OwnerCharacter.Get() : Cast<ACharacter>(GetOwner());
|
||
return InitializeSLSMovementBridge(Character, GenericIntegrationComponent, SLSIntegrationComponent);
|
||
}
|