Skip to content

Commit

Permalink
feat: Added moonpool fabricator BZ (#494)
Browse files Browse the repository at this point in the history
Due to finding out that the asset bundle and prefab is still in BZ and finding I was able to load them using AssetReferenceGameObject this will now allow us to use that model in BZ aswell.
  • Loading branch information
MrPurple6411 committed Oct 25, 2023
1 parent 638fc00 commit fcf6c79
Showing 1 changed file with 50 additions and 29 deletions.
79 changes: 50 additions & 29 deletions Nautilus/Assets/PrefabTemplates/FabricatorTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
namespace Nautilus.Assets.PrefabTemplates;

using System;
using System.Collections;
using Nautilus.Extensions;
using Nautilus.Utility;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UWE;
using Object = UnityEngine.Object;

namespace Nautilus.Assets.PrefabTemplates;

/// <summary>
/// Represents an fabricator template. This template is capable of returning a Fabricator or a Workbench.
Expand All @@ -32,15 +33,13 @@ public enum Model
/// The modification station that upgrades your equipment.
/// </summary>
Workbench,

#if SUBNAUTICA

/// <summary>
/// The style of fabricator found in the Moon Pool and the Cyclops sub.
/// </summary>
MoonPool,
#endif
}

/// <summary>
/// The model this template will use. Leave it to <see cref="Model.Custom"/> if you've got a custom model.
/// </summary>
Expand All @@ -55,13 +54,13 @@ public enum Model
/// <see cref="Utility.ConstructableFlags.Wall"/> for non-workbench fabricators.<br/>
/// And <see cref="Utility.ConstructableFlags.Ground"/> and <see cref="Utility.ConstructableFlags.Rotatable"/> for workbench.
/// </summary>
public ConstructableFlags ConstructableFlags
public ConstructableFlags ConstructableFlags
{
get
{
if (_constructableFlags == ConstructableFlags.None)
{
_constructableFlags = ConstructableFlags.Inside |
_constructableFlags = ConstructableFlags.Inside |
(FabricatorModel == Model.Workbench
? ConstructableFlags.Ground | ConstructableFlags.Rotatable
: ConstructableFlags.Wall);
Expand All @@ -81,14 +80,14 @@ public ConstructableFlags ConstructableFlags
/// Callback that will get called after the prefab is retrieved. Use this to modify or process your prefab further more.
/// </summary>
public System.Action<GameObject> ModifyPrefab { get; set; }

/// <summary>
/// Callback that will get called after the prefab is retrieved. Use this to modify or process your prefab further more asynchronously.
/// </summary>
public System.Func<GameObject, IEnumerator> ModifyPrefabAsync { get; set; }

private readonly CraftTree.Type _craftTreeType;

/// <summary>
/// Creates a <see cref="FabricatorTemplate"/> instance.
/// </summary>
Expand Down Expand Up @@ -121,8 +120,7 @@ public override IEnumerator GetPrefabAsync(TaskResult<GameObject> gameObject)

private IEnumerator CreateFabricator(IOut<GameObject> gameObject)
{
var task = GetReferenceTask();
if (task is null || FabricatorModel == Model.Custom)
if (FabricatorModel == Model.Custom)
{
InternalLogger.Error($"""
Failed retrieving the requested fabricator model. Please ensure the "{nameof(FabricatorTemplate)}.{nameof(FabricatorModel)}" property is assigned.
Expand All @@ -131,25 +129,50 @@ private IEnumerator CreateFabricator(IOut<GameObject> gameObject)

yield break;
}

yield return task;

task.TryGetPrefab(out var prefab);
var obj = Object.Instantiate(prefab);

var task = new TaskResult<GameObject>();

yield return GetReferenceTask(task);

var prefab = task.Get();
if (prefab == null)
{
InternalLogger.Error($"Failed to get prefab for {FabricatorModel}!!!!!!!! PLEASE REPORT THIS BUG TO THE NAUTILUS TEAM!");
}

var obj = GameObject.Instantiate(prefab);
yield return ApplyCrafterPrefab(obj);
gameObject.Set(obj);
}

private IPrefabRequest GetReferenceTask()
private IEnumerator GetReferenceTask(IOut<GameObject> prefab)
{
return FabricatorModel switch
switch (FabricatorModel)
{
Model.Fabricator => PrefabDatabase.GetPrefabAsync(CraftData.GetClassIdForTechType(TechType.Fabricator)),
Model.Workbench => PrefabDatabase.GetPrefabAsync(CraftData.GetClassIdForTechType(TechType.Workbench)),
#if SUBNAUTICA
Model.MoonPool => PrefabDatabase.GetPrefabForFilenameAsync("Submarine/Build/CyclopsFabricator.prefab"),
#endif
_ => null,
case Model.Fabricator:
{
var task = PrefabDatabase.GetPrefabAsync(CraftData.GetClassIdForTechType(TechType.Fabricator));
yield return task;
if (task.TryGetPrefab(out var obj))
prefab.Set(obj);
break;
}
case Model.Workbench:
{
var task = PrefabDatabase.GetPrefabAsync(CraftData.GetClassIdForTechType(TechType.Fabricator));
yield return task;
if (task.TryGetPrefab(out var obj))
prefab.Set(obj);
break;
}
case Model.MoonPool:
{
var task = new AssetReferenceGameObject("Submarine/Build/CyclopsFabricator.prefab").ForceValid().LoadAssetAsync();
yield return task;
if (task.Status == UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationStatus.Succeeded)
prefab.Set(task.Result);
break;
}
};
}

Expand All @@ -163,7 +186,6 @@ private IEnumerator ApplyCrafterPrefab(GameObject obj)
case Model.Custom:
crafter = obj.EnsureComponent<Fabricator>();
break;
#if SUBNAUTICA
case Model.MoonPool:
// Retrieve sub game objects
GameObject cyclopsFabLight = obj.FindChild("fabricatorLight");
Expand All @@ -179,7 +201,6 @@ private IEnumerator ApplyCrafterPrefab(GameObject obj)
obj.EnsureComponent<Constructable>().model = cyclopsFabModel;
crafter = obj.EnsureComponent<Fabricator>();
break;
#endif
case Model.Workbench:
crafter = obj.EnsureComponent<Workbench>();
break;
Expand Down

0 comments on commit fcf6c79

Please sign in to comment.