diff --git a/NewHorizons/Builder/Volumes/MapRestrictionVolumeBuilder.cs b/NewHorizons/Builder/Volumes/MapRestrictionVolumeBuilder.cs new file mode 100644 index 00000000..53e5944f --- /dev/null +++ b/NewHorizons/Builder/Volumes/MapRestrictionVolumeBuilder.cs @@ -0,0 +1,35 @@ +using NewHorizons.Components; +using NewHorizons.External.Modules; +using OWML.Common; +using System; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +namespace NewHorizons.Builder.Volumes +{ + public static class MapRestrictionVolumeBuilder + { + public static MapRestrictionVolume Make(GameObject planetGO, Sector sector, VolumesModule.VolumeInfo info) + { + var go = new GameObject("MapRestrictionVolume"); + go.SetActive(false); + + go.transform.parent = sector?.transform ?? planetGO.transform; + go.transform.position = planetGO.transform.TransformPoint(info.position != null ? (Vector3)info.position : Vector3.zero); + go.layer = LayerMask.NameToLayer("BasicEffectVolume"); + + var shape = go.AddComponent(); + shape.radius = info.radius; + + var owTriggerVolume = go.AddComponent(); + owTriggerVolume._shape = shape; + + var mapRestrictionVolume = go.AddComponent(); + + go.SetActive(true); + + return mapRestrictionVolume; + } + } +} diff --git a/NewHorizons/Builder/Volumes/VolumesBuildManager.cs b/NewHorizons/Builder/Volumes/VolumesBuildManager.cs index f7a664b9..343ccc0c 100644 --- a/NewHorizons/Builder/Volumes/VolumesBuildManager.cs +++ b/NewHorizons/Builder/Volumes/VolumesBuildManager.cs @@ -49,6 +49,13 @@ namespace NewHorizons.Builder.Volumes HazardVolumeBuilder.Make(go, sector, planetBody, hazardVolume, mod); } } + if (config.Volumes.mapRestrictionVolumes != null) + { + foreach (var mapRestrictionVolume in config.Volumes.mapRestrictionVolumes) + { + MapRestrictionVolumeBuilder.Make(go, sector, mapRestrictionVolume); + } + } } } } diff --git a/NewHorizons/Components/MapRestrictionVolume.cs b/NewHorizons/Components/MapRestrictionVolume.cs new file mode 100644 index 00000000..43ebd8d3 --- /dev/null +++ b/NewHorizons/Components/MapRestrictionVolume.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +namespace NewHorizons.Components +{ + [RequireComponent(typeof(OWTriggerVolume))] + public class MapRestrictionVolume : MonoBehaviour + { + private OWTriggerVolume _triggerVolume; + + public void Awake() + { + _triggerVolume = this.GetRequiredComponent(); + _triggerVolume.OnEntry += OnTriggerVolumeEntry; + _triggerVolume.OnExit += OnTriggerVolumeExit; + } + + public void OnDestroy() + { + if (_triggerVolume == null) return; + _triggerVolume.OnEntry -= OnTriggerVolumeEntry; + _triggerVolume.OnExit -= OnTriggerVolumeExit; + } + + public void OnTriggerVolumeEntry(GameObject hitObj) + { + if (hitObj.CompareTag("PlayerDetector")) + { + Locator.GetMapController()?.OnPlayerEnterMapRestriction(); + } + } + + public void OnTriggerVolumeExit(GameObject hitObj) + { + if (hitObj.CompareTag("PlayerDetector")) + { + Locator.GetMapController()?.OnPlayerExitMapRestriction(); + } + } + } +} diff --git a/NewHorizons/External/Modules/VolumesModule.cs b/NewHorizons/External/Modules/VolumesModule.cs index e9659f2a..0dd9e0fb 100644 --- a/NewHorizons/External/Modules/VolumesModule.cs +++ b/NewHorizons/External/Modules/VolumesModule.cs @@ -24,6 +24,11 @@ namespace NewHorizons.External.Modules /// public HazardVolumeInfo[] hazardVolumes; + /// + /// Add map restriction volumes to this planet + /// + public VolumeInfo[] mapRestrictionVolumes; + /// /// Add notification volumes to this planet ///