Advertisement
JohnnyTurbo

Unity Flow Field: GridDebug.cs

Sep 11th, 2020
3,519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.80 KB | None | 0 0
  1. using UnityEditor;
  2. using UnityEngine;
  3.  
  4.  
  5. public enum FlowFieldDisplayType { None, AllIcons, DestinationIcon, CostField, IntegrationField };
  6.  
  7. public class GridDebug : MonoBehaviour
  8. {
  9.     public GridController gridController;
  10.     public bool displayGrid;
  11.  
  12.     public FlowFieldDisplayType curDisplayType;
  13.  
  14.     private Vector2Int gridSize;
  15.     private float cellRadius;
  16.     private FlowField curFlowField;
  17.  
  18.     private Sprite[] ffIcons;
  19.  
  20.     private void Start()
  21.     {
  22.         ffIcons = Resources.LoadAll<Sprite>("Sprites/FFicons");
  23.     }
  24.  
  25.     public void SetFlowField(FlowField newFlowField)
  26.     {
  27.         curFlowField = newFlowField;
  28.         cellRadius = newFlowField.cellRadius;
  29.         gridSize = newFlowField.gridSize;
  30.     }
  31.    
  32.     public void DrawFlowField()
  33.     {
  34.         ClearCellDisplay();
  35.  
  36.         switch (curDisplayType)
  37.         {
  38.             case FlowFieldDisplayType.AllIcons:
  39.                 DisplayAllCells();
  40.                 break;
  41.  
  42.             case FlowFieldDisplayType.DestinationIcon:
  43.                 DisplayDestinationCell();
  44.                 break;
  45.  
  46.             default:
  47.                 break;
  48.         }
  49.     }
  50.  
  51.     private void DisplayAllCells()
  52.     {
  53.         if (curFlowField == null) { return; }
  54.         foreach (Cell curCell in curFlowField.grid)
  55.         {
  56.             DisplayCell(curCell);
  57.         }
  58.     }
  59.  
  60.     private void DisplayDestinationCell()
  61.     {
  62.         if (curFlowField == null) { return; }
  63.         DisplayCell(curFlowField.destinationCell);
  64.     }
  65.  
  66.     private void DisplayCell(Cell cell)
  67.     {
  68.         GameObject iconGO = new GameObject();
  69.         SpriteRenderer iconSR = iconGO.AddComponent<SpriteRenderer>();
  70.         iconGO.transform.parent = transform;
  71.         iconGO.transform.position = cell.worldPos;
  72.  
  73.         if (cell.cost == 0)
  74.         {
  75.             iconSR.sprite = ffIcons[3];
  76.             Quaternion newRot = Quaternion.Euler(90, 0, 0);
  77.             iconGO.transform.rotation = newRot;
  78.         }
  79.         else if (cell.cost == byte.MaxValue)
  80.         {
  81.             iconSR.sprite = ffIcons[2];
  82.             Quaternion newRot = Quaternion.Euler(90, 0, 0);
  83.             iconGO.transform.rotation = newRot;
  84.         }
  85.         else if (cell.bestDirection == GridDirection.North)
  86.         {
  87.             iconSR.sprite = ffIcons[0];
  88.             Quaternion newRot = Quaternion.Euler(90, 0, 0);
  89.             iconGO.transform.rotation = newRot;
  90.         }
  91.         else if (cell.bestDirection == GridDirection.South)
  92.         {
  93.             iconSR.sprite = ffIcons[0];
  94.             Quaternion newRot = Quaternion.Euler(90, 180, 0);
  95.             iconGO.transform.rotation = newRot;
  96.         }
  97.         else if (cell.bestDirection == GridDirection.East)
  98.         {
  99.             iconSR.sprite = ffIcons[0];
  100.             Quaternion newRot = Quaternion.Euler(90, 90, 0);
  101.             iconGO.transform.rotation = newRot;
  102.         }
  103.         else if (cell.bestDirection == GridDirection.West)
  104.         {
  105.             iconSR.sprite = ffIcons[0];
  106.             Quaternion newRot = Quaternion.Euler(90, 270, 0);
  107.             iconGO.transform.rotation = newRot;
  108.         }
  109.         else if (cell.bestDirection == GridDirection.NorthEast)
  110.         {
  111.             iconSR.sprite = ffIcons[1];
  112.             Quaternion newRot = Quaternion.Euler(90, 0, 0);
  113.             iconGO.transform.rotation = newRot;
  114.         }
  115.         else if (cell.bestDirection == GridDirection.NorthWest)
  116.         {
  117.             iconSR.sprite = ffIcons[1];
  118.             Quaternion newRot = Quaternion.Euler(90, 270, 0);
  119.             iconGO.transform.rotation = newRot;
  120.         }
  121.         else if (cell.bestDirection == GridDirection.SouthEast)
  122.         {
  123.             iconSR.sprite = ffIcons[1];
  124.             Quaternion newRot = Quaternion.Euler(90, 90, 0);
  125.             iconGO.transform.rotation = newRot;
  126.         }
  127.         else if (cell.bestDirection == GridDirection.SouthWest)
  128.         {
  129.             iconSR.sprite = ffIcons[1];
  130.             Quaternion newRot = Quaternion.Euler(90, 180, 0);
  131.             iconGO.transform.rotation = newRot;
  132.         }
  133.         else
  134.         {
  135.             iconSR.sprite = ffIcons[0];
  136.         }
  137.     }
  138.  
  139.     public void ClearCellDisplay()
  140.     {
  141.         foreach (Transform t in transform)
  142.         {
  143.             GameObject.Destroy(t.gameObject);
  144.         }
  145.     }
  146.    
  147.     private void OnDrawGizmos()
  148.     {
  149.         if (displayGrid)
  150.         {
  151.             if (curFlowField == null)
  152.             {
  153.                 DrawGrid(gridController.gridSize, Color.yellow, gridController.cellRadius);
  154.             }
  155.             else
  156.             {
  157.                 DrawGrid(gridSize, Color.green, cellRadius);
  158.             }
  159.         }
  160.        
  161.         if (curFlowField == null) { return; }
  162.  
  163.         GUIStyle style = new GUIStyle(GUI.skin.label);
  164.         style.alignment = TextAnchor.MiddleCenter;
  165.  
  166.         switch (curDisplayType)
  167.         {
  168.             case FlowFieldDisplayType.CostField:
  169.  
  170.                 foreach (Cell curCell in curFlowField.grid)
  171.                 {
  172.                     Handles.Label(curCell.worldPos, curCell.cost.ToString(), style);
  173.                 }
  174.                 break;
  175.                
  176.             case FlowFieldDisplayType.IntegrationField:
  177.  
  178.                 foreach (Cell curCell in curFlowField.grid)
  179.                 {
  180.                     Handles.Label(curCell.worldPos, curCell.bestCost.ToString(), style);
  181.                 }
  182.                 break;
  183.                
  184.             default:
  185.                 break;
  186.         }
  187.        
  188.     }
  189.  
  190.     private void DrawGrid(Vector2Int drawGridSize, Color drawColor, float drawCellRadius)
  191.     {
  192.         Gizmos.color = drawColor;
  193.         for (int x = 0; x < drawGridSize.x; x++)
  194.         {
  195.             for (int y = 0; y < drawGridSize.y; y++)
  196.             {
  197.                 Vector3 center = new Vector3(drawCellRadius * 2 * x + drawCellRadius, 0, drawCellRadius * 2 * y + drawCellRadius);
  198.                 Vector3 size = Vector3.one * drawCellRadius * 2;
  199.                 Gizmos.DrawWireCube(center, size);
  200.             }
  201.         }
  202.     }
  203. }
  204.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement