From f3f211a130ead7397b4b50b527b8c6790970d877 Mon Sep 17 00:00:00 2001 From: Noah Pilarski Date: Sat, 26 Apr 2025 21:38:41 -0400 Subject: [PATCH] Switch to using a custom structure implementation instead --- .../Components/Quantum/QuantumPlanet.cs | 29 +-- .../Components/Quantum/QuantumStructure.cs | 190 ++++++++++++++++++ 2 files changed, 207 insertions(+), 12 deletions(-) create mode 100644 NewHorizons/Components/Quantum/QuantumStructure.cs diff --git a/NewHorizons/Components/Quantum/QuantumPlanet.cs b/NewHorizons/Components/Quantum/QuantumPlanet.cs index a79519ae..449d7ee4 100644 --- a/NewHorizons/Components/Quantum/QuantumPlanet.cs +++ b/NewHorizons/Components/Quantum/QuantumPlanet.cs @@ -21,8 +21,8 @@ namespace NewHorizons.Components.Quantum private AlignWithTargetBody _alignment; private OWRigidbody _rb; private OrbitLine _orbitLine; - private QuantumShrine[] _shrines; - private QuantumDarkTrigger[] _darkTriggers; + private QuantumStructure[] _structures = new QuantumStructure[0]; + private QuantumDarkTrigger[] _darkTriggers = new QuantumDarkTrigger[0]; public NHAstroObject astroObject { @@ -65,6 +65,9 @@ namespace NewHorizons.Components.Quantum public void ResetStates(bool changeState) { + _structures = GetComponentsInChildren(true); // finds all quantum structures + _darkTriggers = GetComponentsInChildren(true); // finds all quantum dark triggers + if (changeState) { ChangeQuantumState(true); @@ -73,9 +76,6 @@ namespace NewHorizons.Components.Quantum { SetNewSector(states[CurrentIndex]); } - - _shrines = GetComponentsInChildren(true); // finds all quantum shrines - _darkTriggers = GetComponentsInChildren(true); // finds all quantum dark triggers } public override void OnDestroy() @@ -168,6 +168,11 @@ namespace NewHorizons.Components.Quantum } newState.sector.gameObject.SetActive(true); + + foreach (var structure in _structures) + { + structure.OnQuantumPlanetStateChanged(CurrentIndex); + } } private void SetNewOrbit(AstroObject primaryBody, OrbitalParameters orbitalParameters) @@ -200,9 +205,9 @@ namespace NewHorizons.Components.Quantum trackingOrbitLine.Reset(); } - foreach (var shrine in _shrines) + foreach (var structure in _structures) { - shrine.OnQuantumMoonSurfaceStateChanged(CurrentIndex); + structure.OnQuantumPlanetOrbitChanged(CurrentIndex); } } @@ -235,11 +240,11 @@ namespace NewHorizons.Components.Quantum return states[CurrentIndex].sector.ContainsAnyOccupants(DynamicOccupant.Ship); } - public bool IsPlayerInsideShrine() + public bool IsPlayerInsideStructure() { - foreach (var shrine in _shrines) + foreach (var structure in _structures) { - if (shrine.IsPlayerInside()) + if (structure.IsPlayerInside()) return true; } return false; @@ -252,9 +257,9 @@ namespace NewHorizons.Components.Quantum if (darkTrigger.IsPlayerInDarkness()) return true; } - foreach (var shrine in _shrines) + foreach (var structure in _structures) { - if (shrine.IsPlayerInDarkness()) + if (structure.IsPlayerInDarkness()) return true; } return false; diff --git a/NewHorizons/Components/Quantum/QuantumStructure.cs b/NewHorizons/Components/Quantum/QuantumStructure.cs new file mode 100644 index 00000000..77f3d8d8 --- /dev/null +++ b/NewHorizons/Components/Quantum/QuantumStructure.cs @@ -0,0 +1,190 @@ +using NewHorizons.Utility.OWML; +using System; +using UnityEngine; + +namespace NewHorizons.Components.Quantum +{ + public class QuantumStructure : MonoBehaviour + { + [SerializeField] + public OWTriggerVolume insideTriggerVolume; + + [SerializeField] + public Light[] lights = new Light[0]; + + [SerializeField] + public Light ambientLight; + + [SerializeField] + public FogOverrideVolume fogOverride; + + protected float fadeFraction { get; private set; } = 1f; + + protected bool isPlayerInside { get; private set; } + + protected bool isProbeInside { get; private set; } + + protected bool fading { get; private set; } + + private float _origAmbientIntensity; + + private Color _origFogTint; + + public virtual void Awake() + { + if (ambientLight != null) + { + _origAmbientIntensity = ambientLight.intensity; + } + if (fogOverride != null) + { + _origFogTint = fogOverride.tint; + } + if (insideTriggerVolume != null) + { + insideTriggerVolume.OnEntry += OnEntry; + insideTriggerVolume.OnExit += OnExit; + } + else + { + NHLogger.LogWarning("Missing inside trigger volume for " + this.GetType()); + } + } + + public virtual void Start() + { + } + + public virtual void OnDestroy() + { + if (insideTriggerVolume != null) + { + insideTriggerVolume.OnEntry -= OnEntry; + insideTriggerVolume.OnExit -= OnExit; + } + } + + public virtual bool IsPlayerInside() + { + return isPlayerInside; + } + + public virtual bool IsProbeInside() + { + return isProbeInside; + } + + public virtual bool IsPlayerInDarkness() + { + foreach (var light in lights) + { + if (light.intensity > 0f) + { + return false; + } + } + if (isPlayerInside && fadeFraction == 0f && !isProbeInside && !PlayerState.IsFlashlightOn()) + { + return Locator.GetThrusterLightTracker().GetLightRange() <= 0f; + } + return false; + } + + public virtual void OnQuantumPlanetStateChanged(int currentIndex) + { + } + + public virtual void OnQuantumPlanetOrbitChanged(int state) + { + } + + public virtual void Update() + { + if (fading) + { + float target = (isPlayerInside ? (GetOpenFraction() * 0.7f) : 1f); + fadeFraction = Mathf.MoveTowards(fadeFraction, target, Time.deltaTime * 0.5f); + FadeUpdate(); + } + } + + public virtual float GetOpenFraction() + { + return 1f; + } + + public virtual void FadeUpdate() + { + if (ambientLight != null) + { + ambientLight.intensity = _origAmbientIntensity * fadeFraction; + } + if (fogOverride != null) + { + fogOverride.tint = Color.Lerp(Color.black, _origFogTint, fadeFraction); + } + } + + public void OnEntry(GameObject hitObj) + { + if (hitObj.CompareTag("PlayerDetector")) + { + isPlayerInside = true; + fading = true; + OnPlayerEntry(hitObj); + } + else if (hitObj.CompareTag("ProbeDetector")) + { + isProbeInside = true; + OnProbeEntry(hitObj); + } + else + { + OnOtherEntry(hitObj); + } + } + + public virtual void OnPlayerEntry(GameObject playerDetector) + { + } + + public virtual void OnProbeEntry(GameObject probeDetector) + { + } + + public virtual void OnOtherEntry(GameObject otherDetector) + { + } + + public void OnExit(GameObject hitObj) + { + if (hitObj.CompareTag("PlayerDetector")) + { + isPlayerInside = false; + fading = true; + OnPlayerExit(hitObj); + } + else if (hitObj.CompareTag("ProbeDetector")) + { + isProbeInside = false; + OnProbeExit(hitObj); + } + else + { + OnOtherExit(hitObj); + } + } + + public virtual void OnPlayerExit(GameObject playerDetector) + { + } + + public virtual void OnProbeExit(GameObject probeDetector) + { + } + + public virtual void OnOtherExit(GameObject otherDetector) + { + } + } +}