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-29 01:53:03 +01:00
|
|
|
using Vector2 = System.Numerics.Vector2;
|
2022-12-07 21:59:27 +01:00
|
|
|
|
|
|
|
public class TileMapGenerator : MonoBehaviour
|
|
|
|
{
|
|
|
|
// Start is called before the first frame update
|
|
|
|
|
|
|
|
public Tilemap ground, walls;
|
2022-12-29 01:53:03 +01:00
|
|
|
public Tile ground1, wall1, corner_left_up, corner_left_down, corner_right_down, corner_right_up, left, right, up, down;
|
2022-12-07 21:59:27 +01:00
|
|
|
public Graph graph;
|
2023-01-06 18:17:42 +01:00
|
|
|
public List<Vector3> availablePos;
|
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-29 01:53:03 +01:00
|
|
|
|
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
|
|
|
|
2022-12-29 03:22:45 +01:00
|
|
|
GraphNode playerPosStart = graph.graphNodes[Random.Range(0, graph.graphNodes.Count)];
|
|
|
|
Vector2Int startPos = playerPosStart.positions.ElementAt(Random.Range(0, playerPosStart.positions.Count));
|
|
|
|
Vector3 startPosOfPlayer = default;
|
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);
|
2022-12-29 03:22:45 +01:00
|
|
|
startPosOfPlayer = new Vector3Int(pos.x, pos.y, 0);
|
2023-01-06 18:17:42 +01:00
|
|
|
availablePos.Add(startPosOfPlayer);
|
2022-12-07 21:59:27 +01:00
|
|
|
}
|
|
|
|
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-29 01:53:03 +01:00
|
|
|
|
2022-12-29 03:22:45 +01:00
|
|
|
|
2022-12-29 01:53:03 +01:00
|
|
|
walls.SetTile(new Vector3Int(-1,-1,0), corner_left_down);
|
|
|
|
walls.SetTile(new Vector3Int(-1,100,0), corner_left_up);
|
|
|
|
walls.SetTile(new Vector3Int(100,100,0), corner_right_up);
|
|
|
|
walls.SetTile(new Vector3Int(100,-1,0), corner_right_down);
|
|
|
|
|
|
|
|
for (int i=0; i<100; i++){walls.SetTile(new Vector3Int(-1,i,0), left);}
|
|
|
|
for (int i=0; i<100; i++){walls.SetTile(new Vector3Int(100,i,0), right);}
|
|
|
|
for (int i=0; i<100; i++){walls.SetTile(new Vector3Int(i,-1,0), down);}
|
|
|
|
for (int i=0; i<100; i++){walls.SetTile(new Vector3Int(i,100,0), up);}
|
|
|
|
|
2022-12-29 03:22:45 +01:00
|
|
|
|
2022-12-07 21:59:27 +01:00
|
|
|
Debug.Log(startPos);
|
2022-12-29 01:53:03 +01:00
|
|
|
Debug.Log("cell added");
|
2022-12-29 03:22:45 +01:00
|
|
|
|
|
|
|
GameObject Player = GameObject.FindWithTag("Player");
|
2023-01-07 23:46:54 +01:00
|
|
|
Player.transform.position = randomCoordsForStanding();
|
2023-01-06 18:17:42 +01:00
|
|
|
|
2023-01-07 23:46:54 +01:00
|
|
|
//randomCoordsForStanding();
|
2022-12-16 14:03:23 +01:00
|
|
|
|
|
|
|
return true;
|
2022-12-07 21:59:27 +01:00
|
|
|
}
|
|
|
|
|
2023-01-06 18:17:42 +01:00
|
|
|
|
|
|
|
public Vector3 randomCoordsForStanding()
|
|
|
|
{
|
|
|
|
Tile test = new Tile();
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
int bound = availablePos.Count;
|
|
|
|
int randomVal;
|
|
|
|
randomVal = Random.Range(0, bound);
|
|
|
|
Debug.Log(availablePos[randomVal]);
|
|
|
|
return availablePos[randomVal];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-12-07 21:59:27 +01:00
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|