yep
This commit is contained in:
@@ -4,15 +4,15 @@ namespace PathFinding
|
||||
{
|
||||
public static class Direction
|
||||
{
|
||||
public static readonly Vector2 Up = new(0, -1);
|
||||
public static readonly Vector2 Down = new(0, 1);
|
||||
public static readonly Vector2 Up = new(0, 1);
|
||||
public static readonly Vector2 Down = new(0, -1);
|
||||
public static readonly Vector2 Left = new(-1, 0);
|
||||
public static readonly Vector2 Right = new(1, 0);
|
||||
public static readonly Vector2 UpLeft = new(-1, -1);
|
||||
public static readonly Vector2 UpRight = new(1, -1);
|
||||
public static readonly Vector2 DownLeft = new(-1, 1);
|
||||
public static readonly Vector2 DownRight = new(1, 1);
|
||||
public static readonly Vector2 KnightLeft = new(-1, -2);
|
||||
public static readonly Vector2 KnightRight = new(1, -2);
|
||||
public static readonly Vector2 UpLeft = new(-1, 1);
|
||||
public static readonly Vector2 UpRight = new(1, 1);
|
||||
public static readonly Vector2 DownLeft = new(-1, -1);
|
||||
public static readonly Vector2 DownRight = new(1, -1);
|
||||
public static readonly Vector2 KnightLeft = new(-1, 2);
|
||||
public static readonly Vector2 KnightRight = new(1, 2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
|
||||
namespace PathFinding
|
||||
{
|
||||
public interface IPlanarCollection<T> : IEnumerable<T> where T : IPlanarElement
|
||||
public interface IPlanarCollection<T> where T : IPlanarElement
|
||||
{
|
||||
T? this[float x, float y] { get; set; }
|
||||
int GetLength(int dimension);
|
||||
T? this[Vector2 vector] { get; set; }
|
||||
T? this[int x, int y] { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,11 +14,14 @@ namespace PathFinding
|
||||
private readonly IPlanarCollection<T> collection;
|
||||
private readonly int width;
|
||||
private readonly int height;
|
||||
public PathFinder2D(IPlanarCollection<T> collection)
|
||||
|
||||
/// <param name="width">Horizontal size, in steps, of the pathable plane.</param>
|
||||
/// <param name="height">Vertical size, in steps, of the pathable plane.</param>
|
||||
public PathFinder2D(IPlanarCollection<T> collection, int width, int height)
|
||||
{
|
||||
this.collection = collection;
|
||||
width = collection.GetLength(0);
|
||||
height = collection.GetLength(1);
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -29,13 +32,13 @@ namespace PathFinding
|
||||
/// <param name="destination">The destination.</param>
|
||||
/// <param name="callback">Do cool stuff here.</param>
|
||||
/// <returns>True if the element reached the destination.</returns>
|
||||
public bool PathTo(Vector2 origin, Vector2 destination, Callback callback = null)
|
||||
public bool PathTo(Vector2 origin, Vector2 destination, Callback? callback = null)
|
||||
{
|
||||
if (destination.X > width - 1 || destination.Y > height - 1 || destination.X < 0 || destination.Y < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var element = collection[origin.Y, origin.X];
|
||||
var element = collection[origin];
|
||||
if (element == null) return false;
|
||||
|
||||
var path = FindDirectionTowardsDestination(element.MoveSet.GetMoves(), origin, destination);
|
||||
@@ -50,7 +53,7 @@ namespace PathFinding
|
||||
while (shouldPath && next != destination)
|
||||
{
|
||||
next = Vector2.Add(next, path.Direction);
|
||||
var collider = collection[(int)next.Y, (int)next.X];
|
||||
var collider = collection[next];
|
||||
if (collider != null)
|
||||
{
|
||||
callback?.Invoke(collider, next);
|
||||
@@ -66,7 +69,7 @@ namespace PathFinding
|
||||
|
||||
public void PathEvery(Vector2 from, Callback callback)
|
||||
{
|
||||
var element = collection[from.Y, from.X];
|
||||
var element = collection[from];
|
||||
if (element == null)
|
||||
{
|
||||
Console.WriteLine("Null element in PathEvery");
|
||||
@@ -103,7 +106,7 @@ namespace PathFinding
|
||||
var next = Vector2.Add(origin, direction);
|
||||
while (next.X >= 0 && next.X < width && next.Y >= 0 && next.Y < height)
|
||||
{
|
||||
var element = collection[next.Y, next.X];
|
||||
var element = collection[next];
|
||||
if (element != null) callback(element, next);
|
||||
next = Vector2.Add(next, direction);
|
||||
}
|
||||
|
||||
@@ -7,4 +7,8 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Gameboard.ShogiUI.Sockets.ServiceModels\Gameboard.ShogiUI.Sockets.ServiceModels.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace PathFinding
|
||||
{
|
||||
// TODO: Get rid of this thing in favor of T[,] multi-dimensional array with extension methods.
|
||||
public class PlanarCollection<T> : IPlanarCollection<T>, IEnumerable<T> where T : IPlanarElement
|
||||
{
|
||||
public delegate void ForEachDelegate(T element, int x, int y);
|
||||
private readonly T?[] array;
|
||||
private readonly int width;
|
||||
private readonly int height;
|
||||
|
||||
public PlanarCollection(int width, int height)
|
||||
{
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
array = new T[width * height];
|
||||
}
|
||||
|
||||
public T? this[int y, int x]
|
||||
{
|
||||
get => array[y * width + x];
|
||||
set => array[y * width + x] = value;
|
||||
}
|
||||
public T? this[float y, float x]
|
||||
{
|
||||
get => array[(int)y * width + (int)x];
|
||||
set => array[(int)y * width + (int)x] = value;
|
||||
}
|
||||
|
||||
public int GetLength(int dimension) => dimension switch
|
||||
{
|
||||
0 => height,
|
||||
1 => width,
|
||||
_ => throw new IndexOutOfRangeException()
|
||||
};
|
||||
|
||||
public void ForEachNotNull(ForEachDelegate callback)
|
||||
{
|
||||
for (var x = 0; x < width; x++)
|
||||
{
|
||||
for (var y = 0; y < height; y++)
|
||||
{
|
||||
var elem = this[y, x];
|
||||
if (elem != null)
|
||||
callback(elem, x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
foreach (var item in array)
|
||||
if (item != null) yield return item;
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => array.GetEnumerator();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user