public void AddIfCanWalk(CanWalk canWalk, int dx, int dy) { if (canWalk(x + dx, y + dy)) { children.Add(new Node(x + dx, y + dy, this, 0, 0)); } }
public void HighlightAvailableTiles(BaseUnit unit) { CanWalk w = unit.GetComponent <CanWalk>(); foreach (Vector2Int tilePos in _mapManager.GetAvailableTilesInRangeForMovemntType(unit.Position.x, unit.Position.y, w.Settings.MovemntType, w.Settings.Range)) { HighlightTile(tilePos); } }
private void Awake() { Instance = this; canWalkWithChar = (int x, int y) => { if (x < 0 || x >= width || y < 0 || y >= height) { return(false); } return(GetTile(x, y).CanWalkWithChar); }; canWalkNoChar = (int x, int y) => { if (x < 0 || x >= width || y < 0 || y >= height) { return(false); } return(GetTile(x, y).CanWalkNoChar); }; var xSize = width / 2; var ySize = height / 2; for (var x = -xSize; x < xSize; x++) { for (var y = -ySize; y < ySize; y++) { var clone = Instantiate(tilePrefab, new Vector3(x * tileSize, y * tileSize, 0), new Quaternion()); clone.transform.SetParent(transform); clone.name = "x:" + (x + width / 2) + "y:" + (y + width / 2); var tile = clone.GetComponent <Tile>(); tile.SetSprite(RandSprite()); tile.Pos = new Vector2Int(x + xSize, y + ySize); if (Random.Range(0f, 1f) > 0.9) { tile.Block(); } else { clearTiles.Add(tile); } tiles.Add(tile); } } }
public static AStarPath GetPath(int x1, int y1, int x2, int y2, CanWalk canWalk, bool allowIncomplete = true) { var open = new Dictionary <string, Node>(); var closed = new Dictionary <string, Node>(); var strt = new Node(x1, y1, null, GetH(x1, y1, x2, y2), 0); var closest = strt; open.Add(strt.str, strt); while (open.Count > 0) { var current = LowestF(open); open.Remove(current.str); closed.Add(current.str, current); if (current.x == x2 && current.y == y2) { return(MakePath(current, true)); } if (current.h < closest.h) { closest = current; } current.AddIfCanWalk(canWalk, 0, -1); current.AddIfCanWalk(canWalk, -1, 0); current.AddIfCanWalk(canWalk, 0, 1); current.AddIfCanWalk(canWalk, 1, 0); /*current.AddIfCanWalk(canWalk, -1, -1);*/ /*current.AddIfCanWalk(canWalk, 1, 1);*/ /*current.AddIfCanWalk(canWalk, 1, -1);*/ /*current.AddIfCanWalk(canWalk, -1, 1);*/ foreach (var child in current.children) { if (closed.ContainsKey(child.str)) { continue; } child.g = current.g + 1; child.h = GetH(child.x, child.y, x2, y2); if (open.ContainsKey(child.str)) { continue; } open.Add(child.str, child); } } if (allowIncomplete) { return(MakePath(closest, false)); } return(null); }
public HashSet <Vector2Int> GetAvailableTilesForUnit(BaseUnit unit) { CanWalk w = unit.GetComponent <CanWalk>(); return(GetAvailableTilesInRangeForMovemntType(unit.Position.x, unit.Position.y, w.Settings.MovemntType, w.Settings.Range)); }
public IEnumerable <DirectionalCell> AStar <TPathCell>(TPathCell start, TPathCell end, CanWalk <TPathCell> canWalk, ArroundSelectMode mode) where TPathCell : T, IPathCell { if (Equals(start, end)) { return(new DirectionalCell[0]); } Node <TPathCell>[,] map = new Node <TPathCell> [Width, Height]; for (int x = 0; x < Width; ++x) { for (int y = 0; y < Height; ++y) { map[x, y] = new Node <TPathCell>(); } } Node <TPathCell> startNode = map[start.X, start.Y]; startNode.Modify(null, start, 0); AStar(startNode, end, canWalk, mode, (int)Math.Sqrt(Math.Pow(end.X - start.X, 2) + Math.Pow(end.Y - start.Y, 2)), map, 1); IList <DirectionalCell> result = new List <DirectionalCell>(); var endNode = map[end.X, end.Y]; if (endNode.Parent != null) { result.Add(new DirectionalCell(endNode.Cell, Direction.Stay, endNode.Value)); } for (Node <TPathCell> 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, n.Value)); } } if (result.Count == 0) { throw new Exception("Impossible de trouver un chemin"); } return(result.Reverse()); }
private static Node <TPathCell> AStar <TPathCell>(Node <TPathCell> parent, TPathCell end, CanWalk <TPathCell> canWalk, ArroundSelectMode mode, int minStep, Node <TPathCell>[,] map, int value) where TPathCell : T, IPathCell { foreach (var t in parent.Cell.Arround(mode).Cast <TPathCell>()) { var node = map[t.X, t.Y]; int cost; if (Equals(t, end)) { if (canWalk(t, parent.Cell, out cost)) { if (node.Value > value) { node.Modify(parent, t, value); // + t.CellCost); } } } if (canWalk(t, parent.Cell, out cost)) { if (node.Value > value + cost) // cost = cost; //else // cost = cost; { node.Modify(parent, t, value + cost); var n = AStar(node, end, canWalk, mode, minStep, map, value + 1 + cost); if (n != null) { return(n); } } } } return(null); }