From 851b4608c26f68042d7d26f8c51af13bde9380ac Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 18 Mar 2023 16:48:31 -0400 Subject: [PATCH] Use layer util --- NewHorizons/Builder/Atmosphere/AirBuilder.cs | 3 +- .../Builder/Atmosphere/CloudsBuilder.cs | 7 +- NewHorizons/Builder/Body/RingBuilder.cs | 3 +- .../Builder/Body/SingularityBuilder.cs | 3 +- NewHorizons/Builder/Body/StarBuilder.cs | 9 +- NewHorizons/Builder/Body/WaterBuilder.cs | 5 +- .../Builder/General/DetectorBuilder.cs | 5 +- NewHorizons/Builder/General/GravityBuilder.cs | 3 +- .../Builder/General/RFVolumeBuilder.cs | 3 +- .../Builder/General/SpawnPointBuilder.cs | 7 +- NewHorizons/Builder/Props/DialogueBuilder.cs | 5 +- NewHorizons/Builder/Props/QuantumBuilder.cs | 2 +- NewHorizons/Builder/Props/RemoteBuilder.cs | 3 +- NewHorizons/Builder/Props/SignalBuilder.cs | 3 +- .../TranslatorText/NomaiTextArcArranger.cs | 175 +++++++++--------- .../TranslatorText/TranslatorTextBuilder.cs | 12 +- NewHorizons/Builder/ShipLog/RevealBuilder.cs | 3 +- .../Builder/StarSystem/SkyboxBuilder.cs | 8 +- .../Builder/Volumes/AudioVolumeBuilder.cs | 3 +- .../Builder/Volumes/HazardVolumeBuilder.cs | 5 +- .../Volumes/NotificationVolumeBuilder.cs | 3 +- .../Builder/Volumes/VanishVolumeBuilder.cs | 3 +- NewHorizons/Builder/Volumes/VolumeBuilder.cs | 3 +- .../Achievement/AchievementObserveTrigger.cs | 3 +- NewHorizons/Components/AddPhysics.cs | 5 +- NewHorizons/Main.cs | 2 + .../Utility/DebugUtilities/DebugRaycaster.cs | 58 +++--- NewHorizons/Utility/Geometry/AddDebugShape.cs | 2 +- 28 files changed, 184 insertions(+), 162 deletions(-) diff --git a/NewHorizons/Builder/Atmosphere/AirBuilder.cs b/NewHorizons/Builder/Atmosphere/AirBuilder.cs index e4518e4c..f71d4937 100644 --- a/NewHorizons/Builder/Atmosphere/AirBuilder.cs +++ b/NewHorizons/Builder/Atmosphere/AirBuilder.cs @@ -1,4 +1,5 @@ using NewHorizons.External.Configs; +using NewHorizons.Utility.OWUtilities; using UnityEngine; namespace NewHorizons.Builder.Atmosphere { @@ -8,7 +9,7 @@ namespace NewHorizons.Builder.Atmosphere { var airGO = new GameObject("Air"); airGO.SetActive(false); - airGO.layer = 17; + airGO.layer = LayerUtilities.BasicEffectVolume; airGO.transform.parent = sector?.transform ?? planetGO.transform; var sc = airGO.AddComponent(); diff --git a/NewHorizons/Builder/Atmosphere/CloudsBuilder.cs b/NewHorizons/Builder/Atmosphere/CloudsBuilder.cs index 7eb98eb1..087b0ff7 100644 --- a/NewHorizons/Builder/Atmosphere/CloudsBuilder.cs +++ b/NewHorizons/Builder/Atmosphere/CloudsBuilder.cs @@ -7,6 +7,7 @@ using UnityEngine; using Logger = NewHorizons.Utility.Logger; using System.Collections.Generic; using Tessellation; +using NewHorizons.Utility.OWUtilities; namespace NewHorizons.Builder.Atmosphere { @@ -118,7 +119,7 @@ namespace NewHorizons.Builder.Atmosphere GameObject cloudsFluidGO = new GameObject("CloudsFluid"); cloudsFluidGO.SetActive(false); - cloudsFluidGO.layer = 17; + cloudsFluidGO.layer = LayerUtilities.BasicEffectVolume; cloudsFluidGO.transform.parent = cloudsMainGO.transform; SphereCollider fluidSC = cloudsFluidGO.AddComponent(); @@ -251,7 +252,7 @@ namespace NewHorizons.Builder.Atmosphere if (atmo.clouds.unlit) { - cloudsTopGO.layer = LayerMask.NameToLayer("IgnoreSun"); + cloudsTopGO.layer = LayerUtilities.IgnoreSun; } if (atmo.clouds.rotationSpeed != 0f) @@ -303,7 +304,7 @@ namespace NewHorizons.Builder.Atmosphere { GameObject tcrqcGO = new GameObject("TransparentCloudRenderQueueController"); tcrqcGO.transform.SetParent(cloudsTransparentGO.transform, false); - tcrqcGO.layer = LayerMask.NameToLayer("BasicEffectVolume"); + tcrqcGO.layer = LayerUtilities.BasicEffectVolume; var shape = tcrqcGO.AddComponent(); shape.radius = 1; diff --git a/NewHorizons/Builder/Body/RingBuilder.cs b/NewHorizons/Builder/Body/RingBuilder.cs index 40d5953f..c2adc5b2 100644 --- a/NewHorizons/Builder/Body/RingBuilder.cs +++ b/NewHorizons/Builder/Body/RingBuilder.cs @@ -8,6 +8,7 @@ using NewHorizons.External.Modules; using UnityEngine; using Logger = NewHorizons.Utility.Logger; using NewHorizons.Components.Volumes; +using NewHorizons.Utility.OWUtilities; namespace NewHorizons.Builder.Body { @@ -30,7 +31,7 @@ namespace NewHorizons.Builder.Body ringVolume.transform.localPosition = Vector3.zero; ringVolume.transform.localScale = Vector3.one; ringVolume.transform.localRotation = Quaternion.identity; - ringVolume.layer = LayerMask.NameToLayer("BasicEffectVolume"); + ringVolume.layer = LayerUtilities.BasicEffectVolume; var ringShape = ringVolume.AddComponent(); ringShape.innerRadius = ring.innerRadius; diff --git a/NewHorizons/Builder/Body/SingularityBuilder.cs b/NewHorizons/Builder/Body/SingularityBuilder.cs index 79b4d61b..79a1ed8f 100644 --- a/NewHorizons/Builder/Body/SingularityBuilder.cs +++ b/NewHorizons/Builder/Body/SingularityBuilder.cs @@ -11,6 +11,7 @@ using System.Drawing; using Color = UnityEngine.Color; using NewHorizons.Components.Volumes; using NewHorizons.Utility.OWMLUtilities; +using NewHorizons.Utility.OWUtilities; namespace NewHorizons.Builder.Body { @@ -191,7 +192,7 @@ namespace NewHorizons.Builder.Body if (hasDestructionVolume || targetStarSystem != null) { var destructionVolumeGO = new GameObject("DestructionVolume"); - destructionVolumeGO.layer = LayerMask.NameToLayer("BasicEffectVolume"); + destructionVolumeGO.layer = LayerUtilities.BasicEffectVolume; destructionVolumeGO.transform.parent = singularity.transform; destructionVolumeGO.transform.localScale = Vector3.one; destructionVolumeGO.transform.localPosition = Vector3.zero; diff --git a/NewHorizons/Builder/Body/StarBuilder.cs b/NewHorizons/Builder/Body/StarBuilder.cs index 2da12c1e..f9ee9ceb 100644 --- a/NewHorizons/Builder/Body/StarBuilder.cs +++ b/NewHorizons/Builder/Body/StarBuilder.cs @@ -9,6 +9,7 @@ using OWML.Common; using UnityEngine.InputSystem.XR; using System.Linq; using NewHorizons.Components.Stars; +using NewHorizons.Utility.OWUtilities; namespace NewHorizons.Builder.Body { @@ -122,7 +123,7 @@ namespace NewHorizons.Builder.Body heatVolume.transform.SetParent(starGO.transform, false); heatVolume.transform.localPosition = Vector3.zero; heatVolume.transform.localScale = Vector3.one; - heatVolume.layer = LayerMask.NameToLayer("BasicEffectVolume"); + heatVolume.layer = LayerUtilities.BasicEffectVolume; heatVolume.AddComponent().radius = 1.1f; heatVolume.AddComponent(); heatVolume.AddComponent()._damagePerSecond = 20f; @@ -132,7 +133,7 @@ namespace NewHorizons.Builder.Body deathVolume.transform.SetParent(starGO.transform, false); deathVolume.transform.localPosition = Vector3.zero; deathVolume.transform.localScale = Vector3.one; - deathVolume.layer = LayerMask.NameToLayer("BasicEffectVolume"); + deathVolume.layer = LayerUtilities.BasicEffectVolume; var sphereCollider = deathVolume.AddComponent(); sphereCollider.radius = 1f; sphereCollider.isTrigger = true; @@ -148,7 +149,7 @@ namespace NewHorizons.Builder.Body planetDestructionVolume.transform.SetParent(starGO.transform, false); planetDestructionVolume.transform.localPosition = Vector3.zero; planetDestructionVolume.transform.localScale = Vector3.one; - planetDestructionVolume.layer = LayerMask.NameToLayer("BasicEffectVolume"); + planetDestructionVolume.layer = LayerUtilities.BasicEffectVolume; var planetSphereCollider = planetDestructionVolume.AddComponent(); planetSphereCollider.radius = 0.8f; planetSphereCollider.isTrigger = true; @@ -445,7 +446,7 @@ namespace NewHorizons.Builder.Body supernovaWallAudio.transform.SetParent(supernovaGO.transform, false); supernovaWallAudio.transform.localPosition = Vector3.zero; supernovaWallAudio.transform.localScale = Vector3.one; - supernovaWallAudio.layer = LayerMask.NameToLayer("BasicEffectVolume"); + supernovaWallAudio.layer = LayerUtilities.BasicEffectVolume; var audioSource = supernovaWallAudio.AddComponent(); audioSource.loop = true; audioSource.maxDistance = 2000; diff --git a/NewHorizons/Builder/Body/WaterBuilder.cs b/NewHorizons/Builder/Body/WaterBuilder.cs index e71e957c..e08669bc 100644 --- a/NewHorizons/Builder/Body/WaterBuilder.cs +++ b/NewHorizons/Builder/Body/WaterBuilder.cs @@ -4,6 +4,7 @@ using UnityEngine; using NewHorizons.External.Modules.VariableSize; using Tessellation; using NewHorizons.Utility.OWMLUtilities; +using NewHorizons.Utility.OWUtilities; namespace NewHorizons.Builder.Body { @@ -53,7 +54,7 @@ namespace NewHorizons.Builder.Body // Don't ignore sun when not under clouds waterGO.layer = 0; - Delay.FireOnNextUpdate(() => { if (planetGO.FindChild("Sector/SunOverride") != null) waterGO.layer = 15; }); + Delay.FireOnNextUpdate(() => { if (planetGO.FindChild("Sector/SunOverride") != null) waterGO.layer = LayerUtilities.IgnoreSun; }); TessellatedSphereRenderer TSR = waterGO.AddComponent(); TSR.tessellationMeshGroup = ScriptableObject.CreateInstance(); @@ -100,7 +101,7 @@ namespace NewHorizons.Builder.Body var buoyancyObject = new GameObject("WaterVolume"); buoyancyObject.transform.parent = waterGO.transform; buoyancyObject.transform.localScale = Vector3.one; - buoyancyObject.layer = LayerMask.NameToLayer("BasicEffectVolume"); + buoyancyObject.layer = LayerUtilities.BasicEffectVolume; var sphereCollider = buoyancyObject.AddComponent(); sphereCollider.radius = 1; diff --git a/NewHorizons/Builder/General/DetectorBuilder.cs b/NewHorizons/Builder/General/DetectorBuilder.cs index fff255b5..d557c9cf 100644 --- a/NewHorizons/Builder/General/DetectorBuilder.cs +++ b/NewHorizons/Builder/General/DetectorBuilder.cs @@ -1,6 +1,7 @@ using NewHorizons.Components.Orbital; using NewHorizons.External.Configs; using NewHorizons.Utility; +using NewHorizons.Utility.OWUtilities; using System.Collections.Generic; using UnityEngine; using Logger = NewHorizons.Utility.Logger; @@ -81,7 +82,7 @@ namespace NewHorizons.Builder.General detectorGO.SetActive(false); detectorGO.transform.parent = planetGO.transform; detectorGO.transform.localPosition = Vector3.zero; - detectorGO.layer = LayerMask.NameToLayer("BasicDetector"); + detectorGO.layer = LayerUtilities.BasicDetector; ConstantForceDetector forceDetector = detectorGO.AddComponent(); forceDetector._inheritElement0 = true; @@ -90,7 +91,7 @@ namespace NewHorizons.Builder.General // For falling into sun if (!config.Base.invulnerableToSun && config.Star == null && config.FocalPoint == null) { - detectorGO.layer = LayerMask.NameToLayer("AdvancedDetector"); + detectorGO.layer = LayerUtilities.AdvancedDetector; var fluidDetector = detectorGO.AddComponent(); var sphereCollider = detectorGO.AddComponent(); diff --git a/NewHorizons/Builder/General/GravityBuilder.cs b/NewHorizons/Builder/General/GravityBuilder.cs index 997e7a04..e90f02d7 100644 --- a/NewHorizons/Builder/General/GravityBuilder.cs +++ b/NewHorizons/Builder/General/GravityBuilder.cs @@ -1,5 +1,6 @@ using NewHorizons.External.Configs; using NewHorizons.External.Modules; +using NewHorizons.Utility.OWUtilities; using UnityEngine; using Logger = NewHorizons.Utility.Logger; namespace NewHorizons.Builder.General @@ -23,7 +24,7 @@ namespace NewHorizons.Builder.General var gravityGO = new GameObject("GravityWell"); gravityGO.transform.parent = planetGO.transform; gravityGO.transform.localPosition = Vector3.zero; - gravityGO.layer = 17; + gravityGO.layer = LayerUtilities.BasicEffectVolume; gravityGO.SetActive(false); var SC = gravityGO.AddComponent(); diff --git a/NewHorizons/Builder/General/RFVolumeBuilder.cs b/NewHorizons/Builder/General/RFVolumeBuilder.cs index 7ea5ab61..e5dbc643 100644 --- a/NewHorizons/Builder/General/RFVolumeBuilder.cs +++ b/NewHorizons/Builder/General/RFVolumeBuilder.cs @@ -1,5 +1,6 @@ using NewHorizons.External.Configs; using NewHorizons.External.Modules; +using NewHorizons.Utility.OWUtilities; using UnityEngine; namespace NewHorizons.Builder.General { @@ -12,7 +13,7 @@ namespace NewHorizons.Builder.General var rfGO = new GameObject("RFVolume"); rfGO.transform.parent = planetGO.transform; rfGO.transform.localPosition = Vector3.zero; - rfGO.layer = 19; + rfGO.layer = LayerUtilities.ReferenceFrameVolume; rfGO.SetActive(false); var SC = rfGO.AddComponent(); diff --git a/NewHorizons/Builder/General/SpawnPointBuilder.cs b/NewHorizons/Builder/General/SpawnPointBuilder.cs index f773fc85..acce4bcf 100644 --- a/NewHorizons/Builder/General/SpawnPointBuilder.cs +++ b/NewHorizons/Builder/General/SpawnPointBuilder.cs @@ -1,6 +1,7 @@ using NewHorizons.External.Modules; using NewHorizons.Utility; using NewHorizons.Utility.OWMLUtilities; +using NewHorizons.Utility.OWUtilities; using System; using System.Reflection; using UnityEngine; @@ -17,7 +18,7 @@ namespace NewHorizons.Builder.General { GameObject spawnGO = new GameObject("PlayerSpawnPoint"); spawnGO.transform.parent = planetGO.transform; - spawnGO.layer = 8; + spawnGO.layer = LayerUtilities.PlayerSafetyCollider; spawnGO.transform.localPosition = module.playerSpawnPoint; @@ -39,7 +40,7 @@ namespace NewHorizons.Builder.General { GameObject spawnGO = new GameObject("ShipSpawnPoint"); spawnGO.transform.parent = planetGO.transform; - spawnGO.layer = 8; + spawnGO.layer = LayerUtilities.PlayerSafetyCollider; spawnGO.transform.localPosition = module.shipSpawnPoint; @@ -70,7 +71,7 @@ namespace NewHorizons.Builder.General Logger.LogVerbose("Overriding player spawn to be inside ship"); GameObject playerSpawnGO = new GameObject("PlayerSpawnPoint"); playerSpawnGO.transform.parent = ship.transform; - playerSpawnGO.layer = 8; + playerSpawnGO.layer = LayerUtilities.PlayerSafetyCollider; playerSpawnGO.transform.localPosition = new Vector3(0, 0, 0); diff --git a/NewHorizons/Builder/Props/DialogueBuilder.cs b/NewHorizons/Builder/Props/DialogueBuilder.cs index d412e839..1daae87b 100644 --- a/NewHorizons/Builder/Props/DialogueBuilder.cs +++ b/NewHorizons/Builder/Props/DialogueBuilder.cs @@ -7,6 +7,7 @@ using UnityEngine; using NewHorizons.Utility; using Logger = NewHorizons.Utility.Logger; using NewHorizons.Components; +using NewHorizons.Utility.OWUtilities; namespace NewHorizons.Builder.Props { @@ -103,7 +104,7 @@ namespace NewHorizons.Builder.Props var conversationZone = new GameObject("ConversationZone"); conversationZone.SetActive(false); - conversationZone.layer = LayerMask.NameToLayer("Interactible"); + conversationZone.layer = LayerUtilities.Interactible; var sphere = conversationZone.AddComponent(); sphere.radius = info.radius; @@ -259,7 +260,7 @@ namespace NewHorizons.Builder.Props var playerTrackingZone = new GameObject("PlayerTrackingZone"); playerTrackingZone.SetActive(false); - playerTrackingZone.layer = LayerMask.NameToLayer("BasicEffectVolume"); + playerTrackingZone.layer = LayerUtilities.BasicEffectVolume; playerTrackingZone.SetActive(false); var sphereCollider = playerTrackingZone.AddComponent(); diff --git a/NewHorizons/Builder/Props/QuantumBuilder.cs b/NewHorizons/Builder/Props/QuantumBuilder.cs index e7e1423f..657e6d14 100644 --- a/NewHorizons/Builder/Props/QuantumBuilder.cs +++ b/NewHorizons/Builder/Props/QuantumBuilder.cs @@ -2,7 +2,7 @@ using HarmonyLib; using NewHorizons.Components.Quantum; using NewHorizons.External.Configs; using NewHorizons.External.Modules; -using NewHorizons.Utility.UnityUtilities; +using NewHorizons.Utility.Geometry; using OWML.Common; using System; using System.Collections.Generic; diff --git a/NewHorizons/Builder/Props/RemoteBuilder.cs b/NewHorizons/Builder/Props/RemoteBuilder.cs index 4fa179f3..08ce1a91 100644 --- a/NewHorizons/Builder/Props/RemoteBuilder.cs +++ b/NewHorizons/Builder/Props/RemoteBuilder.cs @@ -1,6 +1,7 @@ using NewHorizons.External.Modules; using NewHorizons.Handlers; using NewHorizons.Utility; +using NewHorizons.Utility.OWUtilities; using OWML.Common; using System; using System.Linq; @@ -86,7 +87,7 @@ namespace NewHorizons.Builder.Props if (_shareStonePrefab == null) { GameObject stone = new GameObject("ShareStoneFallback"); - stone.layer = LayerMask.NameToLayer("Interactible"); + stone.layer = LayerUtilities.Interactible; stone.SetActive(false); SphereCollider sc = stone.AddComponent(); sc.center = Vector3.zero; diff --git a/NewHorizons/Builder/Props/SignalBuilder.cs b/NewHorizons/Builder/Props/SignalBuilder.cs index b6d0669f..0e46ba53 100644 --- a/NewHorizons/Builder/Props/SignalBuilder.cs +++ b/NewHorizons/Builder/Props/SignalBuilder.cs @@ -1,6 +1,7 @@ using NewHorizons.External.Modules; using NewHorizons.Utility; using NewHorizons.Utility.OWMLUtilities; +using NewHorizons.Utility.OWUtilities; using OWML.Common; using OWML.Utils; using System.Collections.Generic; @@ -130,7 +131,7 @@ namespace NewHorizons.Builder.Props var pos = (Vector3)(info.position ?? Vector3.zero); if (info.isRelativeToParent) signalGO.transform.localPosition = pos; else signalGO.transform.position = planetGO.transform.TransformPoint(pos); - signalGO.layer = LayerMask.NameToLayer("AdvancedEffectVolume"); + signalGO.layer = LayerUtilities.AdvancedEffectVolume; var source = signalGO.AddComponent(); var owAudioSource = signalGO.AddComponent(); diff --git a/NewHorizons/Builder/Props/TranslatorText/NomaiTextArcArranger.cs b/NewHorizons/Builder/Props/TranslatorText/NomaiTextArcArranger.cs index 1ef74572..b94f3e6d 100644 --- a/NewHorizons/Builder/Props/TranslatorText/NomaiTextArcArranger.cs +++ b/NewHorizons/Builder/Props/TranslatorText/NomaiTextArcArranger.cs @@ -1,14 +1,13 @@ -using NewHorizons.Utility.UnityUtilities; -using System.Collections; +using NewHorizons.Utility.Geometry; using System.Collections.Generic; using System.Linq; using UnityEngine; -using UnityEngine.Profiling; using Logger = NewHorizons.Utility.Logger; namespace NewHorizons.Builder.Props { - public class NomaiTextArcArranger : MonoBehaviour { + public class NomaiTextArcArranger : MonoBehaviour + { private static int MAX_MOVE_DISTANCE = 2; public List spirals = new List(); @@ -23,7 +22,7 @@ namespace NewHorizons.Builder.Props public float maxY = 2.6f; public float minY = -1f; - public static SpiralManipulator CreateSpiral(NomaiTextArcBuilder.SpiralProfile profile, GameObject spiralMeshHolder) + public static SpiralManipulator CreateSpiral(NomaiTextArcBuilder.SpiralProfile profile, GameObject spiralMeshHolder) { var rootArc = NomaiTextArcBuilder.BuildSpiralGameObject(profile); rootArc.transform.parent = spiralMeshHolder.transform; @@ -31,7 +30,7 @@ namespace NewHorizons.Builder.Props var manip = rootArc.AddComponent(); if (Random.value < 0.5) manip.transform.localScale = new Vector3(-1, 1, 1); // randomly mirror - + // add to arranger var arranger = spiralMeshHolder.GetAddComponent(); if (arranger.root == null) arranger.root = manip; @@ -40,11 +39,11 @@ namespace NewHorizons.Builder.Props return manip; } - - public void FDGSimulationStep() + + public void FDGSimulationStep() { - if (updateToposortOnNextStep) - { + if (updateToposortOnNextStep) + { updateToposortOnNextStep = false; GenerateReverseToposort(); } @@ -56,13 +55,13 @@ namespace NewHorizons.Builder.Props Vector2 force = Vector2.zero; // accumulate the force the children feel - if (childForces.ContainsKey(s1)) + if (childForces.ContainsKey(s1)) { force += 0.9f * childForces[s1]; } - + // push away from fellow spirals - foreach (var s2 in spirals) + foreach (var s2 in spirals) { if (s1 == s2) continue; if (s1.parent == s2) continue; @@ -74,29 +73,29 @@ namespace NewHorizons.Builder.Props var f2 = (s2.localPosition - s1.localPosition); force -= f2 / Mathf.Pow(f2.magnitude, 6); } - + // push away from the edges var MAX_EDGE_PUSH_FORCE = 1; - force += new Vector2(0, -1) * Mathf.Max(0, (s1.transform.localPosition.y + maxY)*(MAX_EDGE_PUSH_FORCE / maxY) - MAX_EDGE_PUSH_FORCE); - force += new Vector2(0, 1) * Mathf.Max(0, (s1.transform.localPosition.y + minY)*(MAX_EDGE_PUSH_FORCE / minY) - MAX_EDGE_PUSH_FORCE); - force += new Vector2(-1, 0) * Mathf.Max(0, (s1.transform.localPosition.x + maxX)*(MAX_EDGE_PUSH_FORCE / maxX) - MAX_EDGE_PUSH_FORCE); - force += new Vector2(1, 0) * Mathf.Max(0, (s1.transform.localPosition.x + minX)*(MAX_EDGE_PUSH_FORCE / minX) - MAX_EDGE_PUSH_FORCE); + force += new Vector2(0, -1) * Mathf.Max(0, (s1.transform.localPosition.y + maxY) * (MAX_EDGE_PUSH_FORCE / maxY) - MAX_EDGE_PUSH_FORCE); + force += new Vector2(0, 1) * Mathf.Max(0, (s1.transform.localPosition.y + minY) * (MAX_EDGE_PUSH_FORCE / minY) - MAX_EDGE_PUSH_FORCE); + force += new Vector2(-1, 0) * Mathf.Max(0, (s1.transform.localPosition.x + maxX) * (MAX_EDGE_PUSH_FORCE / maxX) - MAX_EDGE_PUSH_FORCE); + force += new Vector2(1, 0) * Mathf.Max(0, (s1.transform.localPosition.x + minX) * (MAX_EDGE_PUSH_FORCE / minX) - MAX_EDGE_PUSH_FORCE); // push up just to make everything a little more pretty (this is not neccessary to get an arrangement that simply has no overlap/spirals exiting the bounds) - force += new Vector2(0, 1) * 1; - + force += new Vector2(0, 1) * 1; + // renormalize the force magnitude (keeps force sizes reasonable, and improves stability in the case of small forces) var avg = 1; // the size of vector required to get a medium push var scale = 0.75f; - force = force.normalized * scale * (1 / (1 + Mathf.Exp(avg-force.magnitude)) - 1 / (1 + Mathf.Exp(avg))); // apply a sigmoid-ish smoothing operation, so only giant forces actually move the spirals + force = force.normalized * scale * (1 / (1 + Mathf.Exp(avg - force.magnitude)) - 1 / (1 + Mathf.Exp(avg))); // apply a sigmoid-ish smoothing operation, so only giant forces actually move the spirals // if this is the root spiral, then rotate it instead of trying to move it - if (s1.parent == null) + if (s1.parent == null) { // this is the root spiral, so rotate instead of moving var finalAngle = Mathf.Atan2(force.y, force.x); // root spiral is always at 0, 0 var currentAngle = Mathf.Atan2(s1.center.y, s1.center.x); // root spiral is always at 0, 0 - s1.transform.localEulerAngles = new Vector3(0, 0, finalAngle-currentAngle); + s1.transform.localEulerAngles = new Vector3(0, 0, finalAngle - currentAngle); s1.UpdateChildren(); continue; @@ -105,11 +104,11 @@ namespace NewHorizons.Builder.Props // pick the parent point that's closest to center+force, and move to there var spiral = s1; var parentPoints = spiral.parent.GetComponent().GetPoints(); - + var idealPoint = spiral.position + force; var bestPointIndex = 0; var bestPointDistance = 99999999f; - for (var j = SpiralManipulator.MIN_PARENT_POINT; j < SpiralManipulator.MAX_PARENT_POINT && j < parentPoints.Length; j++) + for (var j = SpiralManipulator.MIN_PARENT_POINT; j < SpiralManipulator.MAX_PARENT_POINT && j < parentPoints.Length; j++) { // don't put this spiral on a point already occupied by a sibling if (j != spiral._parentPointIndex && spiral.parent.pointsOccupiedByChildren.Contains(j)) continue; @@ -118,33 +117,34 @@ namespace NewHorizons.Builder.Props point = spiral.parent.transform.TransformPoint(point); var dist = Vector2.Distance(point, idealPoint); - if (dist < bestPointDistance) { + if (dist < bestPointDistance) + { bestPointDistance = dist; bestPointIndex = j; } } - + // limit the distance a spiral can move in a single step bestPointIndex = spiral._parentPointIndex + Mathf.Min(MAX_MOVE_DISTANCE, Mathf.Max(-MAX_MOVE_DISTANCE, bestPointIndex - spiral._parentPointIndex)); // minimize step size to help stability - + // actually move the spiral spiral.PlaceOnParentPoint(bestPointIndex); - + // Enforce bounds - if (OutsideBounds(s1)) + if (OutsideBounds(s1)) { var start = s1._parentPointIndex; var originalMirror = s1.Mirrored; var success = AttemptToPushSpiralInBounds(s1, start); - if (!success) + if (!success) { // try flipping it if nothing worked with original mirror state - s1.Mirror(); + s1.Mirror(); success = AttemptToPushSpiralInBounds(s1, start); } - if (!success) + if (!success) { // if we couldn't put it inside the bounds, put it back how we found it (this increases stability of the rest of the spirals) if (s1.Mirrored != originalMirror) s1.Mirror(); @@ -164,22 +164,22 @@ namespace NewHorizons.Builder.Props reverseToposortedSpirals = new List(); Queue frontierQueue = new Queue(); frontierQueue.Enqueue(root); - - while(frontierQueue.Count > 0) + + while (frontierQueue.Count > 0) { var spiral = frontierQueue.Dequeue(); reverseToposortedSpirals.Add(spiral); - - foreach(var child in spiral.children) frontierQueue.Enqueue(child); + + foreach (var child in spiral.children) frontierQueue.Enqueue(child); } - + reverseToposortedSpirals.Reverse(); } #region overlap handling // returns whether there was overlap or not - public bool AttemptOverlapResolution() + public bool AttemptOverlapResolution() { var overlappingSpirals = FindOverlap(); if (overlappingSpirals.x < 0) return false; @@ -195,27 +195,27 @@ namespace NewHorizons.Builder.Props return true; } - - public Vector2Int FindOverlap() + + public Vector2Int FindOverlap() { var index = -1; - foreach (var s1 in spirals) + foreach (var s1 in spirals) { index++; if (s1.parent == null) continue; var jndex = -1; - foreach (var s2 in spirals) + foreach (var s2 in spirals) { jndex++; - if (SpiralsOverlap(s1, s2)) return new Vector2Int(index, jndex);; + if (SpiralsOverlap(s1, s2)) return new Vector2Int(index, jndex); ; } } return new Vector2Int(-1, -1); } - public bool SpiralsOverlap(SpiralManipulator s1, SpiralManipulator s2) + public bool SpiralsOverlap(SpiralManipulator s1, SpiralManipulator s2) { if (s1 == s2) return false; if (Vector3.Distance(s1.center, s2.center) > Mathf.Max(s1.NomaiTextLine.GetWorldRadius(), s2.NomaiTextLine.GetWorldRadius())) return false; // no overlap possible - too far away @@ -229,31 +229,32 @@ namespace NewHorizons.Builder.Props if (s1.parent == s2) s1Points.RemoveAt(0); // don't consider the base points so that we can check if children overlap their parents if (s2.parent == s1) s2Points.RemoveAt(0); // (note: the base point of a child is always exactly overlapping with one of the parent's points) - foreach(var p1 in s1Points) + foreach (var p1 in s1Points) { - foreach(var p2 in s2Points) + foreach (var p2 in s2Points) { - if (Vector3.SqrMagnitude(p1-p2) <= thresholdForOverlap) return true; // s1 and s2 overlap + if (Vector3.SqrMagnitude(p1 - p2) <= thresholdForOverlap) return true; // s1 and s2 overlap } } return false; } - + #endregion overlap handling - + #region bounds handling - public bool OutsideBounds(SpiralManipulator spiral) + public bool OutsideBounds(SpiralManipulator spiral) { var points = spiral.NomaiTextLine.GetPoints() .Select(p => spiral.transform.TransformPoint(p)) .Select(p => spiral.transform.parent.InverseTransformPoint(p)) .ToList(); - foreach(var point in points) { + foreach (var point in points) + { if (point.x < minX || point.x > maxX || - point.y < minY || point.y > maxY) + point.y < minY || point.y > maxY) { return true; } @@ -262,29 +263,29 @@ namespace NewHorizons.Builder.Props return false; } - private bool AttemptToPushSpiralInBounds(SpiralManipulator s1, int start) + private bool AttemptToPushSpiralInBounds(SpiralManipulator s1, int start) { - var range = Mathf.Max(start-SpiralManipulator.MIN_PARENT_POINT, SpiralManipulator.MAX_PARENT_POINT-start); + var range = Mathf.Max(start - SpiralManipulator.MIN_PARENT_POINT, SpiralManipulator.MAX_PARENT_POINT - start); for (var i = 1; i <= range; i++) { - if (start-i >= SpiralManipulator.MIN_PARENT_POINT) - { - s1.PlaceOnParentPoint(start-i); + if (start - i >= SpiralManipulator.MIN_PARENT_POINT) + { + s1.PlaceOnParentPoint(start - i); if (!OutsideBounds(s1)) return true; } - - if (start+i <= SpiralManipulator.MAX_PARENT_POINT) - { - s1.PlaceOnParentPoint(start+i); + + if (start + i <= SpiralManipulator.MAX_PARENT_POINT) + { + s1.PlaceOnParentPoint(start + i); if (!OutsideBounds(s1)) return true; } } return false; } - - public void DrawBoundsWithDebugSpheres() + + public void DrawBoundsWithDebugSpheres() { AddDebugShape.AddSphere(this.gameObject, 0.1f, Color.green).transform.localPosition = new Vector3(minX, minY, 0); AddDebugShape.AddSphere(this.gameObject, 0.1f, Color.green).transform.localPosition = new Vector3(minX, maxY, 0); @@ -292,17 +293,18 @@ namespace NewHorizons.Builder.Props AddDebugShape.AddSphere(this.gameObject, 0.1f, Color.green).transform.localPosition = new Vector3(maxX, minY, 0); AddDebugShape.AddSphere(this.gameObject, 0.1f, Color.red).transform.localPosition = new Vector3(0, 0, 0); } - + #endregion bounds handling } - public class SpiralManipulator : MonoBehaviour { + public class SpiralManipulator : MonoBehaviour + { public SpiralManipulator parent; public List children = new List(); public HashSet pointsOccupiedByChildren = new HashSet(); public int _parentPointIndex = -1; - + public static int MIN_PARENT_POINT = 3; public static int MAX_PARENT_POINT = 26; @@ -311,32 +313,33 @@ namespace NewHorizons.Builder.Props public bool Mirrored { get { return this.transform.localScale.x < 0; } } private NomaiTextLine _NomaiTextLine; - public NomaiTextLine NomaiTextLine + public NomaiTextLine NomaiTextLine { - get + get { if (_NomaiTextLine == null) _NomaiTextLine = GetComponent(); return _NomaiTextLine; } } - public Vector2 center - { - get { return NomaiTextLine.GetWorldCenter(); } + public Vector2 center + { + get { return NomaiTextLine.GetWorldCenter(); } } - public Vector2 localPosition + public Vector2 localPosition { get { return new Vector2(this.transform.localPosition.x, this.transform.localPosition.y); } } - public Vector2 position + public Vector2 position { get { return new Vector2(this.transform.position.x, this.transform.position.y); } } - + #endregion properties - - public SpiralManipulator AddChild(NomaiTextArcBuilder.SpiralProfile profile) { + + public SpiralManipulator AddChild(NomaiTextArcBuilder.SpiralProfile profile) + { var child = NomaiTextArcArranger.CreateSpiral(profile, this.transform.parent.gameObject); var index = Random.Range(MIN_PARENT_POINT, MAX_PARENT_POINT); @@ -348,33 +351,33 @@ namespace NewHorizons.Builder.Props return child; } - public void Mirror() - { + public void Mirror() + { this.transform.localScale = new Vector3(-this.transform.localScale.x, 1, 1); if (this.parent != null) this.PlaceOnParentPoint(this._parentPointIndex); } - - public void UpdateChildren() + + public void UpdateChildren() { - foreach(var child in this.children) + foreach (var child in this.children) { child.PlaceOnParentPoint(child._parentPointIndex); } } - public int PlaceOnParentPoint(int parentPointIndex, bool updateChildren=true) + public int PlaceOnParentPoint(int parentPointIndex, bool updateChildren = true) { // validate var _points = parent.GetComponent().GetPoints(); - parentPointIndex = Mathf.Max(0, Mathf.Min(parentPointIndex, _points.Length-1)); - + parentPointIndex = Mathf.Max(0, Mathf.Min(parentPointIndex, _points.Length - 1)); + // track occupied points if (this._parentPointIndex != -1) parent.pointsOccupiedByChildren.Remove(this._parentPointIndex); this._parentPointIndex = parentPointIndex; parent.pointsOccupiedByChildren.Add(parentPointIndex); // calculate the normal - var normal = _points[Mathf.Min(parentPointIndex+1, _points.Length-1)] - _points[Mathf.Max(parentPointIndex-1, 0)]; + var normal = _points[Mathf.Min(parentPointIndex + 1, _points.Length - 1)] - _points[Mathf.Max(parentPointIndex - 1, 0)]; if (parent.transform.localScale.x < 0) normal = new Vector3(normal.x, -normal.y, -normal.z); float rot = Mathf.Atan2(normal.y, normal.x) * Mathf.Rad2Deg; diff --git a/NewHorizons/Builder/Props/TranslatorText/TranslatorTextBuilder.cs b/NewHorizons/Builder/Props/TranslatorText/TranslatorTextBuilder.cs index 533d5fa3..a712a3f0 100644 --- a/NewHorizons/Builder/Props/TranslatorText/TranslatorTextBuilder.cs +++ b/NewHorizons/Builder/Props/TranslatorText/TranslatorTextBuilder.cs @@ -1,20 +1,18 @@ using NewHorizons.External.Modules; using NewHorizons.Handlers; using NewHorizons.Utility; -using OWML.Common; +using NewHorizons.Utility.Geometry; +using NewHorizons.Utility.OWMLUtilities; +using Newtonsoft.Json; +using OWML.Utils; +using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Xml; using UnityEngine; -using Enum = System.Enum; using Logger = NewHorizons.Utility.Logger; using Random = UnityEngine.Random; -using OWML.Utils; -using Newtonsoft.Json; -using System; -using NewHorizons.Utility.OWMLUtilities; -using NewHorizons.Utility.UnityUtilities; namespace NewHorizons.Builder.Props { diff --git a/NewHorizons/Builder/ShipLog/RevealBuilder.cs b/NewHorizons/Builder/ShipLog/RevealBuilder.cs index 87c2e103..a4f0b0c1 100644 --- a/NewHorizons/Builder/ShipLog/RevealBuilder.cs +++ b/NewHorizons/Builder/ShipLog/RevealBuilder.cs @@ -1,5 +1,6 @@ using NewHorizons.Components.Achievement; using NewHorizons.External.Modules; +using NewHorizons.Utility.OWUtilities; using OWML.Common; using UnityEngine; using Logger = NewHorizons.Utility.Logger; @@ -122,7 +123,7 @@ namespace NewHorizons.Builder.ShipLog private static void MakeObservable(GameObject go, Sector sector, VolumesModule.RevealVolumeInfo info, IModBehaviour mod) { - go.layer = LayerMask.NameToLayer("Interactible"); + go.layer = LayerUtilities.Interactible; var sphere = go.AddComponent(); sphere.radius = info.radius; diff --git a/NewHorizons/Builder/StarSystem/SkyboxBuilder.cs b/NewHorizons/Builder/StarSystem/SkyboxBuilder.cs index 0ef97d83..aa6231b7 100644 --- a/NewHorizons/Builder/StarSystem/SkyboxBuilder.cs +++ b/NewHorizons/Builder/StarSystem/SkyboxBuilder.cs @@ -1,15 +1,15 @@ using NewHorizons.External.Configs; using NewHorizons.Utility; using NewHorizons.Utility.OWMLUtilities; +using NewHorizons.Utility.OWUtilities; using OWML.Common; -using System; using UnityEngine; using Logger = NewHorizons.Utility.Logger; + namespace NewHorizons.Builder.StarSystem { public static class SkyboxBuilder { - private static readonly int _skyboxLayer = LayerMask.NameToLayer("Skybox"); private static readonly Shader _unlitShader = Shader.Find("Unlit/Texture"); public static void Make(StarSystemConfig.SkyboxModule module, IModBehaviour mod) @@ -33,7 +33,7 @@ namespace NewHorizons.Builder.StarSystem var skySphere = new GameObject("Sky Sphere"); skySphere.transform.SetParent(skybox.transform, false); - skySphere.layer = _skyboxLayer; + skySphere.layer = LayerUtilities.Skybox; skySphere.transform.localScale = Vector3.one * 5f; BuildSkySphereFace(skySphere, "Right", Quaternion.Euler(0f, 90f, 0f), mesh, rightTex); @@ -56,7 +56,7 @@ namespace NewHorizons.Builder.StarSystem var go = new GameObject(name) { - layer = _skyboxLayer + layer = LayerUtilities.Skybox }; var mf = go.AddComponent(); diff --git a/NewHorizons/Builder/Volumes/AudioVolumeBuilder.cs b/NewHorizons/Builder/Volumes/AudioVolumeBuilder.cs index 8711ddf7..8bf31a78 100644 --- a/NewHorizons/Builder/Volumes/AudioVolumeBuilder.cs +++ b/NewHorizons/Builder/Volumes/AudioVolumeBuilder.cs @@ -1,5 +1,6 @@ using NewHorizons.External.Modules; using NewHorizons.Utility; +using NewHorizons.Utility.OWUtilities; using OWML.Common; using OWML.Utils; using System; @@ -42,7 +43,7 @@ namespace NewHorizons.Builder.Volumes var pos = (Vector3)(info.position ?? Vector3.zero); if (info.isRelativeToParent) go.transform.localPosition = pos; else go.transform.position = planetGO.transform.TransformPoint(pos); - go.layer = LayerMask.NameToLayer("AdvancedEffectVolume"); + go.layer = LayerUtilities.AdvancedEffectVolume; var audioSource = go.AddComponent(); diff --git a/NewHorizons/Builder/Volumes/HazardVolumeBuilder.cs b/NewHorizons/Builder/Volumes/HazardVolumeBuilder.cs index 23a4f1bb..f1c9495b 100644 --- a/NewHorizons/Builder/Volumes/HazardVolumeBuilder.cs +++ b/NewHorizons/Builder/Volumes/HazardVolumeBuilder.cs @@ -1,4 +1,5 @@ using NewHorizons.External.Modules; +using NewHorizons.Utility.OWUtilities; using OWML.Common; using OWML.Utils; using System; @@ -39,7 +40,7 @@ namespace NewHorizons.Builder.Volumes var pos = (Vector3)(info.position ?? Vector3.zero); if (info.isRelativeToParent) go.transform.localPosition = pos; else go.transform.position = planetGO.transform.TransformPoint(pos); - go.layer = LayerMask.NameToLayer("BasicEffectVolume"); + go.layer = LayerUtilities.BasicEffectVolume; var shape = go.AddComponent(); shape.radius = info.radius; @@ -74,7 +75,7 @@ namespace NewHorizons.Builder.Volumes var detectorGO = new GameObject("ConstantFluidDetector"); detectorGO.transform.parent = go.transform; detectorGO.transform.localPosition = Vector3.zero; - detectorGO.layer = LayerMask.NameToLayer("BasicDetector"); + detectorGO.layer = LayerUtilities.BasicDetector; var detector = detectorGO.AddComponent(); detector._onlyDetectableFluid = water; detector._buoyancy.boundingRadius = 1; diff --git a/NewHorizons/Builder/Volumes/NotificationVolumeBuilder.cs b/NewHorizons/Builder/Volumes/NotificationVolumeBuilder.cs index 1e3f07b8..957e5f0e 100644 --- a/NewHorizons/Builder/Volumes/NotificationVolumeBuilder.cs +++ b/NewHorizons/Builder/Volumes/NotificationVolumeBuilder.cs @@ -1,5 +1,6 @@ using NewHorizons.External.Modules; using NewHorizons.Utility; +using NewHorizons.Utility.OWUtilities; using OWML.Common; using System; using System.Collections.Generic; @@ -42,7 +43,7 @@ namespace NewHorizons.Builder.Volumes var pos = (Vector3)(info.position ?? Vector3.zero); if (info.isRelativeToParent) go.transform.localPosition = pos; else go.transform.position = planetGO.transform.TransformPoint(pos); - go.layer = LayerMask.NameToLayer("BasicEffectVolume"); + go.layer = LayerUtilities.BasicEffectVolume; var shape = go.AddComponent(); shape.radius = info.radius; diff --git a/NewHorizons/Builder/Volumes/VanishVolumeBuilder.cs b/NewHorizons/Builder/Volumes/VanishVolumeBuilder.cs index 2048159f..ff2abdad 100644 --- a/NewHorizons/Builder/Volumes/VanishVolumeBuilder.cs +++ b/NewHorizons/Builder/Volumes/VanishVolumeBuilder.cs @@ -1,5 +1,6 @@ using NewHorizons.Components; using NewHorizons.External.Modules; +using NewHorizons.Utility.OWUtilities; using UnityEngine; using Logger = NewHorizons.Utility.Logger; @@ -35,7 +36,7 @@ namespace NewHorizons.Builder.Volumes var pos = (Vector3)(info.position ?? Vector3.zero); if (info.isRelativeToParent) go.transform.localPosition = pos; else go.transform.position = planetGO.transform.TransformPoint(pos); - go.layer = LayerMask.NameToLayer("BasicEffectVolume"); + go.layer = LayerUtilities.BasicEffectVolume; var collider = go.AddComponent(); collider.isTrigger = true; diff --git a/NewHorizons/Builder/Volumes/VolumeBuilder.cs b/NewHorizons/Builder/Volumes/VolumeBuilder.cs index cc35c636..ae494418 100644 --- a/NewHorizons/Builder/Volumes/VolumeBuilder.cs +++ b/NewHorizons/Builder/Volumes/VolumeBuilder.cs @@ -1,5 +1,6 @@ using NewHorizons.Components; using NewHorizons.External.Modules; +using NewHorizons.Utility.OWUtilities; using UnityEngine; using Logger = NewHorizons.Utility.Logger; @@ -35,7 +36,7 @@ namespace NewHorizons.Builder.Volumes var pos = (Vector3)(info.position ?? Vector3.zero); if (info.isRelativeToParent) go.transform.localPosition = pos; else go.transform.position = planetGO.transform.TransformPoint(pos); - go.layer = LayerMask.NameToLayer("BasicEffectVolume"); + go.layer = LayerUtilities.BasicEffectVolume; var shape = go.AddComponent(); shape.radius = info.radius; diff --git a/NewHorizons/Components/Achievement/AchievementObserveTrigger.cs b/NewHorizons/Components/Achievement/AchievementObserveTrigger.cs index 1a6acf5f..f0c0a51f 100644 --- a/NewHorizons/Components/Achievement/AchievementObserveTrigger.cs +++ b/NewHorizons/Components/Achievement/AchievementObserveTrigger.cs @@ -1,4 +1,5 @@ using NewHorizons.OtherMods.AchievementsPlus; +using NewHorizons.Utility.OWUtilities; using System; using System.Collections.Generic; using System.Linq; @@ -20,7 +21,7 @@ namespace NewHorizons.Components.Achievement private void Reset() { - gameObject.layer = LayerMask.NameToLayer("Interactible"); + gameObject.layer = LayerUtilities.Interactible; } private void Awake() diff --git a/NewHorizons/Components/AddPhysics.cs b/NewHorizons/Components/AddPhysics.cs index 14785f9f..8f614994 100644 --- a/NewHorizons/Components/AddPhysics.cs +++ b/NewHorizons/Components/AddPhysics.cs @@ -1,4 +1,5 @@ -using System.Collections; +using NewHorizons.Utility.OWUtilities; +using System.Collections; using UnityEngine; namespace NewHorizons.Components; @@ -38,7 +39,7 @@ public class AddPhysics : MonoBehaviour var owRigidbody = bodyGo.AddComponent(); owRigidbody._simulateInSector = Sector; - bodyGo.layer = LayerMask.NameToLayer("PhysicalDetector"); + bodyGo.layer = LayerUtilities.PhysicalDetector; bodyGo.tag = "DynamicPropDetector"; // this collider is not included in groups. oh well bodyGo.AddComponent().radius = Radius; diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index a8518b84..1538d7bb 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -16,6 +16,8 @@ using NewHorizons.OtherMods.VoiceActing; using NewHorizons.Utility; using NewHorizons.Utility.DebugMenu; using NewHorizons.Utility.DebugUtilities; +using NewHorizons.Utility.OWMLUtilities; +using NewHorizons.Utility.OWUtilities; using OWML.Common; using OWML.ModHelper; using OWML.Utils; diff --git a/NewHorizons/Utility/DebugUtilities/DebugRaycaster.cs b/NewHorizons/Utility/DebugUtilities/DebugRaycaster.cs index d8a65a6b..03542ecc 100644 --- a/NewHorizons/Utility/DebugUtilities/DebugRaycaster.cs +++ b/NewHorizons/Utility/DebugUtilities/DebugRaycaster.cs @@ -1,7 +1,5 @@ -using NewHorizons.Builder.Props; -using NewHorizons.External.Modules; using NewHorizons.Handlers; -using NewHorizons.Utility.UnityUtilities; +using NewHorizons.Utility.Geometry; using UnityEngine; using UnityEngine.InputSystem; @@ -32,7 +30,7 @@ namespace NewHorizons.Utility.DebugUtilities Locator.GetPromptManager().AddScreenPrompt(_raycastPrompt, PromptPosition.UpperRight, false); } } - + private void OnDestroy() { if (_raycastPrompt != null) @@ -79,14 +77,14 @@ namespace NewHorizons.Utility.DebugUtilities var posText = Vector3ToString(data.pos); var normText = Vector3ToString(data.norm); var rotText = Vector3ToString(data.rot.eulerAngles); - - if(_surfaceSphere != null) GameObject.Destroy(_surfaceSphere); - if(_normalSphere1 != null) GameObject.Destroy(_normalSphere1); - if(_normalSphere2 != null) GameObject.Destroy(_normalSphere2); - if(_planeUpRightSphere != null) GameObject.Destroy(_planeUpRightSphere ); - if(_planeUpLeftSphere != null) GameObject.Destroy(_planeUpLeftSphere ); - if(_planeDownLeftSphere != null) GameObject.Destroy(_planeDownLeftSphere ); - if(_planeDownRightSphere != null) GameObject.Destroy(_planeDownRightSphere); + + if (_surfaceSphere != null) GameObject.Destroy(_surfaceSphere); + if (_normalSphere1 != null) GameObject.Destroy(_normalSphere1); + if (_normalSphere2 != null) GameObject.Destroy(_normalSphere2); + if (_planeUpRightSphere != null) GameObject.Destroy(_planeUpRightSphere); + if (_planeUpLeftSphere != null) GameObject.Destroy(_planeUpLeftSphere); + if (_planeDownLeftSphere != null) GameObject.Destroy(_planeDownLeftSphere); + if (_planeDownRightSphere != null) GameObject.Destroy(_planeDownRightSphere); _surfaceSphere = AddDebugShape.AddSphere(data.hitBodyGameObject, 0.1f, Color.green); _normalSphere1 = AddDebugShape.AddSphere(data.hitBodyGameObject, 0.01f, Color.red); @@ -95,22 +93,22 @@ namespace NewHorizons.Utility.DebugUtilities _surfaceSphere.transform.localPosition = data.pos; _normalSphere1.transform.localPosition = data.pos + data.norm * 0.5f; _normalSphere2.transform.localPosition = data.pos + data.norm; - + // plane corners var planeSize = 0.5f; var planePointSize = 0.05f; - _planeUpRightSphere = AddDebugShape.AddSphere(data.hitBodyGameObject, planePointSize, Color.green); - _planeUpLeftSphere = AddDebugShape.AddSphere(data.hitBodyGameObject, planePointSize, Color.cyan) ; - _planeDownLeftSphere = AddDebugShape.AddSphere(data.hitBodyGameObject, planePointSize, Color.blue) ; - _planeDownRightSphere = AddDebugShape.AddSphere(data.hitBodyGameObject, planePointSize, Color.cyan) ; - - _planeUpRightSphere .transform.localPosition = data.plane.origin + data.plane.u*1*planeSize + data.plane.v*1*planeSize; - _planeUpLeftSphere .transform.localPosition = data.plane.origin + data.plane.u*-1*planeSize + data.plane.v*1*planeSize; - _planeDownLeftSphere .transform.localPosition = data.plane.origin + data.plane.u*-1*planeSize + data.plane.v*-1*planeSize; - _planeDownRightSphere.transform.localPosition = data.plane.origin + data.plane.u*1*planeSize + data.plane.v*-1*planeSize; + _planeUpRightSphere = AddDebugShape.AddSphere(data.hitBodyGameObject, planePointSize, Color.green); + _planeUpLeftSphere = AddDebugShape.AddSphere(data.hitBodyGameObject, planePointSize, Color.cyan); + _planeDownLeftSphere = AddDebugShape.AddSphere(data.hitBodyGameObject, planePointSize, Color.blue); + _planeDownRightSphere = AddDebugShape.AddSphere(data.hitBodyGameObject, planePointSize, Color.cyan); + + _planeUpRightSphere.transform.localPosition = data.plane.origin + data.plane.u * 1 * planeSize + data.plane.v * 1 * planeSize; + _planeUpLeftSphere.transform.localPosition = data.plane.origin + data.plane.u * -1 * planeSize + data.plane.v * 1 * planeSize; + _planeDownLeftSphere.transform.localPosition = data.plane.origin + data.plane.u * -1 * planeSize + data.plane.v * -1 * planeSize; + _planeDownRightSphere.transform.localPosition = data.plane.origin + data.plane.u * 1 * planeSize + data.plane.v * -1 * planeSize; Logger.Log($"Raycast hit\n\n\"position\": {posText},\n\"rotation\": {rotText},\n\"normal\": {normText}\n\non collider [{data.colliderPath}] " + - (data.bodyPath != null? $"at rigidbody [{data.bodyPath}]" : "not attached to a rigidbody")); + (data.bodyPath != null ? $"at rigidbody [{data.bodyPath}]" : "not attached to a rigidbody")); } internal DebugRaycastData Raycast() { @@ -132,12 +130,12 @@ namespace NewHorizons.Utility.DebugUtilities var toOrigin = Vector3.ProjectOnPlane((origin - hitInfo.point).normalized, hitInfo.normal); var worldSpaceRot = Quaternion.LookRotation(toOrigin, hitInfo.normal); data.rot = hitInfo.rigidbody.transform.InverseTransformRotation(worldSpaceRot); - + data.colliderPath = hitInfo.collider.transform.GetPath(); data.bodyPath = hitInfo.rigidbody.transform.GetPath(); data.hitBodyGameObject = hitInfo.rigidbody.gameObject; data.hitObject = hitInfo.collider.gameObject; - + data.plane = ConstructPlane(data); } } @@ -155,7 +153,7 @@ namespace NewHorizons.Utility.DebugUtilities var U = data.pos - Vector3.zero; // U is the local "up" direction. the direction directly away from the center of the planet at this point. // pos is always relative to the body, so the body is considered to be at 0,0,0. var R = data.pos; // R is our origin point for the plane var N = data.norm.normalized; // N is the normal for this plane - + if (Vector3.Cross(U, N) == Vector3.zero) U = new Vector3(0, 0, 1); if (Vector3.Cross(U, N) == Vector3.zero) U = new Vector3(0, 1, 0); // if 0,0,1 was actually the same vector U already was (lol), try (0,1,0) instead @@ -163,11 +161,11 @@ namespace NewHorizons.Utility.DebugUtilities // stackoverflow.com/a/9605695 // I don't know exactly how this works, but I'm projecting a point that is located above the plane's origin, relative to the planet, onto the plane. this gets us our v vector - var q = (2*U)-R; + var q = (2 * U) - R; var dist = Vector3.Dot(N, q); - var v_raw = 2*U - dist*N; - var v = (R-v_raw).normalized; - + var v_raw = 2 * U - dist * N; + var v = (R - v_raw).normalized; + var u = Vector3.Cross(N, v); DebugRaycastPlane p = new DebugRaycastPlane() diff --git a/NewHorizons/Utility/Geometry/AddDebugShape.cs b/NewHorizons/Utility/Geometry/AddDebugShape.cs index 53a4af5d..d917d872 100644 --- a/NewHorizons/Utility/Geometry/AddDebugShape.cs +++ b/NewHorizons/Utility/Geometry/AddDebugShape.cs @@ -1,5 +1,5 @@ using UnityEngine; -namespace NewHorizons.Utility.UnityUtilities +namespace NewHorizons.Utility.Geometry { public static class AddDebugShape {