Add notification volumes

This commit is contained in:
Noah Pilarski 2022-08-31 03:09:13 -04:00
parent eaa57a761d
commit 890b496e7a
5 changed files with 301 additions and 0 deletions

View File

@ -0,0 +1,43 @@
using NewHorizons.Components;
using NewHorizons.External.Modules;
using NewHorizons.Utility;
using OWML.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Logger = NewHorizons.Utility.Logger;
using NHNotificationVolume = NewHorizons.Components.NotificationVolume;
namespace NewHorizons.Builder.Props
{
public static class NotificationVolumeBuilder
{
public static NHNotificationVolume Make(GameObject planetGO, Sector sector, PropModule.NotificationVolumeInfo info, IModBehaviour mod)
{
var go = new GameObject("NotificationVolume");
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<SphereShape>();
shape.radius = info.radius;
var owTriggerVolume = go.AddComponent<OWTriggerVolume>();
owTriggerVolume._shape = shape;
var notificationVolume = go.AddComponent<NHNotificationVolume>();
notificationVolume.SetTarget(info.target);
if (info.entryNotification != null) notificationVolume.SetEntryNotification(info.entryNotification.displayMessage, info.entryNotification.duration);
if (info.exitNotification != null) notificationVolume.SetExitNotification(info.exitNotification.displayMessage, info.exitNotification.duration);
go.SetActive(true);
return notificationVolume;
}
}
}

View File

@ -235,6 +235,13 @@ namespace NewHorizons.Builder.Props
}
}
}
if (config.Props.notificationVolumes != null)
{
foreach (var notificationVolume in config.Props.notificationVolumes)
{
NotificationVolumeBuilder.Make(go, sector, notificationVolume, mod);
}
}
}
}
}

View File

@ -0,0 +1,129 @@
using NewHorizons.Handlers;
using OWML.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace NewHorizons.Components
{
[RequireComponent(typeof(OWTriggerVolume))]
public class NotificationVolume : MonoBehaviour
{
private NotificationTarget _target = NotificationTarget.All;
private bool _pin = false;
private OWTriggerVolume _triggerVolume;
private NotificationData _entryNotification;
private NotificationData _exitNotification;
public void Awake()
{
_triggerVolume = this.GetRequiredComponent<OWTriggerVolume>();
_triggerVolume.OnEntry += OnTriggerVolumeEntry;
_triggerVolume.OnExit += OnTriggerVolumeExit;
}
public void OnDestroy()
{
if (_triggerVolume == null) return;
_triggerVolume.OnEntry -= OnTriggerVolumeEntry;
_triggerVolume.OnExit -= OnTriggerVolumeExit;
}
public void SetPinned(bool pin) => _pin = pin;
public void SetTarget(NewHorizons.External.Modules.PropModule.NotificationVolumeInfo.NotificationTarget target) => SetTarget(EnumUtils.Parse<NotificationTarget>(target.ToString(), NotificationTarget.All));
public void SetTarget(NotificationTarget target) => _target = target;
public void SetEntryNotification(string displayMessage, float duration = 5)
{
_entryNotification = new NotificationData(_target, TranslationHandler.GetTranslation(displayMessage, TranslationHandler.TextType.UI), duration);
}
public void SetExitNotification(string displayMessage, float duration = 5)
{
_exitNotification = new NotificationData(_target, TranslationHandler.GetTranslation(displayMessage, TranslationHandler.TextType.UI), duration);
}
public void OnTriggerVolumeEntry(GameObject hitObj)
{
if (_target == NotificationTarget.All)
{
if (hitObj.CompareTag("PlayerDetector") || hitObj.CompareTag("ShipDetector"))
{
PostEntryNotification();
}
}
else if (_target == NotificationTarget.Player)
{
if (hitObj.CompareTag("PlayerDetector"))
{
PostEntryNotification();
}
}
else if (_target == NotificationTarget.Ship)
{
if (hitObj.CompareTag("ShipDetector"))
{
PostEntryNotification();
}
}
}
public void OnTriggerVolumeExit(GameObject hitObj)
{
if (_target == NotificationTarget.All)
{
if (hitObj.CompareTag("PlayerDetector") || hitObj.CompareTag("ShipDetector"))
{
PostExitNotification();
}
}
else if (_target == NotificationTarget.Player)
{
if (hitObj.CompareTag("PlayerDetector"))
{
PostExitNotification();
}
}
else if (_target == NotificationTarget.Ship)
{
if (hitObj.CompareTag("ShipDetector"))
{
PostExitNotification();
}
}
}
public void PostEntryNotification()
{
if (_entryNotification == null) return;
NotificationManager.SharedInstance.PostNotification(_entryNotification, _pin);
}
public void PostExitNotification()
{
if (_exitNotification == null) return;
NotificationManager.SharedInstance.PostNotification(_exitNotification, _pin);
}
public void UnpinEntryNotification()
{
if (_entryNotification == null) return;
if (NotificationManager.SharedInstance.IsPinnedNotification(_entryNotification))
{
NotificationManager.SharedInstance.UnpinNotification(_entryNotification);
}
}
public void UnpinExitNotification()
{
if (_exitNotification == null) return;
if (NotificationManager.SharedInstance.IsPinnedNotification(_exitNotification))
{
NotificationManager.SharedInstance.UnpinNotification(_exitNotification);
}
}
}
}

