87 lines
2.3 KiB
C#
87 lines
2.3 KiB
C#
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Drawing;
|
||
|
using System.Linq;
|
||
|
using UnityEngine;
|
||
|
|
||
|
namespace GUI_Scripts.ProceduralGeneration
|
||
|
{
|
||
|
public class FloodFill
|
||
|
{
|
||
|
private Map map;
|
||
|
private int id;
|
||
|
private List<Vector2Int> positions = new List<Vector2Int>();
|
||
|
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;
|
||
|
foreach (var dir in Directions2D)
|
||
|
{
|
||
|
if (map.tiles.ContainsKey(posiiton + dir) && !positions.Contains(posiiton+dir))
|
||
|
{
|
||
|
queue.Add(posiiton+dir);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return queue.Count == 0;
|
||
|
}
|
||
|
|
||
|
public static readonly Vector2Int[] Directions2D = new Vector2Int[]
|
||
|
{
|
||
|
Vector2Int.left,
|
||
|
Vector2Int.right,
|
||
|
Vector2Int.up,
|
||
|
Vector2Int.down,
|
||
|
};
|
||
|
}
|
||
|
|
||
|
|
||
|
public class Map
|
||
|
{
|
||
|
public Dictionary<Vector2Int, int> tiles;
|
||
|
|
||
|
public Map(Vector2Int size)
|
||
|
{
|
||
|
tiles = new Dictionary<Vector2Int, int>();
|
||
|
for (int x = 0; x < size.x; x++)
|
||
|
{
|
||
|
for (int y = 0; y < size.y; y++)
|
||
|
{
|
||
|
tiles.Add(new Vector2Int(x,y),0);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void Fill(List<FloodFill> fillers)
|
||
|
{
|
||
|
int count = fillers.Count;
|
||
|
while (count > 0)
|
||
|
{
|
||
|
count = fillers.Count;
|
||
|
foreach (var filler in fillers)
|
||
|
{
|
||
|
if (filler.Fill())
|
||
|
{
|
||
|
count--;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|