Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using MoreMountains.Tools;
- namespace jamiesGridEssentials
- {
- // Only import UnityEditor if we're in the editor.
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- [RequireComponent(typeof(PathFinding))]
- [RequireComponent(typeof(PathFindGrid))]
- public class RandomizeStartAndEndPos : MonoBehaviour
- {
- /// <summary>
- ///Randomize Seeker and target position of sebastian lagues A* pathfinder
- /// </summary>
- public bool X_facing;
- [Header("Read only")]
- [SerializeField] Transform startTransform;
- [SerializeField] Transform endTransform;
- [HideInInspector]public int Width, Length;
- [MMReadOnly][SerializeField] Vector3 cornerNXY, cornerXNY, cornerXY, cornerNXNY;
- [SerializeField] private Vector3 OffsetVector;
- [Header("Components")]
- [SerializeField] PathFinding pathFinding;
- [SerializeField] PathFindGrid pathFindGrid;
- [Header("Visuals")]
- [SerializeField] GameObject gridVisuals;
- [SerializeField] Material gridMat;
- // Start is called before the first frame update
- void Start()
- {
- startTransform = pathFinding.seeker.transform;
- endTransform = pathFinding.target.transform;
- UpdateGridVisualsWidth();
- UpdateGridVisualsLength();
- }
- public void UpdateRequiredComponents()
- {
- pathFinding = GetComponent<PathFinding>();
- pathFindGrid = GetComponent<PathFindGrid>();
- pathFindGrid.randomizeStartAndEndPos = this;
- }
- public void RandomizePath()
- {
- MathUtilities.GetRandomPointOnRectangleEdge(X_facing, Width, Length, transform.position, out Vector3 randStartPoint, out Vector3 randEndPoint);
- startTransform.position = randStartPoint;
- endTransform.position = randEndPoint;
- }
- private void UpdateGridVisuals()
- {
- OffsetVector = CalculateBoardCenter(Length % 2 == 0, Width % 2 == 0);
- //set grid mat we are gonna change to the material on the gridVisuals
- gridMat = gridVisuals.GetComponent<Renderer>().sharedMaterial;
- gridVisuals.transform.position = transform.position + OffsetVector;
- gridVisuals.transform.localScale = new Vector3(Width / 10f, 0f, Length / 10f);
- }
- public void UpdateGridVisualsWidth()
- {
- UpdateGridVisuals();
- gridMat.SetFloat("_Width", (float)Width * .5f);
- RandomizePath();
- }
- public void UpdateGridVisualsLength()
- {
- UpdateGridVisuals();
- gridMat.SetFloat("_Length", (float)Length * .5f);
- RandomizePath();
- }
- //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
- private Vector3 CalculateBoardCenter(bool length_is_even, bool Width_is_even)
- {
- //declare some Vectors we will use as shorthand to simplify the function
- Vector2 oddWidth = new(.5f, 0), oddLength = new(0, .5f), oddBoth = new(.5f, .5f);
- if (length_is_even && Width_is_even)
- {
- Debug.Log("both values are even");
- return new Vector3(-.5f, 0, -.5f);
- }
- else if (!length_is_even && Width_is_even)
- {
- Debug.Log("length is odd, Width is even");
- gridMat.SetVector("_UV_Offset", oddLength);
- return -Vector3.right / 2;
- }
- else if (length_is_even && !Width_is_even)
- {
- Debug.Log("length is even, Width is odd");
- gridMat.SetVector("_UV_Offset", oddWidth);
- return -Vector3.forward / 2;
- }
- else if (!length_is_even && !Width_is_even)
- {
- Debug.Log("both values are odd");
- gridMat.SetVector("_UV_Offset", oddBoth);
- return Vector3.zero;
- }
- Debug.LogError("Something is wrong with your code your values are not even or odd");
- return Vector3.zero;
- }
- }
- #if UNITY_EDITOR
- [CustomEditor(typeof(RandomizeStartAndEndPos))]
- public class RandomizeStartAndEndPosEditor : Editor
- {
- //declare serializedProperties These enable undoing and saving to be handled by unity (I think?)
- private SerializedProperty Length, Width;
- private void OnEnable()
- {
- Length = serializedObject.FindProperty("Length");
- Width = serializedObject.FindProperty("Width");
- }
- public override void OnInspectorGUI()
- {
- RandomizeStartAndEndPos randomizeStartAndEndPos = (RandomizeStartAndEndPos)target;
- base.OnInspectorGUI();
- // Update the serializedObject to match the internal state if required.
- serializedObject.Update();
- EditorGUI.BeginChangeCheck();
- EditorGUILayout.PropertyField(Width);
- if (EditorGUI.EndChangeCheck())
- {
- randomizeStartAndEndPos.UpdateGridVisualsWidth();
- }
- EditorGUI.BeginChangeCheck();
- EditorGUILayout.PropertyField(Length);
- if (EditorGUI.EndChangeCheck())
- randomizeStartAndEndPos.UpdateGridVisualsLength();
- // Apply changes made before this call.
- serializedObject.ApplyModifiedProperties();
- EditorGUILayout.Space(5);
- EditorGUILayout.BeginHorizontal();
- if (GUILayout.Button("Randomize Path"))
- {
- randomizeStartAndEndPos.RandomizePath();
- }
- if (GUILayout.Button("Update Required Components"))
- {
- randomizeStartAndEndPos.UpdateRequiredComponents();
- }
- EditorGUILayout.EndHorizontal();
- }
- }
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement