From 9faa08a0c8a5eeb6106a65aaa11a161d4072e95f Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 5 May 2022 22:27:54 -0400 Subject: [PATCH] Finish tornados (missing sound) --- NewHorizons/Builder/Props/PropBuildManager.cs | 2 +- NewHorizons/Builder/Props/TornadoBuilder.cs | 108 +++++++++--------- .../Components/NHTornadoWanderController.cs | 56 +++++++++ NewHorizons/External/PropModule.cs | 7 +- 4 files changed, 118 insertions(+), 55 deletions(-) create mode 100644 NewHorizons/Components/NHTornadoWanderController.cs diff --git a/NewHorizons/Builder/Props/PropBuildManager.cs b/NewHorizons/Builder/Props/PropBuildManager.cs index c64a47f5..95112980 100644 --- a/NewHorizons/Builder/Props/PropBuildManager.cs +++ b/NewHorizons/Builder/Props/PropBuildManager.cs @@ -49,7 +49,7 @@ namespace NewHorizons.Builder.Props { foreach(var tornadoInfo in config.Props.Tornados) { - //TornadoBuilder.Make(go, sector, tornadoInfo, config.Atmosphere?.Cloud != null); + TornadoBuilder.Make(go, sector, tornadoInfo, config.Atmosphere?.Cloud != null); } } if (config.Props.Volcanoes != null) diff --git a/NewHorizons/Builder/Props/TornadoBuilder.cs b/NewHorizons/Builder/Props/TornadoBuilder.cs index ad16c040..64329a21 100644 --- a/NewHorizons/Builder/Props/TornadoBuilder.cs +++ b/NewHorizons/Builder/Props/TornadoBuilder.cs @@ -1,5 +1,6 @@ using NewHorizons.Components; using NewHorizons.External; +using NewHorizons.Handlers; using NewHorizons.Utility; using System; using System.Collections.Generic; @@ -8,88 +9,91 @@ using System.Text; using System.Threading.Tasks; using UnityEngine; using Logger = NewHorizons.Utility.Logger; +using Random = UnityEngine.Random; namespace NewHorizons.Builder.Props { public static class TornadoBuilder { - public static string tornadoParentName = "Tornados"; + private static GameObject upPrefab; + private static GameObject downPrefab; public static void Make(GameObject go, Sector sector, PropModule.TornadoInfo info, bool hasClouds) { - // If we are given elevation choose a random position - Vector3 position; - float elevation = 0f; - - if (info.position != null) + if (upPrefab == null) { - position = info.position; + upPrefab = GameObject.Find("BrittleHollow_Body/Sector_BH/Sector_SouthHemisphere/Sector_SouthPole/Sector_Observatory/Interactables_Observatory/MockUpTornado").InstantiateInactive(); + upPrefab.name = "Tornado_Up_Prefab"; + } + if(downPrefab == null) + { + downPrefab = GameObject.Find("BrittleHollow_Body/Sector_BH/Sector_SouthHemisphere/Sector_SouthPole/Sector_Observatory/Interactables_Observatory/MockDownTornado").InstantiateInactive(); + downPrefab.name = "Tornado_Down_Prefab"; + downPrefab.name = "Tornado_Down_Prefab"; + } + + float elevation; + Vector3 position; + if(info.position != null) + { + position = info.position ?? Random.onUnitSphere * info.elevation; elevation = position.magnitude; } - else if (info.elevation != 0f) + else if(info.elevation != 0) { - Logger.Log("Giving tornado random pos"); - position = UnityEngine.Random.insideUnitSphere * info.elevation; + position = Random.onUnitSphere * info.elevation; elevation = info.elevation; } else { - Logger.LogError($"Couldn't make tornado for {go.name}: No elevation or position was given"); + Logger.LogError($"Need either a position or an elevation for tornados"); return; } - var upPrefab = GameObject.Find("BrittleHollow_Body/Sector_BH/Sector_SouthHemisphere/Sector_SouthPole/Sector_Observatory/Interactables_Observatory/MockUpTornado"); - var downPrefab = GameObject.Find("BrittleHollow_Body/Sector_BH/Sector_SouthHemisphere/Sector_SouthPole/Sector_Observatory/Interactables_Observatory/MockDownTornado"); - // Default radius is 40, height is 837.0669 + var tornadoGO = info.downwards ? downPrefab.InstantiateInactive() : upPrefab.InstantiateInactive(); + tornadoGO.transform.parent = sector.transform; + tornadoGO.transform.localPosition = position; + tornadoGO.transform.rotation = Quaternion.FromToRotation(Vector3.up, sector.transform.TransformDirection(position.normalized)); - var tornadoGO = upPrefab.InstantiateInactive(); - tornadoGO.transform.parent = sector?.transform ?? go.transform; - tornadoGO.transform.localPosition = Vector3.zero; - var tornado = tornadoGO.GetComponent(); - tornado.SetSector(sector); + var scale = info.height == 0 ? 1 : info.height / 10f; - var height = 837.0669f; - if (info.height != 0f) height = info.height; + tornadoGO.transform.localScale = Vector3.one * scale; - var width = 40f; - if (info.width != 0f) width = info.width; + var controller = tornadoGO.GetComponent(); + controller.SetSector(sector); - tornado._bottomBone.localPosition = new Vector3(0, elevation, 0); - tornado._midBone.localPosition = new Vector3(0, elevation + height / 2f, 0); - tornado._topBone.localPosition = new Vector3(0, elevation + height, 0); + controller._bottomStartPos = Vector3.up * -20; + controller._midStartPos = Vector3.up * 150; + controller._topStartPos = Vector3.up * 300; - tornado._startActive = false; + controller._bottomBone.localPosition = controller._bottomStartPos; + controller._midBone.localPosition = controller._midStartPos; + controller._topBone.localPosition = controller._topStartPos; + + OWAssetHandler.LoadObject(tornadoGO); + sector.OnOccupantEnterSector += (sd) => OWAssetHandler.LoadObject(tornadoGO); - // TODO make these settings - tornado._wanderRate = 0.5f; - tornado._wanderDegreesX = 45f; - tornado._wanderDegreesZ = 45f; + tornadoGO.GetComponentInChildren().enabled = true; - /* - if (!hasClouds) + if(info.tint != null) { - var fix = tornadoGO.AddComponent(); - fix.SetSector(sector); - - var top = tornadoGO.transform.Find("UpTornado/Effects_GD_TornadoCyclone/Tornado_Top"); - - // Get rid of the bit that appears above the clouds - GameObject.Destroy(top.transform.Find("Effects_GD_TornadoCloudCap_Large")?.gameObject); - GameObject.Destroy(top.transform.Find("Effects_GD_TornadoCloudCap_Medium")?.gameObject); - GameObject.Destroy(top.transform.Find("Effects_GD_TornadoCloudCap_Small")?.gameObject); - - var top_objects = new GameObject[3]; - top_objects[0] = GameObject.Instantiate(top.transform.Find("Effects_GD_TornadoCloudBlend_Large").gameObject, top.transform); - top_objects[1] = GameObject.Instantiate(top.transform.Find("Effects_GD_TornadoCloudBlend_Medium").gameObject, top.transform); - top_objects[2] = GameObject.Instantiate(top.transform.Find("Effects_GD_TornadoCloudBlend_Small").gameObject, top.transform); - - foreach(var obj in top_objects) + var colour = info.tint.ToColor(); + foreach(var renderer in tornadoGO.GetComponentsInChildren()) { - obj.transform.localPosition = new Vector3(0, -20, 0); - obj.transform.localRotation = Quaternion.Euler(180, 0, 0); + renderer.material.color = colour; + renderer.material.SetColor("_DetailColor", colour); + renderer.material.SetColor("_TintColor", colour); } } - */ + + if(info.wanderRate != 0) + { + var wanderer = tornadoGO.AddComponent(); + wanderer.wanderRate = info.wanderRate; + wanderer.wanderDegreesX = info.wanderDegreesX; + wanderer.wanderDegreesZ = info.wanderDegreesZ; + wanderer.sector = sector; + } tornadoGO.SetActive(true); } diff --git a/NewHorizons/Components/NHTornadoWanderController.cs b/NewHorizons/Components/NHTornadoWanderController.cs new file mode 100644 index 00000000..abb59c2e --- /dev/null +++ b/NewHorizons/Components/NHTornadoWanderController.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; +using Random = UnityEngine.Random; + +namespace NewHorizons.Components +{ + public class NHTornadoWanderController : MonoBehaviour + { + public float wanderRate; + public float wanderDegreesX; + public float wanderDegreesZ; + public Sector sector; + + private float noiseOffset; + private float startDegreesX; + private float startDegreesZ; + + private float elevation; + + public void Awake() + { + noiseOffset = Random.value; + + var x = transform.localPosition.x; + var y = transform.localPosition.y; + var z = transform.localPosition.z; + + elevation = Mathf.Sqrt(x * x + y * y + z * z); + + startDegreesX = Mathf.Rad2Deg * Mathf.Atan2(y, x); //theta + startDegreesZ = Mathf.Rad2Deg * Mathf.Acos(z / elevation); //phi + } + + public void Update() + { + float num = Mathf.PerlinNoise(Time.time * wanderRate + noiseOffset, 0f) * 2f - 1f; + float num2 = Mathf.PerlinNoise(Time.time * wanderRate + noiseOffset, 5f) * 2f - 1f; + + var newDegreesX = startDegreesX + num * wanderDegreesX; + var newDegreesZ = startDegreesZ + num2 * wanderDegreesZ; + + var newX = elevation * Mathf.Sin(Mathf.Deg2Rad * newDegreesZ) * Mathf.Cos(Mathf.Deg2Rad * newDegreesX); + var newY = elevation * Mathf.Sin(Mathf.Deg2Rad * newDegreesZ) * Mathf.Sin(Mathf.Deg2Rad * newDegreesX); + var newZ = elevation * Mathf.Cos(Mathf.Deg2Rad * newDegreesZ); + + var newPos = new Vector3(newX, newY, newZ); + + transform.localPosition = newPos; + transform.rotation = Quaternion.FromToRotation(Vector3.up, sector.transform.TransformDirection(newPos.normalized)); + } + } +} diff --git a/NewHorizons/External/PropModule.cs b/NewHorizons/External/PropModule.cs index 6d2c631e..ab95efd6 100644 --- a/NewHorizons/External/PropModule.cs +++ b/NewHorizons/External/PropModule.cs @@ -57,11 +57,14 @@ namespace NewHorizons.External public class TornadoInfo { + public MVector3 position; public float elevation; - public MVector3 position = null; public float height; - public float width; public MColor tint; + public bool downwards; + public float wanderRate; + public float wanderDegreesX = 45; + public float wanderDegreesZ = 45; } public class VolcanoInfo