This commit is contained in:
2021-03-01 20:55:37 -06:00
parent f55716d2ec
commit 3da187be17
5 changed files with 154 additions and 109 deletions

View File

@@ -4,7 +4,7 @@ namespace PathFinding
{
public interface IPlanarCollection<T> : IEnumerable<T>
{
T this[int x, int y] { get; set; }
T this[float x, float y] { get; set; }
int GetLength(int dimension);
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
@@ -24,46 +25,50 @@ namespace PathFinding
}
/// <summary>
/// Navigate the collection such that each "step" is always towards the destination.
/// Navigate the collection such that each "step" is always towards the destination, respecting the Paths available to the element at origin.
/// </summary>
/// <param name="element">The pathing element.</param>
/// <param name="origin">The starting location.</param>
/// <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(T element, Vector2 origin, Vector2 destination, Callback callback)
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.X, origin.Y];
var path = FindDirectionTowardsDestination(element.GetPaths(), origin, destination);
var next = Vector2.Add(origin, path.Direction);
if (!IsPathable(origin, destination, next))
if (!IsPathable(origin, destination, path.Direction))
{
// Assumption: if a single best-choice step towards the destination cannot happen, no pathing can happen.
return false;
}
var shouldPath = true;
while (shouldPath)
var next = origin;
while (shouldPath && next != destination)
{
next = Vector2.Add(next, path.Direction);
var collider = collection[(int)next.X, (int)next.Y];
if (collider != null) callback(collider, next);
if (next == destination) return true;
if (path.Distance == Distance.OneStep)
if (collider != null)
{
callback?.Invoke(collider, next);
shouldPath = false;
}
else if (path.Distance == Distance.OneStep)
{
shouldPath = false;
}
next = Vector2.Add(next, path.Direction);
}
return true;
return next == destination;
}
public void PathEvery(IPlanarElement element, Vector2 from, Callback callback)
public void PathEvery(Vector2 from, Callback callback)
{
var element = collection[from.X, from.Y];
foreach (var path in element.GetPaths())
{
var shouldPath = true;
@@ -84,6 +89,22 @@ namespace PathFinding
}
}
/// <summary>
/// Path the line from origin to destination, ignoring any Paths defined by the element at origin.
/// </summary>
public void LinePathTo(Vector2 origin, Vector2 direction, Callback callback)
{
direction = Vector2.Normalize(direction);
var next = Vector2.Add(origin, direction);
while (next.X >= 0 && next.X < width && next.Y >= 0 && next.Y < height)
{
var element = collection[next.X, next.Y];
if (element != null) callback(element, next);
next = Vector2.Add(next, direction);
}
}
public Path FindDirectionTowardsDestination(ICollection<Path> paths, Vector2 origin, Vector2 destination) =>
paths.Aggregate((a, b) => Vector2.Distance(destination, Vector2.Add(origin, a.Direction)) < Vector2.Distance(destination, Vector2.Add(origin, b.Direction)) ? a : b);
@@ -91,20 +112,26 @@ namespace PathFinding
public bool IsPathable(Vector2 origin, Vector2 destination, T element)
{
var path = FindDirectionTowardsDestination(element.GetPaths(), origin, destination);
var next = Vector2.Add(origin, path.Direction);
return IsPathable(origin, destination, next);
return IsPathable(origin, destination, path.Direction);
}
public bool IsPathable(Vector2 origin, Vector2 destination, Vector2 next)
public bool IsPathable(Vector2 origin, Vector2 destination, Vector2 direction)
{
if (Vector2.Distance(next, destination) < Vector2.Distance(origin, destination))
direction = Vector2.Normalize(direction);
var next = Vector2.Add(origin, direction);
if (Vector2.Distance(next, destination) >= Vector2.Distance(origin, destination)) return false;
var slope = (destination.Y - origin.Y) / (destination.X - origin.X);
if (float.IsInfinity(slope))
{
// y = mx + b
// b = -mx + y
var slope = (destination.Y - origin.Y) / (destination.X - origin.X);
var yIntercept = -(slope * origin.X) + origin.Y;
return float.IsInfinity(slope) || next.Y == slope * next.X + yIntercept;
return next.X == destination.X;
}
else
{
// b = -mx + y
var yIntercept = -slope * origin.X + origin.Y;
// y = mx + b
return next.Y == slope * next.X + yIntercept;
}
return false;
}
}
}