diff --git a/Grid.cs b/Grid.cs index ffd476a..2881b6e 100644 --- a/Grid.cs +++ b/Grid.cs @@ -47,6 +47,7 @@ public class Cell : Grid.ICell where T : Cell { private readonly int _x, _y; + private readonly Grid _list; private T _up, _left, _down, _right; /// @@ -60,6 +61,7 @@ public Cell(int x, int y, Grid list, bool torus) { _x = x; _y = y; + _list = list; if(torus) { @@ -116,6 +118,21 @@ public int Y get { return _y; } } + public Grid Grid + { + get { return _list; } + } + + public IEnumerable Range(int radius) + { + return Grid.Range(this as T, radius); + } + + public IEnumerable Cricle(int radius) + { + return Grid.Circle(this as T, radius); + } + /// /// liste toutes les cellules voisines en fonction du mode de sélection /// @@ -384,16 +401,19 @@ public struct DirectionalCell { private readonly T _cell; private readonly Direction _direction; + private readonly int _value; - public DirectionalCell(T cell, Direction direction) + public DirectionalCell(T cell, Direction direction, int value) { _cell = cell; _direction = direction; + _value = value; } - public DirectionalCell(T cell, T nextCell) + public DirectionalCell(T cell, T nextCell, int value) { _cell = cell; + _value = value; if (Equals(cell, default(T))) { _direction = Direction.Stay; @@ -440,6 +460,25 @@ public Direction Direction { get { return _direction; } } + + public int Value + { + get { return _value; } + } + + public float ToRad() + { + if (_direction == Direction.Stay) + return 0f; + return (float)((int)_direction * (Math.PI / 180)); + } + + public static float ToRad(Direction dir) + { + if (dir == Direction.Stay) + return 0f; + return (float)((int)dir * (Math.PI / 180)); + } } public class NodeProxy @@ -477,7 +516,7 @@ public class Node public U Cell { get; private set; } public int Value { get; private set; } - public Node(int x, int y) + public Node() //int x, int y) { Parent = null; Cell = default(U); @@ -497,9 +536,10 @@ public void Modify(Node parent, U cell, int value) private readonly T[,] _cells; private readonly int _width, _height; private readonly CreateCell _createCell; - private int _total; + private readonly int _total; - public delegate bool CanWalk(TpathCell first, TpathCell second, out int value); + public delegate bool CanWalk(TPathCell first, TPathCell second, out int value) + where TPathCell : T, IPathCell; /// /// Délégate de création de cellules @@ -607,7 +647,7 @@ public IEnumerable AStar(TPathCell start, TPathCell Node[,] map = new Node[Width, Height]; for (int x = 0; x < Width; ++x) for (int y = 0; y < Height; ++y) - map[x, y] = new Node(x, y); + map[x, y] = new Node(); Node startNode = map[start.X, start.Y]; startNode.Modify(null, start, 0); @@ -617,10 +657,10 @@ public IEnumerable AStar(TPathCell start, TPathCell IList result = new List(); var endNode = map[end.X, end.Y]; if (endNode.Parent != null) - result.Add(new DirectionalCell(endNode.Cell, Direction.Stay)); + result.Add(new DirectionalCell(endNode.Cell, Direction.Stay, endNode.Value)); for (Node n = endNode; n != null; n = n.Parent) if (!Equals(n.Cell, default(TPathCell)) && n.Parent != null) - result.Add(new DirectionalCell(n.Parent.Cell, n.Cell)); + result.Add(new DirectionalCell(n.Parent.Cell, n.Cell, n.Value)); if (result.Count == 0) throw new Exception("Impossible de trouver un chemin"); return result.Reverse(); diff --git a/Helper.cs b/Helper.cs index 1f482f5..a0fd823 100644 --- a/Helper.cs +++ b/Helper.cs @@ -6,6 +6,18 @@ namespace CSharpHelper { public static class Helper { + public static TResult IfNotNull(this TSource source, Func action) + { + return Equals(source, default(TSource)) ? default(TResult) : action(source); + } + + public static void IfNotNull(this TSource source, Action action) + { + if (Equals(source, default(TSource))) + return; + action(source); + } + public static bool IsInArray(this T[] array, int value) { return value >= 0 && value < array.Length;