From f634be434bd8a8ef5ee34109fcf73fc68988b130 Mon Sep 17 00:00:00 2001 From: wasdennnoch <leiter04@gmail.com> Date: Fri, 2 Apr 2021 23:28:49 +0200 Subject: [PATCH] Move Box struct to separate file --- Assets/Scripts/BaseCameraController.cs | 68 +++++++++----------------- Assets/Scripts/Box.cs | 19 +++++++ Assets/Scripts/Box.cs.meta | 11 +++++ 3 files changed, 52 insertions(+), 46 deletions(-) create mode 100644 Assets/Scripts/Box.cs create mode 100644 Assets/Scripts/Box.cs.meta diff --git a/Assets/Scripts/BaseCameraController.cs b/Assets/Scripts/BaseCameraController.cs index b27dc0d..2a5d0ef 100644 --- a/Assets/Scripts/BaseCameraController.cs +++ b/Assets/Scripts/BaseCameraController.cs @@ -121,22 +121,6 @@ public class BaseCameraController : MonoBehaviour RecalculateBounds(insets); } - public void RecalculateBounds(float newInset) - { - RecalculateBounds(new Vector2(newInset, newInset)); - } - - public void RecalculateBounds(Vector2 newInsets) - { - insetVector = newInsets; - RecalculateBounds(); - } - - public void RecalculateBounds() - { - edges = new Box(insetVector, Vector3.one - insetVector); - } - public Vector2 GetOutOfBoundsDirection(Vector2 point) { return new Vector2( @@ -147,7 +131,7 @@ public class BaseCameraController : MonoBehaviour public void DrawGizmos() { - var worldEdges = edges.ToWorldBox(camera, 0); + var worldEdges = GetWorldEdges(0); Gizmos.color = Color.red; Gizmos.DrawLine(worldEdges.bottomLeft, worldEdges.bottomRight); Gizmos.DrawLine(worldEdges.bottomRight, worldEdges.topRight); @@ -157,7 +141,7 @@ public class BaseCameraController : MonoBehaviour public void DrawDebug() { - var worldEdges = edges.ToWorldBox(camera, 0); + var worldEdges = GetWorldEdges(0); var color = Color.magenta; Debug.DrawLine(worldEdges.bottomLeft, worldEdges.bottomRight, color); Debug.DrawLine(worldEdges.bottomRight, worldEdges.topRight, color); @@ -165,40 +149,32 @@ public class BaseCameraController : MonoBehaviour Debug.DrawLine(worldEdges.topLeft, worldEdges.bottomLeft, color); } - private readonly struct Box + public void RecalculateBounds(float newInset) { - public readonly Vector3 bottomLeft; - public readonly Vector3 topRight; - public readonly Vector3 bottomRight; - public readonly Vector3 topLeft; + RecalculateBounds(new Vector2(newInset, newInset)); + } - public Box(Vector3 bottomLeft, Vector3 topRight) : this(bottomLeft, topRight, new Vector3(topRight.x, bottomLeft.y, bottomLeft.z), new Vector3(bottomLeft.x, topRight.y, topRight.z)) { } + public void RecalculateBounds(Vector2 newInsets) + { + insetVector = newInsets; + RecalculateBounds(); + } - public Box(Vector3 bottomLeft, Vector3 topRight, Vector3 bottomRight, Vector3 topLeft) - { - this.bottomLeft = bottomLeft; - this.topRight = topRight; - this.bottomRight = bottomRight; - this.topLeft = topLeft; - } + public void RecalculateBounds() + { + edges = new Box(insetVector, Vector3.one - insetVector); + } - public Box ToWorldBox(Camera camera, float? zOverride = null) + private Box GetWorldEdges(float? zOverride = null) + { + var worldBottomLeft = camera.ViewportToWorldPoint(edges.bottomLeft); + var worldTopRight = camera.ViewportToWorldPoint(edges.topRight); + if (zOverride != null) { - var worldBottomLeft = camera.ViewportToWorldPoint(bottomLeft); - var worldTopRight = camera.ViewportToWorldPoint(topRight); - var worldBottomRight = camera.ViewportToWorldPoint(bottomRight); - var worldTopLeft = camera.ViewportToWorldPoint(topLeft); - if (zOverride != null) - { - worldBottomLeft.z = (float)zOverride; - worldTopRight.z = (float)zOverride; - worldBottomRight.z = (float)zOverride; - worldTopLeft.z = (float)zOverride; - } - return new Box(worldBottomLeft, worldTopRight, worldBottomRight, worldTopLeft); + worldBottomLeft.z = (float)zOverride; + worldTopRight.z = (float)zOverride; } - - public override string ToString() => $"Box{{ Bottom Left: {bottomLeft} Bottom Right: {bottomRight} Top Right: {topRight} Top Left: {topLeft} }}"; + return new Box(worldBottomLeft, worldTopRight); } } diff --git a/Assets/Scripts/Box.cs b/Assets/Scripts/Box.cs new file mode 100644 index 0000000..415eb2a --- /dev/null +++ b/Assets/Scripts/Box.cs @@ -0,0 +1,19 @@ +using UnityEngine; + +public readonly struct Box +{ + public readonly Vector3 bottomLeft; + public readonly Vector3 topRight; + public readonly Vector3 bottomRight; + public readonly Vector3 topLeft; + + public Box(Vector3 bottomLeft, Vector3 topRight) + { + this.bottomLeft = bottomLeft; + this.topRight = topRight; + this.bottomRight = new Vector3(topRight.x, bottomLeft.y, bottomLeft.z); + this.topLeft = new Vector3(bottomLeft.x, topRight.y, topRight.z); + } + + public override string ToString() => $"Box{{ Bottom Left: {bottomLeft} Bottom Right: {bottomRight} Top Right: {topRight} Top Left: {topLeft} }}"; +} diff --git a/Assets/Scripts/Box.cs.meta b/Assets/Scripts/Box.cs.meta new file mode 100644 index 0000000..02992ba --- /dev/null +++ b/Assets/Scripts/Box.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ff0712db098251d41b1a8b32654cf100 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: -- GitLab