66 lines
1.7 KiB
C#
66 lines
1.7 KiB
C#
using Godot;
|
|
using Godot.Collections;
|
|
|
|
public partial class Floor : TileMapLayer
|
|
{
|
|
[Export] public Mob MovementTarget { get; set; }
|
|
[Export] public float MovementSpeed { get; set; }
|
|
|
|
private RemotePathFollow2D _movementPathNode = null;
|
|
|
|
private AStarGrid2D _directionFinder;
|
|
|
|
public override void _Ready()
|
|
{
|
|
base._Ready();
|
|
|
|
MovementTarget.RequestMovement += _HandleMoveRequest;
|
|
|
|
_directionFinder = new AStarGrid2D();
|
|
_directionFinder.Region = GetUsedRect();
|
|
_directionFinder.Update();
|
|
|
|
Array<Vector2I> path = _directionFinder.GetIdPath(GetUsedRect().Position, GetUsedRect().Position + new Vector2I(5, 3));
|
|
Line2D line = new Line2D();
|
|
line.ZIndex = 5;
|
|
foreach (Vector2I point in path)
|
|
{
|
|
line.AddPoint(MapToLocal(point));
|
|
}
|
|
//AddChild(line);
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
base._Process(delta);
|
|
|
|
if (_movementPathNode is { Finished: true })
|
|
{
|
|
_movementPathNode.QueueFree();
|
|
_movementPathNode = null;
|
|
}
|
|
}
|
|
|
|
private void _HandleMoveRequest(Vector2I direction)
|
|
{
|
|
|
|
if (direction == Vector2I.Zero || _movementPathNode != null)
|
|
{
|
|
return;
|
|
}
|
|
_movementPathNode = new RemotePathFollow2D(MovementTarget, MovementSpeed);
|
|
_movementPathNode.Curve = _curveToPoint(MapToLocal(direction) - MapToLocal(Vector2I.Zero));
|
|
_movementPathNode.Start();
|
|
|
|
AddChild(_movementPathNode);
|
|
}
|
|
|
|
private Curve2D _curveToPoint(Vector2 point)
|
|
{
|
|
Curve2D curve = new Curve2D();
|
|
curve.AddPoint(Vector2.Zero);
|
|
curve.AddPoint(point);
|
|
return curve;
|
|
}
|
|
}
|