GodotComponentTest/components/NavigationComponent.cs

47 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using Godot;
using Vector2 = Godot.Vector2;
using Vector3 = Godot.Vector3;
/// <summary>
/// </summary>
public class NavigationComponent
{
public Vector3 currentGoalWorld;
public Vector2 currentGoalOffset;
public List<Vector2> pathOffsetCoords;
public HexCell[] path;
private HexGrid _hexGrid;
public void Plan(Vector3 currentPositionWorld, Vector3 targetPositionWorld, TileWorld tileWorld)
{
if ((targetPositionWorld - currentGoalWorld).LengthSquared() < 0.01)
{
return;
}
currentGoalWorld = targetPositionWorld;
Vector2 currentPositionOffset = tileWorld.WorldToOffsetCoords(currentPositionWorld);
Vector2 currentGoalOffset = tileWorld.WorldToOffsetCoords(targetPositionWorld);
HexCell currentCell = new HexCell();
currentCell.OffsetCoords = currentPositionOffset;
HexCell targetCell = new HexCell();
targetCell.OffsetCoords = currentGoalOffset;
path = currentCell.LineTo(targetCell);
pathOffsetCoords = new List<Vector2>();
foreach (HexCell cell in path)
{
pathOffsetCoords.Append(cell.OffsetCoords);
}
currentGoalOffset = pathOffsetCoords[1];
}
}