2022-11-10 16:00:57 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Drawing;
|
|
|
|
using System.Linq;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace GUI_Scripts.ProceduralGeneration
|
|
|
|
{
|
2022-12-07 21:59:27 +01:00
|
|
|
public class FloodFill : GraphNode
|
2022-11-10 16:00:57 +01:00
|
|
|
{
|
|
|
|
private Map map;
|
2022-12-07 21:59:27 +01:00
|
|
|
|
2022-11-10 16:00:57 +01:00
|
|
|
private List<Vector2Int> queue = new List<Vector2Int>();
|
|
|
|
|
|
|
|
public FloodFill(Map map, int id, Vector2Int startPosition)
|
|
|
|
{
|
|
|
|
this.map = map;
|
|
|
|
this.id = id;
|
|
|
|
queue.Add(startPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Fill()
|
|
|
|
{
|
|
|
|
while (queue.Count > 0)
|
|
|
|
{
|
|
|
|
Vector2Int posiiton = queue[0];
|
|
|
|
queue.RemoveAt(0);
|
|
|
|
if (map.tiles.ContainsKey(posiiton) && map.tiles[posiiton] == 0)
|
|
|
|
{
|
|
|
|
map.tiles[posiiton] = id;
|
2022-12-07 21:59:27 +01:00
|
|
|
positions.Add(posiiton);
|
2022-11-10 16:00:57 +01:00
|
|
|
foreach (var dir in Directions2D)
|
|
|
|
{
|
|
|
|
if (map.tiles.ContainsKey(posiiton + dir) && !positions.Contains(posiiton+dir))
|
|
|
|
{
|
|
|
|
queue.Add(posiiton+dir);
|
|
|
|
}
|
|
|
|
}
|
2022-12-07 21:59:27 +01:00
|
|
|
break;
|
2022-11-10 16:00:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return queue.Count == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static readonly Vector2Int[] Directions2D = new Vector2Int[]
|
|
|
|
{
|
|
|
|
Vector2Int.left,
|
|
|
|
Vector2Int.right,
|
|
|
|
Vector2Int.up,
|
|
|
|
Vector2Int.down,
|
|
|
|
};
|
|
|
|
}
|
2022-12-07 21:59:27 +01:00
|
|
|
|
2022-11-10 16:00:57 +01:00
|
|
|
}
|