private List <Vector2Int> Trace(PathTracingItem item) { List <Vector2Int> path = new List <Vector2Int>(); PathTracingItem currentItem = item; while (currentItem != null) { path.Add(currentItem.point); currentItem = currentItem.root; } path.Reverse(); return(path); }
private PathTracingItem FindPathTracingItem(IEnumerable <PathTracingItem> items) { if (items.Count() == 0) { return(null); } PathTracingItem tracingItem = items.First(); foreach (PathTracingItem item in items) { if (item.H + item.G < tracingItem.H + tracingItem.G) { tracingItem = item; } } return(tracingItem); }
public static PathTracingItem Create(Vector2Int point, Vector2Int from, Vector2Int to, PathTracingItem root = null) { PathTracingItem currentCell = new PathTracingItem(); currentCell.H = Vector2.Distance(point, to); if (root == null) { currentCell.G = Vector2.Distance(from, point); } else { currentCell.G = Vector2.Distance(from, root.point) + Vector2.Distance(root.point, point); } currentCell.point = point; currentCell.root = root; return(currentCell); }
public List <Vector2Int> FindPath(Vector2Int from, Vector2Int to, IShipDataContainer data) { PathTracingItem currentCell; List <Vector2Int> path = new List <Vector2Int>(); List <PathTracingItem> viewed = new List <PathTracingItem>(); List <PathTracingItem> viewing = new List <PathTracingItem>(); if (from == to) { return(path); } viewing.Add(PathTracingItem.Create(from, from, to)); while (viewing.Count > 0) { currentCell = FindPathTracingItem(viewing); foreach (Vector2Int offset in offsets) { var isValid = false; var point = offset + currentCell.point; var blocks = data.GetBlocks(point.x, point.y); foreach (var block in blocks) { if (block.ID == (int)Blocks.Floor) { isValid = true; } if (block.ID != (int)Blocks.Floor) { isValid = false; break; } } if (isValid) { viewing.Add(PathTracingItem.Create(point, from, to, currentCell)); } if (point == to) { path = Trace(currentCell); return(path); } } viewed.Add(currentCell); viewing.RemoveAll(x => viewed.Contains(x)); if (viewed.Count > 1000) { return(path); } } return(path); }