Advertisement
jamieTheCoder

RandomizeStartAndEndPos

Dec 29th, 2022
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.15 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using MoreMountains.Tools;
  6.  
  7. namespace jamiesGridEssentials
  8. {
  9.     // Only import UnityEditor if we're in the editor.
  10. #if UNITY_EDITOR
  11.     using UnityEditor;
  12. #endif
  13.     [RequireComponent(typeof(PathFinding))]
  14.     [RequireComponent(typeof(PathFindGrid))]
  15.     public class RandomizeStartAndEndPos : MonoBehaviour
  16.     {
  17.         /// <summary>
  18.         ///Randomize Seeker and target position of sebastian lagues A* pathfinder
  19.         /// </summary>
  20.  
  21.  
  22.         public bool X_facing;
  23.         [Header("Read only")]
  24.         [SerializeField] Transform startTransform;
  25.         [SerializeField] Transform endTransform;
  26.         [HideInInspector]public int Width, Length;
  27.         [MMReadOnly][SerializeField] Vector3 cornerNXY, cornerXNY, cornerXY, cornerNXNY;
  28.         [SerializeField] private Vector3 OffsetVector;
  29.         [Header("Components")]
  30.         [SerializeField] PathFinding pathFinding;
  31.         [SerializeField] PathFindGrid pathFindGrid;
  32.         [Header("Visuals")]
  33.         [SerializeField] GameObject gridVisuals;
  34.         [SerializeField] Material gridMat;
  35.  
  36.         // Start is called before the first frame update
  37.         void Start()
  38.         {
  39.             startTransform = pathFinding.seeker.transform;
  40.             endTransform = pathFinding.target.transform;
  41.             UpdateGridVisualsWidth();
  42.             UpdateGridVisualsLength();
  43.         }
  44.  
  45.         public void UpdateRequiredComponents()
  46.         {
  47.             pathFinding = GetComponent<PathFinding>();
  48.             pathFindGrid = GetComponent<PathFindGrid>();
  49.             pathFindGrid.randomizeStartAndEndPos = this;
  50.         }
  51.  
  52.         public void RandomizePath()
  53.         {
  54.             MathUtilities.GetRandomPointOnRectangleEdge(X_facing, Width, Length, transform.position, out Vector3 randStartPoint, out Vector3 randEndPoint);
  55.             startTransform.position = randStartPoint;
  56.             endTransform.position = randEndPoint;
  57.         }
  58.  
  59.         private void UpdateGridVisuals()
  60.         {
  61.             OffsetVector = CalculateBoardCenter(Length % 2 == 0, Width % 2 == 0);
  62.             //set grid mat we are gonna change to the material on the gridVisuals
  63.             gridMat = gridVisuals.GetComponent<Renderer>().sharedMaterial;
  64.  
  65.             gridVisuals.transform.position = transform.position + OffsetVector;
  66.  
  67.             gridVisuals.transform.localScale = new Vector3(Width / 10f, 0f, Length / 10f);
  68.         }
  69.         public void UpdateGridVisualsWidth()
  70.         {
  71.             UpdateGridVisuals();
  72.             gridMat.SetFloat("_Width", (float)Width * .5f);
  73.             RandomizePath();
  74.         }
  75.         public void UpdateGridVisualsLength()
  76.         {
  77.             UpdateGridVisuals();
  78.             gridMat.SetFloat("_Length", (float)Length * .5f);
  79.             RandomizePath();
  80.         }
  81.  
  82.         //The below function is not production code and needs to be replaced if this project sees the light of day (as such I will not comment any of it) Thanks for your understanding
  83.         private Vector3 CalculateBoardCenter(bool length_is_even, bool Width_is_even)
  84.         {
  85.             //declare some Vectors we will use as shorthand to simplify the function
  86.             Vector2 oddWidth = new(.5f, 0), oddLength = new(0, .5f), oddBoth = new(.5f, .5f);
  87.  
  88.             if (length_is_even && Width_is_even)
  89.             {
  90.                 Debug.Log("both values are even");
  91.                 return new Vector3(-.5f, 0, -.5f);
  92.             }
  93.             else if (!length_is_even && Width_is_even)
  94.             {
  95.                 Debug.Log("length is odd, Width is even");
  96.                 gridMat.SetVector("_UV_Offset", oddLength);
  97.                 return -Vector3.right / 2;
  98.             }
  99.             else if (length_is_even && !Width_is_even)
  100.             {
  101.                 Debug.Log("length is even, Width is odd");
  102.                 gridMat.SetVector("_UV_Offset", oddWidth);
  103.                 return -Vector3.forward / 2;
  104.             }
  105.             else if (!length_is_even && !Width_is_even)
  106.             {
  107.                 Debug.Log("both values are odd");
  108.                 gridMat.SetVector("_UV_Offset", oddBoth);
  109.                 return Vector3.zero;
  110.             }
  111.             Debug.LogError("Something is wrong with your code your values are not even or odd");
  112.             return Vector3.zero;
  113.         }
  114.     }
  115.  
  116.  
  117.  
  118. #if UNITY_EDITOR
  119.     [CustomEditor(typeof(RandomizeStartAndEndPos))]
  120.     public class RandomizeStartAndEndPosEditor : Editor
  121.     {
  122.         //declare serializedProperties These enable undoing and saving to be handled by unity (I think?)
  123.         private SerializedProperty Length, Width;
  124.         private void OnEnable()
  125.         {
  126.             Length = serializedObject.FindProperty("Length");
  127.             Width = serializedObject.FindProperty("Width");
  128.         }
  129.         public override void OnInspectorGUI()
  130.         {
  131.             RandomizeStartAndEndPos randomizeStartAndEndPos = (RandomizeStartAndEndPos)target;
  132.             base.OnInspectorGUI();
  133.  
  134.             // Update the serializedObject to match the internal state if required.
  135.             serializedObject.Update();
  136.             EditorGUI.BeginChangeCheck();
  137.             EditorGUILayout.PropertyField(Width);
  138.             if (EditorGUI.EndChangeCheck())
  139.             {
  140.                 randomizeStartAndEndPos.UpdateGridVisualsWidth();
  141.                
  142.             }
  143.  
  144.             EditorGUI.BeginChangeCheck();
  145.             EditorGUILayout.PropertyField(Length);
  146.             if (EditorGUI.EndChangeCheck())
  147.                 randomizeStartAndEndPos.UpdateGridVisualsLength();
  148.  
  149.             // Apply changes made before this call.
  150.             serializedObject.ApplyModifiedProperties();
  151.             EditorGUILayout.Space(5);
  152.             EditorGUILayout.BeginHorizontal();
  153.             if (GUILayout.Button("Randomize Path"))
  154.             {
  155.                 randomizeStartAndEndPos.RandomizePath();
  156.             }
  157.             if (GUILayout.Button("Update Required Components"))
  158.             {
  159.                 randomizeStartAndEndPos.UpdateRequiredComponents();
  160.             }
  161.             EditorGUILayout.EndHorizontal();
  162.         }
  163.     }
  164. #endif
  165. }
  166.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement