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

136 lines
2.6 KiB
C#
Raw Normal View History

2025-01-03 12:09:23 +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]
2025-05-27 06:51:48 +00:00
[GlobalClass, Icon("res://addons/rokojori_action_library/Icons/CCJump.svg")]
2025-01-03 12:09:23 +00:00
public partial class Jump:CharacterControllerAction
{
[Export]
2025-01-08 18:46:17 +00:00
public Sensor button;
2025-01-03 12:09:23 +00:00
2025-07-22 14:08:22 +00:00
[Export]
public Action onJump;
2025-05-21 20:44:28 +00:00
[ExportGroup( "Jump Impulse")]
2025-01-03 12:09:23 +00:00
[Export]
2025-05-21 20:44:28 +00:00
public float jumpImpulseStrength;
2025-01-03 12:09:23 +00:00
2025-05-21 20:44:28 +00:00
[Export( PropertyHint.Range, "0,100" )]
public float velocityToJumpDirection = 0.5f;
2025-01-03 12:09:23 +00:00
[Export]
2025-05-21 20:44:28 +00:00
public float velocityTresholdForJumpDirection = 1f;
[ExportGroup( "Air Control")]
2025-01-03 12:09:23 +00:00
[Export]
2025-05-21 20:44:28 +00:00
public Curve airControlCurveStrength = MathX.Curve( 1, 0 );
2025-01-03 12:09:23 +00:00
[Export]
2025-05-21 20:44:28 +00:00
public float airMaxControlStrength;
[Export]
public float maxAirControlDuration;
float jumpedDuration;
bool canJump = false;
2025-07-22 14:08:22 +00:00
bool jumpPressing = false;
2025-05-21 20:44:28 +00:00
bool needsToRelease = false;
2025-01-03 12:09:23 +00:00
2025-07-22 14:08:22 +00:00
bool inAir = false;
2025-07-25 08:13:35 +00:00
float airStart = 0;
2025-07-22 14:08:22 +00:00
public bool IsJumping()
{
return inAir;
}
2025-01-03 12:09:23 +00:00
2025-07-25 08:13:35 +00:00
public float GetAirTime()
{
return airStart == -1 ? 0 : ( TimeLine.osTime - airStart );
}
2025-01-08 18:46:17 +00:00
protected override void _OnTrigger()
2025-01-03 12:09:23 +00:00
{
if ( body.IsOnFloor() )
{
2025-07-22 14:08:22 +00:00
if ( inAir )
{
inAir = false;
2025-07-25 08:13:35 +00:00
airStart = -1;
2025-07-22 14:08:22 +00:00
}
if ( jumpPressing )
2025-05-21 20:44:28 +00:00
{
2025-07-22 14:08:22 +00:00
jumpPressing = false;
2025-05-21 20:44:28 +00:00
needsToRelease = true;
}
2025-01-03 12:09:23 +00:00
}
2025-07-22 14:08:22 +00:00
if ( ! ( jumpPressing || body.IsOnFloor() ) )
2025-01-03 12:09:23 +00:00
{
return;
}
2025-01-08 18:46:17 +00:00
if ( ! button.isActive )
2025-01-03 12:09:23 +00:00
{
2025-07-22 14:08:22 +00:00
jumpPressing = false;
2025-05-21 20:44:28 +00:00
needsToRelease = false;
return;
}
if ( needsToRelease )
{
2025-01-03 12:09:23 +00:00
return;
}
2025-07-22 14:08:22 +00:00
if ( ! jumpPressing )
2025-01-03 12:09:23 +00:00
{
2025-07-22 14:08:22 +00:00
jumpPressing = true;
2025-01-03 12:09:23 +00:00
jumpedDuration = 0;
2025-07-22 14:08:22 +00:00
inAir = true;
2025-07-25 08:13:35 +00:00
airStart = TimeLine.osTime;
2025-05-21 20:44:28 +00:00
2025-07-22 14:08:22 +00:00
Trigger( onJump );
2025-05-21 20:44:28 +00:00
var jumpDirection = Vector3.Up * jumpImpulseStrength;
if ( body.Velocity.Length() > velocityTresholdForJumpDirection )
{
var xz = body.Velocity;
xz.Y = 0;
// xz = xz.Normalized();
jumpDirection += xz * velocityToJumpDirection;
}
SetVelocity( jumpDirection );
return;
2025-01-03 12:09:23 +00:00
}
jumpedDuration += controller.delta;
2025-05-21 20:44:28 +00:00
canJump = jumpedDuration < maxAirControlDuration;
2025-01-03 12:09:23 +00:00
2025-05-21 20:44:28 +00:00
if ( canJump )
2025-01-03 12:09:23 +00:00
{
2025-05-21 20:44:28 +00:00
var jumpStrengthMultiply = airControlCurveStrength.Sample( jumpedDuration / maxAirControlDuration );
AddVelocity( Vector3.Up * airMaxControlStrength * jumpStrengthMultiply );
2025-01-03 12:09:23 +00:00
}
}
}
}