Files
PHY/Source/PHY/Private/Locomotion/PHYSLSMovementBridgeComponent.cpp

108 lines
3.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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);
}