using Godot;
using Rokojori;

[Tool,GlobalClass]
public partial class LaserBeam: Action
{
  [Export]
  public Node3D body;

  [Export]
  public Node3D graphics;

  [Export]
  public Vector3 direction;

  [Export]
  public float speed = 1;

  [Export]
  public Duration duration;

  [Export]
  public Node3D aim;

  [Export]
  public Action onDone;

  [Export]
  public Action onStart;

  [Export]
  public Curve scaleCurve;

  [Export]
  public float forwardFollowDistanceScale = 1.5f;

  [Export]
  public float correctionDistance = 500;

  protected override void _OnTrigger()
  {
    Action.Trigger( onStart );

    TimeLineManager.ScheduleSpanWith( 
      duration, 
      ( span, updateType ) =>
      { 
        if ( ! IsInstanceValid( body )  )
        {
          return;
        }

        if ( scaleCurve != null )
        {
          var scale = scaleCurve.Sample( span.phase );
          graphics.Scale = scale * Vector3.One;
        }

        if ( TimeLineSpanUpdateType.End == updateType )
        {
          Action.Trigger( onDone );
        }
      } 
    );
  }

  public override void _PhysicsProcess( double delta )
  {
    body.GlobalPosition +=  direction * speed * (float)delta;
  }
}
