rokojori_action_library/Runtime/Interactions/CharacterController/Jump.cs

177 lines
3.7 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-12-21 18:56:48 +00:00
[ExportGroup( "Jumping")]
[Export]
public JumpStrength strength;
[Export]
public float minJumpDuration = 0.5f;
[Export]
public Gravity gravity;
[ExportGroup( "Movement")]
2025-01-03 12:09:23 +00:00
[Export]
2025-12-21 18:56:48 +00:00
public float forwardStrength = 0;
[Export]
public float rightStrength = 0;
[Export]
public CharacterMovement movement;
[Export( PropertyHint.Range, "0,5" )]
public float movementToJumpDirection = 0.5f;
2025-01-03 12:09:23 +00:00
2025-05-21 20:44:28 +00:00
[Export( PropertyHint.Range, "0,100" )]
2025-12-21 18:56:48 +00:00
public float movementDirectionToFixedJumpDirection = 0.5f;
2025-01-03 12:09:23 +00:00
[Export]
2025-12-21 18:56:48 +00:00
public float movementTresholdForJumpDirection = 1f;
2025-05-21 20:44:28 +00:00
[ExportGroup( "Air Control")]
2025-01-03 12:09:23 +00:00
[Export]
2025-12-21 18:56:48 +00:00
public GravityStrength airControlGravityStrength;
2025-01-03 12:09:23 +00:00
2025-12-21 18:56:48 +00:00
[Export]
public float maxAirControlDuration;
2025-05-21 20:44:28 +00:00
[Export]
2025-12-21 18:56:48 +00:00
public Curve airControlCurveStrength = MathX.Curve( 1, 0 );
2025-05-21 20:44:28 +00:00
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-12-21 18:56:48 +00:00
bool jumping = false;
float jumpStart = 0;
Vector3 jumpDirection;
public override bool HasActiveForces()
{
return jumping;
}
2025-07-25 08:13:35 +00:00
2025-07-22 14:08:22 +00:00
public bool IsJumping()
{
2025-12-21 18:56:48 +00:00
return jumping;
2025-07-22 14:08:22 +00:00
}
2025-01-03 12:09:23 +00:00
2025-12-21 18:56:48 +00:00
public float GetJumpStartTime()
2025-07-25 08:13:35 +00:00
{
2025-12-21 18:56:48 +00:00
return jumpStart == -1 ? 0 : ( TimeLine.osTime - jumpStart );
2025-07-25 08:13:35 +00:00
}
2025-01-08 18:46:17 +00:00
protected override void _OnTrigger()
2025-01-03 12:09:23 +00:00
{
2025-12-21 18:56:48 +00:00
if ( ! jumping && isGrounded && button.isDown )
2025-01-03 12:09:23 +00:00
{
2025-12-21 18:56:48 +00:00
_StartJump();
2025-01-03 12:09:23 +00:00
}
2025-12-21 18:56:48 +00:00
if ( jumping )
2025-01-03 12:09:23 +00:00
{
2025-12-21 18:56:48 +00:00
_OnJumping();
}
2025-01-03 12:09:23 +00:00
2025-12-21 18:56:48 +00:00
}
2025-05-21 20:44:28 +00:00
2025-12-21 18:56:48 +00:00
protected void _StartJump()
{
// jumpPressing = true;
jumpedDuration = 0;
jumping = true;
jumpStart = TimeLine.osTime;
2025-01-03 12:09:23 +00:00
2025-12-21 18:56:48 +00:00
Trigger( onJump );
jumpDirection = Vector3.Up * strength.GetJumpStrength( this );
2025-05-21 20:44:28 +00:00
2025-12-21 18:56:48 +00:00
if ( movement != null )
{
var currentMovement = movement.smoothedMovement * controller.delta;
this.LogInfo( body.Velocity, currentMovement );
2025-05-21 20:44:28 +00:00
2025-12-21 18:56:48 +00:00
if ( currentMovement.Length() > movementTresholdForJumpDirection )
2025-05-21 20:44:28 +00:00
{
2025-12-21 18:56:48 +00:00
var xz = currentMovement;
xz.Y = 0;
2025-05-21 20:44:28 +00:00
2025-12-21 18:56:48 +00:00
jumpDirection += xz * movementToJumpDirection + xz.Normalized() * movementDirectionToFixedJumpDirection;
2025-05-21 20:44:28 +00:00
}
2025-12-21 18:56:48 +00:00
}
jumpDirection += body.GlobalForward() * forwardStrength;
jumpDirection += body.GlobalRight() * rightStrength;
2025-05-21 20:44:28 +00:00
2025-12-21 18:56:48 +00:00
SetVelocity( jumpDirection, 1 );
this.LogInfo( jumpDirection );
}
2025-05-21 20:44:28 +00:00
2025-12-21 18:56:48 +00:00
protected void _OnJumping()
{
if ( jumping && jumpedDuration < minJumpDuration )
{
if ( controller.grounding != null )
{
controller.grounding.ApplyUngroundedOverwrite();
}
2025-01-03 12:09:23 +00:00
}
2025-12-21 18:56:48 +00:00
if ( airControlGravityStrength != null && gravity != null && button.isHold &&
jumpedDuration < maxAirControlDuration
)
{
var gravityStrength = airControlGravityStrength.GetGravityStrength( gravity );
var gravityStrengthMultiply = 1f;
2025-01-03 12:09:23 +00:00
2025-12-21 18:56:48 +00:00
if ( airControlCurveStrength != null )
{
gravityStrengthMultiply = airControlCurveStrength.Sample( jumpedDuration / maxAirControlDuration );
}
AddVelocity( gravityStrength * Vector3.Up * gravityStrengthMultiply );
}
2025-01-03 12:09:23 +00:00
2025-12-21 18:56:48 +00:00
jumpedDuration += controller.delta;
2025-01-03 12:09:23 +00:00
2025-12-21 18:56:48 +00:00
if ( jumpedDuration > minJumpDuration )
2025-01-03 12:09:23 +00:00
{
2025-12-21 18:56:48 +00:00
jumping = false;
2025-01-03 12:09:23 +00:00
}
}
2025-12-21 18:56:48 +00:00
2025-01-03 12:09:23 +00:00
}
}