Code smells

This commit is contained in:
2021-03-04 20:35:23 -06:00
parent e64f75e3cc
commit 7ed771d467
31 changed files with 310 additions and 339 deletions

18
PathFinding/Direction.cs Normal file
View File

@@ -0,0 +1,18 @@
using System.Numerics;
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 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);
}
}

8
PathFinding/Distance.cs Normal file
View File

@@ -0,0 +1,8 @@
namespace PathFinding
{
public enum Distance
{
OneStep,
MultiStep
}
}

View File

@@ -1,19 +0,0 @@
namespace PathFinding
{
public enum HaltCondition
{
/// <summary>
/// Do not stop until you reach the collection boundary.
/// </summary>
None,
/// <summary>
/// Halt after encountering a non-null element.
/// </summary>
AfterCollide
}
}
public enum Distance
{
OneStep,
MultiStep
}

View File

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

View File

@@ -1,12 +1,9 @@
using System.Collections.Generic;
namespace PathFinding
{
public interface IPlanarElement
{
ICollection<Path> GetPaths();
MoveSet MoveSet { get; }
bool IsUpsideDown { get; }
}
}

View File

@@ -4,11 +4,11 @@ using System.Numerics;
namespace PathFinding
{
[DebuggerDisplay("{Direction} - {Distance}")]
public class Path
public class Move
{
public Vector2 Direction { get; }
public Distance Distance { get; }
public Path(Vector2 direction, Distance distance = Distance.OneStep)
public Move(Vector2 direction, Distance distance = Distance.OneStep)
{
Direction = direction;
Distance = distance;

23
PathFinding/MoveSet.cs Normal file
View File

@@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace PathFinding
{
public class MoveSet
{
private readonly IPlanarElement element;
private readonly ICollection<Move> moves;
private readonly ICollection<Move> upsidedownMoves;
public MoveSet(IPlanarElement element, ICollection<Move> moves)
{
this.element = element;
this.moves = moves;
upsidedownMoves = moves.Select(_ => new Move(Vector2.Negate(_.Direction), _.Distance)).ToList();
}
public ICollection<Move> GetMoves() => element.IsUpsideDown ? upsidedownMoves : moves;
}
}

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
@@ -13,7 +12,6 @@ namespace PathFinding
/// <param name="position"></param>
public delegate void Callback(T collider, Vector2 position);
private readonly IPlanarCollection<T> collection;
private readonly int width;
private readonly int height;
@@ -39,8 +37,9 @@ namespace PathFinding
return false;
}
var element = collection[origin.X, origin.Y];
var path = FindDirectionTowardsDestination(element.GetPaths(), origin, destination);
if (element == null) return false;
var path = FindDirectionTowardsDestination(element.MoveSet.GetMoves(), origin, destination);
if (!IsPathable(origin, destination, path.Direction))
{
// Assumption: if a single best-choice step towards the destination cannot happen, no pathing can happen.
@@ -69,7 +68,7 @@ namespace PathFinding
public void PathEvery(Vector2 from, Callback callback)
{
var element = collection[from.X, from.Y];
foreach (var path in element.GetPaths())
foreach (var path in element.MoveSet.GetMoves())
{
var shouldPath = true;
var next = Vector2.Add(from, path.Direction); ;
@@ -106,16 +105,15 @@ namespace PathFinding
}
}
public Path FindDirectionTowardsDestination(ICollection<Path> paths, Vector2 origin, Vector2 destination) =>
public static Move FindDirectionTowardsDestination(ICollection<Move> 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);
public bool IsPathable(Vector2 origin, Vector2 destination, T element)
public static bool IsPathable(Vector2 origin, Vector2 destination, T element)
{
var path = FindDirectionTowardsDestination(element.GetPaths(), origin, destination);
var path = FindDirectionTowardsDestination(element.MoveSet.GetMoves(), origin, destination);
return IsPathable(origin, destination, path.Direction);
}
public bool IsPathable(Vector2 origin, Vector2 destination, Vector2 direction)
public static bool IsPathable(Vector2 origin, Vector2 destination, Vector2 direction)
{
direction = Vector2.Normalize(direction);
var next = Vector2.Add(origin, direction);

View File

@@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisLevel>5</AnalysisLevel>
</PropertyGroup>
</Project>