Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using MoreMountains.Tools;
- using UnityEngine.UIElements;
- namespace jamiesGridEssentials
- {
- public class PathFindGrid : MonoBehaviour
- {
- public bool onlyDisplayPathGizmos;
- public LayerMask unwalkableMask;
- public float nodeRadius;
- PathFindNode[,] grid;
- float nodeDiameter;
- int GridSizeX, GridSizeY;
- [HideInInspector]public RandomizeStartAndEndPos randomizeStartAndEndPos;
- private void Start()
- {
- nodeDiameter = nodeRadius * 2;
- GridSizeX = Mathf.RoundToInt(randomizeStartAndEndPos.Width / nodeDiameter);
- GridSizeY = Mathf.RoundToInt(randomizeStartAndEndPos.Length / nodeDiameter);
- CreateGrid();
- }
- public int MaxSize
- {
- get
- {
- return GridSizeX * GridSizeY;
- }
- }
- void CreateGrid()
- {
- grid = new PathFindNode[GridSizeX, GridSizeY];
- //What a beautiful piece of code
- Vector3 worldBottomLeft = transform.position - Vector3.right * randomizeStartAndEndPos.Width / 2 - Vector3.forward * randomizeStartAndEndPos.Length / 2;
- for (int x = 0; x < GridSizeX; x++)
- {
- for (int y = 0; y < GridSizeY; y++)
- {
- Vector3 worldPoint = worldBottomLeft + Vector3.right * (x * nodeDiameter + nodeRadius) + Vector3.forward * (y * nodeDiameter + nodeRadius);
- bool walkable = !(Physics.CheckSphere(worldPoint, nodeRadius, unwalkableMask));
- grid[x, y] = new PathFindNode(walkable, worldPoint, x, y);
- }
- }
- }
- public List<PathFindNode> GetNeighbors(PathFindNode node)
- {
- List<PathFindNode> neighbors = new List<PathFindNode>();
- for (int x = -1; x <= 1; x++)
- {
- for (int y = -1; y <= 1; y++)
- {
- if (x == 0 && y == 0)
- continue;
- int checkX = node.gridX + x;
- int checkY = node.gridY + y;
- if (checkX >= 0 && checkX < GridSizeX && checkY >= 0 && checkY < GridSizeY)
- {
- neighbors.Add(grid[checkX, checkY]);
- }
- }
- }
- return neighbors;
- }
- public PathFindNode NodeFromWorldPoint(Vector3 worldPosition)
- {
- float percentX = (worldPosition.x + randomizeStartAndEndPos.Width / 2) / randomizeStartAndEndPos.Width;
- float percentY = (worldPosition.z + randomizeStartAndEndPos.Length / 2) / randomizeStartAndEndPos.Length;
- percentX = Mathf.Clamp01(percentX);
- percentY = Mathf.Clamp01(percentY);
- int x = Mathf.RoundToInt((GridSizeX - 1) * percentX);
- int y = Mathf.RoundToInt((GridSizeY - 1) * percentY);
- return grid[x, y];
- }
- public List<PathFindNode> path;
- private void OnDrawGizmos()
- {
- RandomizeStartAndEndPos randomizeStartAndEndPos = GetComponent<RandomizeStartAndEndPos>();
- Gizmos.DrawWireCube(transform.position, new Vector3(randomizeStartAndEndPos.Width, 1, randomizeStartAndEndPos.Length));
- if (onlyDisplayPathGizmos)
- {
- if (path != null)
- {
- foreach (PathFindNode n in path)
- {
- Gizmos.color = Color.green;
- Gizmos.DrawCube(n.worldPosition, Vector3.one * (nodeDiameter - .1f));
- }
- }
- }
- else
- {
- if (grid != null)
- {
- foreach (PathFindNode n in grid)
- {
- Gizmos.color = (n.walkable) ? Color.white : Color.red;
- if (path != null)
- if (path.Contains(n))
- Gizmos.color = Color.black;
- Gizmos.DrawCube(n.worldPosition, Vector3.one * (nodeDiameter - .1f));
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement