before deleting Rules
This commit is contained in:
59
Gameboard.ShogiUI.Rules/PlanarCollection.cs
Normal file
59
Gameboard.ShogiUI.Rules/PlanarCollection.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using PathFinding;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Gameboard.ShogiUI.Rules
|
||||
{
|
||||
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 x, int y]
|
||||
{
|
||||
get => array[y * width + x];
|
||||
set => array[y * width + x] = value;
|
||||
}
|
||||
public T? this[float x, float y]
|
||||
{
|
||||
get => array[(int)y * width + (int)x];
|
||||
set => array[(int)y * width + (int)x] = value;
|
||||
}
|
||||
|
||||
public int GetLength(int dimension) => dimension switch
|
||||
{
|
||||
0 => width,
|
||||
1 => height,
|
||||
_ => throw new IndexOutOfRangeException()
|
||||
};
|
||||
|
||||
public void ForEachNotNull(ForEachDelegate callback)
|
||||
{
|
||||
for (var x = 0; x < width; x++)
|
||||
{
|
||||
for (var y = 0; y < height; y++)
|
||||
{
|
||||
if (this[x, y] != null)
|
||||
callback(this[x, y], x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
foreach (var item in array) yield return item;
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => array.GetEnumerator();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user