From 1188a75d9ebedc74c6873e4698cffc74c02bcf6a Mon Sep 17 00:00:00 2001 From: Xevion Date: Fri, 5 Jun 2020 18:28:44 -0500 Subject: [PATCH] new GetLineRenderer method, port DrawCircle code --- Boids/Assets/Scripts/Boid.cs | 40 +++++++++++-------------------- Boids/Assets/Scripts/ShapeDraw.cs | 19 +++++++++++++-- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/Boids/Assets/Scripts/Boid.cs b/Boids/Assets/Scripts/Boid.cs index 934cfba..71cb7b7 100644 --- a/Boids/Assets/Scripts/Boid.cs +++ b/Boids/Assets/Scripts/Boid.cs @@ -197,7 +197,7 @@ public class Boid : MonoBehaviour { // Sets up a Boid to be 'Focused', adds Circles around object and changes color public void EnableFocusing() { if (_isFocused) { - Debug.LogWarning("enableFocusing called on previously focused Boid"); + Debug.LogWarning($"enableFocusing called on previously focused Boid ({transform.name})"); return; } @@ -207,6 +207,7 @@ public class Boid : MonoBehaviour { var triangle = transform.GetComponent(); triangle.meshRenderer.material.color = Color.red; + // Draw all focus related elements Draw(false); } @@ -223,6 +224,18 @@ public class Boid : MonoBehaviour { Destroy(child.gameObject); } + /// + /// returns a new LineRenderer component stored on a child GameObject. + /// + /// The name of the associated child GameObject + /// A LineRenderer + public LineRenderer GetLineRenderer(string childName) { + var child = new GameObject(childName); + child.transform.SetParent(transform); + child.transform.position = transform.position; + return child.AddComponent(); + } + public void Draw(bool redraw) { if (redraw) foreach (Transform child in transform) @@ -236,29 +249,4 @@ public class Boid : MonoBehaviour { DrawArcCentered(_parent.boidGroupRange, Util.Vector2ToAngle(_velocity), _parent.boidFOV, "FOV Arc"); } - - private void DrawCircle(float radius, string childName) { - // Create a new child GameObject to hold the LineRenderer Component - var child = new GameObject(childName); - child.transform.SetParent(transform); - child.transform.position = transform.position; - var line = child.AddComponent(); - - // Setup LineRenderer properties - line.useWorldSpace = false; - line.startWidth = _parent.circleWidth; - line.endWidth = _parent.circleWidth; - line.positionCount = _parent.circleVertexCount + 1; - - // Calculate points for circle - var pointCount = _parent.circleVertexCount + 1; - var points = new Vector3[pointCount]; - for (int i = 0; i < pointCount; i++) { - var rad = Mathf.Deg2Rad * (i * 360f / _parent.circleVertexCount); - points[i] = new Vector3(Mathf.Sin(rad) * radius, Mathf.Cos(rad) * radius, 0); - } - - // Add points to LineRenderer - line.SetPositions(points); - } } \ No newline at end of file diff --git a/Boids/Assets/Scripts/ShapeDraw.cs b/Boids/Assets/Scripts/ShapeDraw.cs index 176d37e..b5502f1 100644 --- a/Boids/Assets/Scripts/ShapeDraw.cs +++ b/Boids/Assets/Scripts/ShapeDraw.cs @@ -46,8 +46,23 @@ public class ShapeDraw { /// Draw a Circle with a specific radius and number of vertexes (detail level) /// /// - public static void DrawCircle(LineRenderer lineRenderer) { - + public static void DrawCircle(LineRenderer lineRenderer, float radius) { + // Setup LineRenderer properties + lineRenderer.useWorldSpace = false; + lineRenderer.startWidth = CircleWidth; + lineRenderer.endWidth = CircleWidth; + lineRenderer.positionCount = CircleVertexCount + 1; + + // Calculate points for circle + var pointCount = CircleVertexCount + 1; + var points = new Vector3[pointCount]; + for (int i = 0; i < pointCount; i++) { + var rad = Mathf.Deg2Rad * (i * 360f / CircleVertexCount); + points[i] = new Vector3(Mathf.Sin(rad) * radius, Mathf.Cos(rad) * radius, 0); + } + + // Add points to LineRenderer + lineRenderer.SetPositions(points); } ///