AutoChessGame/Nodes/RemotePathFollow2D.cs

54 lines
1.3 KiB
C#

using Godot;
public partial class RemotePathFollow2D : Path2D
{
[Export] public Node2D Target { get; set; } = null;
[Export] public double Speed { get; set; } = 1;
public bool Started => _started;
public bool Finished => _pathFollowNode.ProgressRatio >= 1;
private PathFollow2D _pathFollowNode = new PathFollow2D();
private bool _started = true;
private Vector2 _startingPosition;
public RemotePathFollow2D() { }
public RemotePathFollow2D(Node2D target) => Target = target;
public RemotePathFollow2D(float speed) => Speed = speed;
public RemotePathFollow2D(Node2D target, float speed) => (Target, Speed) = (target, speed);
public override void _Ready()
{
base._Ready();
_pathFollowNode.Loop = false;
_pathFollowNode.CubicInterp = false;
AddChild(_pathFollowNode);
}
public override void _Process(double delta)
{
base._Process(delta);
}
public override void _PhysicsProcess(double delta)
{
base._Process(delta);
if (Started)
{
_pathFollowNode.Progress += (float)(Speed * delta);
Target.Position = _startingPosition + _pathFollowNode.Position;
}
}
public void Start()
{
_startingPosition = Target.Position;
_started = true;
}
}