Advertisement
jamieTheCoder

PathFindGrid

Dec 29th, 2022 (edited)
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.35 KB | Software | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using MoreMountains.Tools;
  5. using UnityEngine.UIElements;
  6.  
  7. namespace jamiesGridEssentials
  8. {
  9.     public class PathFindGrid : MonoBehaviour
  10.     {
  11.         public bool onlyDisplayPathGizmos;
  12.         public LayerMask unwalkableMask;
  13.         public float nodeRadius;
  14.         PathFindNode[,] grid;
  15.        
  16.  
  17.         float nodeDiameter;
  18.         int GridSizeX, GridSizeY;
  19.         [HideInInspector]public RandomizeStartAndEndPos randomizeStartAndEndPos;
  20.  
  21.         private void Start()
  22.         {
  23.             nodeDiameter = nodeRadius * 2;
  24.             GridSizeX = Mathf.RoundToInt(randomizeStartAndEndPos.Width / nodeDiameter);
  25.             GridSizeY = Mathf.RoundToInt(randomizeStartAndEndPos.Length / nodeDiameter);
  26.             CreateGrid();
  27.         }
  28.         public int MaxSize
  29.         {
  30.             get
  31.             {
  32.                 return GridSizeX * GridSizeY;
  33.             }
  34.         }
  35.         void CreateGrid()
  36.         {
  37.             grid = new PathFindNode[GridSizeX, GridSizeY];
  38.             //What a beautiful piece of code
  39.             Vector3 worldBottomLeft = transform.position - Vector3.right * randomizeStartAndEndPos.Width / 2 - Vector3.forward * randomizeStartAndEndPos.Length / 2;
  40.             for (int x = 0; x < GridSizeX; x++)
  41.             {
  42.                 for (int y = 0; y < GridSizeY; y++)
  43.                 {
  44.                     Vector3 worldPoint = worldBottomLeft + Vector3.right * (x * nodeDiameter + nodeRadius) + Vector3.forward * (y * nodeDiameter + nodeRadius);
  45.                     bool walkable = !(Physics.CheckSphere(worldPoint, nodeRadius, unwalkableMask));
  46.                     grid[x, y] = new PathFindNode(walkable, worldPoint, x, y);
  47.                 }
  48.             }
  49.         }
  50.  
  51.         public List<PathFindNode> GetNeighbors(PathFindNode node)
  52.         {
  53.             List<PathFindNode> neighbors = new List<PathFindNode>();
  54.             for (int x = -1; x <= 1; x++)
  55.             {
  56.                 for (int y = -1; y <= 1; y++)
  57.                 {
  58.                     if (x == 0 && y == 0)
  59.                         continue;
  60.  
  61.  
  62.                     int checkX = node.gridX + x;
  63.                     int checkY = node.gridY + y;
  64.  
  65.                     if (checkX >= 0 && checkX < GridSizeX && checkY >= 0 && checkY < GridSizeY)
  66.                     {
  67.                         neighbors.Add(grid[checkX, checkY]);
  68.                     }
  69.                 }
  70.  
  71.             }
  72.             return neighbors;
  73.         }
  74.         public PathFindNode NodeFromWorldPoint(Vector3 worldPosition)
  75.         {
  76.             float percentX = (worldPosition.x + randomizeStartAndEndPos.Width / 2) / randomizeStartAndEndPos.Width;
  77.             float percentY = (worldPosition.z + randomizeStartAndEndPos.Length / 2) / randomizeStartAndEndPos.Length;
  78.             percentX = Mathf.Clamp01(percentX);
  79.             percentY = Mathf.Clamp01(percentY);
  80.  
  81.             int x = Mathf.RoundToInt((GridSizeX - 1) * percentX);
  82.             int y = Mathf.RoundToInt((GridSizeY - 1) * percentY);
  83.             return grid[x, y];
  84.  
  85.         }
  86.         public List<PathFindNode> path;
  87.         private void OnDrawGizmos()
  88.         {
  89.             RandomizeStartAndEndPos randomizeStartAndEndPos = GetComponent<RandomizeStartAndEndPos>();
  90.             Gizmos.DrawWireCube(transform.position, new Vector3(randomizeStartAndEndPos.Width, 1, randomizeStartAndEndPos.Length));
  91.  
  92.             if (onlyDisplayPathGizmos)
  93.             {
  94.                 if (path != null)
  95.                 {
  96.                     foreach (PathFindNode n in path)
  97.                     {
  98.                         Gizmos.color = Color.green;
  99.                         Gizmos.DrawCube(n.worldPosition, Vector3.one * (nodeDiameter - .1f));
  100.                     }
  101.                 }
  102.             }
  103.             else
  104.             {
  105.  
  106.                 if (grid != null)
  107.                 {
  108.                     foreach (PathFindNode n in grid)
  109.                     {
  110.                         Gizmos.color = (n.walkable) ? Color.white : Color.red;
  111.                         if (path != null)
  112.                             if (path.Contains(n))
  113.                                 Gizmos.color = Color.black;
  114.                         Gizmos.DrawCube(n.worldPosition, Vector3.one * (nodeDiameter - .1f));
  115.                     }
  116.                 }
  117.             }
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement