OLS/Source/OLSAnimation/Private/Libraries/OLSLocomotionBPLibrary.cpp

127 lines
3.8 KiB
C++
Raw Permalink Normal View History

2025-05-13 15:21:02 +07:00
// © 2024 Long Ly. All rights reserved. Any unauthorized use, reproduction, or distribution of this trademark is strictly prohibited and may result in legal action.
#include "Libraries/OLSLocomotionBPLibrary.h"
DEFINE_LOG_CATEGORY_STATIC(LogOLSLocomotionLibrary, Verbose, All);
2025-08-05 10:24:11 +07:00
EOLSMovementDirection UOLSLocomotionBPLibrary::SelectMovementDirectionFromAngle(float angle,
const EOLSRotationMode& rotationMode,
const EOLSGait& gait,
const EOLSMovementDirectionBias& movementDirectionBias,
const FOLSMovementDirectionThresholds& movementDirectionThreshold)
2025-05-13 15:21:02 +07:00
{
2025-08-05 10:24:11 +07:00
EOLSMovementDirection result = EOLSMovementDirection::EForward;
if (rotationMode == EOLSRotationMode::EVelocityDirection || gait == EOLSGait::ESprint)
{
return result;
}
const bool isAngleForwardDirection = FMath::IsWithinInclusive(angle,
movementDirectionThreshold.FL,
movementDirectionThreshold.FR);
const bool isAngleLeftDirection = FMath::IsWithinInclusive(angle,
movementDirectionThreshold.BL,
movementDirectionThreshold.FL);
const bool isAngleRightDirection = FMath::IsWithinInclusive(angle,
movementDirectionThreshold.FR,
movementDirectionThreshold.BR);
2025-05-13 15:21:02 +07:00
2025-08-05 10:24:11 +07:00
if (isAngleForwardDirection)
2025-05-13 15:21:02 +07:00
{
2025-08-05 10:24:11 +07:00
result = EOLSMovementDirection::EForward;
}
else if (isAngleLeftDirection)
{
switch (movementDirectionBias)
2025-05-13 15:21:02 +07:00
{
2025-08-05 10:24:11 +07:00
case EOLSMovementDirectionBias::ELeftFoot_F:
result = EOLSMovementDirection::ELeftLeft;
break;
case EOLSMovementDirectionBias::ERightFoot_F:
result = EOLSMovementDirection::ELeftRight;
break;
2025-05-13 15:21:02 +07:00
}
2025-08-05 10:24:11 +07:00
}
else if (isAngleRightDirection)
{
switch (movementDirectionBias)
2025-05-13 15:21:02 +07:00
{
2025-08-05 10:24:11 +07:00
case EOLSMovementDirectionBias::ELeftFoot_F:
result = EOLSMovementDirection::ERightLeft;
break;
case EOLSMovementDirectionBias::ERightFoot_F:
result = EOLSMovementDirection::ERightRight;
break;
2025-05-13 15:21:02 +07:00
}
}
2025-08-05 10:24:11 +07:00
else
{
result = EOLSMovementDirection::EBackward;
}
2025-05-13 15:21:02 +07:00
2025-08-05 10:24:11 +07:00
return result;
}
EOLSMovementDirection UOLSLocomotionBPLibrary::GetOppositeMovementDirection(
const EOLSMovementDirection& currentDirection,
const EOLSMovementDirectionBias& movementDirectionBias)
{
EOLSMovementDirection result = EOLSMovementDirection::EForward;
if (currentDirection == EOLSMovementDirection::EForward)
2025-05-13 15:21:02 +07:00
{
2025-08-05 10:24:11 +07:00
result = EOLSMovementDirection::EBackward;
2025-05-13 15:21:02 +07:00
}
2025-08-05 10:24:11 +07:00
else if (currentDirection == EOLSMovementDirection::ELeftLeft || currentDirection == EOLSMovementDirection::ELeftRight)
2025-05-13 15:21:02 +07:00
{
2025-08-05 10:24:11 +07:00
switch (movementDirectionBias)
{
case EOLSMovementDirectionBias::ELeftFoot_F:
result = EOLSMovementDirection::ERightLeft;
break;
case EOLSMovementDirectionBias::ERightFoot_F:
result = EOLSMovementDirection::ERightRight;
break;
}
2025-05-13 15:21:02 +07:00
}
2025-08-05 10:24:11 +07:00
else if (currentDirection == EOLSMovementDirection::ERightLeft || currentDirection == EOLSMovementDirection::ERightRight)
2025-05-13 15:21:02 +07:00
{
2025-08-05 10:24:11 +07:00
switch (movementDirectionBias)
{
case EOLSMovementDirectionBias::ELeftFoot_F:
result = EOLSMovementDirection::ELeftLeft;
break;
case EOLSMovementDirectionBias::ERightFoot_F:
result = EOLSMovementDirection::ELeftRight;
break;
}
2025-05-13 15:21:02 +07:00
}
2025-08-05 10:24:11 +07:00
else
2025-05-13 15:21:02 +07:00
{
2025-08-05 10:24:11 +07:00
result = EOLSMovementDirection::EBackward;
2025-05-13 15:21:02 +07:00
}
2025-08-05 10:24:11 +07:00
return result;
2025-05-13 15:21:02 +07:00
}
void UOLSLocomotionBPLibrary::TryLinkAnimLayer(USkeletalMeshComponent* mesh,
TSubclassOf<UAnimInstance> animClass,
FName groupName,
bool shouldUnlinkGroupIfInvalid)
{
if (!animClass->IsValidLowLevelFast())
{
if (shouldUnlinkGroupIfInvalid)
{
if (const TObjectPtr<UAnimInstance> linkedAnimInstance = mesh->GetLinkedAnimLayerInstanceByGroup(groupName))
{
mesh->UnlinkAnimClassLayers(linkedAnimInstance.GetClass());
}
}
return;
}
mesh->LinkAnimClassLayers(animClass);
}