66 lines
1.7 KiB
C#
66 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Random = System.Random;
|
|
|
|
namespace Assets.Map
|
|
{
|
|
public class LocationGenerator
|
|
{
|
|
private Graph<LocationSite> _graph;
|
|
private readonly List<(int, Location)> _queue = new List<(int, Location)>();
|
|
private Random _random;
|
|
|
|
public bool Done => _queue.Count == 0;
|
|
public Graph<LocationSite> Result => _graph;
|
|
|
|
public LocationGenerator(Graph<LocationSite> graph, Random random = null)
|
|
{
|
|
_graph = graph;
|
|
_random = random ?? new Random();
|
|
}
|
|
|
|
public void Init(params (int, Location)[] start)
|
|
{
|
|
foreach (var (vertex, location) in start)
|
|
SetLocation(vertex, location);
|
|
}
|
|
|
|
private (int, Location) Dequeue()
|
|
{
|
|
while (_queue.Count > 0)
|
|
{
|
|
var index = _random.Next(_queue.Count);
|
|
var pair = _queue[index];
|
|
|
|
_queue.RemoveAt(index);
|
|
|
|
if (_graph.Vertices[pair.Item1].Location == null) return pair;
|
|
}
|
|
|
|
throw new Exception("No more elements.");
|
|
}
|
|
|
|
public void Step()
|
|
{
|
|
try
|
|
{
|
|
var (vertex, location) = Dequeue();
|
|
|
|
SetLocation(vertex, location);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
}
|
|
}
|
|
|
|
private void SetLocation(int vertex, Location location)
|
|
{
|
|
_graph.Vertices[vertex].Location = location;
|
|
|
|
foreach (var neighbour in _graph.Neighbours(vertex))
|
|
if (_graph.Vertices[neighbour].Location == null)
|
|
_queue.Add((neighbour, location));
|
|
}
|
|
}
|
|
} |