diff --git a/Boids/Assets/Editor/BoidControllerEditor.cs b/Boids/Assets/Editor/BoidControllerEditor.cs index e24d384..460995e 100644 --- a/Boids/Assets/Editor/BoidControllerEditor.cs +++ b/Boids/Assets/Editor/BoidControllerEditor.cs @@ -14,7 +14,7 @@ public class BoidControllerEditor : UnityEditor.Editor { if (EditorGUI.EndChangeCheck() && Application.isPlaying) { int diff = controller.boidCount - controller.boids.Count; if (diff > 1) - controller.AddBoids(diff); + controller.AddBoids(diff, controller.boids.Count > 15); else if (diff < 0) controller.RemoveBoids(Mathf.Abs(diff)); } diff --git a/Boids/Assets/Scripts/Boid.cs b/Boids/Assets/Scripts/Boid.cs index 75c4e27..fbe5136 100644 --- a/Boids/Assets/Scripts/Boid.cs +++ b/Boids/Assets/Scripts/Boid.cs @@ -285,4 +285,14 @@ public class Boid : MonoBehaviour { _lineRenderers[2].transform.Rotate(0, 0, _parent.boidFov / 2f); } } + + /// + /// Return a random position nearby a certain distance away + /// + /// Vector2 position + /// + public Vector2 GetNearby(float distance) { + return _position + Util.RotateBy(new Vector2(distance, distance), + Random.Range(0f, 360f)); + } } \ No newline at end of file diff --git a/Boids/Assets/Scripts/BoidController.cs b/Boids/Assets/Scripts/BoidController.cs index 883c3b0..1f3bf65 100644 --- a/Boids/Assets/Scripts/BoidController.cs +++ b/Boids/Assets/Scripts/BoidController.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using UnityEditor; using UnityEngine; -using Debug = System.Diagnostics.Debug; using Random = UnityEngine.Random; public class BoidController : MonoBehaviour { @@ -106,15 +105,16 @@ public class BoidController : MonoBehaviour { /// /// Adds a number of boids. /// - /// - public void AddBoids(int n) { + /// Number of Boids to add + /// Spawn Boids nearby each other + public void AddBoids(int n, bool useNearby = false) { // Skip if negative or zero if (n <= 0) return; for (int i = 0; i < n; i++) { // Instantiate a Boid prefab within the boundaries randomly - Vector2 position = RandomPosition() * 0.90f; + Vector2 position = useNearby ? RandomNearbyPosition() : RandomPosition() * 0.90f; GameObject boid = Instantiate(boidObject, position, Quaternion.identity); // Set parent, add Boid component to Boids list @@ -162,4 +162,28 @@ public class BoidController : MonoBehaviour { Random.Range(-Boundary.size.x, Boundary.size.x) / 2, Random.Range(-Boundary.size.y, Boundary.size.y) / 2); } + + /// + /// Returns a random valid position near a Boid + /// + /// A Vector2 position within the set boundaries + private Vector2 RandomNearbyPosition(int maxRetries = 5) { + for (int i = 0; i < maxRetries; i++) { + Vector2 possible = RandomBoid().GetNearby(boidSeparationRange); + if (Boundary.Contains(possible)) + return possible; + } + + // if MaxRetries exceeded, fall back to RandomPosition() + Debug.Log($"{maxRetries} retries failed, falling back to RandomPosition"); + return RandomPosition(); + } + + /// + /// Returns a random Boid + /// + /// + public Boid RandomBoid() { + return boids[Random.Range(0, boids.Count - 1)]; + } } \ No newline at end of file diff --git a/Boids/Assets/Scripts/UIController.cs b/Boids/Assets/Scripts/UIController.cs index d0bd109..9495ae9 100644 --- a/Boids/Assets/Scripts/UIController.cs +++ b/Boids/Assets/Scripts/UIController.cs @@ -235,8 +235,8 @@ public class UIController : MonoBehaviour { GameObject adjPanelGo; LeanTween .move((adjPanelGo = adjPanel.gameObject), - adjPanelGo.transform.position + _adjustmentsDiff * (away ? 1 : -1), 1.15f) - .setDelay(away ? 0f : 0.15f) + adjPanelGo.transform.position + _adjustmentsDiff * (away ? 1 : -1), away ? 0.9f : 1.15f) + .setDelay(away ? 0f : 0.05f) .setEase(LeanTweenType.easeInOutCubic) .setOnComplete(StartTween()); break;