diff --git a/NewHorizons/Builder/Props/GeneralPropBuilder.cs b/NewHorizons/Builder/Props/GeneralPropBuilder.cs index ec6f9aa1..eb20dd1f 100644 --- a/NewHorizons/Builder/Props/GeneralPropBuilder.cs +++ b/NewHorizons/Builder/Props/GeneralPropBuilder.cs @@ -12,7 +12,7 @@ namespace NewHorizons.Builder.Props { public static class GeneralPropBuilder { - public static GameObject MakeFromExisting(GameObject go, GameObject planetGO, Sector sector, PropModule.PositionedPropInfo info, bool alignToBody = false, MVector3 normal = null) + public static GameObject MakeFromExisting(GameObject go, GameObject planetGO, Sector sector, PropModule.PositionedPropInfo info, bool alignToBody = false, MVector3 normal = null, MVector3 defaultPosition = null, string defaultParentPath = null) { if (!string.IsNullOrEmpty(info.rename)) { @@ -21,21 +21,23 @@ namespace NewHorizons.Builder.Props go.transform.parent = sector?.transform ?? planetGO.transform; - if (!string.IsNullOrEmpty(info.parentPath)) + var parentPath = info.parentPath ?? defaultParentPath; + + if (!string.IsNullOrEmpty(parentPath)) { - var newParent = go.transform.Find(info.parentPath); + var newParent = go.transform.Find(parentPath); if (newParent != null) { go.transform.parent = newParent.transform; } else { - Logger.LogError($"Cannot find parent object at path: {go.name}/{info.parentPath}"); + Logger.LogError($"Cannot find parent object at path: {go.name}/{parentPath}"); } } - Vector3 pos = (Vector3)(info.position ?? Vector3.zero); - Quaternion rot = Quaternion.identity; + var pos = (Vector3)(info.position ?? defaultPosition ?? Vector3.zero); + var rot = Quaternion.identity; if (info is PropModule.PositionedAndRotatedPropInfo rotInfo) { rot = rotInfo.rotation != null ? Quaternion.Euler(rotInfo.rotation) : Quaternion.identity; @@ -59,18 +61,18 @@ namespace NewHorizons.Builder.Props return go; } - public static GameObject MakeNew(string defaultName, GameObject planetGO, Sector sector, PropModule.PositionedPropInfo info, bool alignToBody = false, MVector3 normal = null) + public static GameObject MakeNew(string defaultName, GameObject planetGO, Sector sector, PropModule.PositionedPropInfo info, bool alignToBody = false, MVector3 normal = null, MVector3 defaultPosition = null, string defaultParentPath = null) { - GameObject go = new GameObject(defaultName); + var go = new GameObject(defaultName); go.SetActive(false); - return MakeFromExisting(go, planetGO, sector, info, alignToBody, normal); + return MakeFromExisting(go, planetGO, sector, info, alignToBody, normal, defaultPosition, defaultParentPath); } - public static GameObject MakeFromPrefab(GameObject prefab, string defaultName, GameObject planetGO, Sector sector, PropModule.PositionedPropInfo info, bool alignToBody = false, MVector3 normal = null) + public static GameObject MakeFromPrefab(GameObject prefab, string defaultName, GameObject planetGO, Sector sector, PropModule.PositionedPropInfo info, bool alignToBody = false, MVector3 normal = null, MVector3 defaultPosition = null, string defaultParentPath = null) { - GameObject go = prefab.InstantiateInactive(); + var go = prefab.InstantiateInactive(); go.name = defaultName; - return MakeFromExisting(go, planetGO, sector, info, alignToBody, normal); + return MakeFromExisting(go, planetGO, sector, info, alignToBody, normal, defaultPosition, defaultParentPath); } } } diff --git a/NewHorizons/Builder/Props/RemoteBuilder.cs b/NewHorizons/Builder/Props/RemoteBuilder.cs index 4fa179f3..2f77e21a 100644 --- a/NewHorizons/Builder/Props/RemoteBuilder.cs +++ b/NewHorizons/Builder/Props/RemoteBuilder.cs @@ -251,37 +251,7 @@ namespace NewHorizons.Builder.Props public static void MakeStone(GameObject go, Sector sector, NomaiRemoteCameraPlatform.ID id, Texture2D decal, PropModule.RemoteInfo.StoneInfo info, IModBehaviour mod) { - var shareStone = _shareStonePrefab.InstantiateInactive(); - - shareStone.name = !string.IsNullOrEmpty(info.rename) ? info.rename : ("ShareStone_" + id.ToString()); - - shareStone.transform.parent = sector?.transform ?? go.transform; - - if (!string.IsNullOrEmpty(info.parentPath)) - { - var newParent = go.transform.Find(info.parentPath); - if (newParent != null) - { - shareStone.transform.parent = newParent; - } - else - { - Logger.LogError($"Cannot find parent object at path: {go.name}/{info.parentPath}"); - } - } - - var pos = (Vector3)(info.position ?? Vector3.zero); - var rot = Quaternion.Euler((Vector3)(info.rotation ?? Vector3.zero)); - if (info.isRelativeToParent) - { - shareStone.transform.localPosition = pos; - shareStone.transform.localRotation = rot; - } - else - { - shareStone.transform.position = go.transform.TransformPoint(pos); - shareStone.transform.rotation = go.transform.TransformRotation(rot); - } + var shareStone = GeneralPropBuilder.MakeFromPrefab(_shareStonePrefab, "ShareStone_" + id.ToString(), go, sector, info); shareStone.GetComponent()._connectedPlatform = id; diff --git a/NewHorizons/Builder/Props/TornadoBuilder.cs b/NewHorizons/Builder/Props/TornadoBuilder.cs index 4ba7fa85..4fbf081d 100644 --- a/NewHorizons/Builder/Props/TornadoBuilder.cs +++ b/NewHorizons/Builder/Props/TornadoBuilder.cs @@ -99,13 +99,7 @@ namespace NewHorizons.Builder.Props private static void MakeTornado(GameObject planetGO, Sector sector, PropModule.TornadoInfo info, Vector3 position, bool downwards) { var prefab = downwards ? _downPrefab.InstantiateInactive() : _upPrefab.InstantiateInactive(); - var tornadoGO = GeneralPropBuilder.MakeFromPrefab(prefab, downwards ? "Tornado_Down" : "Tornado_Up", planetGO, sector, info, true); - - // Override general prop position with randomized default position - if (info.position == null) - { - tornadoGO.transform.position = planetGO.transform.TransformPoint(position); - } + var tornadoGO = GeneralPropBuilder.MakeFromPrefab(prefab, downwards ? "Tornado_Down" : "Tornado_Up", planetGO, sector, info, true, defaultPosition: position); // Add the sound thing before changing the scale var soundGO = _soundPrefab.InstantiateInactive(); diff --git a/NewHorizons/Builder/ShipLog/RevealBuilder.cs b/NewHorizons/Builder/ShipLog/RevealBuilder.cs index 87c2e103..76fff039 100644 --- a/NewHorizons/Builder/ShipLog/RevealBuilder.cs +++ b/NewHorizons/Builder/ShipLog/RevealBuilder.cs @@ -1,3 +1,4 @@ +using NewHorizons.Builder.Props; using NewHorizons.Components.Achievement; using NewHorizons.External.Modules; using OWML.Common; @@ -9,7 +10,7 @@ namespace NewHorizons.Builder.ShipLog { public static void Make(GameObject go, Sector sector, VolumesModule.RevealVolumeInfo info, IModBehaviour mod) { - var newRevealGO = MakeGameObject(go, sector, info, mod); + var newRevealGO = GeneralPropBuilder.MakeNew("Reveal Volume (" + info.revealOn + ")", go, sector, info); switch (info.revealOn) { case VolumesModule.RevealVolumeInfo.RevealVolumeType.Enter: @@ -36,37 +37,6 @@ namespace NewHorizons.Builder.ShipLog return newShape; } - private static GameObject MakeGameObject(GameObject planetGO, Sector sector, VolumesModule.RevealVolumeInfo info, IModBehaviour mod) - { - GameObject revealTriggerVolume = new GameObject("Reveal Volume (" + info.revealOn + ")"); - revealTriggerVolume.SetActive(false); - revealTriggerVolume.transform.parent = sector?.transform ?? planetGO.transform; - - if (!string.IsNullOrEmpty(info.rename)) - { - revealTriggerVolume.name = info.rename; - } - - if (!string.IsNullOrEmpty(info.parentPath)) - { - var newParent = planetGO.transform.Find(info.parentPath); - if (newParent != null) - { - revealTriggerVolume.transform.parent = newParent; - } - else - { - Logger.LogError($"Cannot find parent object at path: {planetGO.name}/{info.parentPath}"); - } - } - - var pos = (Vector3)(info.position ?? Vector3.zero); - if (info.isRelativeToParent) revealTriggerVolume.transform.localPosition = pos; - else revealTriggerVolume.transform.position = planetGO.transform.TransformPoint(pos); - - return revealTriggerVolume; - } - private static void MakeTrigger(GameObject go, Sector sector, VolumesModule.RevealVolumeInfo info, IModBehaviour mod) { var shape = MakeShape(go, info, Shape.CollisionMode.Volume); diff --git a/NewHorizons/Builder/Volumes/AudioVolumeBuilder.cs b/NewHorizons/Builder/Volumes/AudioVolumeBuilder.cs index 8711ddf7..1a5f2f2a 100644 --- a/NewHorizons/Builder/Volumes/AudioVolumeBuilder.cs +++ b/NewHorizons/Builder/Volumes/AudioVolumeBuilder.cs @@ -1,3 +1,4 @@ +using NewHorizons.Builder.Props; using NewHorizons.External.Modules; using NewHorizons.Utility; using OWML.Common; @@ -16,32 +17,7 @@ namespace NewHorizons.Builder.Volumes { public static AudioVolume Make(GameObject planetGO, Sector sector, VolumesModule.AudioVolumeInfo info, IModBehaviour mod) { - var go = new GameObject("AudioVolume"); - go.SetActive(false); - - go.transform.parent = sector?.transform ?? planetGO.transform; - - if (!string.IsNullOrEmpty(info.rename)) - { - go.name = info.rename; - } - - if (!string.IsNullOrEmpty(info.parentPath)) - { - var newParent = planetGO.transform.Find(info.parentPath); - if (newParent != null) - { - go.transform.parent = newParent; - } - else - { - Logger.LogWarning($"Cannot find parent object at path: {planetGO.name}/{info.parentPath}"); - } - } - - var pos = (Vector3)(info.position ?? Vector3.zero); - if (info.isRelativeToParent) go.transform.localPosition = pos; - else go.transform.position = planetGO.transform.TransformPoint(pos); + var go = GeneralPropBuilder.MakeNew("AudioVolume", planetGO, sector, info); go.layer = LayerMask.NameToLayer("AdvancedEffectVolume"); var audioSource = go.AddComponent(); diff --git a/NewHorizons/Builder/Volumes/HazardVolumeBuilder.cs b/NewHorizons/Builder/Volumes/HazardVolumeBuilder.cs index 23a4f1bb..3a12aeb2 100644 --- a/NewHorizons/Builder/Volumes/HazardVolumeBuilder.cs +++ b/NewHorizons/Builder/Volumes/HazardVolumeBuilder.cs @@ -1,3 +1,4 @@ +using NewHorizons.Builder.Props; using NewHorizons.External.Modules; using OWML.Common; using OWML.Utils; @@ -13,32 +14,7 @@ namespace NewHorizons.Builder.Volumes { public static HazardVolume Make(GameObject planetGO, Sector sector, OWRigidbody owrb, VolumesModule.HazardVolumeInfo info, IModBehaviour mod) { - var go = new GameObject("HazardVolume"); - go.SetActive(false); - - go.transform.parent = sector?.transform ?? planetGO.transform; - - if (!string.IsNullOrEmpty(info.rename)) - { - go.name = info.rename; - } - - if (!string.IsNullOrEmpty(info.parentPath)) - { - var newParent = planetGO.transform.Find(info.parentPath); - if (newParent != null) - { - go.transform.parent = newParent; - } - else - { - Logger.LogError($"Cannot find parent object at path: {planetGO.name}/{info.parentPath}"); - } - } - - var pos = (Vector3)(info.position ?? Vector3.zero); - if (info.isRelativeToParent) go.transform.localPosition = pos; - else go.transform.position = planetGO.transform.TransformPoint(pos); + var go = GeneralPropBuilder.MakeNew("HazardVolume", planetGO, sector, info); go.layer = LayerMask.NameToLayer("BasicEffectVolume"); var shape = go.AddComponent(); diff --git a/NewHorizons/Builder/Volumes/NotificationVolumeBuilder.cs b/NewHorizons/Builder/Volumes/NotificationVolumeBuilder.cs index 1e3f07b8..d9d6fa02 100644 --- a/NewHorizons/Builder/Volumes/NotificationVolumeBuilder.cs +++ b/NewHorizons/Builder/Volumes/NotificationVolumeBuilder.cs @@ -1,3 +1,4 @@ +using NewHorizons.Builder.Props; using NewHorizons.External.Modules; using NewHorizons.Utility; using OWML.Common; @@ -16,32 +17,7 @@ namespace NewHorizons.Builder.Volumes { public static NHNotificationVolume Make(GameObject planetGO, Sector sector, VolumesModule.NotificationVolumeInfo info, IModBehaviour mod) { - var go = new GameObject("NotificationVolume"); - go.SetActive(false); - - go.transform.parent = sector?.transform ?? planetGO.transform; - - if (!string.IsNullOrEmpty(info.rename)) - { - go.name = info.rename; - } - - if (!string.IsNullOrEmpty(info.parentPath)) - { - var newParent = planetGO.transform.Find(info.parentPath); - if (newParent != null) - { - go.transform.parent = newParent; - } - else - { - Logger.LogError($"Cannot find parent object at path: {planetGO.name}/{info.parentPath}"); - } - } - - var pos = (Vector3)(info.position ?? Vector3.zero); - if (info.isRelativeToParent) go.transform.localPosition = pos; - else go.transform.position = planetGO.transform.TransformPoint(pos); + var go = GeneralPropBuilder.MakeNew("NotificationVolume", planetGO, sector, info); go.layer = LayerMask.NameToLayer("BasicEffectVolume"); var shape = go.AddComponent(); diff --git a/NewHorizons/Builder/Volumes/VanishVolumeBuilder.cs b/NewHorizons/Builder/Volumes/VanishVolumeBuilder.cs index 2048159f..fd625ccb 100644 --- a/NewHorizons/Builder/Volumes/VanishVolumeBuilder.cs +++ b/NewHorizons/Builder/Volumes/VanishVolumeBuilder.cs @@ -1,3 +1,4 @@ +using NewHorizons.Builder.Props; using NewHorizons.Components; using NewHorizons.External.Modules; using UnityEngine; @@ -9,32 +10,7 @@ namespace NewHorizons.Builder.Volumes { public static TVolume Make(GameObject planetGO, Sector sector, VolumesModule.VanishVolumeInfo info) where TVolume : VanishVolume { - var go = new GameObject(typeof(TVolume).Name); - go.SetActive(false); - - go.transform.parent = sector?.transform ?? planetGO.transform; - - if (!string.IsNullOrEmpty(info.rename)) - { - go.name = info.rename; - } - - if (!string.IsNullOrEmpty(info.parentPath)) - { - var newParent = planetGO.transform.Find(info.parentPath); - if (newParent != null) - { - go.transform.parent = newParent; - } - else - { - Logger.LogError($"Cannot find parent object at path: {planetGO.name}/{info.parentPath}"); - } - } - - var pos = (Vector3)(info.position ?? Vector3.zero); - if (info.isRelativeToParent) go.transform.localPosition = pos; - else go.transform.position = planetGO.transform.TransformPoint(pos); + var go = GeneralPropBuilder.MakeNew(typeof(TVolume).Name, planetGO, sector, info); go.layer = LayerMask.NameToLayer("BasicEffectVolume"); var collider = go.AddComponent();