attempts to fix, more debug print/text/label statements & cleanup

This commit is contained in:
Xevion
2020-05-17 00:39:41 -05:00
parent b933decc4e
commit a4479aa653
2 changed files with 29 additions and 12 deletions

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEditor;
using UnityEngine; using UnityEngine;
using Random = UnityEngine.Random; using Random = UnityEngine.Random;
@@ -49,9 +50,15 @@ public class Boid : MonoBehaviour {
ScreenWrap(); ScreenWrap();
} }
void OnDrawGizmos() {
Handles.color = _isWrappingX || _isWrappingY ? Color.red : Color.white;
Vector3 viewportPosition = _parent.cam.WorldToViewportPoint(transform.position);
Handles.Label(transform.position, $"{(Vector2) viewportPosition} {(_isWrappingX ? "Y" : "N")} {(_isWrappingY ? "Y" : "N")}");
}
void ScreenWrap() { void ScreenWrap() {
foreach (var _renderer in _renderers) foreach (var trenderer in _renderers)
if (_renderer.isVisible) { if (trenderer.isVisible) {
_isWrappingX = false; _isWrappingX = false;
_isWrappingY = false; _isWrappingY = false;
return; return;
@@ -62,14 +69,16 @@ public class Boid : MonoBehaviour {
// Activate Wrap, Move // Activate Wrap, Move
Vector2 newPosition = transform.position; Vector2 newPosition = transform.position;
Vector3 viewportPosition = _parent._cam.WorldToViewportPoint(newPosition); Vector3 viewportPosition = _parent.cam.WorldToViewportPoint(newPosition);
if (!_isWrappingX && (viewportPosition.x > 1 || viewportPosition.x < 0)) { if (!_isWrappingX && (viewportPosition.x > 1 || viewportPosition.x < 0)) {
print($"{transform.name} - Boid Wrapped on X Axis ({viewportPosition.x})");
newPosition.x = -newPosition.x; newPosition.x = -newPosition.x;
_isWrappingX = true; _isWrappingX = true;
} }
if (!_isWrappingY && (viewportPosition.y > 1 || viewportPosition.y < 0)) { if (!_isWrappingY && (viewportPosition.y > 1 || viewportPosition.y < 0)) {
print($"{transform.name} - Boid Wrapped on Y Axis ({viewportPosition.y})");
newPosition.y = -newPosition.y; newPosition.y = -newPosition.y;
_isWrappingY = true; _isWrappingY = true;
} }

View File

@@ -5,7 +5,7 @@ using Random = UnityEngine.Random;
public class BoidController : MonoBehaviour { public class BoidController : MonoBehaviour {
// Controller Attributes // Controller Attributes
[NonSerialized] public Rect space; [NonSerialized] public Rect Space;
// Swarm Attributes // Swarm Attributes
public int boidCount = 50; public int boidCount = 50;
@@ -26,27 +26,35 @@ public class BoidController : MonoBehaviour {
[HideInInspector] public List<Boid> boids = new List<Boid>(); [HideInInspector] public List<Boid> boids = new List<Boid>();
// Used for wrapping // Used for wrapping
internal Camera _cam; public Camera cam;
private void OnDrawGizmos() { private void OnDrawGizmos() {
Gizmos.DrawWireCube(space.center, space.size); // Draw a Wire Cube for the Rectangle Area
Gizmos.DrawWireCube(Space.center, Space.size);
// Draw a Wire Cube for the Cam's Viewport Area
Vector3 screenBottomLeft = cam.ViewportToWorldPoint(new Vector3(0, 0, transform.position.z));
Vector3 screenTopRight = cam.ViewportToWorldPoint(new Vector3(1, 1, transform.position.z));
var screenWidth = screenTopRight.x - screenBottomLeft.x;
var screenHeight = screenTopRight.y - screenBottomLeft.y;
Gizmos.DrawWireCube(cam.transform.position, new Vector3(screenWidth, screenHeight, 1));
} }
private void Start() { private void Start() {
// Setup Camera // Setup Camera
_cam = Camera.main; cam = Camera.main;
// Size the Rectangle based on the Camera's Orthographic View // Size the Rectangle based on the Camera's Orthographic View
float height = 2f * _cam.orthographicSize; float height = 2f * cam.orthographicSize;
Vector2 size = new Vector2(height * _cam.aspect, height); Vector2 size = new Vector2(height * cam.aspect, height);
space = new Rect((Vector2) transform.position - size / 2, size); Space = new Rect((Vector2) transform.position - size / 2, size);
// Add in Boid Objects / Spawn Boid Prefabs // Add in Boid Objects / Spawn Boid Prefabs
for (int i = 0; i < boidCount; i++) { for (int i = 0; i < boidCount; i++) {
// Generate a new position within the Rect boundaries (minus a little) // Generate a new position within the Rect boundaries (minus a little)
var position = new Vector2( var position = new Vector2(
Random.Range(-space.size.x, space.size.x) / 2 * 0.95f, Random.Range(-Space.size.x, Space.size.x) / 2 * 0.95f,
Random.Range(-space.size.y, space.size.y) / 2 * 0.95f); Random.Range(-Space.size.y, Space.size.y) / 2 * 0.95f);
// Spawn a new Boid prefab // Spawn a new Boid prefab
GameObject boid = Instantiate(boidObject, position, Quaternion.identity); GameObject boid = Instantiate(boidObject, position, Quaternion.identity);