Change volumes to use GeneralPropBuilder and cleaned up some others

This commit is contained in:
Joshua Thome 2023-03-18 07:48:43 -05:00
parent 9426ec48d0
commit c8bd63e5d5
8 changed files with 26 additions and 186 deletions

View File

@ -12,7 +12,7 @@ namespace NewHorizons.Builder.Props
{ {
public static class GeneralPropBuilder public static class GeneralPropBuilder
{ {
public static GameObject MakeFromExisting(GameObject go, GameObject planetGO, Sector sector, PropModule.PositionedPropInfo info, bool alignToBody = false, MVector3 normal = null) public static GameObject MakeFromExisting(GameObject go, GameObject planetGO, Sector sector, PropModule.PositionedPropInfo info, bool alignToBody = false, MVector3 normal = null, MVector3 defaultPosition = null, string defaultParentPath = null)
{ {
if (!string.IsNullOrEmpty(info.rename)) if (!string.IsNullOrEmpty(info.rename))
{ {
@ -21,21 +21,23 @@ namespace NewHorizons.Builder.Props
go.transform.parent = sector?.transform ?? planetGO.transform; go.transform.parent = sector?.transform ?? planetGO.transform;
if (!string.IsNullOrEmpty(info.parentPath)) var parentPath = info.parentPath ?? defaultParentPath;
if (!string.IsNullOrEmpty(parentPath))
{ {
var newParent = go.transform.Find(info.parentPath); var newParent = go.transform.Find(parentPath);
if (newParent != null) if (newParent != null)
{ {
go.transform.parent = newParent.transform; go.transform.parent = newParent.transform;
} }
else else
{ {
Logger.LogError($"Cannot find parent object at path: {go.name}/{info.parentPath}"); Logger.LogError($"Cannot find parent object at path: {go.name}/{parentPath}");
} }
} }
Vector3 pos = (Vector3)(info.position ?? Vector3.zero); var pos = (Vector3)(info.position ?? defaultPosition ?? Vector3.zero);
Quaternion rot = Quaternion.identity; var rot = Quaternion.identity;
if (info is PropModule.PositionedAndRotatedPropInfo rotInfo) if (info is PropModule.PositionedAndRotatedPropInfo rotInfo)
{ {
rot = rotInfo.rotation != null ? Quaternion.Euler(rotInfo.rotation) : Quaternion.identity; rot = rotInfo.rotation != null ? Quaternion.Euler(rotInfo.rotation) : Quaternion.identity;
@ -59,18 +61,18 @@ namespace NewHorizons.Builder.Props
return go; return go;
} }
public static GameObject MakeNew(string defaultName, GameObject planetGO, Sector sector, PropModule.PositionedPropInfo info, bool alignToBody = false, MVector3 normal = null) public static GameObject MakeNew(string defaultName, GameObject planetGO, Sector sector, PropModule.PositionedPropInfo info, bool alignToBody = false, MVector3 normal = null, MVector3 defaultPosition = null, string defaultParentPath = null)
{ {
GameObject go = new GameObject(defaultName); var go = new GameObject(defaultName);
go.SetActive(false); go.SetActive(false);
return MakeFromExisting(go, planetGO, sector, info, alignToBody, normal); return MakeFromExisting(go, planetGO, sector, info, alignToBody, normal, defaultPosition, defaultParentPath);
} }
public static GameObject MakeFromPrefab(GameObject prefab, string defaultName, GameObject planetGO, Sector sector, PropModule.PositionedPropInfo info, bool alignToBody = false, MVector3 normal = null) public static GameObject MakeFromPrefab(GameObject prefab, string defaultName, GameObject planetGO, Sector sector, PropModule.PositionedPropInfo info, bool alignToBody = false, MVector3 normal = null, MVector3 defaultPosition = null, string defaultParentPath = null)
{ {
GameObject go = prefab.InstantiateInactive(); var go = prefab.InstantiateInactive();
go.name = defaultName; go.name = defaultName;
return MakeFromExisting(go, planetGO, sector, info, alignToBody, normal); return MakeFromExisting(go, planetGO, sector, info, alignToBody, normal, defaultPosition, defaultParentPath);
} }
} }
} }

View File

@ -251,37 +251,7 @@ namespace NewHorizons.Builder.Props
public static void MakeStone(GameObject go, Sector sector, NomaiRemoteCameraPlatform.ID id, Texture2D decal, PropModule.RemoteInfo.StoneInfo info, IModBehaviour mod) public static void MakeStone(GameObject go, Sector sector, NomaiRemoteCameraPlatform.ID id, Texture2D decal, PropModule.RemoteInfo.StoneInfo info, IModBehaviour mod)
{ {
var shareStone = _shareStonePrefab.InstantiateInactive(); var shareStone = GeneralPropBuilder.MakeFromPrefab(_shareStonePrefab, "ShareStone_" + id.ToString(), go, sector, info);
shareStone.name = !string.IsNullOrEmpty(info.rename) ? info.rename : ("ShareStone_" + id.ToString());
shareStone.transform.parent = sector?.transform ?? go.transform;
if (!string.IsNullOrEmpty(info.parentPath))
{
var newParent = go.transform.Find(info.parentPath);
if (newParent != null)
{
shareStone.transform.parent = newParent;
}
else
{
Logger.LogError($"Cannot find parent object at path: {go.name}/{info.parentPath}");
}
}
var pos = (Vector3)(info.position ?? Vector3.zero);
var rot = Quaternion.Euler((Vector3)(info.rotation ?? Vector3.zero));
if (info.isRelativeToParent)
{
shareStone.transform.localPosition = pos;
shareStone.transform.localRotation = rot;
}
else
{
shareStone.transform.position = go.transform.TransformPoint(pos);
shareStone.transform.rotation = go.transform.TransformRotation(rot);
}
shareStone.GetComponent<SharedStone>()._connectedPlatform = id; shareStone.GetComponent<SharedStone>()._connectedPlatform = id;

View File

@ -99,13 +99,7 @@ namespace NewHorizons.Builder.Props
private static void MakeTornado(GameObject planetGO, Sector sector, PropModule.TornadoInfo info, Vector3 position, bool downwards) private static void MakeTornado(GameObject planetGO, Sector sector, PropModule.TornadoInfo info, Vector3 position, bool downwards)
{ {
var prefab = downwards ? _downPrefab.InstantiateInactive() : _upPrefab.InstantiateInactive(); var prefab = downwards ? _downPrefab.InstantiateInactive() : _upPrefab.InstantiateInactive();
var tornadoGO = GeneralPropBuilder.MakeFromPrefab(prefab, downwards ? "Tornado_Down" : "Tornado_Up", planetGO, sector, info, true); var tornadoGO = GeneralPropBuilder.MakeFromPrefab(prefab, downwards ? "Tornado_Down" : "Tornado_Up", planetGO, sector, info, true, defaultPosition: position);
// Override general prop position with randomized default position
if (info.position == null)
{
tornadoGO.transform.position = planetGO.transform.TransformPoint(position);
}
// Add the sound thing before changing the scale // Add the sound thing before changing the scale
var soundGO = _soundPrefab.InstantiateInactive(); var soundGO = _soundPrefab.InstantiateInactive();

View File

@ -1,3 +1,4 @@
using NewHorizons.Builder.Props;
using NewHorizons.Components.Achievement; using NewHorizons.Components.Achievement;
using NewHorizons.External.Modules; using NewHorizons.External.Modules;
using OWML.Common; using OWML.Common;
@ -9,7 +10,7 @@ namespace NewHorizons.Builder.ShipLog
{ {
public static void Make(GameObject go, Sector sector, VolumesModule.RevealVolumeInfo info, IModBehaviour mod) public static void Make(GameObject go, Sector sector, VolumesModule.RevealVolumeInfo info, IModBehaviour mod)
{ {
var newRevealGO = MakeGameObject(go, sector, info, mod); var newRevealGO = GeneralPropBuilder.MakeNew("Reveal Volume (" + info.revealOn + ")", go, sector, info);
switch (info.revealOn) switch (info.revealOn)
{ {
case VolumesModule.RevealVolumeInfo.RevealVolumeType.Enter: case VolumesModule.RevealVolumeInfo.RevealVolumeType.Enter:
@ -36,37 +37,6 @@ namespace NewHorizons.Builder.ShipLog
return newShape; return newShape;
} }
private static GameObject MakeGameObject(GameObject planetGO, Sector sector, VolumesModule.RevealVolumeInfo info, IModBehaviour mod)
{
GameObject revealTriggerVolume = new GameObject("Reveal Volume (" + info.revealOn + ")");
revealTriggerVolume.SetActive(false);
revealTriggerVolume.transform.parent = sector?.transform ?? planetGO.transform;
if (!string.IsNullOrEmpty(info.rename))
{
revealTriggerVolume.name = info.rename;
}
if (!string.IsNullOrEmpty(info.parentPath))
{
var newParent = planetGO.transform.Find(info.parentPath);
if (newParent != null)
{
revealTriggerVolume.transform.parent = newParent;
}
else
{
Logger.LogError($"Cannot find parent object at path: {planetGO.name}/{info.parentPath}");
}
}
var pos = (Vector3)(info.position ?? Vector3.zero);
if (info.isRelativeToParent) revealTriggerVolume.transform.localPosition = pos;
else revealTriggerVolume.transform.position = planetGO.transform.TransformPoint(pos);
return revealTriggerVolume;
}
private static void MakeTrigger(GameObject go, Sector sector, VolumesModule.RevealVolumeInfo info, IModBehaviour mod) private static void MakeTrigger(GameObject go, Sector sector, VolumesModule.RevealVolumeInfo info, IModBehaviour mod)
{ {
var shape = MakeShape(go, info, Shape.CollisionMode.Volume); var shape = MakeShape(go, info, Shape.CollisionMode.Volume);

View File

@ -1,3 +1,4 @@
using NewHorizons.Builder.Props;
using NewHorizons.External.Modules; using NewHorizons.External.Modules;
using NewHorizons.Utility; using NewHorizons.Utility;
using OWML.Common; using OWML.Common;
@ -16,32 +17,7 @@ namespace NewHorizons.Builder.Volumes
{ {
public static AudioVolume Make(GameObject planetGO, Sector sector, VolumesModule.AudioVolumeInfo info, IModBehaviour mod) public static AudioVolume Make(GameObject planetGO, Sector sector, VolumesModule.AudioVolumeInfo info, IModBehaviour mod)
{ {
var go = new GameObject("AudioVolume"); var go = GeneralPropBuilder.MakeNew("AudioVolume", planetGO, sector, info);
go.SetActive(false);
go.transform.parent = sector?.transform ?? planetGO.transform;
if (!string.IsNullOrEmpty(info.rename))
{
go.name = info.rename;
}
if (!string.IsNullOrEmpty(info.parentPath))
{
var newParent = planetGO.transform.Find(info.parentPath);
if (newParent != null)
{
go.transform.parent = newParent;
}
else
{
Logger.LogWarning($"Cannot find parent object at path: {planetGO.name}/{info.parentPath}");
}
}
var pos = (Vector3)(info.position ?? Vector3.zero);
if (info.isRelativeToParent) go.transform.localPosition = pos;
else go.transform.position = planetGO.transform.TransformPoint(pos);
go.layer = LayerMask.NameToLayer("AdvancedEffectVolume"); go.layer = LayerMask.NameToLayer("AdvancedEffectVolume");
var audioSource = go.AddComponent<AudioSource>(); var audioSource = go.AddComponent<AudioSource>();

View File

@ -1,3 +1,4 @@
using NewHorizons.Builder.Props;
using NewHorizons.External.Modules; using NewHorizons.External.Modules;
using OWML.Common; using OWML.Common;
using OWML.Utils; using OWML.Utils;
@ -13,32 +14,7 @@ namespace NewHorizons.Builder.Volumes
{ {
public static HazardVolume Make(GameObject planetGO, Sector sector, OWRigidbody owrb, VolumesModule.HazardVolumeInfo info, IModBehaviour mod) public static HazardVolume Make(GameObject planetGO, Sector sector, OWRigidbody owrb, VolumesModule.HazardVolumeInfo info, IModBehaviour mod)
{ {
var go = new GameObject("HazardVolume"); var go = GeneralPropBuilder.MakeNew("HazardVolume", planetGO, sector, info);
go.SetActive(false);
go.transform.parent = sector?.transform ?? planetGO.transform;
if (!string.IsNullOrEmpty(info.rename))
{
go.name = info.rename;
}
if (!string.IsNullOrEmpty(info.parentPath))
{
var newParent = planetGO.transform.Find(info.parentPath);
if (newParent != null)
{
go.transform.parent = newParent;
}
else
{
Logger.LogError($"Cannot find parent object at path: {planetGO.name}/{info.parentPath}");
}
}
var pos = (Vector3)(info.position ?? Vector3.zero);
if (info.isRelativeToParent) go.transform.localPosition = pos;
else go.transform.position = planetGO.transform.TransformPoint(pos);
go.layer = LayerMask.NameToLayer("BasicEffectVolume"); go.layer = LayerMask.NameToLayer("BasicEffectVolume");
var shape = go.AddComponent<SphereShape>(); var shape = go.AddComponent<SphereShape>();

View File

@ -1,3 +1,4 @@
using NewHorizons.Builder.Props;
using NewHorizons.External.Modules; using NewHorizons.External.Modules;
using NewHorizons.Utility; using NewHorizons.Utility;
using OWML.Common; using OWML.Common;
@ -16,32 +17,7 @@ namespace NewHorizons.Builder.Volumes
{ {
public static NHNotificationVolume Make(GameObject planetGO, Sector sector, VolumesModule.NotificationVolumeInfo info, IModBehaviour mod) public static NHNotificationVolume Make(GameObject planetGO, Sector sector, VolumesModule.NotificationVolumeInfo info, IModBehaviour mod)
{ {
var go = new GameObject("NotificationVolume"); var go = GeneralPropBuilder.MakeNew("NotificationVolume", planetGO, sector, info);
go.SetActive(false);
go.transform.parent = sector?.transform ?? planetGO.transform;
if (!string.IsNullOrEmpty(info.rename))
{
go.name = info.rename;
}
if (!string.IsNullOrEmpty(info.parentPath))
{
var newParent = planetGO.transform.Find(info.parentPath);
if (newParent != null)
{
go.transform.parent = newParent;
}
else
{
Logger.LogError($"Cannot find parent object at path: {planetGO.name}/{info.parentPath}");
}
}
var pos = (Vector3)(info.position ?? Vector3.zero);
if (info.isRelativeToParent) go.transform.localPosition = pos;
else go.transform.position = planetGO.transform.TransformPoint(pos);
go.layer = LayerMask.NameToLayer("BasicEffectVolume"); go.layer = LayerMask.NameToLayer("BasicEffectVolume");
var shape = go.AddComponent<SphereShape>(); var shape = go.AddComponent<SphereShape>();

View File

@ -1,3 +1,4 @@
using NewHorizons.Builder.Props;
using NewHorizons.Components; using NewHorizons.Components;
using NewHorizons.External.Modules; using NewHorizons.External.Modules;
using UnityEngine; using UnityEngine;
@ -9,32 +10,7 @@ namespace NewHorizons.Builder.Volumes
{ {
public static TVolume Make<TVolume>(GameObject planetGO, Sector sector, VolumesModule.VanishVolumeInfo info) where TVolume : VanishVolume public static TVolume Make<TVolume>(GameObject planetGO, Sector sector, VolumesModule.VanishVolumeInfo info) where TVolume : VanishVolume
{ {
var go = new GameObject(typeof(TVolume).Name); var go = GeneralPropBuilder.MakeNew(typeof(TVolume).Name, planetGO, sector, info);
go.SetActive(false);
go.transform.parent = sector?.transform ?? planetGO.transform;
if (!string.IsNullOrEmpty(info.rename))
{
go.name = info.rename;
}
if (!string.IsNullOrEmpty(info.parentPath))
{
var newParent = planetGO.transform.Find(info.parentPath);
if (newParent != null)
{
go.transform.parent = newParent;
}
else
{
Logger.LogError($"Cannot find parent object at path: {planetGO.name}/{info.parentPath}");
}
}
var pos = (Vector3)(info.position ?? Vector3.zero);
if (info.isRelativeToParent) go.transform.localPosition = pos;
else go.transform.position = planetGO.transform.TransformPoint(pos);
go.layer = LayerMask.NameToLayer("BasicEffectVolume"); go.layer = LayerMask.NameToLayer("BasicEffectVolume");
var collider = go.AddComponent<SphereCollider>(); var collider = go.AddComponent<SphereCollider>();