Skip to content

Commit

Permalink
Merge pull request #57 from Alexejhero/sneaky-fumo
Browse files Browse the repository at this point in the history
fumo spawn thing
  • Loading branch information
Alexejhero authored Apr 23, 2024
2 parents 2e7520e + 171864c commit 2854ae7
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 38 deletions.
15 changes: 14 additions & 1 deletion SCHIZO/Items/FumoItem/FumoItemLoader.BelowZero.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
using Nautilus.Assets;
using Nautilus.Utility;
using UnityEngine;

namespace SCHIZO.Items.FumoItem;

partial class FumoItemLoader
{
public override void Load(ModItem modItem)
{
base.Load(modItem);
FumoItemPatches.Register(modItem, spawn);
CustomPrefab prefab = new(spawnerClassId, null, null);
prefab.SetGameObject(() =>
{
GameObject instance = GameObject.Instantiate(spawnerPrefab);
PrefabUtils.AddBasicComponents(instance, spawnerClassId, TechType.None, LargeWorldEntity.CellLevel.Global);
instance.SetActive(false); // why do we have to do this manually again
return instance;
});
prefab.Register();
FumoItemPatches.Register(spawnerClassId);
}
}
42 changes: 15 additions & 27 deletions SCHIZO/Items/FumoItem/FumoItemPatches.BelowZero.cs
Original file line number Diff line number Diff line change
@@ -1,64 +1,52 @@
using System.Collections;
using HarmonyLib;
using SCHIZO.Spawns;
using UnityEngine;
using UWE;

