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 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))
{
@ -21,21 +21,23 @@ namespace NewHorizons.Builder.Props
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)
{
go.transform.parent = newParent.transform;
}
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);
Quaternion rot = Quaternion.identity;
var pos = (Vector3)(info.position ?? defaultPosition ?? Vector3.zero);
var rot = Quaternion.identity;
if (info is PropModule.PositionedAndRotatedPropInfo rotInfo)
{
rot = rotInfo.rotation != null ? Quaternion.Euler(rotInfo.rotation) : Quaternion.identity;
@ -59,18 +61,18 @@ namespace NewHorizons.Builder.Props
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);
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;
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)
{
var shareStone = _shareStonePrefab.InstantiateInactive();
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);
}
var shareStone = GeneralPropBuilder.MakeFromPrefab(_shareStonePrefab, "ShareStone_" + id.ToString(), go, sector, info);
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)
{
var prefab = downwards ? _downPrefab.InstantiateInactive() : _upPrefab.InstantiateInactive();
var tornadoGO = GeneralPropBuilder.MakeFromPrefab(prefab, downwards ? "Tornado_Down" : "Tornado_Up", planetGO, sector, info, true);
// Override general prop position with randomized default position
if (info.position == null)
{
tornadoGO.transform.position = planetGO.transform.TransformPoint(position);
}
var tornadoGO = GeneralPropBuilder.MakeFromPrefab(prefab, downwards ? "Tornado_Down" : "Tornado_Up", planetGO, sector, info, true, defaultPosition: position);
// Add the sound thing before changing the scale
var soundGO = _soundPrefab.InstantiateInactive();

View File

@ -1,3 +1,4 @@
using NewHorizons.Builder.Props;
using NewHorizons.Components.Achievement;
using NewHorizons.External.Modules;
using OWML.Common;
@ -9,7 +10,7 @@ namespace NewHorizons.Builder.ShipLog
{
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)
{
case VolumesModule.RevealVolumeInfo.RevealVolumeType.Enter:
@ -36,37 +37,6 @@ namespace NewHorizons.Builder.ShipLog
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)
{
var shape = MakeShape(go, info, Shape.CollisionMode.Volume);

View File

@ -1,3 +1,4 @@
using NewHorizons.Builder.Props;
using NewHorizons.External.Modules;
using NewHorizons.Utility;
using OWML.Common;
@ -16,32 +17,7 @@ namespace NewHorizons.Builder.Volumes
{
public static AudioVolume Make(GameObject planetGO, Sector sector, VolumesModule.AudioVolumeInfo info, IModBehaviour mod)
{
var go = new GameObject("AudioVolume");
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);
var go = GeneralPropBuilder.MakeNew("AudioVolume", planetGO, sector, info);
go.layer = LayerMask.NameToLayer("AdvancedEffectVolume");
var audioSource = go.AddComponent<AudioSource>();

View File

@ -1,3 +1,4 @@
using NewHorizons.Builder.Props;
using NewHorizons.External.Modules;
using OWML.Common;
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)
{
var go = new GameObject("HazardVolume");
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);
var go = GeneralPropBuilder.MakeNew("HazardVolume", planetGO, sector, info);
go.layer = LayerMask.NameToLayer("BasicEffectVolume");
var shape = go.AddComponent<SphereShape>();

View File

@ -1,3 +1,4 @@
using NewHorizons.Builder.Props;
using NewHorizons.External.Modules;
using NewHorizons.Utility;
using OWML.Common;
@ -16,32 +17,7 @@ namespace NewHorizons.Builder.Volumes
{
public static NHNotificationVolume Make(GameObject planetGO, Sector sector, VolumesModule.NotificationVolumeInfo info, IModBehaviour mod)
{
var go = new GameObject("NotificationVolume");
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);
var go = GeneralPropBuilder.MakeNew("NotificationVolume", planetGO, sector, info);
go.layer = LayerMask.NameToLayer("BasicEffectVolume");
var shape = go.AddComponent<SphereShape>();

View File

@ -1,3 +1,4 @@
using NewHorizons.Builder.Props;
using NewHorizons.Components;
using NewHorizons.External.Modules;
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
{
var go = new GameObject(typeof(TVolume).Name);
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);
var go = GeneralPropBuilder.MakeNew(typeof(TVolume).Name, planetGO, sector, info);
go.layer = LayerMask.NameToLayer("BasicEffectVolume");
var collider = go.AddComponent<SphereCollider>();