Before changing Piece[,] to Dictionary<string,Piece>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
|
||||
@@ -34,7 +35,7 @@ namespace PathFinding
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var element = collection[origin.X, origin.Y];
|
||||
var element = collection[origin.Y, origin.X];
|
||||
if (element == null) return false;
|
||||
|
||||
var path = FindDirectionTowardsDestination(element.MoveSet.GetMoves(), origin, destination);
|
||||
@@ -49,7 +50,7 @@ namespace PathFinding
|
||||
while (shouldPath && next != destination)
|
||||
{
|
||||
next = Vector2.Add(next, path.Direction);
|
||||
var collider = collection[(int)next.X, (int)next.Y];
|
||||
var collider = collection[(int)next.Y, (int)next.X];
|
||||
if (collider != null)
|
||||
{
|
||||
callback?.Invoke(collider, next);
|
||||
@@ -65,14 +66,19 @@ namespace PathFinding
|
||||
|
||||
public void PathEvery(Vector2 from, Callback callback)
|
||||
{
|
||||
var element = collection[from.X, from.Y];
|
||||
var element = collection[from.Y, from.X];
|
||||
if (element == null)
|
||||
{
|
||||
Console.WriteLine("Null element in PathEvery");
|
||||
return;
|
||||
}
|
||||
foreach (var path in element.MoveSet.GetMoves())
|
||||
{
|
||||
var shouldPath = true;
|
||||
var next = Vector2.Add(from, path.Direction); ;
|
||||
while (shouldPath && next.X < width && next.Y < height && next.X >= 0 && next.Y >= 0)
|
||||
{
|
||||
var collider = collection[(int)next.X, (int)next.Y];
|
||||
var collider = collection[(int)next.Y, (int)next.X];
|
||||
if (collider != null)
|
||||
{
|
||||
callback(collider, next);
|
||||
@@ -97,7 +103,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.X, next.Y];
|
||||
var element = collection[next.Y, next.X];
|
||||
if (element != null) callback(element, next);
|
||||
next = Vector2.Add(next, direction);
|
||||
}
|
||||
|
||||
61
PathFinding/PlanarCollection.cs
Normal file
61
PathFinding/PlanarCollection.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
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