View File

@ -98,6 +98,11 @@ namespace NewHorizons.External.Modules
/// </summary>
public RemoteInfo[] remotes;
/// <summary>
/// Add notification volumes to this planet
/// </summary>
public NotificationVolumeInfo[] notificationVolumes;
[JsonObject]
public class ScatterInfo
{
@ -1006,6 +1011,58 @@ namespace NewHorizons.External.Modules
public string rename;
}
}
[JsonObject]
public class NotificationVolumeInfo
{
/// <summary>
/// What the notification will show for.
/// </summary>
[DefaultValue("all")] public NotificationTarget target = NotificationTarget.All;
/// <summary>
/// The location of this notification volume. Optional (will default to 0,0,0).
/// </summary>
public MVector3 position;
/// <summary>
/// The radius of this notification volume.
/// </summary>
public float radius;
/// <summary>
/// The notification that will play when you enter this volume.
/// </summary>
public NotificationInfo entryNotification;
/// <summary>
/// The notification that will play when you exit this volume.
/// </summary>
public NotificationInfo exitNotification;
[JsonObject]
public class NotificationInfo
{
/// <summary>
/// The message that will be displayed.
/// </summary>
public string displayMessage;
/// <summary>
/// The duration this notification will be displayed.
/// </summary>
[DefaultValue(5f)] public float duration = 5f;
}
[JsonConverter(typeof(StringEnumConverter))]
public enum NotificationTarget
{
[EnumMember(Value = @"all")] All = 0,
[EnumMember(Value = @"ship")] Ship = 1,
[EnumMember(Value = @"player")] Player = 2,
}
}
}
[JsonConverter(typeof(StringEnumConverter))]

View File

@ -1001,6 +1001,13 @@
"items": {
"$ref": "#/definitions/RemoteInfo"
}
},
"notificationVolumes": {
"type": "array",
"description": "Add notification volumes to this planet",
"items": {
"$ref": "#/definitions/NotificationVolumeInfo"
}
}
}
},
@ -2090,6 +2097,64 @@
}
}
},
"NotificationVolumeInfo": {
"type": "object",
"additionalProperties": false,
"properties": {
"target": {
"description": "What the notification will show for.",
"default": "all",
"$ref": "#/definitions/NotificationTarget"
},
"position": {
"description": "The location of this notification volume. Optional (will default to 0,0,0).",
"$ref": "#/definitions/MVector3"
},
"radius": {
"type": "number",
"description": "The radius of this notification volume.",
"format": "float"
},
"entryNotification": {
"description": "The notification that will play when you enter this volume.",
"$ref": "#/definitions/NotificationInfo"
},
"exitNotification": {
"description": "The notification that will play when you exit this volume.",
"$ref": "#/definitions/NotificationInfo"
}
}
},
"NotificationTarget": {
"type": "string",
"description": "",
"x-enumNames": [
"All",
"Ship",
"Player"
],
"enum": [
"all",
"ship",
"player"
]
},
"NotificationInfo": {
"type": "object",
"additionalProperties": false,
"properties": {
"displayMessage": {
"type": "string",
"description": "The message that will be displayed."
},
"duration": {
"type": "number",
"description": "The duration this notification will be displayed.",
"format": "float",
"default": 5.0
}
}
},
"ReferenceFrameModule": {
"type": "object",
"additionalProperties": false,