diff --git a/Assets/Scripts/Pathfinding/NodeMap.cs b/Assets/Scripts/Pathfinding/NodeMap.cs index fa848ff1..3c909805 100644 --- a/Assets/Scripts/Pathfinding/NodeMap.cs +++ b/Assets/Scripts/Pathfinding/NodeMap.cs @@ -1,149 +1,162 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using UnityEngine; -using UnityEngine.Tilemaps; - -public class NodeMap : MonoBehaviour -{ - public Vector2Int mapSize; - public Node[,] nodeGrid = new Node[0,0]; - public float cellSize; - public TileData[] TileDatas; - public Dictionary DataFromTiles = new Dictionary(); - public bool hasEverRun = false; - public Tilemap tilemap; - // Start is called before the first frame update - void LateUpdate() - { - if (!hasEverRun){ - DataFromTiles = new Dictionary(); - foreach (var tileData in TileDatas) - { - foreach (var tile in tileData.Tiles) - { - DataFromTiles.Add(tile, tileData); - } - } - - hasEverRun = true; - CreateNodes(); - TileCheck(); - } - - } - - - - public void TileCheck() - { - foreach (var n in nodeGrid) - { - Vector2Int gridPosition = n.gridPosition; - - Vector3Int TileGridPosition = tilemap.WorldToCell(n.worldPosition); - TileBase Tile = tilemap.GetTile(TileGridPosition); - if (Tile == null) continue; - if (!DataFromTiles.ContainsKey(Tile)) - { - continue; - } - n.walkable = DataFromTiles[Tile].walkable; - } - } - - // Update is called once per frame - void Update() - { - - } - - public void TeleportTo(GameObject g, Node node) - { - g.transform.position = node.worldPosition; - } - - public List GetNeighbours(Node node) - { - List neighbours = new List(); - - for (int x = -1; x <= 1; x++) - { - for (int y = -1; y <= 1; y++) - { - if (x == 0 && y == 0) - continue; - - int checkX = node.gridPosition.x + x; - int checkY = node.gridPosition.y + y; - - if (checkX >= 0 && checkX < mapSize.x && checkY >= 0 && checkY < mapSize.y) - { - Node item = nodeGrid[checkX, checkY]; - neighbours.Add(item); - } - } - } - - return neighbours; - } - - - public void CreateNodes() - { - nodeGrid = new Node[mapSize.x, mapSize.y]; - for (int x = 0; x < nodeGrid.GetLength(0); x++) - { - for (int y = 0; y < nodeGrid.GetLength(1); y++) - { - Vector2Int gridPosition = new Vector2Int(x, y); - nodeGrid[x, y] = new Node(gridPosition, GetWorldPosition(gridPosition)); - Vector3Int TileGridPosition = tilemap.WorldToCell(nodeGrid[x,y].worldPosition); - TileBase Tile = tilemap.GetTile(TileGridPosition); - - //nodeGrid[x, y].walkable = DataFromTiles[Tile].walkable; - - } - } - } - - public Node NodeFromWorldPoint(Vector2 worldPosition) - { - float percentX = (worldPosition.x + mapSize.x / 2) / mapSize.x; - float percentY = (worldPosition.y + mapSize.y / 2) / mapSize.y; - percentX = Mathf.Clamp01(percentX); - percentY = Mathf.Clamp01(percentY); - - int x = Mathf.RoundToInt((mapSize.x - 1) * percentX); - int y = Mathf.RoundToInt((mapSize.y - 1) * percentY); - return nodeGrid[x, y]; - } - - - public Vector3 GetWorldPosition(Vector2Int position) - { - Vector3 worldBottomLeft = transform.position - (Vector3.right * (mapSize.x / 2)) - (Vector3.up * (mapSize.y / 2)); - Vector3 worldPoint = worldBottomLeft + (Vector3.right * position.x) + (Vector3.up * position.y); - Vector3 cellSizeOffset = new Vector3(cellSize / 2, cellSize / 2, 0); - return worldPoint * cellSize + cellSizeOffset; - } - - public bool showIndividualNodes; - - - public void OnDrawGizmos() - { - Gizmos.DrawWireCube(transform.position,new Vector3(mapSize.x,mapSize.y,1) * cellSize); - if (!showIndividualNodes) - { - return; - } - - foreach (var n in nodeGrid) - { - Vector3 size = Vector3.one * cellSize; - Gizmos.DrawWireCube(n.worldPosition, size); - } - } -} +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; +using UnityEngine.Tilemaps; + +public class NodeMap : MonoBehaviour +{ + public Vector2Int mapSize; + public Node[,] nodeGrid = new Node[0,0]; + public float cellSize; + public TileData[] TileDatas; + public Dictionary DataFromTiles = new Dictionary(); + public bool hasEverRun = false; + public Tilemap tilemap; + // Start is called before the first frame update + + + void Start() + { + DataFromTiles = new Dictionary(); + foreach (var tileData in TileDatas) + { + foreach (var tile in tileData.Tiles) + { + DataFromTiles.Add(tile, tileData); + } + } + hasEverRun = true; + CreateNodes(); + TileCheck(); + } + + public void Generate() + { + DataFromTiles = new Dictionary(); + foreach (var tileData in TileDatas) + { + foreach (var tile in tileData.Tiles) + { + DataFromTiles.Add(tile, tileData); + } + } + hasEverRun = true; + CreateNodes(); + TileCheck(); + } + + + + public void TileCheck() + { + foreach (var n in nodeGrid) + { + Vector2Int gridPosition = n.gridPosition; + + Vector3Int TileGridPosition = tilemap.WorldToCell(n.worldPosition); + TileBase Tile = tilemap.GetTile(TileGridPosition); + if (Tile == null) continue; + if (!DataFromTiles.ContainsKey(Tile)) + { + continue; + } + n.walkable = DataFromTiles[Tile].walkable; + } + } + + // Update is called once per frame + void Update() + { + + } + + public void TeleportTo(GameObject g, Node node) + { + g.transform.position = node.worldPosition; + } + + public List GetNeighbours(Node node) + { + List neighbours = new List(); + + for (int x = -1; x <= 1; x++) + { + for (int y = -1; y <= 1; y++) + { + if (x == 0 && y == 0) + continue; + + int checkX = node.gridPosition.x + x; + int checkY = node.gridPosition.y + y; + + if (checkX >= 0 && checkX < mapSize.x && checkY >= 0 && checkY < mapSize.y) + { + Node item = nodeGrid[checkX, checkY]; + neighbours.Add(item); + } + } + } + + return neighbours; + } + + + public void CreateNodes() + { + nodeGrid = new Node[mapSize.x, mapSize.y]; + for (int x = 0; x < nodeGrid.GetLength(0); x++) + { + for (int y = 0; y < nodeGrid.GetLength(1); y++) + { + Vector2Int gridPosition = new Vector2Int(x, y); + nodeGrid[x, y] = new Node(gridPosition, GetWorldPosition(gridPosition)); + Vector3Int TileGridPosition = tilemap.WorldToCell(nodeGrid[x,y].worldPosition); + TileBase Tile = tilemap.GetTile(TileGridPosition); + + //nodeGrid[x, y].walkable = DataFromTiles[Tile].walkable; + + } + } + } + + public Node NodeFromWorldPoint(Vector2 worldPosition) + { + float percentX = (worldPosition.x + mapSize.x / 2) / mapSize.x; + float percentY = (worldPosition.y + mapSize.y / 2) / mapSize.y; + percentX = Mathf.Clamp01(percentX); + percentY = Mathf.Clamp01(percentY); + + int x = Mathf.RoundToInt((mapSize.x - 1) * percentX); + int y = Mathf.RoundToInt((mapSize.y - 1) * percentY); + return nodeGrid[x, y]; + } + + + public Vector3 GetWorldPosition(Vector2Int position) + { + Vector3 worldBottomLeft = transform.position - (Vector3.right * (mapSize.x / 2)) - (Vector3.up * (mapSize.y / 2)); + Vector3 worldPoint = worldBottomLeft + (Vector3.right * position.x) + (Vector3.up * position.y); + Vector3 cellSizeOffset = new Vector3(cellSize / 2, cellSize / 2, 0); + return worldPoint * cellSize + cellSizeOffset; + } + + public bool showIndividualNodes; + + + public void OnDrawGizmos() + { + Gizmos.DrawWireCube(transform.position,new Vector3(mapSize.x,mapSize.y,1) * cellSize); + if (!showIndividualNodes) + { + return; + } + + foreach (var n in nodeGrid) + { + Vector3 size = Vector3.one * cellSize; + Gizmos.DrawWireCube(n.worldPosition, size); + } + } +} diff --git a/Assets/Scripts/ProceduralGeneration/TileMapGenerator.cs b/Assets/Scripts/ProceduralGeneration/TileMapGenerator.cs index 0fd48ade..817b951f 100644 --- a/Assets/Scripts/ProceduralGeneration/TileMapGenerator.cs +++ b/Assets/Scripts/ProceduralGeneration/TileMapGenerator.cs @@ -1,142 +1,148 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using GUI_Scripts.ProceduralGeneration; -using UnityEngine; -using UnityEngine.Tilemaps; -using Random = UnityEngine.Random; -using Vector2 = System.Numerics.Vector2; - -public class TileMapGenerator : MonoBehaviour -{ - // Start is called before the first frame update - - public Tilemap ground, walls; - public Tile ground1, wall1, corner_left_up, corner_left_down, corner_right_down, corner_right_up, left, right, up, down; - public Graph graph; - public List availablePos; - - private void Start() - { - while (!Generate()) - { - - } - } - - - bool Generate() - { - Map map = new Map(new Vector2Int(100, 100)); - List fillers = new List(); - - - for (int i = 0; i < 60; i++) - { - FloodFill filler = new FloodFill(map, 1, new Vector2Int(Random.Range(0, 70), Random.Range(0, 70))); - 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; - } - - } - - if (graph.graphNodes.Count > count * 0.8) - { - return false; - } - - GraphNode playerPosStart = graph.graphNodes[Random.Range(0, graph.graphNodes.Count)]; - Vector2Int startPos = playerPosStart.positions.ElementAt(Random.Range(0, playerPosStart.positions.Count)); - Vector3 possiblePos = default; - 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); - possiblePos = new Vector3Int(pos.x, pos.y, 0); - availablePos.Add(possiblePos); - } - else if (id == 2) - { - walls.SetTile(new Vector3Int(pos.x,pos.y,0), wall1); - - } - } - } - - - - 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);} - - - Debug.Log(startPos); - Debug.Log("cell added"); - - GameObject Player = GameObject.FindWithTag("Player"); - Player.transform.position = randomCoordsForStanding(); - - //randomCoordsForStanding(); - - return true; - } - - - public Vector3 randomCoordsForStanding() - { - //Tile test = new Tile(); - while (true) - { - int bound = availablePos.Count; - int randomVal; - randomVal = Random.Range(0, bound); - - return availablePos[randomVal] + new Vector3( 0.5f, 0.5f, 0f); - } - - - } - - // Update is called once per frame - void Update() - { - - } -} +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using GUI_Scripts.ProceduralGeneration; +using UnityEngine; +using UnityEngine.Tilemaps; +using Random = UnityEngine.Random; +using Vector2 = System.Numerics.Vector2; + +public class TileMapGenerator : MonoBehaviour +{ + // Start is called before the first frame update + + public Tilemap ground, walls; + public Tile ground1, wall1, corner_left_up, corner_left_down, corner_right_down, corner_right_up, left, right, up, down; + public Graph graph; + public List availablePos; + public bool generated=false; + public bool nodeMapGenerated = false; + + private void Start() + { + while (!(generated=Generate())) + { + + } + } + + void Update() + { + if (generated && !nodeMapGenerated) + { + GameObject.FindObjectOfType().Generate(); + nodeMapGenerated = true; + } + } + + + bool Generate() + { + Map map = new Map(new Vector2Int(100, 100)); + List fillers = new List(); + + + for (int i = 0; i < 60; i++) + { + FloodFill filler = new FloodFill(map, 1, new Vector2Int(Random.Range(0, 70), Random.Range(0, 70))); + 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; + } + + } + + if (graph.graphNodes.Count > count * 0.8) + { + return false; + } + + GraphNode playerPosStart = graph.graphNodes[Random.Range(0, graph.graphNodes.Count)]; + Vector2Int startPos = playerPosStart.positions.ElementAt(Random.Range(0, playerPosStart.positions.Count)); + Vector3 possiblePos = default; + 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); + possiblePos = new Vector3Int(pos.x, pos.y, 0); + availablePos.Add(possiblePos); + } + else if (id == 2) + { + walls.SetTile(new Vector3Int(pos.x,pos.y,0), wall1); + + } + } + } + + + + 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);} + + + Debug.Log(startPos); + Debug.Log("cell added"); + + GameObject Player = GameObject.FindWithTag("Player"); + Player.transform.position = randomCoordsForStanding(); + + //randomCoordsForStanding(); + + return true; + } + + + public Vector3 randomCoordsForStanding() + { + //Tile test = new Tile(); + while (true) + { + int bound = availablePos.Count; + int randomVal; + randomVal = Random.Range(0, bound); + + return availablePos[randomVal] + new Vector3( 0.5f, 0.5f, 0f); + } + + + } + +}