public PriorityCell(MazeCell cell, int priority, PriorityCell prev) { this.cell = cell; this.prev = prev; this.priority = priority; }
public void MakePath(MazeCell end) { List <PriorityCell> queue = new List <PriorityCell>(); PriorityCell first = new PriorityCell(enemy.current, 0, null); queue.Add(first); int i = 0; bool isAtEnd = (queue[i].cell.x == end.x && queue[i].cell.y == end.y); Debug.Log(queue[i].cell.x + ", " + queue[i].cell.y + " " + end.x + ", " + end.y); int inc = 1; while (!isAtEnd) { PriorityCell cur = queue[i]; //Up if (cur.cell.y < size_y - 1 && cells[cur.cell.x, cur.cell.y].CellEdges[0] == 1) { PriorityCell next = new PriorityCell(cells[cur.cell.x, cur.cell.y + 1], (queue[i].GetPriority() + inc), cur); queue.Add(next); inc = 1; } //Right if (cur.cell.x < size_x - 1 && cells[cur.cell.x, cur.cell.y].CellEdges[1] == 1) { PriorityCell next = new PriorityCell(cells[cur.cell.x + 1, cur.cell.y], (queue[i].GetPriority() + inc), cur); queue.Add(next); inc = 1; } //Down if (cur.cell.y > 0 && cells[cur.cell.x, cur.cell.y].CellEdges[2] == 1) { PriorityCell next = new PriorityCell(cells[cur.cell.x, cur.cell.y - 1], (queue[i].GetPriority() + inc), cur); queue.Add(next); inc = 1; } //Left if (cur.cell.x > 0 && cells[cur.cell.x, cur.cell.y].CellEdges[3] == 1) { PriorityCell next = new PriorityCell(cells[cur.cell.x - 1, cur.cell.y], (queue[i].GetPriority() + inc), cur); queue.Add(next); inc = 1; } i++; isAtEnd = (queue[i].cell.x == end.x && queue[i].cell.y == end.y); } PriorityCell current = queue[i]; List <MazeCell> path = new List <MazeCell>(); while (current.GetPriority() != 0) { path.Insert(0, current.GetCell()); current = current.prev; } enemy.current.probability = 0; if (path.Count > 0) { path[0].probability = 1; } }