using Godot;
using System.Collections;
using System.Collections.Generic;
using Godot.Collections;

namespace Rokojori
{
  [GlobalClass]
  public partial class Jump:CharacterControllerAction
  {
    [Export]
    public Sensor button;

    [Export]
    public float jumpStrength;

    [Export]
    public float maxJumpDuration;

    [Export]
    public Curve jumpCurveStrength = MathX.Curve( 1, 0 );
    
    [Export]
    public float jumpedDuration;

    [Export]
    public bool canJump = false;

    [Export]
    public bool jumping = false;

    

    protected override void _OnTrigger()
    { 
      if ( body.IsOnFloor() )
      {
        jumping = false;
      }

      if ( ! ( jumping || body.IsOnFloor() ) )
      {
        return;
      }

      if ( ! button.isActive )
      {
        jumping = false;
        return;
      }

      if ( ! jumping )
      {
        jumping = true;
        jumpedDuration = 0;
      }

      jumpedDuration += controller.delta;

      canJump = jumpedDuration < maxJumpDuration;


      if ( jumpedDuration < maxJumpDuration )
      {
        var jumpStrengthMultiply = jumpCurveStrength.Sample( jumpedDuration / maxJumpDuration );
        AddVelocity( Vector3.Up * jumpStrength );
      }

      
    }
  }
}