2022-12-16 14:03:23 +01:00
|
|
|
using System;
|
2022-12-07 21:59:27 +01:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using GUI_Scripts.ProceduralGeneration;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.Tilemaps;
|
2022-12-16 14:03:23 +01:00
|
|
|
using Random = UnityEngine.Random;
|
2022-12-07 21:59:27 +01:00
|
|
|
|
|
|
|
public class TileMapGenerator : MonoBehaviour
|
|
|
|
{
|
|
|
|
// Start is called before the first frame update
|
|
|
|
|
|
|
|
public Tilemap ground, walls;
|
|
|
|
public Tile ground1, wall1;
|
|
|
|
public Graph graph;
|
2022-12-16 14:03:23 +01:00
|
|
|
|
|
|
|
private void Start()
|
|
|
|
{
|
|
|
|
while (!Generate())
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Generate()
|
2022-12-07 21:59:27 +01:00
|
|
|
{
|
|
|
|
Map map = new Map(new Vector2Int(100, 100));
|
|
|
|
List<FloodFill> fillers = new List<FloodFill>();
|
|
|
|
|
2022-12-10 02:24:32 +01:00
|
|
|
for (int i = 0; i < 70; i++)
|
2022-12-07 21:59:27 +01:00
|
|
|
{
|
2022-12-10 02:24:32 +01:00
|
|
|
FloodFill filler = new FloodFill(map, 1, new Vector2Int(Random.Range(0, 70), Random.Range(0, 70)));
|
2022-12-07 21:59:27 +01:00
|
|
|
fillers.Add(filler);
|
|
|
|
}
|
|
|
|
map.Fill(fillers);
|
|
|
|
|
|
|
|
Graph graph = new Graph(map);
|
|
|
|
foreach (var filler in fillers)
|
|
|
|
{
|
|
|
|
graph.graphNodes.Add(filler);
|
|
|
|
}
|
|
|
|
graph.Connect();
|
|
|
|
Debug.Log(graph.checkConnectivity());
|
|
|
|
|
|
|
|
int count = fillers.Count;
|
|
|
|
for (int i = 0; i < 101; i++)
|
|
|
|
{
|
|
|
|
GraphNode random = graph.graphNodes[Random.Range(0, graph.graphNodes.Count)];
|
|
|
|
graph.removeNode(random);
|
|
|
|
random.id = 2;
|
|
|
|
|
|
|
|
if (!graph.checkConnectivity())
|
|
|
|
{
|
|
|
|
graph.AddNode(random);
|
|
|
|
random.id = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (graph.graphNodes.Count < count / 2)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-12-16 14:03:23 +01:00
|
|
|
|
|
|
|
if (graph.graphNodes.Count > count * 0.8)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2022-12-07 21:59:27 +01:00
|
|
|
|
|
|
|
foreach (var filler in fillers)
|
|
|
|
{
|
|
|
|
int id = filler.id;
|
|
|
|
foreach (var pos in filler.positions)
|
|
|
|
{
|
|
|
|
if (id == 1)
|
|
|
|
{
|
|
|
|
ground.SetTile(new Vector3Int(pos.x,pos.y,0), ground1);
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (id == 2)
|
|
|
|
{
|
2022-12-16 14:03:23 +01:00
|
|
|
walls.SetTile(new Vector3Int(pos.x,pos.y,0), wall1);
|
2022-12-07 21:59:27 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-16 14:03:23 +01:00
|
|
|
|
2022-12-07 21:59:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GraphNode playerPosStart = graph.graphNodes[Random.Range(0, graph.graphNodes.Count)];
|
|
|
|
Vector2Int startPos = playerPosStart.positions.ElementAt(Random.Range(0, playerPosStart.positions.Count));
|
|
|
|
Debug.Log(startPos);
|
2022-12-16 14:03:23 +01:00
|
|
|
|
|
|
|
return true;
|
2022-12-07 21:59:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|