rj-action-library/Runtime/Interactions/CharacterController/CharacterMovement.cs

130 lines
3.1 KiB
C#
Raw Normal View History

2024-12-01 17:07:41 +00:00
using Godot;
using System.Collections;
using System.Collections.Generic;
using Godot.Collections;
namespace Rokojori
{
2025-05-21 20:44:28 +00:00
[Tool]
2024-12-01 17:07:41 +00:00
[GlobalClass]
public partial class CharacterMovement:CharacterControllerAction
{
[Export]
2025-05-21 20:44:28 +00:00
public float onFloorMultiply = 1f;
2024-12-01 17:07:41 +00:00
2025-01-03 12:09:23 +00:00
[Export]
2025-05-21 20:44:28 +00:00
public float inAirMultiply = 0.01f;
2025-01-03 12:09:23 +00:00
[Export]
2025-05-21 20:44:28 +00:00
public CameraTargetOffset cameraTargetOffset;
2025-01-03 12:09:23 +00:00
[ExportGroup( "Direction Source" )]
[Export]
public Node3D directionSource;
public enum DirectionProcessing
{
None,
Zero_Y_And_Normalize,
Project_On_TransformPlane
2025-05-21 20:44:28 +00:00
}
2025-01-03 12:09:23 +00:00
2025-05-21 20:44:28 +00:00
[ExportGroup("Movement")]
2024-12-01 17:07:41 +00:00
[Export]
public float moveSpeed;
[Export]
2025-05-21 20:44:28 +00:00
public CharacterMovementType controllerMovementType;
2024-12-01 17:07:41 +00:00
[Export]
2025-05-21 20:44:28 +00:00
public CharacterMovementType mouseKeyboardMovementType;
2024-12-01 17:07:41 +00:00
[Export]
2025-05-21 20:44:28 +00:00
public CharacterMovementType currentMovementType;
2024-12-01 17:07:41 +00:00
[Export]
2025-05-21 20:44:28 +00:00
public Smoothing onFloorMovementSmoothing = new FrameSmoothing();
2024-12-01 17:07:41 +00:00
[Export]
2025-05-21 20:44:28 +00:00
public Smoothing inAirMovementSmoothing = new FrameSmoothing();
2024-12-01 17:07:41 +00:00
2025-01-03 12:09:23 +00:00
[ExportGroup( "Rotation" )]
[Export]
public bool adjustRotation = true;
[Export]
2025-05-21 20:44:28 +00:00
public Curve forwardToRotationSmoothingFrames = MathX.Curve( 0, 1 );
2025-01-03 12:09:23 +00:00
2025-05-21 20:44:28 +00:00
[Export(PropertyHint.Range, "0,600")]
public int airRotationSmoothingFrames = 120;
2025-01-03 12:09:23 +00:00
2025-05-21 20:44:28 +00:00
FrameSmoothing rotationSmoothing = new FrameSmoothing();
protected CharacterMovementData characterMovementData = new CharacterMovementData();
2025-01-03 12:09:23 +00:00
2025-01-08 18:46:17 +00:00
protected override void _OnTrigger()
2024-12-01 17:07:41 +00:00
{
2025-05-21 20:44:28 +00:00
var onFloor = body.IsOnFloor();
characterMovementData.Reset( this );
if ( currentMovementType == null )
2025-01-03 12:09:23 +00:00
{
return;
}
2025-05-21 20:44:28 +00:00
currentMovementType.ProcessMovement( characterMovementData );
2024-12-01 17:07:41 +00:00
2025-05-21 20:44:28 +00:00
if ( cameraTargetOffset != null )
2025-01-03 12:09:23 +00:00
{
2025-05-21 20:44:28 +00:00
cameraTargetOffset.SetMovingForward( characterMovementData.isMovingForward ? 1f : 0f );
2025-01-03 12:09:23 +00:00
}
2025-05-21 20:44:28 +00:00
var movementMultiply = onFloor ? onFloorMultiply : inAirMultiply;
2025-01-03 12:09:23 +00:00
2025-05-21 20:44:28 +00:00
characterMovementData.movement *= movementMultiply;
2025-01-03 12:09:23 +00:00
2024-12-01 17:07:41 +00:00
2025-01-03 12:09:23 +00:00
if ( adjustRotation )
{
2025-05-21 20:44:28 +00:00
var nextRotation = Math3D.LookRotation( characterMovementData.forwardDirection );
var speed = MathX.Clamp01( characterMovementData.movement.Length() / moveSpeed );
if ( speed > 0 )
{
Quaternion rotation;
if ( onFloor )
{
rotationSmoothing.frames = Mathf.Round( forwardToRotationSmoothingFrames.Sample( speed ) );
}
else
{
rotationSmoothing.frames = airRotationSmoothingFrames;
}
rotation = rotationSmoothing.Smooth( nextRotation, controller.delta );
body.SetGlobalQuaternion( rotation );
}
}
2025-01-03 12:09:23 +00:00
2025-05-21 20:44:28 +00:00
var smoothedMovement = Vector3.Zero;
2025-01-03 12:09:23 +00:00
2025-05-21 20:44:28 +00:00
if ( onFloor )
{
smoothedMovement = onFloorMovementSmoothing.Smooth( characterMovementData.movement, controller.delta );
}
else
{
smoothedMovement = inAirMovementSmoothing.Smooth( characterMovementData.movement, controller.delta );
2025-01-03 12:09:23 +00:00
}
2025-05-21 20:44:28 +00:00
Velocity( smoothedMovement, onFloor );
2024-12-01 17:07:41 +00:00
}
}
}