using Godot;
using Rokojori;

[Tool,GlobalClass]
public partial class ShootLaser: Action
{
  [Export]
  public PackedScene laserScene;

  [Export]
  public Node3D projectileOrigin;

  [Export]
  public float speed = 1f;

  [Export]
  public Node container;

  [Export]
  public float forwardFollowDistanceScale = 1.5f;

  [Export]
  public float yOffset = -5;

  protected override void _OnTrigger()
  {
    var cam = Unique<Camera3D>.Get();
    var distance =  projectileOrigin.DistanceTo( cam );

    var aim = cam.GlobalForward() * forwardFollowDistanceScale + cam.GlobalPosition;
    aim.Y += yOffset;

    var dir = projectileOrigin.DirectionTowards( aim ).Normalized();

    var laserScene = this.laserScene.Instantiate();

    laserScene.SetParent( container );

    var laserBeam = laserScene.Get<LaserBeam>();
    laserBeam.direction = dir;
    laserBeam.speed = speed;
    laserBeam.body.LookTowards( dir );
    laserBeam.forwardFollowDistanceScale = 1.5f;

    laserBeam.body.GlobalPosition = projectileOrigin.GlobalPosition;

    Action.Trigger( laserBeam );
  }
}
