mirror of
https://github.com/Xevion/Boids.git
synced 2026-01-31 08:23:35 -06:00
RandomNearbyPosition for spawning non-startup boids near eachother (needs pre-simulation!) new Boid.GetNearby func, fix Debug import, change UIController AdjustmentsPanel delay, AddBoids docs
This commit is contained in:
@@ -14,7 +14,7 @@ public class BoidControllerEditor : UnityEditor.Editor {
|
|||||||
if (EditorGUI.EndChangeCheck() && Application.isPlaying) {
|
if (EditorGUI.EndChangeCheck() && Application.isPlaying) {
|
||||||
int diff = controller.boidCount - controller.boids.Count;
|
int diff = controller.boidCount - controller.boids.Count;
|
||||||
if (diff > 1)
|
if (diff > 1)
|
||||||
controller.AddBoids(diff);
|
controller.AddBoids(diff, controller.boids.Count > 15);
|
||||||
else if (diff < 0)
|
else if (diff < 0)
|
||||||
controller.RemoveBoids(Mathf.Abs(diff));
|
controller.RemoveBoids(Mathf.Abs(diff));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -285,4 +285,14 @@ public class Boid : MonoBehaviour {
|
|||||||
_lineRenderers[2].transform.Rotate(0, 0, _parent.boidFov / 2f);
|
_lineRenderers[2].transform.Rotate(0, 0, _parent.boidFov / 2f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Return a random position nearby a certain distance away
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="distance">Vector2 position</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public Vector2 GetNearby(float distance) {
|
||||||
|
return _position + Util.RotateBy(new Vector2(distance, distance),
|
||||||
|
Random.Range(0f, 360f));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,6 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using Debug = System.Diagnostics.Debug;
|
|
||||||
using Random = UnityEngine.Random;
|
using Random = UnityEngine.Random;
|
||||||
|
|
||||||
public class BoidController : MonoBehaviour {
|
public class BoidController : MonoBehaviour {
|
||||||
@@ -106,15 +105,16 @@ public class BoidController : MonoBehaviour {
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds a number of boids.
|
/// Adds a number of boids.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="n"></param>
|
/// <param name="n">Number of Boids to add</param>
|
||||||
public void AddBoids(int n) {
|
/// <param name="useNearby">Spawn Boids nearby each other</param>
|
||||||
|
public void AddBoids(int n, bool useNearby = false) {
|
||||||
// Skip if negative or zero
|
// Skip if negative or zero
|
||||||
if (n <= 0)
|
if (n <= 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
// Instantiate a Boid prefab within the boundaries randomly
|
// 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);
|
GameObject boid = Instantiate(boidObject, position, Quaternion.identity);
|
||||||
|
|
||||||
// Set parent, add Boid component to Boids list
|
// 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.x, Boundary.size.x) / 2,
|
||||||
Random.Range(-Boundary.size.y, Boundary.size.y) / 2);
|
Random.Range(-Boundary.size.y, Boundary.size.y) / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a random valid position near a Boid
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A Vector2 position within the set boundaries</returns>
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a random Boid
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public Boid RandomBoid() {
|
||||||
|
return boids[Random.Range(0, boids.Count - 1)];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -235,8 +235,8 @@ public class UIController : MonoBehaviour {
|
|||||||
GameObject adjPanelGo;
|
GameObject adjPanelGo;
|
||||||
LeanTween
|
LeanTween
|
||||||
.move((adjPanelGo = adjPanel.gameObject),
|
.move((adjPanelGo = adjPanel.gameObject),
|
||||||
adjPanelGo.transform.position + _adjustmentsDiff * (away ? 1 : -1), 1.15f)
|
adjPanelGo.transform.position + _adjustmentsDiff * (away ? 1 : -1), away ? 0.9f : 1.15f)
|
||||||
.setDelay(away ? 0f : 0.15f)
|
.setDelay(away ? 0f : 0.05f)
|
||||||
.setEase(LeanTweenType.easeInOutCubic)
|
.setEase(LeanTweenType.easeInOutCubic)
|
||||||
.setOnComplete(StartTween());
|
.setOnComplete(StartTween());
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user