mirror of
https://github.com/Xevion/Boids.git
synced 2025-12-14 02:11:05 -06:00
remove unnessecary SerializeField and NonSerializable attributes, improving comments/documentation
This commit is contained in:
@@ -1,15 +1,15 @@
|
|||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
[CustomEditor(typeof(Boid))]
|
// [CustomEditor(typeof(Boid))]
|
||||||
public class BoidEditor : UnityEditor.Editor {
|
// public class BoidEditor : UnityEditor.Editor {
|
||||||
public override void OnInspectorGUI() {
|
// public override void OnInspectorGUI() {
|
||||||
base.OnInspectorGUI();
|
// base.OnInspectorGUI();
|
||||||
|
//
|
||||||
Boid boid = (Boid) target;
|
// Boid boid = (Boid) target;
|
||||||
GUILayout.Label($"Boid heading at ({boid._velocity.x}, {boid._velocity.y})");
|
// GUILayout.Label($"Boid heading at ({boid.velocity.x}, {boid.velocity.y})");
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
public class BoidGizmoDrawer {
|
public class BoidGizmoDrawer {
|
||||||
[DrawGizmo(GizmoType.NonSelected | GizmoType.Selected)]
|
[DrawGizmo(GizmoType.NonSelected | GizmoType.Selected)]
|
||||||
@@ -17,7 +17,7 @@ public class BoidGizmoDrawer {
|
|||||||
// Simply draw the # of Boids within the perceived flock
|
// Simply draw the # of Boids within the perceived flock
|
||||||
Handles.Label(boid.transform.position, $"{boid.transform.name}/{boid.latestNeighborhoodCount}");
|
Handles.Label(boid.transform.position, $"{boid.transform.name}/{boid.latestNeighborhoodCount}");
|
||||||
|
|
||||||
if(boid._isFocused)
|
if(boid.isFocused)
|
||||||
foreach(Boid flockmate in boid.latestNeighborhood)
|
foreach(Boid flockmate in boid.latestNeighborhood)
|
||||||
Handles.DrawDottedLine(boid.transform.position, flockmate.transform.position, 1f);
|
Handles.DrawDottedLine(boid.transform.position, flockmate.transform.position, 1f);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,35 +1,38 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using UnityEditor;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using Random = UnityEngine.Random;
|
|
||||||
|
|
||||||
// Boids are represented by a moving, rotating triangle.
|
|
||||||
// Boids should communicate with sibling Boids via the parental BoidController object
|
|
||||||
public class Boid : MonoBehaviour {
|
public class Boid : MonoBehaviour {
|
||||||
[NonSerialized] private Vector2 _position = Vector2.zero;
|
// Basic Boid Physics Attributes
|
||||||
[NonSerialized] public Vector2 _velocity;
|
private Vector2 _position = Vector2.zero;
|
||||||
[NonSerialized] private bool _isWrappingX = false;
|
public Vector2 velocity;
|
||||||
[NonSerialized] private bool _isWrappingY = false;
|
|
||||||
[NonSerialized] private Vector2 _centeringVelocity;
|
|
||||||
[NonSerialized] public int latestNeighborhoodCount = 0;
|
|
||||||
[NonSerialized] public List<Boid> latestNeighborhood;
|
|
||||||
[NonSerialized] private BoidController _parent;
|
|
||||||
[NonSerialized] public bool _isFocused = false;
|
|
||||||
[NonSerialized] private LineRenderer[] _lineRenderers;
|
|
||||||
|
|
||||||
|
// Wrapping related attributes
|
||||||
|
private bool _isWrappingX;
|
||||||
|
private bool _isWrappingY;
|
||||||
|
private Vector2 _centeringVelocity;
|
||||||
|
|
||||||
|
// Used for tracking Gizmo drawing
|
||||||
|
public int latestNeighborhoodCount = 0;
|
||||||
|
public List<Boid> latestNeighborhood;
|
||||||
|
|
||||||
|
// Parent Boid Controller
|
||||||
|
private BoidController _parent;
|
||||||
|
|
||||||
|
public bool isFocused; // Whether or not the current Boid is focused
|
||||||
|
private LineRenderer[] _lineRenderers; // Store LineRenderers used by Focused Boids
|
||||||
|
|
||||||
private void Start() {
|
private void Start() {
|
||||||
_parent = transform.parent
|
_parent = transform.parent
|
||||||
.GetComponent<BoidController>(); // Parent used to perform physics math without caching
|
.GetComponent<BoidController>(); // Parent used to perform physics math without caching
|
||||||
_velocity = Util.GetRandomVelocity(Random.Range(_parent.minSpeed, _parent.maxSpeed)); // Acquire a Velocity Vector with a magnitude
|
velocity = Util.GetRandomVelocity(Random.Range(_parent.minSpeed,
|
||||||
|
_parent.maxSpeed)); // Acquire a Velocity Vector with a magnitude
|
||||||
_position = transform.position; // Track 2D position separately
|
_position = transform.position; // Track 2D position separately
|
||||||
transform.name = $"Boid {transform.GetSiblingIndex()}"; // Name the Game Object so Boids can be tracked somewhat
|
transform.name = $"Boid {transform.GetSiblingIndex()}"; // Name the Game Object so Boids can be tracked somewhat
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Update() {
|
private void Update() {
|
||||||
// Updates the rotation of the object based on the Velocity
|
// Updates the rotation of the object based on the Velocity
|
||||||
transform.eulerAngles = new Vector3(0, 0, Mathf.Rad2Deg * -Mathf.Atan2(_velocity.x, _velocity.y));
|
transform.eulerAngles = new Vector3(0, 0, Mathf.Rad2Deg * -Mathf.Atan2(velocity.x, velocity.y));
|
||||||
|
|
||||||
// Skip Flock Calculations if wrapping in progress
|
// Skip Flock Calculations if wrapping in progress
|
||||||
if (_isWrappingX || _isWrappingY) {
|
if (_isWrappingX || _isWrappingY) {
|
||||||
@@ -44,7 +47,7 @@ public class Boid : MonoBehaviour {
|
|||||||
latestNeighborhoodCount = flock.Count;
|
latestNeighborhoodCount = flock.Count;
|
||||||
|
|
||||||
// Only update latest neighborhood when we need it for focused boid Gizmo draws
|
// Only update latest neighborhood when we need it for focused boid Gizmo draws
|
||||||
if (_isFocused)
|
if (isFocused)
|
||||||
latestNeighborhood = flock;
|
latestNeighborhood = flock;
|
||||||
|
|
||||||
// Calculate all offsets and multiple by magnitudes given
|
// Calculate all offsets and multiple by magnitudes given
|
||||||
@@ -62,13 +65,13 @@ public class Boid : MonoBehaviour {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Limit the Velocity Vector to a certain Magnitude
|
// Limit the Velocity Vector to a certain Magnitude
|
||||||
_velocity += acceleration * Time.deltaTime;
|
velocity += acceleration * Time.deltaTime;
|
||||||
float speed = _velocity.magnitude;
|
float speed = velocity.magnitude;
|
||||||
Vector2 dir = _velocity / speed;
|
Vector2 dir = velocity / speed;
|
||||||
speed = Mathf.Clamp(speed, _parent.minSpeed, _parent.maxSpeed);
|
speed = Mathf.Clamp(speed, _parent.minSpeed, _parent.maxSpeed);
|
||||||
_velocity = dir * speed;
|
velocity = dir * speed;
|
||||||
|
|
||||||
_position += _velocity * Time.deltaTime;
|
_position += velocity * Time.deltaTime;
|
||||||
transform.position = _position;
|
transform.position = _position;
|
||||||
// transform.forward = dir;
|
// transform.forward = dir;
|
||||||
}
|
}
|
||||||
@@ -83,7 +86,7 @@ public class Boid : MonoBehaviour {
|
|||||||
/// <param name="vector">Force Vector being applied by a rule</param>
|
/// <param name="vector">Force Vector being applied by a rule</param>
|
||||||
/// <returns>Vector2 force to be applied</returns>
|
/// <returns>Vector2 force to be applied</returns>
|
||||||
private Vector2 SteerTowards(Vector2 vector) {
|
private Vector2 SteerTowards(Vector2 vector) {
|
||||||
Vector2 v = vector.normalized * _parent.maxSpeed - _velocity;
|
Vector2 v = vector.normalized * _parent.maxSpeed - velocity;
|
||||||
return Vector2.ClampMagnitude(v, _parent.maxSteerForce);
|
return Vector2.ClampMagnitude(v, _parent.maxSteerForce);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,9 +153,9 @@ public class Boid : MonoBehaviour {
|
|||||||
|
|
||||||
Vector2 perceived = Vector2.zero;
|
Vector2 perceived = Vector2.zero;
|
||||||
foreach (Boid boid in flock)
|
foreach (Boid boid in flock)
|
||||||
perceived += boid._velocity;
|
perceived += boid.velocity;
|
||||||
perceived /= flock.Count;
|
perceived /= flock.Count;
|
||||||
return (perceived - _velocity) / 8.0f;
|
return (perceived - velocity) / 8.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Asks Boids to stay within the Boundaries set
|
// Asks Boids to stay within the Boundaries set
|
||||||
@@ -189,13 +192,13 @@ public class Boid : MonoBehaviour {
|
|||||||
|
|
||||||
// FOV Check
|
// FOV Check
|
||||||
if (_parent.enableFovChecks) {
|
if (_parent.enableFovChecks) {
|
||||||
float angle1 = Util.Vector2ToAngle(_velocity); // Current Heading
|
float angle1 = Util.Vector2ToAngle(velocity); // Current Heading
|
||||||
float angle2 = Util.AngleBetween(transform.position, boid.transform.position); // Angle between Boid and other Boid
|
float angle2 =
|
||||||
|
Util.AngleBetween(transform.position, boid.transform.position); // Angle between Boid and other Boid
|
||||||
|
|
||||||
// Outside of FOV range, skip
|
// Outside of FOV range, skip
|
||||||
if (Mathf.Abs(Mathf.DeltaAngle(angle1, angle2)) > _parent.boidFov / 2)
|
if (Mathf.Abs(Mathf.DeltaAngle(angle1, angle2)) > _parent.boidFov / 2)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Boid passed all checks, add to local Flock list
|
// Boid passed all checks, add to local Flock list
|
||||||
@@ -207,12 +210,12 @@ public class Boid : MonoBehaviour {
|
|||||||
|
|
||||||
// Sets up a Boid to be 'Focused', adds Circles around object and changes color
|
// Sets up a Boid to be 'Focused', adds Circles around object and changes color
|
||||||
public void EnableFocusing() {
|
public void EnableFocusing() {
|
||||||
if (_isFocused) {
|
if (isFocused) {
|
||||||
Debug.LogWarning($"enableFocusing called on previously focused Boid ({transform.name})");
|
Debug.LogWarning($"enableFocusing called on previously focused Boid ({transform.name})");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_isFocused = true;
|
isFocused = true;
|
||||||
|
|
||||||
// Create all LineRenderers
|
// Create all LineRenderers
|
||||||
_lineRenderers = new LineRenderer[3];
|
_lineRenderers = new LineRenderer[3];
|
||||||
@@ -230,7 +233,7 @@ public class Boid : MonoBehaviour {
|
|||||||
|
|
||||||
// Disable focusing, removing LineRenderers and resetting color
|
// Disable focusing, removing LineRenderers and resetting color
|
||||||
public void DisableFocusing() {
|
public void DisableFocusing() {
|
||||||
_isFocused = false;
|
isFocused = false;
|
||||||
|
|
||||||
// Update Mesh Material Color
|
// Update Mesh Material Color
|
||||||
var oldTriangle = transform.GetComponent<Triangle>();
|
var oldTriangle = transform.GetComponent<Triangle>();
|
||||||
|
|||||||
@@ -6,42 +6,43 @@ using Debug = System.Diagnostics.Debug;
|
|||||||
using Random = UnityEngine.Random;
|
using Random = UnityEngine.Random;
|
||||||
|
|
||||||
public class BoidController : MonoBehaviour {
|
public class BoidController : MonoBehaviour {
|
||||||
// Controller Attributes
|
|
||||||
[NonSerialized] public Rect Space;
|
// Manually managed/set
|
||||||
[NonSerialized] public Rect Boundary;
|
public GameObject boidObject; // Boid Object Prefab
|
||||||
|
|
||||||
// Swarm Attributes
|
// Swarm Attributes
|
||||||
[SerializeField] public int boidCount = 250;
|
public int boidCount = 250;
|
||||||
[SerializeField] public float boidGroupRange = 3.3f;
|
public float boidGroupRange = 3.3f;
|
||||||
[SerializeField] public float minSpeed;
|
public float minSpeed;
|
||||||
[SerializeField] public float maxSpeed;
|
public float maxSpeed;
|
||||||
|
|
||||||
// Boid Rules are multiplied by this to allow rule 'tweaking'
|
// Boid Rules are multiplied by this to allow rule 'tweaking'
|
||||||
[SerializeField] public float globalBias = 1.0f;
|
public float globalBias = 1.0f;
|
||||||
[SerializeField] public float separationBias = 2f;
|
public float separationBias = 2f;
|
||||||
[SerializeField] public float alignmentBias = 0.288f;
|
public float alignmentBias = 0.288f;
|
||||||
[SerializeField] public float cohesionBias = 0.3f;
|
public float cohesionBias = 0.3f;
|
||||||
[SerializeField] public float boundaryBias = 1.5f;
|
public float boundaryBias = 1.5f;
|
||||||
|
|
||||||
// Enable/Disable Boid Rules Altogether
|
// Enable/Disable Boid Rules Altogether
|
||||||
[SerializeField] public bool enableSeparation = true;
|
public bool enableSeparation = true;
|
||||||
[SerializeField] public bool enableAlignment = true;
|
public bool enableAlignment = true;
|
||||||
[SerializeField] public bool enableCohesion = true;
|
public bool enableCohesion = true;
|
||||||
[SerializeField] public bool enableBoundary = true;
|
public bool enableBoundary = true;
|
||||||
[SerializeField] public bool enableFovChecks = true;
|
public bool enableFovChecks = true;
|
||||||
|
|
||||||
[SerializeField] public float boidSeparationRange = 1.4f; // Boid Separation rule's activation distance
|
public float boidSeparationRange = 1.4f; // Boid Separation rule's activation distance
|
||||||
[SerializeField] public float boundaryForce = 50f; // The force applied when a Boid hits the boundary
|
public float boundaryForce = 50f; // The force applied when a Boid hits the boundary
|
||||||
[SerializeField] public bool localFlocks = true; // Calculate Local 'Neighborhood' for flocks?
|
public bool localFlocks = true; // Calculate Local 'Neighborhood' for flocks?
|
||||||
[SerializeField] public bool edgeWrapping = true; // Enforce Edge Wrapping
|
public bool edgeWrapping = true; // Enforce Edge Wrapping
|
||||||
[SerializeField] public float maxSteerForce = 400f;
|
public float maxSteerForce = 400f;
|
||||||
[SerializeField] public float boidFov = 240;
|
public float boidFov = 240;
|
||||||
|
|
||||||
|
// Runtime Controller Attributes
|
||||||
public Boid focusedBoid; // A focused Boid has special rendering
|
|
||||||
public GameObject boidObject; // Boid Object Prefab
|
|
||||||
[NonSerialized] public List<Boid> boids = new List<Boid>(); // Boid Objects for Updates
|
[NonSerialized] public List<Boid> boids = new List<Boid>(); // Boid Objects for Updates
|
||||||
|
[NonSerialized] public Rect Space;
|
||||||
|
[NonSerialized] public Rect Boundary;
|
||||||
[NonSerialized] public Camera Cam; // Used for wrapping detection
|
[NonSerialized] public Camera Cam; // Used for wrapping detection
|
||||||
|
public Boid focusedBoid; // A focused Boid has special rendering
|
||||||
|
|
||||||
private void OnDrawGizmos() {
|
private void OnDrawGizmos() {
|
||||||
// Draw a Wire Cube for the Rectangle Area
|
// Draw a Wire Cube for the Rectangle Area
|
||||||
|
|||||||
Reference in New Issue
Block a user