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

112 lines
2.1 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-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;
bool jumping = false;
bool needsToRelease = false;
2025-01-03 12:09:23 +00:00
2025-01-08 18:46:17 +00:00
protected override void _OnTrigger()
2025-01-03 12:09:23 +00:00
{
if ( body.IsOnFloor() )
{
2025-05-21 20:44:28 +00:00
if ( jumping )
{
jumping = false;
needsToRelease = true;
}
2025-01-03 12:09:23 +00:00
}
if ( ! ( jumping || body.IsOnFloor() ) )
{
return;
}
2025-01-08 18:46:17 +00:00
if ( ! button.isActive )
2025-01-03 12:09:23 +00:00
{
jumping = false;
2025-05-21 20:44:28 +00:00
needsToRelease = false;
return;
}
if ( needsToRelease )
{
2025-01-03 12:09:23 +00:00
return;
}
if ( ! jumping )
{
jumping = true;
jumpedDuration = 0;
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
}
}
}
}