namespace SCHIZO.Items.FumoItem;
[HarmonyPatch]
partial class FumoItemPatches
{
private static bool _registered;
private static ModItem fumoItem;
private static SpawnLocation spawnLoc; // local to lifepod
private static string _spawnerClassId;

public static void Register(ModItem modItem, SpawnLocation loc)
public static void Register(string spawnerClassId)
{
if (_registered) LOGGER.LogWarning("Fumo item spawns already registered - overriding previous");
_registered = true;

fumoItem = modItem;
spawnLoc = loc;
if (_spawnerClassId is { })
LOGGER.LogWarning("Fumo item spawns already registered - overriding previous");
_spawnerClassId = spawnerClassId;
}

[HarmonyPatch(typeof(LifepodDrop), nameof(LifepodDrop.OnSettled))]
[HarmonyPostfix]
public static void SpawnFumoInLifepodDrop(LifepodDrop __instance)
{
if (!_registered) return;
if (_spawnerClassId is null) return;

CoroutineHost.StartCoroutine(SpawnFumoCoro(__instance));
}

// code "adapted" from nautilus's EntitySpawner
private static IEnumerator SpawnFumoCoro(LifepodDrop pod)
{
string fumoItemClassId = fumoItem.PrefabInfo.ClassID;

IPrefabRequest request = PrefabDatabase.GetPrefabAsync(fumoItemClassId);
IPrefabRequest request = PrefabDatabase.GetPrefabAsync(_spawnerClassId);
yield return request;
if (!request.TryGetPrefab(out GameObject prefab))
{
LOGGER.LogWarning("Could not get prefab for fumo item, will not spawn in lifepod - unlocking automatically");
KnownTech.Add(fumoItem, false, false);
}

// shouldn't spawn twice anymore w/ new code but shrug
if (Object.FindObjectOfType<FumoItemTool>())
if (!request.TryGetPrefab(out GameObject spawnerPrefab))
{
LOGGER.LogWarning("Attempted to spawn fumo in lifepod twice");
LOGGER.LogError("Could not get prefab for fumo spawner, will not spawn in lifepod");
yield break;
}
yield return new WaitUntil(() => LargeWorldStreamer.main && LargeWorldStreamer.main.IsReady());
LargeWorldStreamer lws = LargeWorldStreamer.main;

Int3 batch = lws.GetContainingBatch(pod.transform.localToWorldMatrix.MultiplyPoint(spawnLoc.position));
Int3 batch = lws.GetContainingBatch(pod.transform.TransformPoint(spawnerPrefab.transform.position));
yield return new WaitUntil(() => lws.IsBatchReadyToCompile(batch));

yield return new WaitUntil(() => LargeWorld.main && LargeWorld.main.streamer.globalRoot != null);

LOGGER.LogMessage("Spawning fumo in lifepod drop");
GameObject obj = UWE.Utils.InstantiateDeactivated(prefab, pod.transform, spawnLoc.position, Quaternion.Euler(spawnLoc.rotation));
obj.SetActive(true);
LOGGER.LogMessage("Spawning fumo spawner in lifepod drop");
GameObject obj = GameObject.Instantiate(spawnerPrefab, pod.transform, false);
LargeWorldEntity lwe = obj.EnsureComponent<LargeWorldEntity>();
lwe.cellLevel = LargeWorldEntity.CellLevel.Global; // do not unload under any circumstances

LargeWorldEntity.Register(obj);
}
Expand Down
72 changes: 72 additions & 0 deletions SCHIZO/Items/FumoItem/SneakyFumoSpawner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Collections;
using SCHIZO.Helpers;
using UnityEngine;
using UWE;

namespace SCHIZO.Items.FumoItem;

partial class SneakyFumoSpawner
{
// prevent dupes or whatever
private const string _storyGoal = "SneakyFumo";
private GameObject _prefab;

private IEnumerator Start()
{
if (StoryGoalHelpers.IsCompleted(_storyGoal))
yield return TheFumoAppears();
else
yield return NeuroFumoIs100mFromYourLocationAndRapidlyApproaching();
}

private IEnumerator NeuroFumoIs100mFromYourLocationAndRapidlyApproaching()
{
// Log("starting");
// player enters drop pod for the first time, no fumo is present
while (!StoryGoalHelpers.IsCompleted("OnEnterLifepod"))
yield return new WaitForSecondsRealtime(5);
// Log("entered");

// player leaves for a while
while (!StoryGoalHelpers.IsCompleted("OnExitLifepod"))
yield return new WaitForSecondsRealtime(5);
// Log("exited");

yield return new WaitForSeconds(minAwayTime); // probably completely useless
// Log("waited for time, waiting for distance");
while ((Player.main.transform.position - transform.position).magnitude < minAwayDistance)
yield return new WaitForSecondsRealtime(1);
// Log("spawning");

yield return TheFumoAppears();
}
// private void Log(string msg) => LOGGER.LogDebug($"{name}: {msg}");

private IEnumerator GetPrefab()
{
IPrefabRequest request = PrefabDatabase.GetPrefabAsync(spawnData.classId);
yield return request;
if (!request.TryGetPrefab(out _prefab))
{
LOGGER.LogError("Could not get prefab for fumo item, will not spawn in lifepod");
// if there's no prefab, unlocking it is probably not a good idea
// KnownTech.Add(_fumoItem, false, false);
Destroy(this);
yield break;
}
}

private IEnumerator TheFumoAppears()
{
StoryGoalHelpers.Trigger(_storyGoal);

yield return GetPrefab();
if (!_prefab) yield break;

GameObject fumo = GameObject.Instantiate(_prefab, transform);
fumo.transform.parent = transform.parent; // reparent to drop pod (keeping the spawner's local pos/rot)
LargeWorldEntity.Register(fumo);

Destroy(this);
}
}
2 changes: 1 addition & 1 deletion SCHIZO/Resources/AssetBundles/Assets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace SCHIZO.Resources;

public static class Assets
{
private const int _rnd = -474431403;
private const int _rnd = 1026605148;

private static readonly UnityEngine.AssetBundle _a = ResourceManager.GetAssetBundle("assets");

Expand Down
Binary file modified SCHIZO/Resources/AssetBundles/assets
Binary file not shown.
7 changes: 3 additions & 4 deletions Unity/Assets/Mod/Neurofumo/Neurofumo item data.asset
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ MonoBehaviour:
isCraftable: 1
isBuildable: 0
craftingTime: 3
itemSounds: {fileID: 0}
pdaEncyInfo: {fileID: 0}
knownTechInfo: {fileID: 11400000, guid: c1716260159e05a49912f7981ea989c6, type: 2}
spawnData: {fileID: 0}
Expand Down Expand Up @@ -64,6 +63,6 @@ MonoBehaviour:
00000000:
type: {class: FumoItemLoader, ns: SCHIZO.Items.FumoItem, asm: SCHIZO}
data:
spawn:
position: {x: 0, y: 1.335, z: 0}
rotation: {x: 0, y: 180, z: 0}
spawnerClassId: sneakyfumospawner
spawnerPrefab: {fileID: 1478916698337670458, guid: ba3872e982d6d3b4083f89a38d44f16c,
type: 3}
1 change: 0 additions & 1 deletion Unity/Assets/Mod/Neurofumo/Neurofumo.asset
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ MonoBehaviour:
registryItems:
- {fileID: 11400000, guid: d650fbe7797f4e44180438b95b09620f, type: 2}
- {fileID: 11400000, guid: b81a466a47b11be4e870eaaa30acf5aa, type: 2}
- {fileID: 11400000, guid: e84785f0b1b5820488bdff72c72ebe81, type: 2}
75 changes: 75 additions & 0 deletions Unity/Assets/Mod/Neurofumo/Sneaky fumo spawner.prefab
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &1478916698337670458
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 7622607892110866216}
- component: {fileID: 4312043234953409674}
- component: {fileID: 821427263523968193}
- component: {fileID: -5845103137200979273}
m_Layer: 0
m_Name: Sneaky fumo spawner
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &7622607892110866216
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1478916698337670458}
m_LocalRotation: {x: 0, y: 1, z: 0, w: 0}
m_LocalPosition: {x: 0, y: 1.335, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 180, z: 0}
--- !u!114 &4312043234953409674
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1478916698337670458}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9c68da6d9fffcf849a9b674eb8c9dfc9, type: 3}
m_Name:
m_EditorClassIdentifier:
minAwayTime: 60
minAwayDistance: 100
spawnData: {fileID: 11400000, guid: b81a466a47b11be4e870eaaa30acf5aa, type: 2}
--- !u!114 &821427263523968193
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1478916698337670458}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 7fb4446287c5460fbe57e2cf1d3c2623, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &-5845103137200979273
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1478916698337670458}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d97a98bece9544178554e9acad0eb069, type: 3}
m_Name:
m_EditorClassIdentifier:
cellLevel: 100
7 changes: 7 additions & 0 deletions Unity/Assets/Mod/Neurofumo/Sneaky fumo spawner.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions Unity/Assets/Scripts/SCHIZO/Items/FumoItem/FumoItemLoader.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using SCHIZO.Items.Data;
using SCHIZO.Spawns;
using TriInspector;
using UnityEngine;

namespace SCHIZO.Items.FumoItem
{
public sealed partial class FumoItemLoader : ItemLoader
{
[InfoBox("This is LOCAL to the drop pod prefab since it lands in a random location")]
public SpawnLocation spawn;
public string spawnerClassId;
[SerializeField]
private GameObject spawnerPrefab;
public override TriValidationResult AcceptsItem(ItemData item)
{
return item.classId.ToLower() == "neurofumoitem"
return item.classId.Equals("neurofumoitem", System.StringComparison.OrdinalIgnoreCase)
? TriValidationResult.Valid
: TriValidationResult.Error("FumoItemLoader only accepts a NeuroFumoItem");
}
Expand Down
12 changes: 12 additions & 0 deletions Unity/Assets/Scripts/SCHIZO/Items/FumoItem/SneakyFumoSpawner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using SCHIZO.Items.Data;
using UnityEngine;

namespace SCHIZO.Items.FumoItem
{
public partial class SneakyFumoSpawner : MonoBehaviour
{
public float minAwayTime = 60f;
public float minAwayDistance = 100f;
public ItemData spawnData;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2854ae7

Please sign in to comment.