Beispiel #1
0
    private Vector3Int[] FloodFillFromToPath(Vector3Int start, Vector3Int to)
    {
        Queue <FFTile> frontier = new Queue <FFTile>();
        List <FFTile>  visited  = new List <FFTile>();

        if (isWalkable(start))
        {
            frontier.Enqueue(new FFTile(start, null));
        }

        while (frontier.Count > 0)
        {
            if (frontier.Peek().pos == to)
            {
                break;
            }
            var a = frontier.Dequeue();
            visited.Add(a);



            //if (isWalkable(new Vector3Int(a.pos.x - 1, a.pos.y, 0))) frontier.Enqueue(new FFTile(new Vector3Int(a.pos.x - 1, a.pos.y, 0),a));
            //if (isWalkable(new Vector3Int(a.pos.x + 1, a.pos.y, 0))) frontier.Enqueue(new FFTile(new Vector3Int(a.pos.x + 1, a.pos.y, 0), a));
            //if (isWalkable(new Vector3Int(a.pos.x, a.pos.y - 1, 0))) frontier.Enqueue(new FFTile(new Vector3Int(a.pos.x, a.pos.y - 1, 0), a));
            //if (isWalkable(new Vector3Int(a.pos.x, a.pos.y + 1, 0))) frontier.Enqueue(new FFTile(new Vector3Int(a.pos.x, a.pos.y + 1, 0), a));

            TryAddFFTile(frontier, new Vector3Int(a.pos.x - 1, a.pos.y, 0), visited, a);
            TryAddFFTile(frontier, new Vector3Int(a.pos.x + 1, a.pos.y, 0), visited, a);
            TryAddFFTile(frontier, new Vector3Int(a.pos.x, a.pos.y - 1, 0), visited, a);
            TryAddFFTile(frontier, new Vector3Int(a.pos.x, a.pos.y + 1, 0), visited, a);
        }

        List <Vector3Int> path = new List <Vector3Int>();

        try
        {
            FFTile c = frontier.Dequeue();
            while (c.from != null)
            {
                path.Add(c.pos);
                c = c.from;
            }
        }
        catch (InvalidOperationException) { }

        return(path.ToArray());
    }
Beispiel #2
0
 public FFTile(Vector3Int pos, FFTile from)
 {
     this.pos  = pos;
     this.from = from;
 }
Beispiel #3
0
 void TryAddFFTile(Queue <FFTile> frontier, Vector3Int item, List <FFTile> visited, FFTile a)
 {
     if (isWalkable(item) && !visited.Contains(new FFTile(item, a)))
     {
         frontier.Enqueue(new FFTile(item, a));
     }
 }