diff --git a/NewHorizons/Builder/Body/ProxyBuilder.cs b/NewHorizons/Builder/Body/ProxyBuilder.cs index 49befd13..87888e45 100644 --- a/NewHorizons/Builder/Body/ProxyBuilder.cs +++ b/NewHorizons/Builder/Body/ProxyBuilder.cs @@ -151,7 +151,7 @@ namespace NewHorizons.Builder.Body { foreach (var detailInfo in body.Config.Props.proxyDetails) { - DetailBuilder.Make(newProxy, null, body.Config, body.Mod, detailInfo); + DetailBuilder.Make(newProxy, null, body.Mod, detailInfo); } } diff --git a/NewHorizons/Builder/Props/DetailBuilder.cs b/NewHorizons/Builder/Props/DetailBuilder.cs index fa460991..fd2e8201 100644 --- a/NewHorizons/Builder/Props/DetailBuilder.cs +++ b/NewHorizons/Builder/Props/DetailBuilder.cs @@ -20,29 +20,80 @@ namespace NewHorizons.Builder.Props return detailInfoToCorrespondingSpawnedGameObject[detail]; } - public static void Make(GameObject go, Sector sector, PlanetConfig config, IModBehaviour mod, PropModule.DetailInfo detail) + public static void RegisterDetailInfo(PropModule.DetailInfo detail, GameObject detailGO) { - GameObject detailGO = null; + detailInfoToCorrespondingSpawnedGameObject[detail] = detailGO; + } + public static GameObject Make(GameObject go, Sector sector, IModBehaviour mod, PropModule.DetailInfo detail) + { + GameObject prefab; if (detail.assetBundle != null) { - var prefab = AssetBundleUtilities.LoadPrefab(detail.assetBundle, detail.path, mod); + // Shouldn't happen + if (mod == null) return null; - detailGO = MakeDetail(go, sector, prefab, detail); + prefab = AssetBundleUtilities.LoadPrefab(detail.assetBundle, detail.path, mod); } else { - var prefab = SearchUtilities.Find(detail.path); - if (prefab == null) Logger.LogError($"Couldn't find detail {detail.path}"); - else detailGO = MakeDetail(go, sector, prefab, detail); + prefab = SearchUtilities.Find(detail.path); } - if (detailGO == null) return; + if (prefab == null) Logger.LogError($"Couldn't find detail {detail.path}"); + + return MakeDetail(go, sector, prefab, detail); + } + + public static GameObject MakeDetail(GameObject go, Sector sector, GameObject prefab, PropModule.DetailInfo detail) + { + if (prefab == null) return null; + + GameObject prop = prefab.InstantiateInactive(); + prop.name = prefab.name; + prop.transform.parent = sector?.transform ?? go.transform; + + StreamingHandler.SetUpStreaming(prop, sector); + + var isTorch = prop.GetComponent() != null; + + foreach (var component in prop.GetComponentsInChildren(true)) + { + if (sector == null) + { + if (FixUnsectoredComponent(component)) continue; + } + else FixSectoredComponent(component, sector, isTorch); + + FixComponent(component, go, prefab.name); + } + + prop.transform.position = detail.position == null ? go.transform.position : go.transform.TransformPoint(detail.position); + + Quaternion rot = detail.rotation == null ? Quaternion.identity : Quaternion.Euler(detail.rotation); + + if (detail.alignToNormal) + { + // Apply the rotation after aligning it with normal + var up = go.transform.InverseTransformPoint(prop.transform.position).normalized; + prop.transform.rotation = Quaternion.FromToRotation(Vector3.up, up); + prop.transform.rotation *= rot; + } + else + { + prop.transform.rotation = go.transform.TransformRotation(rot); + } + + prop.transform.localScale = detail.scale != 0 ? Vector3.one * detail.scale : prefab.transform.localScale; + + prop.SetActive(true); + + if (prop == null) return null; if (detail.removeChildren != null) { - var detailPath = detailGO.transform.GetPath(); - var transforms = detailGO.GetComponentsInChildren(true); + var detailPath = prop.transform.GetPath(); + var transforms = prop.GetComponentsInChildren(true); foreach (var childPath in detail.removeChildren) { // Multiple children can have the same path so we delete all that match @@ -62,12 +113,12 @@ namespace NewHorizons.Builder.Props if (detail.removeComponents) { // Just swap all the children to a new game object - var newDetailGO = new GameObject(detailGO.name); - newDetailGO.transform.position = detailGO.transform.position; - newDetailGO.transform.parent = detailGO.transform.parent; + var newDetailGO = new GameObject(prop.name); + newDetailGO.transform.position = prop.transform.position; + newDetailGO.transform.parent = prop.transform.parent; // Can't modify parents while looping through children bc idk var children = new List(); - foreach (Transform child in detailGO.transform) + foreach (Transform child in prop.transform) { children.Add(child); } @@ -75,13 +126,13 @@ namespace NewHorizons.Builder.Props { child.parent = newDetailGO.transform; } - GameObject.Destroy(detailGO); - detailGO = newDetailGO; + GameObject.Destroy(prop); + prop = newDetailGO; } if (detail.rename != null) { - detailGO.name = detail.rename; + prop.name = detail.rename; } if (!string.IsNullOrEmpty(detail.parentPath)) @@ -89,56 +140,10 @@ namespace NewHorizons.Builder.Props var newParent = go.transform.Find(detail.parentPath); if (newParent != null) { - detailGO.transform.parent = newParent.transform; + prop.transform.parent = newParent.transform; } } - detailInfoToCorrespondingSpawnedGameObject[detail] = detailGO; - } - - public static GameObject MakeDetail(GameObject planetGO, Sector sector, GameObject prefab, PropModule.DetailInfo info) - { - if (prefab == null) return null; - - GameObject prop = prefab.InstantiateInactive(); - prop.name = prefab.name; - prop.transform.parent = sector?.transform ?? planetGO.transform; - - StreamingHandler.SetUpStreaming(prop, sector); - - var isTorch = prop.GetComponent() != null; - - foreach (var component in prop.GetComponentsInChildren(true)) - { - if (sector == null) - { - if (FixUnsectoredComponent(component)) continue; - } - else FixSectoredComponent(component, sector, isTorch); - - FixComponent(component, planetGO, prefab.name); - } - - prop.transform.position = info.position == null ? planetGO.transform.position : planetGO.transform.TransformPoint(info.position); - - Quaternion rot = info.rotation == null ? Quaternion.identity : Quaternion.Euler(info.rotation); - - if (info.alignToNormal) - { - // Apply the rotation after aligning it with normal - var up = planetGO.transform.InverseTransformPoint(prop.transform.position).normalized; - prop.transform.rotation = Quaternion.FromToRotation(Vector3.up, up); - prop.transform.rotation *= rot; - } - else - { - prop.transform.rotation = planetGO.transform.TransformRotation(rot); - } - - prop.transform.localScale = info.scale != 0 ? Vector3.one * info.scale : prefab.transform.localScale; - - prop.SetActive(true); - return prop; } diff --git a/NewHorizons/Builder/Props/PropBuildManager.cs b/NewHorizons/Builder/Props/PropBuildManager.cs index d90e1776..8f442041 100644 --- a/NewHorizons/Builder/Props/PropBuildManager.cs +++ b/NewHorizons/Builder/Props/PropBuildManager.cs @@ -29,7 +29,8 @@ namespace NewHorizons.Builder.Props { try { - DetailBuilder.Make(go, sector, config, mod, detail); + var detailGO = DetailBuilder.Make(go, sector, mod, detail); + DetailBuilder.RegisterDetailInfo(detail, detailGO); } catch (Exception ex) {