Scriptum/Assets/Scripts/ProceduralGeneration/TileMapGenerator.cs

86 lines
2.3 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using GUI_Scripts.ProceduralGeneration;
using UnityEngine;
using UnityEngine.Tilemaps;
public class TileMapGenerator : MonoBehaviour
{
// Start is called before the first frame update
public Tilemap ground, walls;
public Tile ground1, wall1;
public Graph graph;
void Start()
{
Map map = new Map(new Vector2Int(100, 100));
List<FloodFill> fillers = new List<FloodFill>();
for (int i = 0; i < 50; i++)
{
FloodFill filler = new FloodFill(map, 1, new Vector2Int(Random.Range(0, 50), Random.Range(0, 50)));
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;
}
}
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)
{
ground.SetTile(new Vector3Int(pos.x,pos.y,0), wall1);
}
}
}
GraphNode playerPosStart = graph.graphNodes[Random.Range(0, graph.graphNodes.Count)];
Vector2Int startPos = playerPosStart.positions.ElementAt(Random.Range(0, playerPosStart.positions.Count));
Debug.Log(startPos);
}
// Update is called once per frame
void Update()
{
}
}