From 6caa2d8991ca8808bc37c0aafc5c935593e57d6f Mon Sep 17 00:00:00 2001 From: Noah Pilarski Date: Sat, 10 Sep 2022 23:09:56 -0400 Subject: [PATCH] Get prefabs from solar system and then head back to eye --- .../Builder/Atmosphere/AtmosphereBuilder.cs | 17 +- .../Builder/Atmosphere/CloudsBuilder.cs | 52 +++++- .../Builder/Atmosphere/EffectsBuilder.cs | 15 +- NewHorizons/Builder/Atmosphere/FogBuilder.cs | 37 ++-- .../Builder/Atmosphere/VolumesBuilder.cs | 33 ++-- .../Builder/Body/BrambleDimensionBuilder.cs | 36 +++- NewHorizons/Builder/Body/CloakBuilder.cs | 30 +++- NewHorizons/Builder/Body/CometTailBuilder.cs | 11 +- NewHorizons/Builder/Body/FunnelBuilder.cs | 27 ++- NewHorizons/Builder/Body/GeometryBuilder.cs | 14 +- NewHorizons/Builder/Body/LavaBuilder.cs | 19 +- NewHorizons/Builder/Body/SandBuilder.cs | 28 ++- .../Builder/Body/SingularityBuilder.cs | 65 +++++-- NewHorizons/Builder/Body/StarBuilder.cs | 64 +++++-- .../Builder/Body/SupernovaEffectBuilder.cs | 21 ++- NewHorizons/Builder/Body/WaterBuilder.cs | 36 +++- .../Builder/General/DetectorBuilder.cs | 114 ++++++------ .../Builder/General/RigidBodyBuilder.cs | 2 +- .../Builder/General/SpawnPointBuilder.cs | 47 ++--- .../Builder/Props/BrambleNodeBuilder.cs | 14 +- NewHorizons/Builder/Props/GeyserBuilder.cs | 11 +- NewHorizons/Builder/Props/NomaiTextBuilder.cs | 104 ++++------- .../Builder/Props/ProjectionBuilder.cs | 91 +++++++--- NewHorizons/Builder/Props/RaftBuilder.cs | 16 +- NewHorizons/Builder/Props/RemoteBuilder.cs | 166 ++++++++++-------- NewHorizons/Builder/Props/TornadoBuilder.cs | 38 ++-- NewHorizons/Builder/Props/VolcanoBuilder.cs | 11 +- NewHorizons/Main.cs | 47 +++++ .../Patches/EyeOfTheUniversePatches.cs | 9 +- NewHorizons/Utility/NewHorizonExtensions.cs | 23 +++ 30 files changed, 788 insertions(+), 410 deletions(-) diff --git a/NewHorizons/Builder/Atmosphere/AtmosphereBuilder.cs b/NewHorizons/Builder/Atmosphere/AtmosphereBuilder.cs index 4bae5764..cefbe67c 100644 --- a/NewHorizons/Builder/Atmosphere/AtmosphereBuilder.cs +++ b/NewHorizons/Builder/Atmosphere/AtmosphereBuilder.cs @@ -1,6 +1,7 @@ using NewHorizons.External.Modules; using NewHorizons.Utility; using System.Collections.Generic; +using System.Linq; using UnityEngine; namespace NewHorizons.Builder.Atmosphere { @@ -18,18 +19,26 @@ namespace NewHorizons.Builder.Atmosphere Skys.Clear(); } + private static GameObject _atmospherePrefab; + private static GameObject _proxyAtmospherePrefab; + + internal static void InitPrefabs() + { + if (_atmospherePrefab == null) _atmospherePrefab = SearchUtilities.Find("TimberHearth_Body/Atmosphere_TH/AtmoSphere").InstantiateInactive().Rename("Atmosphere").DontDestroyOnLoad(); + if (_proxyAtmospherePrefab == null) _proxyAtmospherePrefab = GameObject.FindObjectOfType()._proxies.FirstOrDefault(apt => apt.astroName == AstroObject.Name.TimberHearth).proxyPrefab.FindChild("Atmosphere_TH/Atmosphere_LOD3").InstantiateInactive().Rename("ProxyAtmosphere").DontDestroyOnLoad(); + } + public static GameObject Make(GameObject planetGO, Sector sector, AtmosphereModule atmosphereModule, float surfaceSize, bool proxy = false) { + InitPrefabs(); + GameObject atmoGO = new GameObject("Atmosphere"); atmoGO.SetActive(false); atmoGO.transform.parent = sector?.transform ?? planetGO.transform; if (atmosphereModule.useAtmosphereShader) { - GameObject prefab; - if (proxy) prefab = (SearchUtilities.Find("TimberHearth_DistantProxy", false) ?? SearchUtilities.Find("TimberHearth_DistantProxy(Clone)", false))? - .FindChild("Atmosphere_TH/Atmosphere_LOD3"); - else prefab = SearchUtilities.Find("TimberHearth_Body/Atmosphere_TH/AtmoSphere"); + GameObject prefab = proxy ? _proxyAtmospherePrefab : _atmospherePrefab; if (prefab != null) { diff --git a/NewHorizons/Builder/Atmosphere/CloudsBuilder.cs b/NewHorizons/Builder/Atmosphere/CloudsBuilder.cs index d5b15f89..da8f2f81 100644 --- a/NewHorizons/Builder/Atmosphere/CloudsBuilder.cs +++ b/NewHorizons/Builder/Atmosphere/CloudsBuilder.cs @@ -5,12 +5,18 @@ using OWML.Common; using System; using UnityEngine; using Logger = NewHorizons.Utility.Logger; +using System.Collections.Generic; +using Tessellation; + namespace NewHorizons.Builder.Atmosphere { public static class CloudsBuilder { private static Material[] _gdCloudMaterials; private static Material[] _qmCloudMaterials; + private static Material[] _qmBottomMaterials; + private static Mesh _gdTopCloudMesh; + private static Tessellation.MeshGroup _qmBottomMeshGroup; private static Material _transparentCloud; private static GameObject _lightningPrefab; private static Texture2D _colorRamp; @@ -21,10 +27,35 @@ namespace NewHorizons.Builder.Atmosphere private static readonly int CapTex = Shader.PropertyToID("_CapTex"); private static readonly int Smoothness = Shader.PropertyToID("_Glossiness"); + internal static void InitPrefabs() + { + if (_lightningPrefab == null) _lightningPrefab = SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Clouds_GD/LightningGenerator_GD").InstantiateInactive().Rename("LightningGenerator").DontDestroyOnLoad(); + if (_colorRamp == null) _colorRamp = ImageUtilities.GetTexture(Main.Instance, "Assets/textures/Clouds_Bottom_ramp.png"); + if (_gdTopCloudMesh == null) _gdTopCloudMesh = SearchUtilities.Find("CloudsTopLayer_GD").GetComponent().mesh.DontDestroyOnLoad(); + if (_gdCloudMaterials == null) _gdCloudMaterials = SearchUtilities.Find("CloudsTopLayer_GD").GetComponent().sharedMaterials.MakePrefabMaterials(); + if (_qmCloudMaterials == null) _qmCloudMaterials = SearchUtilities.Find("CloudsTopLayer_QM").GetComponent().sharedMaterials.MakePrefabMaterials(); + if (_qmBottomMaterials == null) _qmBottomMaterials = SearchUtilities.Find("CloudsBottomLayer_QM").GetComponent().sharedMaterials.MakePrefabMaterials(); + if (_qmBottomMeshGroup == null) + { + var originalMeshGroup = SearchUtilities.Find("CloudsBottomLayer_QM").GetComponent().tessellationMeshGroup; + _qmBottomMeshGroup = ScriptableObject.CreateInstance().Rename("BottomClouds").DontDestroyOnLoad(); + var variants = new List(); + foreach (var variant in originalMeshGroup.variants) + { + var mesh = new Mesh(); + mesh.CopyPropertiesFrom(variant); + mesh.name = variant.name; + mesh.DontDestroyOnLoad(); + variants.Add(mesh); + } + _qmBottomMeshGroup.variants = variants.ToArray(); + } + if (_transparentCloud == null) _transparentCloud = Main.NHAssetBundle.LoadAsset("Assets/Resources/TransparentCloud.mat"); + } + public static void Make(GameObject planetGO, Sector sector, AtmosphereModule atmo, bool cloaked, IModBehaviour mod) { - if (_lightningPrefab == null) _lightningPrefab = SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Clouds_GD/LightningGenerator_GD"); - if (_colorRamp == null) _colorRamp = ImageUtilities.GetTexture(Main.Instance, "Assets/textures/Clouds_Bottom_ramp.png"); + InitPrefabs(); GameObject cloudsMainGO = new GameObject("Clouds"); cloudsMainGO.SetActive(false); @@ -46,8 +77,8 @@ namespace NewHorizons.Builder.Atmosphere cloudsBottomGO.transform.localScale = Vector3.one * atmo.clouds.innerCloudRadius; TessellatedSphereRenderer bottomTSR = cloudsBottomGO.AddComponent(); - bottomTSR.tessellationMeshGroup = SearchUtilities.Find("CloudsBottomLayer_QM").GetComponent().tessellationMeshGroup; - var bottomTSRMaterials = SearchUtilities.Find("CloudsBottomLayer_QM").GetComponent().sharedMaterials; + bottomTSR.tessellationMeshGroup = _qmBottomMeshGroup; + var bottomTSRMaterials = _qmBottomMaterials; // If they set a colour apply it to all the materials else keep the default QM one if (atmo.clouds.tint != null) @@ -118,6 +149,8 @@ namespace NewHorizons.Builder.Atmosphere public static CloudLightningGenerator MakeLightning(GameObject rootObject, Sector sector, AtmosphereModule atmo, bool noAudio = false) { + InitPrefabs(); + var lightning = _lightningPrefab.InstantiateInactive(); lightning.name = "LightningGenerator"; lightning.transform.parent = rootObject.transform; @@ -149,6 +182,8 @@ namespace NewHorizons.Builder.Atmosphere public static GameObject MakeTopClouds(GameObject rootObject, AtmosphereModule atmo, IModBehaviour mod) { + InitPrefabs(); + Texture2D image, cap, ramp; try @@ -173,12 +208,10 @@ namespace NewHorizons.Builder.Atmosphere cloudsTopGO.transform.localScale = Vector3.one * atmo.clouds.outerCloudRadius; MeshFilter topMF = cloudsTopGO.AddComponent(); - topMF.mesh = SearchUtilities.Find("CloudsTopLayer_GD").GetComponent().mesh; + topMF.mesh = _gdTopCloudMesh; MeshRenderer topMR = cloudsTopGO.AddComponent(); - if (_gdCloudMaterials == null) _gdCloudMaterials = SearchUtilities.Find("CloudsTopLayer_GD").GetComponent().sharedMaterials; - if (_qmCloudMaterials == null) _qmCloudMaterials = SearchUtilities.Find("CloudsTopLayer_QM").GetComponent().sharedMaterials; Material[] prefabMaterials = atmo.clouds.cloudsPrefab == CloudPrefabType.GiantsDeep ? _gdCloudMaterials : _qmCloudMaterials; var tempArray = new Material[2]; @@ -231,6 +264,8 @@ namespace NewHorizons.Builder.Atmosphere public static GameObject MakeTransparentClouds(GameObject rootObject, AtmosphereModule atmo, IModBehaviour mod, bool isProxy = false) { + InitPrefabs(); + Texture2D image; try @@ -249,10 +284,9 @@ namespace NewHorizons.Builder.Atmosphere cloudsTransparentGO.transform.localScale = Vector3.one * atmo.clouds.outerCloudRadius; MeshFilter filter = cloudsTransparentGO.AddComponent(); - filter.mesh = SearchUtilities.Find("CloudsTopLayer_GD").GetComponent().mesh; + filter.mesh = _gdTopCloudMesh; MeshRenderer renderer = cloudsTransparentGO.AddComponent(); - if (_transparentCloud == null) _transparentCloud = Main.NHAssetBundle.LoadAsset("Assets/Resources/TransparentCloud.mat"); var material = new Material(_transparentCloud); material.name = "TransparentClouds_" + image.name; material.SetTexture(MainTex, image); diff --git a/NewHorizons/Builder/Atmosphere/EffectsBuilder.cs b/NewHorizons/Builder/Atmosphere/EffectsBuilder.cs index 6394e02f..f177dd57 100644 --- a/NewHorizons/Builder/Atmosphere/EffectsBuilder.cs +++ b/NewHorizons/Builder/Atmosphere/EffectsBuilder.cs @@ -6,8 +6,19 @@ namespace NewHorizons.Builder.Atmosphere { public static class EffectsBuilder { + private static GameObject _rainEmitterPrefab; + private static GameObject _snowEmitterPrefab; + + internal static void InitPrefabs() + { + if (_rainEmitterPrefab == null) _rainEmitterPrefab = SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Sector_GDInterior/Effects_GDInterior/Effects_GD_Rain").InstantiateInactive().Rename("Prefab_Effects_Rain").DontDestroyOnLoad(); + if (_snowEmitterPrefab == null) _snowEmitterPrefab = SearchUtilities.Find("BrittleHollow_Body/Sector_BH/Effects_BH/Effects_BH_Snowflakes").InstantiateInactive().Rename("Prefab_Effects_Snowflakes").DontDestroyOnLoad(); + } + public static void Make(GameObject planetGO, Sector sector, PlanetConfig config, float surfaceSize) { + InitPrefabs(); + GameObject effectsGO = new GameObject("Effects"); effectsGO.SetActive(false); effectsGO.transform.parent = sector?.transform ?? planetGO.transform; @@ -34,7 +45,7 @@ namespace NewHorizons.Builder.Atmosphere if (config.Atmosphere.hasRain) { - var rainGO = GameObject.Instantiate(SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Sector_GDInterior/Effects_GDInterior/Effects_GD_Rain"), effectsGO.transform); + var rainGO = GameObject.Instantiate(_rainEmitterPrefab, effectsGO.transform); rainGO.name = "RainEmitter"; rainGO.transform.position = planetGO.transform.position; @@ -58,7 +69,7 @@ namespace NewHorizons.Builder.Atmosphere snowGO.transform.position = planetGO.transform.position; for (int i = 0; i < 5; i++) { - var snowEmitter = GameObject.Instantiate(SearchUtilities.Find("BrittleHollow_Body/Sector_BH/Effects_BH/Effects_BH_Snowflakes"), snowGO.transform); + var snowEmitter = GameObject.Instantiate(_snowEmitterPrefab, snowGO.transform); snowEmitter.name = "SnowEmitter"; snowEmitter.transform.position = planetGO.transform.position; diff --git a/NewHorizons/Builder/Atmosphere/FogBuilder.cs b/NewHorizons/Builder/Atmosphere/FogBuilder.cs index 5dd67381..1dbb6fce 100644 --- a/NewHorizons/Builder/Atmosphere/FogBuilder.cs +++ b/NewHorizons/Builder/Atmosphere/FogBuilder.cs @@ -14,31 +14,39 @@ namespace NewHorizons.Builder.Atmosphere private static readonly int DensityExponent = Shader.PropertyToID("_DensityExp"); private static readonly int ColorRampTexture = Shader.PropertyToID("_ColorRampTex"); - public static PlanetaryFogController Make(GameObject planetGO, Sector sector, AtmosphereModule atmo) + private static Texture3D _lookupTexture; + private static Mesh _dbImpostorMesh; + private static Material[] _dbImpostorMaterials; + + internal static void InitPrefabs() { if (_ramp == null) _ramp = ImageUtilities.GetTexture(Main.Instance, "Assets/textures/FogColorRamp.png"); + // Going to copy from dark bramble + if (_lookupTexture == null) _lookupTexture = SearchUtilities.Find("DarkBramble_Body/Atmosphere_DB/FogSphere_DB")?.GetComponent()?.fogLookupTexture.DontDestroyOnLoad(); + if (_dbImpostorMesh == null) _dbImpostorMesh = SearchUtilities.Find("DarkBramble_Body/Atmosphere_DB/FogLOD").GetComponent().mesh.DontDestroyOnLoad(); + if (_dbImpostorMaterials == null) _dbImpostorMaterials = SearchUtilities.Find("DarkBramble_Body/Atmosphere_DB/FogLOD").GetComponent().sharedMaterials.MakePrefabMaterials(); + } + + public static PlanetaryFogController Make(GameObject planetGO, Sector sector, AtmosphereModule atmo) + { + InitPrefabs(); + GameObject fogGO = new GameObject("FogSphere"); fogGO.SetActive(false); fogGO.transform.parent = sector?.transform ?? planetGO.transform; fogGO.transform.localScale = Vector3.one * atmo.fogSize; - // Going to copy from dark bramble - var dbFog = SearchUtilities.Find("DarkBramble_Body/Atmosphere_DB/FogLOD"); - if (dbFog == null) return null; - var dbPlanetaryFogController = SearchUtilities.Find("DarkBramble_Body/Atmosphere_DB/FogSphere_DB")?.GetComponent(); - if (dbPlanetaryFogController == null) return null; - MeshFilter MF = fogGO.AddComponent(); - MF.mesh = dbFog.GetComponent().mesh; + MF.mesh = _dbImpostorMesh; MeshRenderer MR = fogGO.AddComponent(); - MR.materials = dbFog.GetComponent().materials; + MR.materials = _dbImpostorMaterials; MR.allowOcclusionWhenDynamic = true; PlanetaryFogController PFC = fogGO.AddComponent(); PFC._fogImpostor = MR; - PFC.fogLookupTexture = dbPlanetaryFogController.fogLookupTexture; + PFC.fogLookupTexture = _lookupTexture; PFC.fogRadius = atmo.fogSize; PFC.lodFadeDistance = PFC.fogRadius * 0.5f; PFC.fogDensity = atmo.fogDensity; @@ -65,21 +73,18 @@ namespace NewHorizons.Builder.Atmosphere public static Renderer MakeProxy(GameObject proxyGO, AtmosphereModule atmo) { - if (_ramp == null) _ramp = ImageUtilities.GetTexture(Main.Instance, "Assets/textures/FogColorRamp.png"); + InitPrefabs(); GameObject fogGO = new GameObject("FogSphere"); fogGO.SetActive(false); fogGO.transform.parent = proxyGO.transform; fogGO.transform.localScale = Vector3.one * atmo.fogSize; - var fog = (SearchUtilities.Find("TimberHearth_DistantProxy", false) ?? SearchUtilities.Find("TimberHearth_DistantProxy(Clone)", false))?.FindChild("Atmosphere_TH/FogSphere"); - if (fog == null) return null; - MeshFilter MF = fogGO.AddComponent(); - MF.mesh = fog.GetComponent().mesh; + MF.mesh = _dbImpostorMesh; MeshRenderer MR = fogGO.AddComponent(); - MR.materials = fog.GetComponent().materials; + MR.materials = _dbImpostorMaterials; MR.allowOcclusionWhenDynamic = true; var colorRampTexture = atmo.fogTint == null ? _ramp : ImageUtilities.TintImage(_ramp, atmo.fogTint.ToColor()); diff --git a/NewHorizons/Builder/Atmosphere/VolumesBuilder.cs b/NewHorizons/Builder/Atmosphere/VolumesBuilder.cs index ac924555..aae39106 100644 --- a/NewHorizons/Builder/Atmosphere/VolumesBuilder.cs +++ b/NewHorizons/Builder/Atmosphere/VolumesBuilder.cs @@ -7,8 +7,19 @@ namespace NewHorizons.Builder.Atmosphere { private static readonly int FogColor = Shader.PropertyToID("_FogColor"); + private static Material _gdMaterial; + private static Material _gdCloudMaterial; + + internal static void InitPrefabs() + { + if (_gdMaterial == null) _gdMaterial = new Material(SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Volumes_GD/RulesetVolumes_GD").GetComponent()._material).DontDestroyOnLoad(); + if (_gdCloudMaterial == null) _gdCloudMaterial = new Material(SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Volumes_GD/RulesetVolumes_GD").GetComponent()._cloudMaterial).DontDestroyOnLoad(); + } + public static void Make(GameObject planetGO, OWRigidbody owrb, PlanetConfig config, float sphereOfInfluence) { + InitPrefabs(); + var innerRadius = config.Base.surfaceSize; GameObject volumesGO = new GameObject("Volumes"); @@ -37,21 +48,17 @@ namespace NewHorizons.Builder.Atmosphere rulesetGO.AddComponent(); - var gdRuleset = SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Volumes_GD/RulesetVolumes_GD")?.GetComponent(); - if (gdRuleset != null) + EffectRuleset ER = rulesetGO.AddComponent(); + ER._type = EffectRuleset.BubbleType.Underwater; + + ER._material = _gdMaterial; + + var cloudMaterial = new Material(_gdCloudMaterial); + if (config.Atmosphere?.clouds?.tint != null) { - EffectRuleset ER = rulesetGO.AddComponent(); - ER._type = EffectRuleset.BubbleType.Underwater; - - ER._material = gdRuleset._material; - - var cloudMaterial = new Material(gdRuleset._cloudMaterial); - if (config.Atmosphere?.clouds?.tint != null) - { - cloudMaterial.SetColor(FogColor, config.Atmosphere.clouds.tint.ToColor()); - } - ER._cloudMaterial = cloudMaterial; + cloudMaterial.SetColor(FogColor, config.Atmosphere.clouds.tint.ToColor()); } + ER._cloudMaterial = cloudMaterial; if (config.Base.zeroGravityRadius != 0) { diff --git a/NewHorizons/Builder/Body/BrambleDimensionBuilder.cs b/NewHorizons/Builder/Body/BrambleDimensionBuilder.cs index 18927c1f..babc53aa 100644 --- a/NewHorizons/Builder/Body/BrambleDimensionBuilder.cs +++ b/NewHorizons/Builder/Body/BrambleDimensionBuilder.cs @@ -47,23 +47,43 @@ namespace NewHorizons.Builder.Body _unpairedDimensions = new(); } + private static GameObject _atmosphere; + private static GameObject _volumes; + private static GameObject _effects; + private static GameObject _geometry; + private static GameObject _exitWarps; + private static GameObject _repelVolume; + private static Material _material; + + internal static void InitPrefabs() + { + if (_atmosphere == null) _atmosphere = SearchUtilities.Find("DB_HubDimension_Body/Sector_HubDimension/Atmosphere_HubDimension").InstantiateInactive().Rename("Prefab_Bramble_Atmosphere").DontDestroyOnLoad(); + if (_volumes == null) _volumes = SearchUtilities.Find("DB_HubDimension_Body/Sector_HubDimension/Volumes_HubDimension").InstantiateInactive().Rename("Prefab_Bramble_Volumes").DontDestroyOnLoad(); + if (_effects == null) _effects = SearchUtilities.Find("DB_HubDimension_Body/Sector_HubDimension/Effects_HubDimension").InstantiateInactive().Rename("Prefab_Bramble_Effects").DontDestroyOnLoad(); + if (_geometry == null) _geometry = SearchUtilities.Find("DB_HubDimension_Body/Sector_HubDimension/Geometry_HubDimension").InstantiateInactive().Rename("Prefab_Bramble_Geometry").DontDestroyOnLoad(); + if (_exitWarps == null) _exitWarps = SearchUtilities.Find("DB_HubDimension_Body/Sector_HubDimension/Interactables_HubDimension/OuterWarp_Hub").InstantiateInactive().Rename("Prefab_Bramble_OuterWarp").DontDestroyOnLoad(); + if (_repelVolume == null) _repelVolume = SearchUtilities.Find("DB_HubDimension_Body/BrambleRepelVolume").InstantiateInactive().Rename("Prefab_Bramble_RepelVolume").DontDestroyOnLoad(); + if (_material == null) _material = new Material(GameObject.Find("DB_PioneerDimension_Body/Sector_PioneerDimension").GetComponent()._material).DontDestroyOnLoad(); + } + public static GameObject Make(NewHorizonsBody body, GameObject go, NHAstroObject ao, Sector sector, OWRigidbody owRigidBody) { + InitPrefabs(); + var config = body.Config.Bramble.dimension; ao.IsDimension = true; sector._name = Sector.Name.BrambleDimension; - var atmo = SearchUtilities.Find("DB_HubDimension_Body/Sector_HubDimension/Atmosphere_HubDimension").InstantiateInactive(); - var volumes = SearchUtilities.Find("DB_HubDimension_Body/Sector_HubDimension/Volumes_HubDimension").InstantiateInactive(); - var effects = SearchUtilities.Find("DB_HubDimension_Body/Sector_HubDimension/Effects_HubDimension").InstantiateInactive(); + var atmo = _atmosphere.InstantiateInactive(); + var volumes = _volumes.InstantiateInactive(); + var effects = _effects.InstantiateInactive(); - var prefab = SearchUtilities.Find("DB_HubDimension_Body/Sector_HubDimension/Geometry_HubDimension"); var detailInfo = new PropModule.DetailInfo(); - var geometry = DetailBuilder.Make(go, sector, prefab, detailInfo); + var geometry = DetailBuilder.Make(go, sector, _geometry, detailInfo); - var exitWarps = SearchUtilities.Find("DB_HubDimension_Body/Sector_HubDimension/Interactables_HubDimension/OuterWarp_Hub").InstantiateInactive(); - var repelVolume = SearchUtilities.Find("DB_HubDimension_Body/BrambleRepelVolume").InstantiateInactive(); + var exitWarps = _exitWarps.InstantiateInactive(); + var repelVolume = _repelVolume.InstantiateInactive(); atmo.name = "Atmosphere"; atmo.transform.parent = sector.transform; @@ -124,7 +144,7 @@ namespace NewHorizons.Builder.Body effectRuleset._underwaterDistortScale = 0.001f; effectRuleset._underwaterMaxDistort = 0.1f; effectRuleset._underwaterMinDistort = 0.005f; - effectRuleset._material = GameObject.Find("DB_PioneerDimension_Body/Sector_PioneerDimension").GetComponent()._material; // TODO: cache this + effectRuleset._material = _material; var antiTravelMusicRuleset = sector.gameObject.AddComponent(); antiTravelMusicRuleset._attachedBody = owRigidBody; diff --git a/NewHorizons/Builder/Body/CloakBuilder.cs b/NewHorizons/Builder/Body/CloakBuilder.cs index 8a6e518d..6373ddb8 100644 --- a/NewHorizons/Builder/Body/CloakBuilder.cs +++ b/NewHorizons/Builder/Body/CloakBuilder.cs @@ -9,18 +9,32 @@ namespace NewHorizons.Builder.Body { public static class CloakBuilder { + private static GameObject _prefab; + + internal static void InitPrefab() + { + if (_prefab == null) + { + _prefab = SearchUtilities.Find("RingWorld_Body/CloakingField_IP")?.InstantiateInactive()?.Rename("CloakingField")?.DontDestroyOnLoad(); + if (_prefab == null) + { + Logger.LogWarning($"Tried to make a cloak but couldn't. Do you have the DLC installed?"); + return; + } + else + _prefab.AddComponent()._destroyOnDLCNotOwned = true; + } + } + public static void Make(GameObject planetGO, Sector sector, OWRigidbody OWRB, CloakModule module, bool keepReferenceFrame, IModBehaviour mod) { + InitPrefab(); + + if (_prefab == null) return; + var radius = module.radius; - var cloak = SearchUtilities.Find("RingWorld_Body/CloakingField_IP"); - if (cloak == null) - { - Logger.LogWarning($"Tried to make a cloak but couldn't. Do you have the DLC installed?"); - return; - } - - var newCloak = GameObject.Instantiate(cloak, sector?.transform ?? planetGO.transform); + var newCloak = GameObject.Instantiate(_prefab, sector?.transform ?? planetGO.transform); newCloak.transform.position = planetGO.transform.position; newCloak.transform.name = "CloakingField"; newCloak.transform.localScale = Vector3.one * radius; diff --git a/NewHorizons/Builder/Body/CometTailBuilder.cs b/NewHorizons/Builder/Body/CometTailBuilder.cs index 5980b2d1..92267af6 100644 --- a/NewHorizons/Builder/Body/CometTailBuilder.cs +++ b/NewHorizons/Builder/Body/CometTailBuilder.cs @@ -5,9 +5,18 @@ namespace NewHorizons.Builder.Body { public static class CometTailBuilder { + private static GameObject _tailPrefab; + + internal static void InitPrefab() + { + if (_tailPrefab == null) _tailPrefab = SearchUtilities.Find("Comet_Body/Sector_CO/Effects_CO/Effects_CO_TailMeshes").InstantiateInactive().Rename("Prefab_CO_Tail").DontDestroyOnLoad(); + } + public static void Make(GameObject planetGO, Sector sector, PlanetConfig config) { - var cometTail = GameObject.Instantiate(SearchUtilities.Find("Comet_Body/Sector_CO/Effects_CO/Effects_CO_TailMeshes"), sector?.transform ?? planetGO.transform); + InitPrefab(); + + var cometTail = GameObject.Instantiate(_tailPrefab, sector?.transform ?? planetGO.transform); cometTail.transform.position = planetGO.transform.position; cometTail.name = "CometTail"; cometTail.transform.localScale = Vector3.one * config.Base.surfaceSize / 110; diff --git a/NewHorizons/Builder/Body/FunnelBuilder.cs b/NewHorizons/Builder/Body/FunnelBuilder.cs index acc75bf7..70cc97fe 100644 --- a/NewHorizons/Builder/Body/FunnelBuilder.cs +++ b/NewHorizons/Builder/Body/FunnelBuilder.cs @@ -13,8 +13,25 @@ namespace NewHorizons.Builder.Body private static readonly int EmissionColor = Shader.PropertyToID("_EmissionColor"); private static readonly int HeightScale = Shader.PropertyToID("_HeightScale"); + private static GameObject _proxySandFunnel; + private static GameObject _geoSandFunnel; + private static GameObject _volumesSandFunnel; + private static Material[] _waterMaterials; + private static Material _lavaMaterial; + + internal static void InitPrefabs() + { + if (_proxySandFunnel == null) _proxySandFunnel = SearchUtilities.Find("SandFunnel_Body/ScaleRoot/Proxy_SandFunnel").InstantiateInactive().Rename("Prefab_Funnel_Proxy").DontDestroyOnLoad(); + if (_geoSandFunnel == null) _geoSandFunnel = SearchUtilities.Find("SandFunnel_Body/ScaleRoot/Geo_SandFunnel").InstantiateInactive().Rename("Prefab_Funnel_Geo").DontDestroyOnLoad(); + if (_volumesSandFunnel == null) _volumesSandFunnel = SearchUtilities.Find("SandFunnel_Body/ScaleRoot/Volumes_SandFunnel").InstantiateInactive().Rename("Prefab_Funnel_Volumes").DontDestroyOnLoad(); + if (_waterMaterials == null) _waterMaterials = SearchUtilities.Find("TimberHearth_Body/Sector_TH/Geometry_TH/Terrain_TH_Water_v3/Village_Upper_Water/Village_Upper_Water_Geo").GetComponent().sharedMaterials.MakePrefabMaterials(); + if (_lavaMaterial == null) _lavaMaterial = new Material(SearchUtilities.Find("VolcanicMoon_Body/MoltenCore_VM/LavaSphere").GetComponent().sharedMaterial).DontDestroyOnLoad(); + } + public static void Make(GameObject planetGO, ConstantForceDetector detector, OWRigidbody rigidbody, FunnelModule module) { + InitPrefabs(); + var funnelType = module.type; var funnelGO = new GameObject($"{planetGO.name.Replace("_Body", "")}Funnel_Body"); @@ -43,13 +60,13 @@ namespace NewHorizons.Builder.Body scaleRoot.transform.localPosition = Vector3.zero; scaleRoot.transform.localScale = new Vector3(1, 1, 1); - var proxyGO = GameObject.Instantiate(SearchUtilities.Find("SandFunnel_Body/ScaleRoot/Proxy_SandFunnel"), scaleRoot.transform); + var proxyGO = GameObject.Instantiate(_proxySandFunnel, scaleRoot.transform); proxyGO.name = "Proxy_Funnel"; - var geoGO = GameObject.Instantiate(SearchUtilities.Find("SandFunnel_Body/ScaleRoot/Geo_SandFunnel"), scaleRoot.transform); + var geoGO = GameObject.Instantiate(_geoSandFunnel, scaleRoot.transform); geoGO.name = "Geo_Funnel"; - var volumesGO = GameObject.Instantiate(SearchUtilities.Find("SandFunnel_Body/ScaleRoot/Volumes_SandFunnel"), scaleRoot.transform); + var volumesGO = GameObject.Instantiate(_volumesSandFunnel, scaleRoot.transform); volumesGO.name = "Volumes_Funnel"; var sfv = volumesGO.GetComponentInChildren(); var fluidVolume = sfv.gameObject; @@ -64,7 +81,7 @@ namespace NewHorizons.Builder.Body GameObject.Destroy(geoGO.transform.Find("Effects_HT_SandColumn/SandColumn_Interior").gameObject); - var waterMaterials = SearchUtilities.Find("TimberHearth_Body/Sector_TH/Geometry_TH/Terrain_TH_Water_v3/Village_Upper_Water/Village_Upper_Water_Geo").GetComponent().materials; + var waterMaterials = _waterMaterials; var materials = new Material[waterMaterials.Length]; for (int i = 0; i < waterMaterials.Length; i++) { @@ -112,7 +129,7 @@ namespace NewHorizons.Builder.Body GameObject.Destroy(geoGO.transform.Find("Effects_HT_SandColumn/SandColumn_Interior").gameObject); - var lavaMaterial = new Material(SearchUtilities.Find("VolcanicMoon_Body/MoltenCore_VM/LavaSphere").GetComponent().material); + var lavaMaterial = new Material(_lavaMaterial); lavaMaterial.mainTextureOffset = new Vector2(0.1f, 0.2f); lavaMaterial.mainTextureScale = new Vector2(1f, 3f); diff --git a/NewHorizons/Builder/Body/GeometryBuilder.cs b/NewHorizons/Builder/Body/GeometryBuilder.cs index b05f8ac0..dc7c99cf 100644 --- a/NewHorizons/Builder/Body/GeometryBuilder.cs +++ b/NewHorizons/Builder/Body/GeometryBuilder.cs @@ -4,18 +4,26 @@ namespace NewHorizons.Builder.Body { public static class GeometryBuilder { + private static Mesh _topLayerMesh; + + internal static void InitPrefab() + { + if (_topLayerMesh == null) _topLayerMesh = SearchUtilities.Find("CloudsTopLayer_GD")?.GetComponent()?.mesh?.DontDestroyOnLoad(); + } + public static GameObject Make(GameObject planetGO, Sector sector, float groundScale) { + InitPrefab(); + GameObject groundGO = GameObject.CreatePrimitive(PrimitiveType.Sphere); groundGO.transform.name = "GroundSphere"; groundGO.transform.parent = sector?.transform ?? planetGO.transform; groundGO.transform.localScale = new Vector3(groundScale, groundScale, groundScale); groundGO.transform.position = planetGO.transform.position; - var topLayer = SearchUtilities.Find("CloudsTopLayer_GD"); - if (topLayer != null) + if (_topLayerMesh != null) { - groundGO.GetComponent().mesh = topLayer.GetComponent().mesh; + groundGO.GetComponent().mesh = _topLayerMesh; groundGO.GetComponent().radius = 1f; } else diff --git a/NewHorizons/Builder/Body/LavaBuilder.cs b/NewHorizons/Builder/Body/LavaBuilder.cs index 95878f9e..b8fe5aaa 100644 --- a/NewHorizons/Builder/Body/LavaBuilder.cs +++ b/NewHorizons/Builder/Body/LavaBuilder.cs @@ -10,8 +10,21 @@ namespace NewHorizons.Builder.Body private static readonly int HeightScale = Shader.PropertyToID("_HeightScale"); private static readonly int EmissionColor = Shader.PropertyToID("_EmissionColor"); + private static GameObject _lavaSphere; + private static GameObject _moltenCoreProxy; + private static GameObject _destructionVolume; + + internal static void InitPrefabs() + { + if (_lavaSphere == null) _lavaSphere = SearchUtilities.Find("VolcanicMoon_Body/MoltenCore_VM/LavaSphere").InstantiateInactive().Rename("Prefab_VM_LavaSphere").DontDestroyOnLoad(); + if (_moltenCoreProxy == null) _moltenCoreProxy = SearchUtilities.Find("VolcanicMoon_Body/MoltenCore_VM/MoltenCore_Proxy").InstantiateInactive().Rename("Prefab_VM_MoltenCore_Proxy").DontDestroyOnLoad(); + if (_destructionVolume == null) _destructionVolume = SearchUtilities.Find("VolcanicMoon_Body/MoltenCore_VM/DestructionVolume").InstantiateInactive().Rename("Prefab_VM_DestructionVolume").DontDestroyOnLoad(); + } + public static void Make(GameObject planetGO, Sector sector, OWRigidbody rb, LavaModule module) { + InitPrefabs(); + var heightScale = module.size; if (module.curve != null) { @@ -29,7 +42,7 @@ namespace NewHorizons.Builder.Body moltenCore.transform.position = planetGO.transform.position; moltenCore.transform.localScale = Vector3.one * module.size; - var lavaSphere = GameObject.Instantiate(SearchUtilities.Find("VolcanicMoon_Body/MoltenCore_VM/LavaSphere"), moltenCore.transform); + var lavaSphere = GameObject.Instantiate(_lavaSphere, moltenCore.transform); lavaSphere.transform.localScale = Vector3.one; lavaSphere.transform.name = "LavaSphere"; lavaSphere.GetComponent().material.SetFloat(HeightScale, heightScale); @@ -38,7 +51,7 @@ namespace NewHorizons.Builder.Body var sectorCullGroup = lavaSphere.GetComponent(); sectorCullGroup.SetSector(sector); - var moltenCoreProxy = GameObject.Instantiate(SearchUtilities.Find("VolcanicMoon_Body/MoltenCore_VM/MoltenCore_Proxy"), moltenCore.transform); ; + var moltenCoreProxy = GameObject.Instantiate(_moltenCoreProxy, moltenCore.transform); ; moltenCoreProxy.name = "MoltenCore_Proxy"; var proxyLavaSphere = moltenCoreProxy.transform.Find("LavaSphere (1)"); @@ -51,7 +64,7 @@ namespace NewHorizons.Builder.Body sectorProxy._renderers = new List { proxyLavaSphere.GetComponent() }; sectorProxy.SetSector(sector); - var destructionVolume = GameObject.Instantiate(SearchUtilities.Find("VolcanicMoon_Body/MoltenCore_VM/DestructionVolume"), moltenCore.transform); + var destructionVolume = GameObject.Instantiate(_destructionVolume, moltenCore.transform); destructionVolume.name = "DestructionVolume"; destructionVolume.GetComponent().radius = 1; destructionVolume.SetActive(true); diff --git a/NewHorizons/Builder/Body/SandBuilder.cs b/NewHorizons/Builder/Body/SandBuilder.cs index 43b86ac5..cc08a782 100644 --- a/NewHorizons/Builder/Body/SandBuilder.cs +++ b/NewHorizons/Builder/Body/SandBuilder.cs @@ -6,12 +6,28 @@ namespace NewHorizons.Builder.Body { public static class SandBuilder { + private static GameObject _sandSphere; + private static GameObject _sandCollider; + private static GameObject _sandOcclusion; + private static GameObject _sandProxyShadowCaster; + + internal static void InitPrefabs() + { + if (_sandSphere == null) _sandSphere = SearchUtilities.Find("TowerTwin_Body/SandSphere_Draining/SandSphere").InstantiateInactive().Rename("Prefab_TT_SandSphere").DontDestroyOnLoad(); + if (_sandCollider == null) _sandCollider = SearchUtilities.Find("TowerTwin_Body/SandSphere_Draining/Collider").InstantiateInactive().Rename("Prefab_TT_SandCollider").DontDestroyOnLoad(); + if (_sandOcclusion == null) _sandOcclusion = SearchUtilities.Find("TowerTwin_Body/SandSphere_Draining/OcclusionSphere").InstantiateInactive().Rename("Prefab_TT_SandOcclusion").DontDestroyOnLoad(); + if (_sandProxyShadowCaster == null) _sandProxyShadowCaster = SearchUtilities.Find("TowerTwin_Body/SandSphere_Draining/ProxyShadowCaster").InstantiateInactive().Rename("Prefab_TT_SandProxyShadowCaster").DontDestroyOnLoad(); + } + public static void Make(GameObject planetGO, Sector sector, OWRigidbody rb, SandModule module) { + InitPrefabs(); + var sandGO = new GameObject("Sand"); sandGO.SetActive(false); - var sandSphere = GameObject.Instantiate(SearchUtilities.Find("TowerTwin_Body/SandSphere_Draining/SandSphere"), sandGO.transform); + var sandSphere = GameObject.Instantiate(_sandSphere, sandGO.transform); + sandSphere.name = "Sphere"; if (module.tint != null) { var oldMR = sandSphere.GetComponent(); @@ -28,13 +44,15 @@ namespace NewHorizons.Builder.Body sandMR.sharedMaterials[1].color = module.tint.ToColor(); } - var collider = GameObject.Instantiate(SearchUtilities.Find("TowerTwin_Body/SandSphere_Draining/Collider"), sandGO.transform); - var sphereCollider = collider.GetComponent(); + var collider = GameObject.Instantiate(_sandCollider, sandGO.transform); + collider.name = "Collider"; collider.SetActive(true); - var occlusionSphere = GameObject.Instantiate(SearchUtilities.Find("TowerTwin_Body/SandSphere_Draining/OcclusionSphere"), sandGO.transform); + var occlusionSphere = GameObject.Instantiate(_sandOcclusion, sandGO.transform); + occlusionSphere.name = "Occlusion"; - var proxyShadowCasterGO = GameObject.Instantiate(SearchUtilities.Find("TowerTwin_Body/SandSphere_Draining/ProxyShadowCaster"), sandGO.transform); + var proxyShadowCasterGO = GameObject.Instantiate(_sandProxyShadowCaster, sandGO.transform); + proxyShadowCasterGO.name = "ProxyShadowCaster"; var proxyShadowCaster = proxyShadowCasterGO.GetComponent(); proxyShadowCaster.SetSuperGroup(sandGO.GetComponent()); diff --git a/NewHorizons/Builder/Body/SingularityBuilder.cs b/NewHorizons/Builder/Body/SingularityBuilder.cs index 4b9d4cdf..8042e486 100644 --- a/NewHorizons/Builder/Body/SingularityBuilder.cs +++ b/NewHorizons/Builder/Body/SingularityBuilder.cs @@ -30,8 +30,41 @@ namespace NewHorizons.Builder.Body private static Dictionary _singularitiesByID; + private static Mesh _blackHoleMesh; + private static GameObject _blackHoleAmbience; + private static GameObject _blackHoleEmissionOneShot; + private static GameObject _blackHoleVolume; + + private static Mesh _whiteHoleMesh; + private static GameObject _whiteHoleAmbientLight; + private static GameObject _whiteHoleZeroGVolume; + private static GameObject _whiteHoleRulesetVolume; + private static GameObject _whiteHoleVolume; + + internal static void InitPrefabs() + { + if (_blackHoleProxyPrefab == null) _blackHoleProxyPrefab = SearchUtilities.Find(_blackHoleProxyPath).InstantiateInactive().Rename("BlackHoleSingularity").DontDestroyOnLoad(); + if (_whiteHoleProxyPrefab == null) _whiteHoleProxyPrefab = SearchUtilities.Find(_whiteHoleProxyPath).InstantiateInactive().Rename("WhiteHoleSingularity").DontDestroyOnLoad(); + + if (blackHoleShader == null) blackHoleShader = SearchUtilities.Find("BrittleHollow_Body/BlackHole_BH/BlackHoleRenderer").GetComponent().sharedMaterial.shader; + if (whiteHoleShader == null) whiteHoleShader = SearchUtilities.Find("WhiteHole_Body/WhiteHoleVisuals/Singularity").GetComponent().sharedMaterial.shader; + + if (_blackHoleMesh == null) _blackHoleMesh = SearchUtilities.Find("BrittleHollow_Body/BlackHole_BH/BlackHoleRenderer").GetComponent().mesh.DontDestroyOnLoad(); + if (_blackHoleAmbience == null) _blackHoleAmbience = SearchUtilities.Find("BrittleHollow_Body/BlackHole_BH/BlackHoleAmbience").InstantiateInactive().Rename("BlackHoleAmbience").DontDestroyOnLoad(); + if (_blackHoleEmissionOneShot == null) _blackHoleEmissionOneShot = SearchUtilities.Find("BrittleHollow_Body/BlackHole_BH/BlackHoleEmissionOneShot").InstantiateInactive().Rename("BlackHoleEmissionOneShot").DontDestroyOnLoad(); + if (_blackHoleVolume == null) _blackHoleVolume = SearchUtilities.Find("BrittleHollow_Body/BlackHole_BH/BlackHoleVolume").InstantiateInactive().Rename("BlackHoleVolume").DontDestroyOnLoad(); + + if (_whiteHoleMesh == null) _whiteHoleMesh = SearchUtilities.Find("WhiteHole_Body/WhiteHoleVisuals/Singularity").GetComponent().mesh.DontDestroyOnLoad(); + if (_whiteHoleAmbientLight == null) _whiteHoleAmbientLight = SearchUtilities.Find("WhiteHole_Body/WhiteHoleVisuals/AmbientLight_WH").InstantiateInactive().Rename("WhiteHoleAmbientLight").DontDestroyOnLoad(); + if (_whiteHoleZeroGVolume == null) _whiteHoleZeroGVolume = SearchUtilities.Find("WhiteHole_Body/ZeroGVolume").InstantiateInactive().Rename("WhiteHoleZeroGVolume").DontDestroyOnLoad(); + if (_whiteHoleRulesetVolume == null) _whiteHoleRulesetVolume = SearchUtilities.Find("WhiteHole_Body/Sector_WhiteHole/RulesetVolumes_WhiteHole").InstantiateInactive().Rename("WhiteHoleRulesetVolume").DontDestroyOnLoad(); + if (_whiteHoleVolume == null) _whiteHoleVolume = SearchUtilities.Find("WhiteHole_Body/WhiteHoleVolume").InstantiateInactive().Rename("WhiteHoleVolume").DontDestroyOnLoad(); + } + public static void Make(GameObject go, Sector sector, OWRigidbody OWRB, PlanetConfig config, SingularityModule singularity) { + InitPrefabs(); + // If we've reloaded the first one will now be null so we have to refresh the list if (_singularitiesByID?.Values?.FirstOrDefault() == null) _singularitiesByID = new Dictionary(); @@ -81,6 +114,8 @@ namespace NewHorizons.Builder.Body public static void PairSingularities(string blackHoleID, string whiteHoleID, GameObject blackHole, GameObject whiteHole) { + InitPrefabs(); + if (blackHole == null || whiteHole == null) return; Logger.LogVerbose($"Pairing singularities [{blackHoleID}], [{whiteHoleID}]"); @@ -102,6 +137,8 @@ namespace NewHorizons.Builder.Body public static GameObject MakeBlackHole(GameObject planetGO, Sector sector, Vector3 localPosition, float size, bool hasDestructionVolume, string targetSolarSystem = null, TimeValuePair[] curve = null, bool makeAudio = true) { + InitPrefabs(); + var blackHole = new GameObject("BlackHole"); blackHole.SetActive(false); blackHole.transform.parent = sector?.transform ?? planetGO.transform; @@ -120,7 +157,7 @@ namespace NewHorizons.Builder.Body if (makeAudio) { - var blackHoleAmbience = GameObject.Instantiate(SearchUtilities.Find("BrittleHollow_Body/BlackHole_BH/BlackHoleAmbience"), blackHole.transform); + var blackHoleAmbience = GameObject.Instantiate(_blackHoleAmbience, blackHole.transform); blackHoleAmbience.name = "BlackHoleAmbience"; blackHoleAmbience.GetComponent().SetSector(sector); @@ -130,7 +167,7 @@ namespace NewHorizons.Builder.Body blackHoleAmbience.transform.localPosition = Vector3.zero; if (sizeController != null) sizeController.audioSource = blackHoleAudioSource; - var blackHoleOneShot = GameObject.Instantiate(SearchUtilities.Find("BrittleHollow_Body/BlackHole_BH/BlackHoleEmissionOneShot"), blackHole.transform); + var blackHoleOneShot = GameObject.Instantiate(_blackHoleEmissionOneShot, blackHole.transform); blackHoleOneShot.name = "BlackHoleEmissionOneShot"; var oneShotAudioSource = blackHoleOneShot.GetComponent(); oneShotAudioSource.maxDistance = size * 3f; @@ -160,7 +197,7 @@ namespace NewHorizons.Builder.Body } else { - var blackHoleVolume = GameObject.Instantiate(SearchUtilities.Find("BrittleHollow_Body/BlackHole_BH/BlackHoleVolume"), blackHole.transform); + var blackHoleVolume = GameObject.Instantiate(_blackHoleVolume, blackHole.transform); blackHoleVolume.name = "BlackHoleVolume"; var blackHoleSphereCollider = blackHoleVolume.GetComponent(); blackHoleSphereCollider.radius = size * 0.4f; @@ -173,16 +210,17 @@ namespace NewHorizons.Builder.Body public static MeshRenderer MakeBlackHoleGraphics(GameObject blackHole, float size) { + InitPrefabs(); + var blackHoleRender = new GameObject("BlackHoleRender"); blackHoleRender.transform.parent = blackHole.transform; blackHoleRender.transform.localPosition = Vector3.zero; blackHoleRender.transform.localScale = Vector3.one * size; var meshFilter = blackHoleRender.AddComponent(); - meshFilter.mesh = SearchUtilities.Find("BrittleHollow_Body/BlackHole_BH/BlackHoleRenderer").GetComponent().mesh; + meshFilter.mesh = _blackHoleMesh; var meshRenderer = blackHoleRender.AddComponent(); - if (blackHoleShader == null) blackHoleShader = SearchUtilities.Find("BrittleHollow_Body/BlackHole_BH/BlackHoleRenderer").GetComponent().sharedMaterial.shader; meshRenderer.material = new Material(blackHoleShader); meshRenderer.material.SetFloat(Radius, size * 0.4f); meshRenderer.material.SetFloat(MaxDistortRadius, size * 0.95f); @@ -195,6 +233,8 @@ namespace NewHorizons.Builder.Body public static GameObject MakeWhiteHole(GameObject planetGO, Sector sector, OWRigidbody OWRB, Vector3 localPosition, float size, TimeValuePair[] curve, bool makeZeroGVolume = true) { + InitPrefabs(); + var whiteHole = new GameObject("WhiteHole"); whiteHole.SetActive(false); whiteHole.transform.parent = sector?.transform ?? planetGO.transform; @@ -214,10 +254,9 @@ namespace NewHorizons.Builder.Body } var meshFilter = whiteHoleRenderer.AddComponent(); - meshFilter.mesh = SearchUtilities.Find("WhiteHole_Body/WhiteHoleVisuals/Singularity").GetComponent().mesh; + meshFilter.mesh = _whiteHoleMesh; var meshRenderer = whiteHoleRenderer.AddComponent(); - if (whiteHoleShader == null) whiteHoleShader = SearchUtilities.Find("WhiteHole_Body/WhiteHoleVisuals/Singularity").GetComponent().sharedMaterial.shader; meshRenderer.material = new Material(whiteHoleShader); meshRenderer.sharedMaterial.SetFloat(Radius, size * 0.4f); meshRenderer.sharedMaterial.SetFloat(DistortFadeDist, size); @@ -226,7 +265,7 @@ namespace NewHorizons.Builder.Body meshRenderer.sharedMaterial.SetColor(Color1, new Color(1.88f, 1.88f, 1.88f, 1f)); if (sizeController != null) sizeController.material = meshRenderer.sharedMaterial; - var ambientLight = GameObject.Instantiate(SearchUtilities.Find("WhiteHole_Body/WhiteHoleVisuals/AmbientLight_WH")); + var ambientLight = GameObject.Instantiate(_whiteHoleAmbientLight); ambientLight.transform.parent = whiteHole.transform; ambientLight.transform.localScale = Vector3.one; ambientLight.transform.localPosition = Vector3.zero; @@ -235,7 +274,7 @@ namespace NewHorizons.Builder.Body light.range = size * 7f; if (sizeController != null) sizeController.light = light; - GameObject whiteHoleVolumeGO = GameObject.Instantiate(SearchUtilities.Find("WhiteHole_Body/WhiteHoleVolume")); + GameObject whiteHoleVolumeGO = GameObject.Instantiate(_whiteHoleVolume); whiteHoleVolumeGO.transform.parent = whiteHole.transform; whiteHoleVolumeGO.transform.localPosition = Vector3.zero; whiteHoleVolumeGO.transform.localScale = Vector3.one; @@ -264,13 +303,13 @@ namespace NewHorizons.Builder.Body if (makeZeroGVolume) { - var zeroGVolume = GameObject.Instantiate(SearchUtilities.Find("WhiteHole_Body/ZeroGVolume"), whiteHole.transform); + var zeroGVolume = GameObject.Instantiate(_whiteHoleZeroGVolume, whiteHole.transform); zeroGVolume.name = "ZeroGVolume"; zeroGVolume.transform.localPosition = Vector3.zero; zeroGVolume.GetComponent().radius = size * 10f; zeroGVolume.GetComponent()._attachedBody = OWRB; - var rulesetVolume = GameObject.Instantiate(SearchUtilities.Find("WhiteHole_Body/Sector_WhiteHole/RulesetVolumes_WhiteHole"), planetGO.transform); + var rulesetVolume = GameObject.Instantiate(_whiteHoleRulesetVolume, planetGO.transform); rulesetVolume.name = "RulesetVolume"; rulesetVolume.transform.localPosition = Vector3.zero; rulesetVolume.transform.localScale = Vector3.one * size / 100f; @@ -289,7 +328,7 @@ namespace NewHorizons.Builder.Body public static GameObject MakeBlackHoleProxy(GameObject rootObject, MVector3 position, float size, TimeValuePair[] curve = null) { - if (_blackHoleProxyPrefab == null) _blackHoleProxyPrefab = SearchUtilities.Find(_blackHoleProxyPath); + InitPrefabs(); var blackHoleShader = _blackHoleProxyPrefab.GetComponent().material.shader; if (blackHoleShader == null) blackHoleShader = _blackHoleProxyPrefab.GetComponent().sharedMaterial.shader; @@ -318,7 +357,7 @@ namespace NewHorizons.Builder.Body public static GameObject MakeWhiteHoleProxy(GameObject rootObject, MVector3 position, float size, TimeValuePair[] curve = null) { - if (_whiteHoleProxyPrefab == null) _whiteHoleProxyPrefab = SearchUtilities.Find(_whiteHoleProxyPath); + InitPrefabs(); var whiteHoleShader = _whiteHoleProxyPrefab.GetComponent().material.shader; if (whiteHoleShader == null) whiteHoleShader = _whiteHoleProxyPrefab.GetComponent().sharedMaterial.shader; diff --git a/NewHorizons/Builder/Body/StarBuilder.cs b/NewHorizons/Builder/Body/StarBuilder.cs index 7e32c3d3..0933fa50 100644 --- a/NewHorizons/Builder/Body/StarBuilder.cs +++ b/NewHorizons/Builder/Body/StarBuilder.cs @@ -23,11 +23,39 @@ namespace NewHorizons.Builder.Body private static readonly int InnerRadius = Shader.PropertyToID("_InnerRadius"); private static readonly int OuterRadius = Shader.PropertyToID("_OuterRadius"); + private static GameObject _starAudio; + private static GameObject _starAtmosphere; + private static GameObject _starProxyAtmosphere; + private static GameObject _starAmbientLight; + private static GameObject _sunLight; + private static GameObject _starSurface; + private static GameObject _starSolarFlareEmitter; + private static GameObject _supernovaPrefab; + private static Material _mainSequenceMaterial; + private static Material _giantMaterial; + + internal static void InitPrefabs() + { + if (_colorOverTime == null) _colorOverTime = ImageUtilities.GetTexture(Main.Instance, "Assets/textures/StarColorOverTime.png"); + if (_starAudio == null) _starAudio = SearchUtilities.Find("Sun_Body/Sector_SUN/Audio_SUN").InstantiateInactive().Rename("Prefab_Audio_Star").DontDestroyOnLoad(); + if (_starAtmosphere == null) _starAtmosphere = SearchUtilities.Find("Sun_Body/Atmosphere_SUN").InstantiateInactive().Rename("Prefab_Atmosphere_Star").DontDestroyOnLoad(); + if (_starAmbientLight == null) _starAmbientLight = SearchUtilities.Find("Sun_Body/AmbientLight_SUN").InstantiateInactive().Rename("Prefab_AmbientLight_Star").DontDestroyOnLoad(); + if (_sunLight == null) _sunLight = SearchUtilities.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight").InstantiateInactive().Rename("Prefab_SunLight").DontDestroyOnLoad(); + if (_starProxyAtmosphere == null) _starProxyAtmosphere = GameObject.FindObjectOfType()._sunProxyPrefab.FindChild("Sun_Proxy_Body/Atmosphere_SUN").InstantiateInactive().Rename("Prefab_ProxyAtmosphere_Star").DontDestroyOnLoad(); + if (_starSurface == null) _starSurface = SearchUtilities.Find("Sun_Body/Sector_SUN/Geometry_SUN/Surface").InstantiateInactive().Rename("Prefab_Surface_Star").DontDestroyOnLoad(); + if (_starSolarFlareEmitter == null) _starSolarFlareEmitter = SearchUtilities.Find("Sun_Body/Sector_SUN/Effects_SUN/SolarFlareEmitter").InstantiateInactive().Rename("Prefab_SolarFlareEmitter_Star").DontDestroyOnLoad(); + if (_supernovaPrefab == null) _supernovaPrefab = SearchUtilities.Find("Sun_Body/Sector_SUN/Effects_SUN/Supernova").InstantiateInactive().Rename("Prefab_Supernova").DontDestroyOnLoad(); + if (_mainSequenceMaterial == null) _mainSequenceMaterial = new Material(SearchUtilities.Find("Sun_Body").GetComponent()._startSurfaceMaterial).DontDestroyOnLoad(); + if (_giantMaterial == null) _giantMaterial = new Material(SearchUtilities.Find("Sun_Body").GetComponent()._endSurfaceMaterial).DontDestroyOnLoad(); + } + public static (GameObject, StarController, StarEvolutionController) Make(GameObject planetGO, Sector sector, StarModule starModule, IModBehaviour mod, bool isStellarRemnant) { + InitPrefabs(); + var (starGO, starEvolutionController, supernova) = SharedStarGeneration(planetGO, sector, mod, starModule, isStellarRemnant); - var sunAudio = Object.Instantiate(SearchUtilities.Find("Sun_Body/Sector_SUN/Audio_SUN"), starGO.transform); + var sunAudio = Object.Instantiate(_starAudio, starGO.transform); sunAudio.transform.localPosition = Vector3.zero; sunAudio.transform.localScale = Vector3.one; sunAudio.transform.Find("SurfaceAudio_Sun").GetComponent().maxDistance = starModule.size * 2f; @@ -42,7 +70,7 @@ namespace NewHorizons.Builder.Body GameObject sunAtmosphere = null; if (starModule.hasAtmosphere) { - sunAtmosphere = Object.Instantiate(SearchUtilities.Find("Sun_Body/Atmosphere_SUN"), starGO.transform); + sunAtmosphere = Object.Instantiate(_starAtmosphere, starGO.transform); sunAtmosphere.transform.position = planetGO.transform.position; sunAtmosphere.transform.localScale = Vector3.one * OuterRadiusRatio; sunAtmosphere.name = "Atmosphere_Star"; @@ -71,7 +99,7 @@ namespace NewHorizons.Builder.Body } } - var ambientLightGO = Object.Instantiate(SearchUtilities.Find("Sun_Body/AmbientLight_SUN"), starGO.transform); + var ambientLightGO = Object.Instantiate(_starAmbientLight, starGO.transform); ambientLightGO.transform.localPosition = Vector3.zero; ambientLightGO.name = "AmbientLight_Star"; @@ -123,7 +151,7 @@ namespace NewHorizons.Builder.Body sunLight.transform.localScale = Vector3.one; var light = sunLight.AddComponent(); - light.CopyPropertiesFrom(SearchUtilities.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight").GetComponent()); + light.CopyPropertiesFrom(_sunLight.GetComponent()); light.intensity *= starModule.solarLuminosity; light.range = starModule.lightRadius; light.range *= Mathf.Sqrt(starModule.solarLuminosity); @@ -135,12 +163,12 @@ namespace NewHorizons.Builder.Body ambientLight.color = new Color(lightColour.r, lightColour.g, lightColour.b, lightColour.a == 0 ? 0.0001f : lightColour.a); var faceActiveCamera = sunLight.AddComponent(); - faceActiveCamera.CopyPropertiesFrom(SearchUtilities.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight").GetComponent()); + faceActiveCamera.CopyPropertiesFrom(_sunLight.GetComponent()); var csmTextureCacher = sunLight.AddComponent(); - csmTextureCacher.CopyPropertiesFrom(SearchUtilities.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight").GetComponent()); + csmTextureCacher.CopyPropertiesFrom(_sunLight.GetComponent()); csmTextureCacher._light = light; var proxyShadowLight = sunLight.AddComponent(); - proxyShadowLight.CopyPropertiesFrom(SearchUtilities.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight").GetComponent()); + proxyShadowLight.CopyPropertiesFrom(_sunLight.GetComponent()); proxyShadowLight._light = light; // Star controller (works on atmospheric shaders) @@ -187,13 +215,15 @@ namespace NewHorizons.Builder.Body public static (GameObject, Renderer, Renderer) MakeStarProxy(GameObject planet, GameObject proxyGO, StarModule starModule, IModBehaviour mod, bool isStellarRemnant) { + InitPrefabs(); + var (starGO, controller, supernova) = SharedStarGeneration(proxyGO, null, mod, starModule, isStellarRemnant); Renderer atmosphere = null; Renderer fog = null; if (starModule.hasAtmosphere) { - GameObject sunAtmosphere = Object.Instantiate(SearchUtilities.Find("SunProxy/Sun_Proxy_Body/Atmosphere_SUN", false) ?? SearchUtilities.Find("SunProxy(Clone)/Sun_Proxy_Body/Atmosphere_SUN"), starGO.transform); + GameObject sunAtmosphere = Object.Instantiate(_starProxyAtmosphere, starGO.transform); sunAtmosphere.transform.position = proxyGO.transform.position; sunAtmosphere.transform.localScale = Vector3.one * OuterRadiusRatio; sunAtmosphere.name = "Atmosphere_Star"; @@ -236,6 +266,8 @@ namespace NewHorizons.Builder.Body private static (GameObject, StarEvolutionController, StellarDeathController) SharedStarGeneration(GameObject planetGO, Sector sector, IModBehaviour mod, StarModule starModule, bool isStellarRemnant) { + InitPrefabs(); + var starGO = MakeStarGraphics(planetGO, sector, starModule, mod); starGO.SetActive(false); @@ -280,17 +312,17 @@ namespace NewHorizons.Builder.Body public static GameObject MakeStarGraphics(GameObject rootObject, Sector sector, StarModule starModule, IModBehaviour mod) { - if (_colorOverTime == null) _colorOverTime = ImageUtilities.GetTexture(Main.Instance, "Assets/textures/StarColorOverTime.png"); + InitPrefabs(); var starGO = new GameObject("Star"); starGO.transform.parent = sector?.transform ?? rootObject.transform; - var sunSurface = Object.Instantiate(SearchUtilities.Find("Sun_Body/Sector_SUN/Geometry_SUN/Surface"), starGO.transform); + var sunSurface = Object.Instantiate(_starSurface, starGO.transform); sunSurface.transform.position = rootObject.transform.position; sunSurface.transform.localScale = Vector3.one; sunSurface.name = "Surface"; - var solarFlareEmitter = Object.Instantiate(SearchUtilities.Find("Sun_Body/Sector_SUN/Effects_SUN/SolarFlareEmitter"), starGO.transform); + var solarFlareEmitter = Object.Instantiate(_starSolarFlareEmitter, starGO.transform); solarFlareEmitter.transform.localPosition = Vector3.zero; solarFlareEmitter.transform.localScale = Vector3.one; solarFlareEmitter.name = "SolarFlareEmitter"; @@ -318,11 +350,7 @@ namespace NewHorizons.Builder.Body { var colour = starModule.tint.ToColor(); - var sun = SearchUtilities.Find("Sun_Body"); - var mainSequenceMaterial = sun.GetComponent()._startSurfaceMaterial; - var giantMaterial = sun.GetComponent()._endSurfaceMaterial; - - surface.sharedMaterial = new Material(starModule.size >= 3000 ? giantMaterial : mainSequenceMaterial); + surface.sharedMaterial = new Material(starModule.size >= 3000 ? _giantMaterial : _mainSequenceMaterial); var modifier = Mathf.Max(1f, 2f * Mathf.Sqrt(starModule.solarLuminosity)); var adjustedColour = new Color(colour.r * modifier, colour.g * modifier, colour.b * modifier); surface.sharedMaterial.color = adjustedColour; @@ -353,7 +381,9 @@ namespace NewHorizons.Builder.Body public static StellarDeathController MakeSupernova(GameObject starGO, StarModule starModule, bool noAudio = false) { - var supernovaGO = SearchUtilities.Find("Sun_Body/Sector_SUN/Effects_SUN/Supernova").InstantiateInactive(); + InitPrefabs(); + + var supernovaGO = _supernovaPrefab.InstantiateInactive(); supernovaGO.name = "Supernova"; supernovaGO.transform.SetParent(starGO.transform); supernovaGO.transform.localPosition = Vector3.zero; diff --git a/NewHorizons/Builder/Body/SupernovaEffectBuilder.cs b/NewHorizons/Builder/Body/SupernovaEffectBuilder.cs index a072dc76..af237a23 100644 --- a/NewHorizons/Builder/Body/SupernovaEffectBuilder.cs +++ b/NewHorizons/Builder/Body/SupernovaEffectBuilder.cs @@ -10,10 +10,18 @@ namespace NewHorizons.Builder.Body { public static class SupernovaEffectBuilder { + private static Mesh _shockLayerMesh; + private static Material _shockLayerMaterial; + + internal static void InitPrefabs() + { + if (_shockLayerMesh == null) _shockLayerMesh = SearchUtilities.Find("GiantsDeep_Body/Shocklayer_GD").GetComponent().sharedMesh.DontDestroyOnLoad(); + if (_shockLayerMaterial == null) _shockLayerMaterial = new Material(SearchUtilities.Find("GiantsDeep_Body/Shocklayer_GD").GetComponent().sharedMaterial).Rename("ShockLayer_mat").DontDestroyOnLoad(); + } + public static NHSupernovaPlanetEffectController Make(GameObject planetGO, Sector sector, PlanetConfig config, IModBehaviour mod, GameObject procGen, Light ambientLight, PlanetaryFogController fog, LODGroup atmosphere, Renderer atmosphereRenderer, Renderer fogImpostor) { - var shockLayerGD = SearchUtilities.Find("GiantsDeep_Body/Shocklayer_GD"); - if (shockLayerGD == null) return null; + InitPrefabs(); var vanillaController = planetGO.GetComponentInChildren(); if (vanillaController != null) @@ -53,13 +61,10 @@ namespace NewHorizons.Builder.Body var shockLayer = new GameObject("ShockLayer"); shockLayer.transform.SetParent(sector?.transform ?? planetGO.transform, false); - shockLayer.AddComponent().sharedMesh = shockLayerGD.GetComponent().sharedMesh; - - var shockLayerMaterial = new Material(shockLayerGD.GetComponent().sharedMaterial); - shockLayerMaterial.name = "ShockLayer_mat"; + shockLayer.AddComponent().sharedMesh = _shockLayerMesh; var shockLayerRenderer = shockLayer.AddComponent(); - shockLayerRenderer.sharedMaterial = shockLayerMaterial; + shockLayerRenderer.sharedMaterial = new Material(_shockLayerMaterial); supernovaEffectController._shockLayer = shockLayerRenderer; var biggestSize = config.Base.surfaceSize; @@ -180,7 +185,7 @@ namespace NewHorizons.Builder.Body supernovaEffectController.shockLayerStartRadius = vanillaController._shockLayerStartRadius; supernovaEffectController.shockLayerTrailFlare = vanillaController._shockLayerTrailFlare; supernovaEffectController.shockLayerTrailLength = vanillaController._shockLayerTrailLength; - supernovaEffectController.SunController = SearchUtilities.Find("Sun_Body").GetComponent(); + supernovaEffectController.SunController = SearchUtilities.Find("Sun_Body")?.GetComponent(); Object.Destroy(vanillaController); } } diff --git a/NewHorizons/Builder/Body/WaterBuilder.cs b/NewHorizons/Builder/Body/WaterBuilder.cs index 5cbc5d70..08e6e598 100644 --- a/NewHorizons/Builder/Body/WaterBuilder.cs +++ b/NewHorizons/Builder/Body/WaterBuilder.cs @@ -9,8 +9,33 @@ namespace NewHorizons.Builder.Body { public static class WaterBuilder { + private static MeshGroup _oceanMeshGroup; + private static Material[] _oceanLowAltitudeMaterials; + private static GameObject _oceanFog; + + internal static void InitPrefabs() + { + if (_oceanMeshGroup == null) + { + _oceanMeshGroup = ScriptableObject.CreateInstance().Rename("Ocean").DontDestroyOnLoad(); + var gdVariants = SearchUtilities.Find("Ocean_GD").GetComponent().tessellationMeshGroup.variants; + for (int i = 0; i < 16; i++) + { + var mesh = new Mesh(); + mesh.CopyPropertiesFrom(gdVariants[i]); + mesh.name = gdVariants[i].name; + mesh.DontDestroyOnLoad(); + _oceanMeshGroup.variants[i] = mesh; + } + } + if (_oceanLowAltitudeMaterials == null) _oceanLowAltitudeMaterials = SearchUtilities.Find("Ocean_GD").GetComponent()._lowAltitudeMaterials.MakePrefabMaterials(); + if (_oceanFog == null) _oceanFog = SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Sector_GDInterior/Effects_GDInterior/OceanFog").InstantiateInactive().Rename("Prefab_GD_OceanFog").DontDestroyOnLoad(); + } + public static void Make(GameObject planetGO, Sector sector, OWRigidbody rb, WaterModule module) { + InitPrefabs(); + var waterSize = module.size; GameObject waterGO = new GameObject("Water"); @@ -19,18 +44,16 @@ namespace NewHorizons.Builder.Body waterGO.transform.parent = sector?.transform ?? planetGO.transform; waterGO.transform.localScale = new Vector3(waterSize, waterSize, waterSize); - var GDTSR = SearchUtilities.Find("Ocean_GD").GetComponent(); - TessellatedSphereRenderer TSR = waterGO.AddComponent(); TSR.tessellationMeshGroup = ScriptableObject.CreateInstance(); for (int i = 0; i < 16; i++) { var mesh = new Mesh(); - mesh.CopyPropertiesFrom(GDTSR.tessellationMeshGroup.variants[i]); + mesh.CopyPropertiesFrom(_oceanMeshGroup.variants[i]); TSR.tessellationMeshGroup.variants[i] = mesh; } - var GDSharedMaterials = SearchUtilities.Find("Ocean_GD").GetComponent()._lowAltitudeMaterials; + var GDSharedMaterials = _oceanLowAltitudeMaterials; var tempArray = new Material[GDSharedMaterials.Length]; for (int i = 0; i < GDSharedMaterials.Length; i++) { @@ -49,10 +72,9 @@ namespace NewHorizons.Builder.Body OEC._sector = sector; OEC._ocean = TSR; - var GDOLC = GDTSR.GetComponent(); var OLC = waterGO.AddComponent(); OLC._sector = sector; - OLC._ambientLight = GDOLC._ambientLight; // this needs to be set or else is black + OLC._ambientLight = null; // this needs to be set or else is black // trigger sector enter Delay.FireOnNextUpdate(() => @@ -87,7 +109,7 @@ namespace NewHorizons.Builder.Body fluidVolume._radius = waterSize; fluidVolume._layer = LayerMask.NameToLayer("BasicEffectVolume"); - var fogGO = GameObject.Instantiate(SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Sector_GDInterior/Effects_GDInterior/OceanFog"), waterGO.transform); + var fogGO = GameObject.Instantiate(_oceanFog, waterGO.transform); fogGO.name = "OceanFog"; fogGO.transform.localPosition = Vector3.zero; fogGO.transform.localScale = Vector3.one; diff --git a/NewHorizons/Builder/General/DetectorBuilder.cs b/NewHorizons/Builder/General/DetectorBuilder.cs index 579d564a..bdca0f17 100644 --- a/NewHorizons/Builder/General/DetectorBuilder.cs +++ b/NewHorizons/Builder/General/DetectorBuilder.cs @@ -8,8 +8,69 @@ namespace NewHorizons.Builder.General { public static class DetectorBuilder { + private static List _splashEffects; + + internal static void InitPrefabs() + { + if (_splashEffects == null) + { + _splashEffects = new List(); + + var cometDetector = SearchUtilities.Find("Comet_Body/Detector_CO")?.GetComponent(); + if (cometDetector != null) + { + foreach (var splashEffect in cometDetector._splashEffects) + { + _splashEffects.Add(new SplashEffect + { + fluidType = splashEffect.fluidType, + ignoreSphereAligment = splashEffect.ignoreSphereAligment, + triggerEvent = splashEffect.triggerEvent, + minImpactSpeed = 15, + splashPrefab = splashEffect.splashPrefab + }); + } + } + + var islandDetector = SearchUtilities.Find("GabbroIsland_Body/Detector_GabbroIsland")?.GetComponent(); + if (islandDetector != null) + { + foreach (var splashEffect in islandDetector._splashEffects) + { + _splashEffects.Add(new SplashEffect + { + fluidType = splashEffect.fluidType, + ignoreSphereAligment = splashEffect.ignoreSphereAligment, + triggerEvent = splashEffect.triggerEvent, + minImpactSpeed = 15, + splashPrefab = splashEffect.splashPrefab + }); + } + } + + var shipDetector = SearchUtilities.Find("Ship_Body/ShipDetector")?.GetComponent(); + if (shipDetector != null) + { + foreach (var splashEffect in shipDetector._splashEffects) + { + if (splashEffect.fluidType == FluidVolume.Type.SAND) + _splashEffects.Add(new SplashEffect + { + fluidType = splashEffect.fluidType, + ignoreSphereAligment = splashEffect.ignoreSphereAligment, + triggerEvent = splashEffect.triggerEvent, + minImpactSpeed = 15, + splashPrefab = splashEffect.splashPrefab + }); + } + } + } + } + public static GameObject Make(GameObject planetGO, OWRigidbody OWRB, AstroObject primaryBody, AstroObject astroObject, PlanetConfig config) { + InitPrefabs(); + GameObject detectorGO = new GameObject("FieldDetector"); detectorGO.SetActive(false); detectorGO.transform.parent = planetGO.transform; @@ -35,58 +96,7 @@ namespace NewHorizons.Builder.General OWRB.RegisterAttachedFluidDetector(fluidDetector); - var splashEffects = new List(); - - var cometDetector = SearchUtilities.Find("Comet_Body/Detector_CO")?.GetComponent(); - if (cometDetector != null) - { - foreach (var splashEffect in cometDetector._splashEffects) - { - splashEffects.Add(new SplashEffect - { - fluidType = splashEffect.fluidType, - ignoreSphereAligment = splashEffect.ignoreSphereAligment, - triggerEvent = splashEffect.triggerEvent, - minImpactSpeed = 15, - splashPrefab = splashEffect.splashPrefab - }); - } - } - - var islandDetector = SearchUtilities.Find("GabbroIsland_Body/Detector_GabbroIsland")?.GetComponent(); - if (islandDetector != null) - { - foreach (var splashEffect in islandDetector._splashEffects) - { - splashEffects.Add(new SplashEffect - { - fluidType = splashEffect.fluidType, - ignoreSphereAligment = splashEffect.ignoreSphereAligment, - triggerEvent = splashEffect.triggerEvent, - minImpactSpeed = 15, - splashPrefab = splashEffect.splashPrefab - }); - } - } - - var shipDetector = SearchUtilities.Find("Ship_Body/ShipDetector")?.GetComponent(); - if (shipDetector != null) - { - foreach (var splashEffect in shipDetector._splashEffects) - { - if (splashEffect.fluidType == FluidVolume.Type.SAND) - splashEffects.Add(new SplashEffect - { - fluidType = splashEffect.fluidType, - ignoreSphereAligment = splashEffect.ignoreSphereAligment, - triggerEvent = splashEffect.triggerEvent, - minImpactSpeed = 15, - splashPrefab = splashEffect.splashPrefab - }); - } - } - - fluidDetector._splashEffects = splashEffects.ToArray(); + fluidDetector._splashEffects = _splashEffects.ToArray(); } if (!config.Orbit.isStatic) SetDetector(primaryBody, astroObject, forceDetector); diff --git a/NewHorizons/Builder/General/RigidBodyBuilder.cs b/NewHorizons/Builder/General/RigidBodyBuilder.cs index d016f469..ebfcd11e 100644 --- a/NewHorizons/Builder/General/RigidBodyBuilder.cs +++ b/NewHorizons/Builder/General/RigidBodyBuilder.cs @@ -27,7 +27,7 @@ namespace NewHorizons.Builder.General owRigidBody._maintainOriginalCenterOfMass = true; owRigidBody._rigidbody = rigidBody; owRigidBody._kinematicRigidbody = kinematicRigidBody; - owRigidBody._origParent = SearchUtilities.Find("SolarSystemRoot").transform; + owRigidBody._origParent = SearchUtilities.Find("SolarSystemRoot")?.transform; owRigidBody.EnableKinematicSimulation(); owRigidBody.MakeKinematic(); diff --git a/NewHorizons/Builder/General/SpawnPointBuilder.cs b/NewHorizons/Builder/General/SpawnPointBuilder.cs index a224333d..bf6f82f6 100644 --- a/NewHorizons/Builder/General/SpawnPointBuilder.cs +++ b/NewHorizons/Builder/General/SpawnPointBuilder.cs @@ -47,33 +47,36 @@ namespace NewHorizons.Builder.General spawnPoint._triggerVolumes = new OWTriggerVolume[0]; var ship = SearchUtilities.Find("Ship_Body"); - ship.transform.position = spawnPoint.transform.position; - - if (module.shipSpawnRotation != null) + if (ship != null) { - ship.transform.rotation = Quaternion.Euler(module.shipSpawnRotation); - } - else - { - ship.transform.rotation = Quaternion.FromToRotation(Vector3.up, (spawnPoint.transform.position - planetGO.transform.position).normalized); - // Move it up a bit more when aligning to surface - ship.transform.position = ship.transform.position + ship.transform.TransformDirection(Vector3.up) * 4f; - } + ship.transform.position = spawnPoint.transform.position; - ship.GetRequiredComponent().SetBodyToMatch(owRigidBody); + if (module.shipSpawnRotation != null) + { + ship.transform.rotation = Quaternion.Euler(module.shipSpawnRotation); + } + else + { + ship.transform.rotation = Quaternion.FromToRotation(Vector3.up, (spawnPoint.transform.position - planetGO.transform.position).normalized); + // Move it up a bit more when aligning to surface + ship.transform.position = ship.transform.position + ship.transform.TransformDirection(Vector3.up) * 4f; + } - if (Main.Instance.IsWarpingFromShip) - { - Logger.LogVerbose("Overriding player spawn to be inside ship"); - GameObject playerSpawnGO = new GameObject("PlayerSpawnPoint"); - playerSpawnGO.transform.parent = ship.transform; - playerSpawnGO.layer = 8; + ship.GetRequiredComponent().SetBodyToMatch(owRigidBody); - playerSpawnGO.transform.localPosition = new Vector3(0, 0, 0); + if (Main.Instance.IsWarpingFromShip) + { + Logger.LogVerbose("Overriding player spawn to be inside ship"); + GameObject playerSpawnGO = new GameObject("PlayerSpawnPoint"); + playerSpawnGO.transform.parent = ship.transform; + playerSpawnGO.layer = 8; - playerSpawn = playerSpawnGO.AddComponent(); - playerSpawn._triggerVolumes = new OWTriggerVolume[0]; - playerSpawnGO.transform.localRotation = Quaternion.Euler(0, 0, 0); + playerSpawnGO.transform.localPosition = new Vector3(0, 0, 0); + + playerSpawn = playerSpawnGO.AddComponent(); + playerSpawn._triggerVolumes = new OWTriggerVolume[0]; + playerSpawnGO.transform.localRotation = Quaternion.Euler(0, 0, 0); + } } } diff --git a/NewHorizons/Builder/Props/BrambleNodeBuilder.cs b/NewHorizons/Builder/Props/BrambleNodeBuilder.cs index f87d0d28..b73c6e6f 100644 --- a/NewHorizons/Builder/Props/BrambleNodeBuilder.cs +++ b/NewHorizons/Builder/Props/BrambleNodeBuilder.cs @@ -28,8 +28,8 @@ namespace NewHorizons.Builder.Props public static readonly Dictionary namedNodes = new(); public static readonly Dictionary builtBrambleNodes = new(); - private static string _brambleSeedPrefabPath = "DB_PioneerDimension_Body/Sector_PioneerDimension/Interactables_PioneerDimension/SeedWarp_ToPioneer (1)"; - private static string _brambleNodePrefabPath = "DB_HubDimension_Body/Sector_HubDimension/Interactables_HubDimension/InnerWarp_ToCluster"; + private static GameObject _brambleSeedPrefab; + private static GameObject _brambleNodePrefab; public static void Init(PlanetConfig[] dimensionConfigs) { @@ -41,6 +41,12 @@ namespace NewHorizons.Builder.Props PropagateSignals(dimensionConfigs); } + internal static void InitPrefabs() + { + if (_brambleSeedPrefab == null) _brambleSeedPrefab = SearchUtilities.Find("DB_PioneerDimension_Body/Sector_PioneerDimension/Interactables_PioneerDimension/SeedWarp_ToPioneer (1)").InstantiateInactive().Rename("Prefab_DB_Seed").DontDestroyOnLoad(); + if (_brambleNodePrefab == null) _brambleNodePrefab = SearchUtilities.Find("DB_HubDimension_Body/Sector_HubDimension/Interactables_HubDimension/InnerWarp_ToCluster").InstantiateInactive().Rename("Prefab_DB_Node").DontDestroyOnLoad(); + } + public static void FinishPairingNodesForDimension(string dimensionName, AstroObject dimensionAO = null) { Logger.LogVerbose($"Pairing missed for {dimensionName}"); @@ -165,7 +171,9 @@ namespace NewHorizons.Builder.Props public static GameObject Make(GameObject go, Sector sector, BrambleNodeInfo config, IModBehaviour mod) { - var prefab = SearchUtilities.Find(config.isSeed ? _brambleSeedPrefabPath : _brambleNodePrefabPath); + InitPrefabs(); + + var prefab = config.isSeed ? _brambleSeedPrefab : _brambleNodePrefab; // Spawn the bramble node var brambleNode = prefab.InstantiateInactive(); diff --git a/NewHorizons/Builder/Props/GeyserBuilder.cs b/NewHorizons/Builder/Props/GeyserBuilder.cs index f9d3afaa..2ef988d8 100644 --- a/NewHorizons/Builder/Props/GeyserBuilder.cs +++ b/NewHorizons/Builder/Props/GeyserBuilder.cs @@ -5,9 +5,18 @@ namespace NewHorizons.Builder.Props { public static class GeyserBuilder { + private static GameObject _geyserPrefab; + + internal static void InitPrefab() + { + if (_geyserPrefab == null) _geyserPrefab = SearchUtilities.Find("TimberHearth_Body/Sector_TH/Interactables_TH/Geysers/Geyser_Village").InstantiateInactive().Rename("Prefab_TH_Geyser").DontDestroyOnLoad(); + } + public static void Make(GameObject planetGO, Sector sector, PropModule.GeyserInfo info) { - var geyserGO = SearchUtilities.Find("TimberHearth_Body/Sector_TH/Interactables_TH/Geysers/Geyser_Village").InstantiateInactive(); + InitPrefab(); + + var geyserGO = _geyserPrefab.InstantiateInactive(); geyserGO.transform.parent = sector?.transform ?? planetGO.transform; geyserGO.name = "Geyser"; diff --git a/NewHorizons/Builder/Props/NomaiTextBuilder.cs b/NewHorizons/Builder/Props/NomaiTextBuilder.cs index 5cab45e2..b2856f53 100644 --- a/NewHorizons/Builder/Props/NomaiTextBuilder.cs +++ b/NewHorizons/Builder/Props/NomaiTextBuilder.cs @@ -45,53 +45,11 @@ namespace NewHorizons.Builder.Props public static List GetChildArcPrefabs() { return _childArcPrefabs; } public static List GetGhostArcPrefabs() { return _ghostArcPrefabs; } - private static void InitPrefabs() + internal static void InitPrefabs() { - if (Main.Instance.CurrentStarSystem == "EyeOfTheUniverse") + // Just take every scroll and get the first arc + if (_arcPrefabs == null || _childArcPrefabs == null) { - // Just take every wall text and get the first arc - var existingArcs = GameObject.FindObjectsOfType().Select(x => x?.gameObject?.transform?.Find("Arc 1")?.gameObject).Where(x => x != null).ToArray(); - _arcPrefabs = new List(); - _childArcPrefabs = new List(); - _ghostArcPrefabs = new List(); - foreach (var existingArc in existingArcs) - { - var arc = existingArc.InstantiateInactive(); - arc.name = "Arc"; - _arcPrefabs.Add(arc); - } - - _scrollPrefab = new GameObject("Prefab_NOM_Scroll"); - _scrollPrefab.SetActive(false); - _scrollPrefab.transform.rotation = Quaternion.identity; - - _computerPrefab = new GameObject("Prefab_NOM_Computer"); - _computerPrefab.SetActive(false); - _computerPrefab.transform.rotation = Quaternion.identity; - - _preCrashComputerPrefab = new GameObject("Prefab_NOM_Vessel_Computer"); - _preCrashComputerPrefab.SetActive(false); - _preCrashComputerPrefab.transform.rotation = Quaternion.identity; - - _cairnPrefab = new GameObject("Prefab_NOM_Cairn"); - _cairnPrefab.SetActive(false); - _cairnPrefab.transform.rotation = Quaternion.identity; - - _recorderPrefab = new GameObject("Prefab_NOM_Recorder"); - _recorderPrefab.SetActive(false); - _recorderPrefab.transform.rotation = Quaternion.identity; - - _preCrashRecorderPrefab = SearchUtilities.Find("Vessel_Body/Sector_VesselBridge/Interactibles_VesselBridge/Prefab_NOM_Recorder").InstantiateInactive(); - _preCrashRecorderPrefab.name = "Prefab_NOM_Recorder_Vessel"; - _preCrashRecorderPrefab.transform.rotation = Quaternion.identity; - - _trailmarkerPrefab = new GameObject("Prefab_NOM_Trailmarker"); - _trailmarkerPrefab.SetActive(false); - _trailmarkerPrefab.transform.rotation = Quaternion.identity; - } - else - { - // Just take every scroll and get the first arc var existingArcs = GameObject.FindObjectsOfType().Select(x => x?._nomaiWallText?.gameObject?.transform?.Find("Arc 1")?.gameObject).Where(x => x != null).ToArray(); _arcPrefabs = new List(); _childArcPrefabs = new List(); @@ -99,59 +57,67 @@ namespace NewHorizons.Builder.Props { if (existingArc.GetComponent().material.name.Contains("Child")) { - var arc = existingArc.InstantiateInactive(); - arc.name = "Arc (Child)"; - _childArcPrefabs.Add(arc); + _childArcPrefabs.Add(existingArc.InstantiateInactive().Rename("Arc (Child)").DontDestroyOnLoad()); } else { - var arc = existingArc.InstantiateInactive(); - arc.name = "Arc"; - _arcPrefabs.Add(arc); + _arcPrefabs.Add(existingArc.InstantiateInactive().Rename("Arc").DontDestroyOnLoad()); } } + } + if (_ghostArcPrefabs == null) + { var existingGhostArcs = GameObject.FindObjectsOfType().Select(x => x?._textLine?.gameObject).Where(x => x != null).ToArray(); _ghostArcPrefabs = new List(); foreach (var existingArc in existingGhostArcs) { - var arc = existingArc.InstantiateInactive(); - arc.name = "Arc (Ghost)"; - _ghostArcPrefabs.Add(arc); + _ghostArcPrefabs.Add(existingArc.InstantiateInactive().Rename("Arc (Ghost)").DontDestroyOnLoad()); } + } - _scrollPrefab = SearchUtilities.Find("BrittleHollow_Body/Sector_BH/Sector_NorthHemisphere/Sector_NorthPole/Sector_HangingCity/Sector_HangingCity_District2/Interactables_HangingCity_District2/Prefab_NOM_Scroll").InstantiateInactive(); - _scrollPrefab.name = "Prefab_NOM_Scroll"; + if (_scrollPrefab == null) _scrollPrefab = SearchUtilities.Find("BrittleHollow_Body/Sector_BH/Sector_NorthHemisphere/Sector_NorthPole/Sector_HangingCity/Sector_HangingCity_District2/Interactables_HangingCity_District2/Prefab_NOM_Scroll").InstantiateInactive().Rename("Prefab_NOM_Scroll").DontDestroyOnLoad(); - _computerPrefab = SearchUtilities.Find("VolcanicMoon_Body/Sector_VM/Interactables_VM/Prefab_NOM_Computer").InstantiateInactive(); - _computerPrefab.name = "Prefab_NOM_Computer"; + if (_computerPrefab == null) + { + _computerPrefab = SearchUtilities.Find("VolcanicMoon_Body/Sector_VM/Interactables_VM/Prefab_NOM_Computer").InstantiateInactive().Rename("Prefab_NOM_Computer").DontDestroyOnLoad(); _computerPrefab.transform.rotation = Quaternion.identity; + } - _preCrashComputerPrefab = SearchUtilities.Find("BrittleHollow_Body/Sector_BH/Sector_EscapePodCrashSite/Sector_CrashFragment/EscapePod_Socket/Interactibles_EscapePod/Prefab_NOM_Vessel_Computer").InstantiateInactive(); - _preCrashComputerPrefab.name = "Prefab_NOM_Vessel_Computer"; + if (_preCrashComputerPrefab == null) + { + _preCrashComputerPrefab = SearchUtilities.Find("BrittleHollow_Body/Sector_BH/Sector_EscapePodCrashSite/Sector_CrashFragment/EscapePod_Socket/Interactibles_EscapePod/Prefab_NOM_Vessel_Computer").InstantiateInactive().Rename("Prefab_NOM_Vessel_Computer").DontDestroyOnLoad(); _preCrashComputerPrefab.transform.rotation = Quaternion.identity; + } - _cairnPrefab = SearchUtilities.Find("BrittleHollow_Body/Sector_BH/Sector_Crossroads/Interactables_Crossroads/Trailmarkers/Prefab_NOM_BH_Cairn_Arc (1)").InstantiateInactive(); - _cairnPrefab.name = "Prefab_NOM_Cairn"; + if (_cairnPrefab == null) + { + _cairnPrefab = SearchUtilities.Find("BrittleHollow_Body/Sector_BH/Sector_Crossroads/Interactables_Crossroads/Trailmarkers/Prefab_NOM_BH_Cairn_Arc (1)").InstantiateInactive().Rename("Prefab_NOM_Cairn").DontDestroyOnLoad(); _cairnPrefab.transform.rotation = Quaternion.identity; + } - _recorderPrefab = SearchUtilities.Find("Comet_Body/Prefab_NOM_Shuttle/Sector_NomaiShuttleInterior/Interactibles_NomaiShuttleInterior/Prefab_NOM_Recorder").InstantiateInactive(); - _recorderPrefab.name = "Prefab_NOM_Recorder"; + if (_recorderPrefab == null) + { + _recorderPrefab = SearchUtilities.Find("Comet_Body/Prefab_NOM_Shuttle/Sector_NomaiShuttleInterior/Interactibles_NomaiShuttleInterior/Prefab_NOM_Recorder").InstantiateInactive().Rename("Prefab_NOM_Recorder").DontDestroyOnLoad(); _recorderPrefab.transform.rotation = Quaternion.identity; + } - _preCrashRecorderPrefab = SearchUtilities.Find("BrittleHollow_Body/Sector_BH/Sector_EscapePodCrashSite/Sector_CrashFragment/Interactables_CrashFragment/Prefab_NOM_Recorder").InstantiateInactive(); - _preCrashRecorderPrefab.name = "Prefab_NOM_Recorder_Vessel"; + if (_preCrashRecorderPrefab == null) + { + _preCrashRecorderPrefab = SearchUtilities.Find("BrittleHollow_Body/Sector_BH/Sector_EscapePodCrashSite/Sector_CrashFragment/Interactables_CrashFragment/Prefab_NOM_Recorder").InstantiateInactive().Rename("Prefab_NOM_Recorder_Vessel").DontDestroyOnLoad(); _preCrashRecorderPrefab.transform.rotation = Quaternion.identity; + } - _trailmarkerPrefab = SearchUtilities.Find("BrittleHollow_Body/Sector_BH/Sector_NorthHemisphere/Sector_NorthPole/Sector_HangingCity/Sector_HangingCity_District2/Interactables_HangingCity_District2/Prefab_NOM_Sign").InstantiateInactive(); - _trailmarkerPrefab.name = "Prefab_NOM_Trailmarker"; + if (_trailmarkerPrefab == null) + { + _trailmarkerPrefab = SearchUtilities.Find("BrittleHollow_Body/Sector_BH/Sector_NorthHemisphere/Sector_NorthPole/Sector_HangingCity/Sector_HangingCity_District2/Interactables_HangingCity_District2/Prefab_NOM_Sign").InstantiateInactive().Rename("Prefab_NOM_Trailmarker").DontDestroyOnLoad(); _trailmarkerPrefab.transform.rotation = Quaternion.identity; } } public static GameObject Make(GameObject planetGO, Sector sector, PropModule.NomaiTextInfo info, IModBehaviour mod) { - if (_scrollPrefab == null) InitPrefabs(); + InitPrefabs(); var xmlPath = File.ReadAllText(mod.ModHelper.Manifest.ModFolderPath + info.xmlFile); diff --git a/NewHorizons/Builder/Props/ProjectionBuilder.cs b/NewHorizons/Builder/Props/ProjectionBuilder.cs index f6cb399d..b03a6faf 100644 --- a/NewHorizons/Builder/Props/ProjectionBuilder.cs +++ b/NewHorizons/Builder/Props/ProjectionBuilder.cs @@ -13,8 +13,49 @@ namespace NewHorizons.Builder.Props { private static GameObject _slideReelPrefab; private static GameObject _autoPrefab; + private static GameObject _visionTorchDetectorPrefab; + private static GameObject _standingVisionTorchPrefab; private static readonly int EmissionMap = Shader.PropertyToID("_EmissionMap"); + internal static void InitPrefabs() + { + if (_slideReelPrefab == null) + { + _slideReelPrefab = SearchUtilities.Find("RingWorld_Body/Sector_RingInterior/Sector_Zone1/Sector_SlideBurningRoom_Zone1/Interactables_SlideBurningRoom_Zone1/Prefab_IP_SecretAlcove/RotationPivot/SlideReelSocket/Prefab_IP_Reel_1_LibraryPath")?.gameObject?.InstantiateInactive()?.Rename("Prefab_IP_Reel")?.DontDestroyOnLoad(); + if (_slideReelPrefab == null) + Logger.LogWarning($"Tried to make slide reel prefab but couldn't. Do you have the DLC installed?"); + else + _slideReelPrefab.AddComponent()._destroyOnDLCNotOwned = true; + } + + if (_autoPrefab == null) + { + _autoPrefab = SearchUtilities.Find("RingWorld_Body/Sector_RingInterior/Sector_Zone4/Sector_BlightedShore/Sector_JammingControlRoom_Zone4/Interactables_JammingControlRoom_Zone4/AutoProjector_SignalJammer/Prefab_IP_AutoProjector_SignalJammer")?.gameObject?.InstantiateInactive()?.Rename("Prefab_IP_AutoProjector")?.DontDestroyOnLoad(); + if (_autoPrefab == null) + Logger.LogWarning($"Tried to make auto projector prefab but couldn't. Do you have the DLC installed?"); + else + _autoPrefab.AddComponent()._destroyOnDLCNotOwned = true; + } + + if (_visionTorchDetectorPrefab == null) + { + _visionTorchDetectorPrefab = SearchUtilities.Find("DreamWorld_Body/Sector_DreamWorld/Sector_Underground/Sector_PrisonCell/Ghosts_PrisonCell/GhostDirector_Prisoner/Prefab_IP_GhostBird_Prisoner/Ghostbird_IP_ANIM/Ghostbird_Skin_01:Ghostbird_Rig_V01:Base/Ghostbird_Skin_01:Ghostbird_Rig_V01:Root/Ghostbird_Skin_01:Ghostbird_Rig_V01:Spine01/Ghostbird_Skin_01:Ghostbird_Rig_V01:Spine02/Ghostbird_Skin_01:Ghostbird_Rig_V01:Spine03/Ghostbird_Skin_01:Ghostbird_Rig_V01:Spine04/Ghostbird_Skin_01:Ghostbird_Rig_V01:Neck01/Ghostbird_Skin_01:Ghostbird_Rig_V01:Neck02/Ghostbird_Skin_01:Ghostbird_Rig_V01:Head/PrisonerHeadDetector")?.gameObject?.InstantiateInactive()?.Rename("Prefab_IP_VisionTorchDetector")?.DontDestroyOnLoad(); + if (_visionTorchDetectorPrefab == null) + Logger.LogWarning($"Tried to make vision torch detector prefab but couldn't. Do you have the DLC installed?"); + else + _visionTorchDetectorPrefab.AddComponent()._destroyOnDLCNotOwned = true; + } + + if (_standingVisionTorchPrefab == null) + { + _standingVisionTorchPrefab = SearchUtilities.Find("RingWorld_Body/Sector_RingWorld/Sector_SecretEntrance/Interactibles_SecretEntrance/Experiment_1/VisionTorchApparatus/VisionTorchRoot/Prefab_IP_VisionTorchProjector")?.gameObject?.InstantiateInactive()?.Rename("Prefab_IP_VisionTorchProjector")?.DontDestroyOnLoad(); + if (_standingVisionTorchPrefab == null) + Logger.LogWarning($"Tried to make standing vision torch prefab but couldn't. Do you have the DLC installed?"); + else + _standingVisionTorchPrefab.AddComponent()._destroyOnDLCNotOwned = true; + } + } + public static void Make(GameObject go, Sector sector, PropModule.ProjectionInfo info, IModBehaviour mod) { switch (info.type) @@ -37,18 +78,11 @@ namespace NewHorizons.Builder.Props } } - private static void MakeSlideReel(GameObject planetGO, Sector sector, PropModule.ProjectionInfo info, IModBehaviour mod) + private static GameObject MakeSlideReel(GameObject planetGO, Sector sector, PropModule.ProjectionInfo info, IModBehaviour mod) { - if (_slideReelPrefab == null) - { - _slideReelPrefab = SearchUtilities.Find("RingWorld_Body/Sector_RingInterior/Sector_Zone1/Sector_SlideBurningRoom_Zone1/Interactables_SlideBurningRoom_Zone1/Prefab_IP_SecretAlcove/RotationPivot/SlideReelSocket/Prefab_IP_Reel_1_LibraryPath")?.gameObject?.InstantiateInactive(); - if (_slideReelPrefab == null) - { - Logger.LogWarning($"Tried to make a slide reel but couldn't. Do you have the DLC installed?"); - return; - } - _slideReelPrefab.name = "Prefab_IP_Reel"; - } + InitPrefabs(); + + if (_slideReelPrefab == null) return null; var slideReelObj = _slideReelPrefab.InstantiateInactive(); slideReelObj.name = $"Prefab_IP_Reel_{mod.ModHelper.Manifest.Name}"; @@ -146,20 +180,15 @@ namespace NewHorizons.Builder.Props StreamingHandler.SetUpStreaming(slideReelObj, sector); slideReelObj.SetActive(true); + + return slideReelObj; } - public static void MakeAutoProjector(GameObject planetGO, Sector sector, PropModule.ProjectionInfo info, IModBehaviour mod) + public static GameObject MakeAutoProjector(GameObject planetGO, Sector sector, PropModule.ProjectionInfo info, IModBehaviour mod) { - if (_autoPrefab == null) - { - _autoPrefab = SearchUtilities.Find("RingWorld_Body/Sector_RingInterior/Sector_Zone4/Sector_BlightedShore/Sector_JammingControlRoom_Zone4/Interactables_JammingControlRoom_Zone4/AutoProjector_SignalJammer/Prefab_IP_AutoProjector_SignalJammer")?.gameObject?.InstantiateInactive(); - if (_autoPrefab == null) - { - Logger.LogWarning($"Tried to make a auto projector but couldn't. Do you have the DLC installed?"); - return; - } - _autoPrefab.name = "Prefab_IP_AutoProjector"; - } + InitPrefabs(); + + if (_autoPrefab == null) return null; var projectorObj = _autoPrefab.InstantiateInactive(); projectorObj.name = $"Prefab_IP_AutoProjector_{mod.ModHelper.Manifest.Name}"; @@ -215,20 +244,24 @@ namespace NewHorizons.Builder.Props lens.materials[1].SetTexture(EmissionMap, slideCollection.slides[0]._image); projectorObj.SetActive(true); + + return projectorObj; } // Makes a target for a vision torch to scan public static GameObject MakeMindSlidesTarget(GameObject planetGO, Sector sector, PropModule.ProjectionInfo info, IModBehaviour mod) { + InitPrefabs(); + + if (_visionTorchDetectorPrefab == null) return null; + // spawn a trigger for the vision torch - var path = "DreamWorld_Body/Sector_DreamWorld/Sector_Underground/Sector_PrisonCell/Ghosts_PrisonCell/GhostDirector_Prisoner/Prefab_IP_GhostBird_Prisoner/Ghostbird_IP_ANIM/Ghostbird_Skin_01:Ghostbird_Rig_V01:Base/Ghostbird_Skin_01:Ghostbird_Rig_V01:Root/Ghostbird_Skin_01:Ghostbird_Rig_V01:Spine01/Ghostbird_Skin_01:Ghostbird_Rig_V01:Spine02/Ghostbird_Skin_01:Ghostbird_Rig_V01:Spine03/Ghostbird_Skin_01:Ghostbird_Rig_V01:Spine04/Ghostbird_Skin_01:Ghostbird_Rig_V01:Neck01/Ghostbird_Skin_01:Ghostbird_Rig_V01:Neck02/Ghostbird_Skin_01:Ghostbird_Rig_V01:Head/PrisonerHeadDetector"; - var prefab = SearchUtilities.Find(path); var detailInfo = new PropModule.DetailInfo() { position = info.position, scale = 2 }; - var g = DetailBuilder.Make(planetGO, sector, prefab, detailInfo); + var g = DetailBuilder.Make(planetGO, sector, _visionTorchDetectorPrefab, detailInfo); if (!string.IsNullOrEmpty(info.parentPath)) { @@ -286,15 +319,17 @@ namespace NewHorizons.Builder.Props public static GameObject MakeStandingVisionTorch(GameObject planetGO, Sector sector, PropModule.ProjectionInfo info, IModBehaviour mod) { + InitPrefabs(); + + if (_standingVisionTorchPrefab == null) return null; + // Spawn the torch itself - var path = "RingWorld_Body/Sector_RingWorld/Sector_SecretEntrance/Interactibles_SecretEntrance/Experiment_1/VisionTorchApparatus/VisionTorchRoot/Prefab_IP_VisionTorchProjector"; - var prefab = SearchUtilities.Find(path); var detailInfo = new PropModule.DetailInfo() { position = info.position, rotation = info.rotation }; - var standingTorch = DetailBuilder.Make(planetGO, sector, prefab, detailInfo); + var standingTorch = DetailBuilder.Make(planetGO, sector, _standingVisionTorchPrefab, detailInfo); if (!string.IsNullOrEmpty(info.parentPath)) { diff --git a/NewHorizons/Builder/Props/RaftBuilder.cs b/NewHorizons/Builder/Props/RaftBuilder.cs index 6ad6277f..4f22b1bb 100644 --- a/NewHorizons/Builder/Props/RaftBuilder.cs +++ b/NewHorizons/Builder/Props/RaftBuilder.cs @@ -1,4 +1,4 @@ -using NewHorizons.Components; +using NewHorizons.Components; using NewHorizons.External.Modules; using NewHorizons.Handlers; using NewHorizons.Utility; @@ -10,18 +10,26 @@ namespace NewHorizons.Builder.Props { private static GameObject _prefab; - public static void Make(GameObject planetGO, Sector sector, PropModule.RaftInfo info, OWRigidbody planetBody) + internal static void InitPrefab() { if (_prefab == null) { - _prefab = GameObject.FindObjectOfType()?.gameObject?.InstantiateInactive(); + _prefab = GameObject.FindObjectOfType()?.gameObject?.InstantiateInactive()?.Rename("Raft_Body_Prefab")?.DontDestroyOnLoad(); if (_prefab == null) { Logger.LogWarning($"Tried to make a raft but couldn't. Do you have the DLC installed?"); return; } - _prefab.name = "Raft_Body_Prefab"; + else + _prefab.AddComponent()._destroyOnDLCNotOwned = true; } + } + + public static void Make(GameObject planetGO, Sector sector, PropModule.RaftInfo info, OWRigidbody planetBody) + { + InitPrefab(); + + if (_prefab == null) return; GameObject raftObject = _prefab.InstantiateInactive(); raftObject.name = "Raft_Body"; diff --git a/NewHorizons/Builder/Props/RemoteBuilder.cs b/NewHorizons/Builder/Props/RemoteBuilder.cs index b82ec0e5..21f5d4b7 100644 --- a/NewHorizons/Builder/Props/RemoteBuilder.cs +++ b/NewHorizons/Builder/Props/RemoteBuilder.cs @@ -17,92 +17,102 @@ namespace NewHorizons.Builder.Props private static GameObject _whiteboardPrefab; private static GameObject _shareStonePrefab; - private static void InitPrefabs() + internal static void InitPrefabs() { - _decalMaterial = new Material(Shader.Find("Standard (Decal)")); - _decalMaterial.name = "Decal"; - _decalMaterial.SetTexture("_MainTex", Texture2D.whiteTexture); - _decalMaterial.SetTexture("_EmissionMap", Texture2D.whiteTexture); - _decalMaterial.SetFloat("_Glossiness", 0); - _decalMaterial.SetFloat("_BumpScale", 0); - _decalMaterial.SetColor("_Color", new Color(0.3529412f, 0.3843137f, 1)); - _decalMaterial.SetColor("_EmissionColor", new Color(0.2422811f, 0.2917706f, 2.440062f)); - _decalMaterialGold = new Material(_decalMaterial); - _decalMaterialGold.name = "DecalGold"; - _decalMaterialGold.SetColor("_Color", new Color(1, 0.6392157f, 0.3803922f)); - _decalMaterialGold.SetColor("_EmissionColor", new Color(1, 0.3662527f, 0.1195384f)); + if (_decalMaterial == null) + { + _decalMaterial = new Material(Shader.Find("Standard (Decal)")).DontDestroyOnLoad(); + _decalMaterial.name = "Decal"; + _decalMaterial.SetTexture("_MainTex", Texture2D.whiteTexture); + _decalMaterial.SetTexture("_EmissionMap", Texture2D.whiteTexture); + _decalMaterial.SetFloat("_Glossiness", 0); + _decalMaterial.SetFloat("_BumpScale", 0); + _decalMaterial.SetColor("_Color", new Color(0.3529412f, 0.3843137f, 1)); + _decalMaterial.SetColor("_EmissionColor", new Color(0.2422811f, 0.2917706f, 2.440062f)); + _decalMaterialGold = new Material(_decalMaterial); + _decalMaterialGold.name = "DecalGold"; + _decalMaterialGold.SetColor("_Color", new Color(1, 0.6392157f, 0.3803922f)); + _decalMaterialGold.SetColor("_EmissionColor", new Color(1, 0.3662527f, 0.1195384f)); + } - _remoteCameraPlatformPrefab = SearchUtilities.Find("OrbitalProbeCannon_Body/Sector_OrbitalProbeCannon/Sector_Module_Broken/Interactables_Module_Broken/Prefab_NOM_RemoteViewer").InstantiateInactive(); - _remoteCameraPlatformPrefab.name = "Prefab_NOM_RemoteViewer"; - var remoteCameraPlatform = _remoteCameraPlatformPrefab.GetComponent(); - remoteCameraPlatform.enabled = true; - remoteCameraPlatform._id = NomaiRemoteCameraPlatform.ID.None; - remoteCameraPlatform._platformState = NomaiRemoteCameraPlatform.State.Disconnected; - remoteCameraPlatform._dataPointID = string.Empty; - remoteCameraPlatform._visualSector = null; - var AstroBodySymbolRenderer = _remoteCameraPlatformPrefab.FindChild("PedestalAnchor/Prefab_NOM_SharedPedestal/SharedPedestal_side01_bottom_jnt/SharedPedestal_side01_top_jnt/AstroBodySymbolRenderer"); - var quad = GameObject.CreatePrimitive(PrimitiveType.Quad); - quad.transform.parent = AstroBodySymbolRenderer.transform.parent; - quad.transform.localPosition = AstroBodySymbolRenderer.transform.localPosition; - quad.transform.localRotation = AstroBodySymbolRenderer.transform.localRotation; - quad.transform.localScale = AstroBodySymbolRenderer.transform.localScale; - quad.AddComponent(); - quad.GetComponent().sharedMaterial = _decalMaterial; - quad.name = "AstroBodySymbolRenderer"; - GameObject.DestroyImmediate(AstroBodySymbolRenderer); + if (_remoteCameraPlatformPrefab == null) + { + _remoteCameraPlatformPrefab = SearchUtilities.Find("OrbitalProbeCannon_Body/Sector_OrbitalProbeCannon/Sector_Module_Broken/Interactables_Module_Broken/Prefab_NOM_RemoteViewer").InstantiateInactive().Rename("Prefab_NOM_RemoteViewer").DontDestroyOnLoad(); + var remoteCameraPlatform = _remoteCameraPlatformPrefab.GetComponent(); + remoteCameraPlatform.enabled = true; + remoteCameraPlatform._id = NomaiRemoteCameraPlatform.ID.None; + remoteCameraPlatform._platformState = NomaiRemoteCameraPlatform.State.Disconnected; + remoteCameraPlatform._dataPointID = string.Empty; + remoteCameraPlatform._visualSector = null; + var AstroBodySymbolRenderer = _remoteCameraPlatformPrefab.FindChild("PedestalAnchor/Prefab_NOM_SharedPedestal/SharedPedestal_side01_bottom_jnt/SharedPedestal_side01_top_jnt/AstroBodySymbolRenderer"); + var quad = GameObject.CreatePrimitive(PrimitiveType.Quad); + quad.transform.parent = AstroBodySymbolRenderer.transform.parent; + quad.transform.localPosition = AstroBodySymbolRenderer.transform.localPosition; + quad.transform.localRotation = AstroBodySymbolRenderer.transform.localRotation; + quad.transform.localScale = AstroBodySymbolRenderer.transform.localScale; + quad.AddComponent(); + quad.GetComponent().sharedMaterial = _decalMaterial; + quad.name = "AstroBodySymbolRenderer"; + GameObject.DestroyImmediate(AstroBodySymbolRenderer); + } - _whiteboardPrefab = SearchUtilities.Find("OrbitalProbeCannon_Body/Sector_OrbitalProbeCannon/Sector_Module_Broken/Interactables_Module_Broken/Prefab_NOM_Whiteboard_Shared").InstantiateInactive(); - _whiteboardPrefab.name = "Prefab_NOM_Whiteboard_Shared"; - var whiteboard = _whiteboardPrefab.GetComponent(); - whiteboard.enabled = true; - whiteboard._id = NomaiRemoteCameraPlatform.ID.None; - _whiteboardPrefab.FindChild("ArcSocket").transform.DestroyAllChildrenImmediate(); - whiteboard._remoteIDs = new NomaiRemoteCameraPlatform.ID[0]; - whiteboard._nomaiTexts = new NomaiWallText[0]; - var AstroBodySymbolRendererW = _whiteboardPrefab.FindChild("PedestalAnchor/Prefab_NOM_SharedPedestal/SharedPedestal_side01_bottom_jnt/SharedPedestal_side01_top_jnt/AstroBodySymbolRenderer"); - var quadW = GameObject.CreatePrimitive(PrimitiveType.Quad); - quadW.transform.parent = AstroBodySymbolRendererW.transform.parent; - quadW.transform.localPosition = AstroBodySymbolRendererW.transform.localPosition; - quadW.transform.localRotation = AstroBodySymbolRendererW.transform.localRotation; - quadW.transform.localScale = AstroBodySymbolRendererW.transform.localScale; - quadW.AddComponent(); - quadW.GetComponent().sharedMaterial = _decalMaterial; - quadW.name = "AstroBodySymbolRenderer"; - GameObject.DestroyImmediate(AstroBodySymbolRendererW); + if (_whiteboardPrefab == null) + { + _whiteboardPrefab = SearchUtilities.Find("OrbitalProbeCannon_Body/Sector_OrbitalProbeCannon/Sector_Module_Broken/Interactables_Module_Broken/Prefab_NOM_Whiteboard_Shared").InstantiateInactive().Rename("Prefab_NOM_Whiteboard_Shared").DontDestroyOnLoad(); + var whiteboard = _whiteboardPrefab.GetComponent(); + whiteboard.enabled = true; + whiteboard._id = NomaiRemoteCameraPlatform.ID.None; + _whiteboardPrefab.FindChild("ArcSocket").transform.DestroyAllChildrenImmediate(); + whiteboard._remoteIDs = new NomaiRemoteCameraPlatform.ID[0]; + whiteboard._nomaiTexts = new NomaiWallText[0]; + var AstroBodySymbolRendererW = _whiteboardPrefab.FindChild("PedestalAnchor/Prefab_NOM_SharedPedestal/SharedPedestal_side01_bottom_jnt/SharedPedestal_side01_top_jnt/AstroBodySymbolRenderer"); + var quadW = GameObject.CreatePrimitive(PrimitiveType.Quad); + quadW.transform.parent = AstroBodySymbolRendererW.transform.parent; + quadW.transform.localPosition = AstroBodySymbolRendererW.transform.localPosition; + quadW.transform.localRotation = AstroBodySymbolRendererW.transform.localRotation; + quadW.transform.localScale = AstroBodySymbolRendererW.transform.localScale; + quadW.AddComponent(); + quadW.GetComponent().sharedMaterial = _decalMaterial; + quadW.name = "AstroBodySymbolRenderer"; + GameObject.DestroyImmediate(AstroBodySymbolRendererW); + } - GameObject stone = new GameObject("ShareStoneFallback"); - stone.layer = LayerMask.NameToLayer("Interactible"); - stone.SetActive(false); - SphereCollider sc = stone.AddComponent(); - sc.center = Vector3.zero; - sc.radius = 0.4f; - sc.isTrigger = false; - OWCollider owc = stone.AddComponent(); - owc._collider = sc; - SharedStone item = stone.AddComponent(); - item._connectedPlatform = NomaiRemoteCameraPlatform.ID.None; - item._animDuration = 0.4f; - item._animOffsetY = 0.08f; - GameObject animRoot = new GameObject("AnimRoot"); - animRoot.transform.parent = stone.transform; - TransformAnimator transformAnimator = animRoot.AddComponent(); - item._animator = transformAnimator; - OWRenderer renderer = SearchUtilities.FindResourceOfTypeAndName("Props_NOM_SharedStone"); - if (renderer != null) GameObject.Instantiate(renderer.gameObject, animRoot.transform); - GameObject planetDecal = GameObject.CreatePrimitive(PrimitiveType.Quad); - planetDecal.name = "PlanetDecal"; - planetDecal.transform.parent = animRoot.transform; - planetDecal.transform.localPosition = new Vector3(0, 0.053f, 0); - planetDecal.transform.localEulerAngles = new Vector3(90, 0, 0); - planetDecal.transform.localScale = new Vector3(0.4f, 0.4f, 0.4f); - planetDecal.AddComponent(); - planetDecal.GetComponent().sharedMaterial = _decalMaterialGold; - _shareStonePrefab = stone; + if (_shareStonePrefab == null) + { + GameObject stone = new GameObject("ShareStoneFallback"); + stone.layer = LayerMask.NameToLayer("Interactible"); + stone.SetActive(false); + SphereCollider sc = stone.AddComponent(); + sc.center = Vector3.zero; + sc.radius = 0.4f; + sc.isTrigger = false; + OWCollider owc = stone.AddComponent(); + owc._collider = sc; + SharedStone item = stone.AddComponent(); + item._connectedPlatform = NomaiRemoteCameraPlatform.ID.None; + item._animDuration = 0.4f; + item._animOffsetY = 0.08f; + GameObject animRoot = new GameObject("AnimRoot"); + animRoot.transform.parent = stone.transform; + TransformAnimator transformAnimator = animRoot.AddComponent(); + item._animator = transformAnimator; + OWRenderer renderer = SearchUtilities.FindResourceOfTypeAndName("Props_NOM_SharedStone"); + if (renderer != null) GameObject.Instantiate(renderer.gameObject, animRoot.transform); + GameObject planetDecal = GameObject.CreatePrimitive(PrimitiveType.Quad); + planetDecal.name = "PlanetDecal"; + planetDecal.transform.parent = animRoot.transform; + planetDecal.transform.localPosition = new Vector3(0, 0.053f, 0); + planetDecal.transform.localEulerAngles = new Vector3(90, 0, 0); + planetDecal.transform.localScale = new Vector3(0.4f, 0.4f, 0.4f); + planetDecal.AddComponent(); + planetDecal.GetComponent().sharedMaterial = _decalMaterialGold; + _shareStonePrefab = stone.DontDestroyOnLoad(); + } } public static void Make(GameObject go, Sector sector, PropModule.RemoteInfo info, IModBehaviour mod) { - if (_shareStonePrefab == null) InitPrefabs(); + InitPrefabs(); var id = RemoteHandler.GetPlatformID(info.id); diff --git a/NewHorizons/Builder/Props/TornadoBuilder.cs b/NewHorizons/Builder/Props/TornadoBuilder.cs index 9f962fe5..28bace04 100644 --- a/NewHorizons/Builder/Props/TornadoBuilder.cs +++ b/NewHorizons/Builder/Props/TornadoBuilder.cs @@ -23,22 +23,13 @@ namespace NewHorizons.Builder.Props private static readonly int MainTex = Shader.PropertyToID("_MainTex"); private static readonly int FresnelColor = Shader.PropertyToID("_FresnelColor"); - public static void Make(GameObject planetGO, Sector sector, PropModule.TornadoInfo info, bool hasClouds) + internal static void InitPrefabs() { - if (_upPrefab == null) - { - _upPrefab = SearchUtilities.Find("BrittleHollow_Body/Sector_BH/Sector_SouthHemisphere/Sector_SouthPole/Sector_Observatory/Interactables_Observatory/MockUpTornado").InstantiateInactive(); - _upPrefab.name = "Tornado_Up_Prefab"; - } - if (_downPrefab == null) - { - _downPrefab = SearchUtilities.Find("BrittleHollow_Body/Sector_BH/Sector_SouthHemisphere/Sector_SouthPole/Sector_Observatory/Interactables_Observatory/MockDownTornado").InstantiateInactive(); - _downPrefab.name = "Tornado_Down_Prefab"; - } + if (_upPrefab == null) _upPrefab = SearchUtilities.Find("BrittleHollow_Body/Sector_BH/Sector_SouthHemisphere/Sector_SouthPole/Sector_Observatory/Interactables_Observatory/MockUpTornado").InstantiateInactive().Rename("Tornado_Up_Prefab").DontDestroyOnLoad(); + if (_downPrefab == null) _downPrefab = SearchUtilities.Find("BrittleHollow_Body/Sector_BH/Sector_SouthHemisphere/Sector_SouthPole/Sector_Observatory/Interactables_Observatory/MockDownTornado").InstantiateInactive().Rename("Tornado_Down_Prefab").DontDestroyOnLoad(); if (_hurricanePrefab == null) { - _hurricanePrefab = SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Sector_GDInterior/Tornadoes_GDInterior/Hurricane").InstantiateInactive(); - _hurricanePrefab.name = "Hurricane_Prefab"; + _hurricanePrefab = SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Sector_GDInterior/Tornadoes_GDInterior/Hurricane").InstantiateInactive().Rename("Hurricane_Prefab").DontDestroyOnLoad(); // For some reason they put the hurricane at the origin and offset all its children (450) // Increasing by 40 will keep the bottom above the ground foreach (Transform child in _hurricanePrefab.transform) @@ -50,19 +41,14 @@ namespace NewHorizons.Builder.Props renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; } } - if (_soundPrefab == null) - { - _soundPrefab = SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Sector_GDInterior/Tornadoes_GDInterior/SouthernTornadoes/DownTornado_Pivot/DownTornado/AudioRail").InstantiateInactive(); - _soundPrefab.name = "AudioRail_Prefab"; - } - if (_mainTexture == null) - { - _mainTexture = ImageUtilities.GetTexture(Main.Instance, "Assets/textures/Tornado_BH_Cyclone_02_d.png"); - } - if (_detailTexture == null) - { - _detailTexture = ImageUtilities.GetTexture(Main.Instance, "Assets/textures/Tornado_BH_CycloneDetail_d.png"); - } + if (_soundPrefab == null) _soundPrefab = SearchUtilities.Find("GiantsDeep_Body/Sector_GD/Sector_GDInterior/Tornadoes_GDInterior/SouthernTornadoes/DownTornado_Pivot/DownTornado/AudioRail").InstantiateInactive().Rename("AudioRail_Prefab").DontDestroyOnLoad(); + if (_mainTexture == null) _mainTexture = ImageUtilities.GetTexture(Main.Instance, "Assets/textures/Tornado_BH_Cyclone_02_d.png"); + if (_detailTexture == null) _detailTexture = ImageUtilities.GetTexture(Main.Instance, "Assets/textures/Tornado_BH_CycloneDetail_d.png"); + } + + public static void Make(GameObject planetGO, Sector sector, PropModule.TornadoInfo info, bool hasClouds) + { + InitPrefabs(); Vector3 position; if (info.position != null) diff --git a/NewHorizons/Builder/Props/VolcanoBuilder.cs b/NewHorizons/Builder/Props/VolcanoBuilder.cs index a5ddd810..211cdb6d 100644 --- a/NewHorizons/Builder/Props/VolcanoBuilder.cs +++ b/NewHorizons/Builder/Props/VolcanoBuilder.cs @@ -10,11 +10,18 @@ namespace NewHorizons.Builder.Props private static readonly int Color1 = Shader.PropertyToID("_Color"); private static readonly int EmissionColor = Shader.PropertyToID("_EmissionColor"); + private static GameObject _meteorLauncherPrefab; + + internal static void InitPrefab() + { + if (_meteorLauncherPrefab == null) _meteorLauncherPrefab = SearchUtilities.Find("VolcanicMoon_Body/Sector_VM/Effects_VM/VolcanoPivot (2)/MeteorLauncher").InstantiateInactive().Rename("Prefab_VM_MeteorLauncher").DontDestroyOnLoad(); + } + public static void Make(GameObject planetGO, Sector sector, PropModule.VolcanoInfo info) { - var prefab = SearchUtilities.Find("VolcanicMoon_Body/Sector_VM/Effects_VM/VolcanoPivot (2)/MeteorLauncher"); + InitPrefab(); - var launcherGO = prefab.InstantiateInactive(); + var launcherGO = _meteorLauncherPrefab.InstantiateInactive(); launcherGO.transform.parent = sector.transform; launcherGO.transform.position = planetGO.transform.TransformPoint(info.position == null ? Vector3.zero : (Vector3)info.position); launcherGO.transform.rotation = Quaternion.FromToRotation(launcherGO.transform.TransformDirection(Vector3.up), ((Vector3)info.position).normalized).normalized; diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index c1e510e2..8b6e13b7 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -1,6 +1,7 @@ using HarmonyLib; using NewHorizons.Builder.Atmosphere; using NewHorizons.Builder.Body; +using NewHorizons.Builder.General; using NewHorizons.Builder.Props; using NewHorizons.Components; using NewHorizons.Components.Orbital; @@ -55,6 +56,7 @@ namespace NewHorizons public string CurrentStarSystem => _currentStarSystem; public bool IsWarpingFromShip { get; private set; } = false; public bool IsWarpingFromVessel { get; private set; } = false; + public bool IsWarpingBackToEye { get; internal set; } = false; public bool WearingSuit { get; private set; } = false; public bool IsChangingStarSystem { get; private set; } = false; @@ -254,7 +256,52 @@ namespace NewHorizons var isCreditsFinal = scene.name == LoadManager.SceneToName(OWScene.Credits_Final); var isPostCredits = scene.name == LoadManager.SceneToName(OWScene.PostCreditsScene); + if (isSolarSystem) + { + try + { + AtmosphereBuilder.InitPrefabs(); + BrambleDimensionBuilder.InitPrefabs(); + BrambleNodeBuilder.InitPrefabs(); + CloudsBuilder.InitPrefabs(); + CometTailBuilder.InitPrefab(); + DetectorBuilder.InitPrefabs(); + EffectsBuilder.InitPrefabs(); + FogBuilder.InitPrefabs(); + FunnelBuilder.InitPrefabs(); + GeometryBuilder.InitPrefab(); + GeyserBuilder.InitPrefab(); + LavaBuilder.InitPrefabs(); + NomaiTextBuilder.InitPrefabs(); + RemoteBuilder.InitPrefabs(); + SandBuilder.InitPrefabs(); + SingularityBuilder.InitPrefabs(); + StarBuilder.InitPrefabs(); + SupernovaEffectBuilder.InitPrefabs(); + TornadoBuilder.InitPrefabs(); + VolcanoBuilder.InitPrefab(); + VolumesBuilder.InitPrefabs(); + WaterBuilder.InitPrefabs(); + + ProjectionBuilder.InitPrefabs(); + CloakBuilder.InitPrefab(); + RaftBuilder.InitPrefab(); + } + catch (Exception e) + { + Logger.LogError($"Couldn't init prefabs:\n{e}"); + } + } + if (isEyeOfTheUniverse) _currentStarSystem = "EyeOfTheUniverse"; + else if (IsWarpingBackToEye) + { + IsWarpingBackToEye = false; + OWTime.Pause(OWTime.PauseType.Loading); + LoadManager.LoadSceneImmediate(OWScene.EyeOfTheUniverse); + OWTime.Unpause(OWTime.PauseType.Loading); + return; + } if (!SystemDict.ContainsKey(_currentStarSystem) || !BodyDict.ContainsKey(_currentStarSystem)) { diff --git a/NewHorizons/Patches/EyeOfTheUniversePatches.cs b/NewHorizons/Patches/EyeOfTheUniversePatches.cs index f942a2ce..33c51a13 100644 --- a/NewHorizons/Patches/EyeOfTheUniversePatches.cs +++ b/NewHorizons/Patches/EyeOfTheUniversePatches.cs @@ -15,7 +15,7 @@ namespace NewHorizons.Patches private static void OnLoadScene(OWScene scene) { - if (scene == OWScene.SolarSystem) + if (scene == OWScene.SolarSystem && !Main.Instance.IsWarpingBackToEye) { PlayerData.SaveEyeCompletion(); @@ -39,7 +39,12 @@ namespace NewHorizons.Patches public static void SubmitActionLoadScene_ConfirmSubmit(SubmitActionLoadScene __instance) { // Title screen can warp you to eye and cause problems. - if (__instance._sceneToLoad == SubmitActionLoadScene.LoadableScenes.EYE) Main.Instance._currentStarSystem = "EyeOfTheUniverse"; + if (__instance._sceneToLoad == SubmitActionLoadScene.LoadableScenes.EYE) + { + Utility.Logger.LogWarning("Warping to solar system and then back to eye"); + Main.Instance.IsWarpingBackToEye = true; + __instance._sceneToLoad = SubmitActionLoadScene.LoadableScenes.GAME; + } } [HarmonyPrefix] diff --git a/NewHorizons/Utility/NewHorizonExtensions.cs b/NewHorizons/Utility/NewHorizonExtensions.cs index 389975be..9fb057a5 100644 --- a/NewHorizons/Utility/NewHorizonExtensions.cs +++ b/NewHorizons/Utility/NewHorizonExtensions.cs @@ -187,6 +187,29 @@ namespace NewHorizons.Utility return copy; } + public static T DontDestroyOnLoad(this T target) where T : UnityEngine.Object + { + UnityEngine.Object.DontDestroyOnLoad(target); + return target; + } + + public static Material[] MakePrefabMaterials(this Material[] target) + { + var materials = new List(); + foreach (var material in target) + { + materials.Add(new Material(material).DontDestroyOnLoad()); + } + return materials.ToArray(); + } + + public static T Rename(this T target, string name) where T : UnityEngine.Object + { + target.name = name; + return target; + } + + public static Quaternion TransformRotation(this Transform transform, Quaternion localRotation) { return transform.rotation * localRotation;