Disable time loop properly if u remove the warp core and warp away

This commit is contained in:
xen-42 2024-10-05 22:01:31 -04:00
parent fafb0a5df0
commit 2ad32409ab

View File

@ -5,8 +5,8 @@ using NewHorizons.Utility;
using NewHorizons.Utility.OWML;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace NewHorizons.Handlers;
@ -34,6 +34,8 @@ public static class HeldItemHandler
/// </summary>
private static string _currentStarSystem;
private const string ADVANCED_WARP_CORE = "TowerTwin_Body/Sector_TowerTwin/Sector_TimeLoopInterior/Interactables_TimeLoopInterior/WarpCoreSocket/Prefab_NOM_WarpCoreVessel";
[HarmonyPrefix, HarmonyPatch(typeof(ItemTool), nameof(ItemTool.Awake))]
private static void Init()
{
@ -79,12 +81,7 @@ public static class HeldItemHandler
return newObject;
}
private static void OnStarSystemChanging()
{
if (_currentlyHeldItem != null)
{
// Track it so that when we return to this system we can delete the original
if (_trackedPaths.TryGetValue(_currentlyHeldItem, out var path))
private static void TrackPath(string path)
{
if (!_pathOfItemTakenFromSystem.ContainsKey(Main.Instance.CurrentStarSystem))
{
@ -93,11 +90,32 @@ public static class HeldItemHandler
_pathOfItemTakenFromSystem[Main.Instance.CurrentStarSystem].Add(path);
}
private static void OnStarSystemChanging()
{
if (_currentlyHeldItem != null)
{
// Track it so that when we return to this system we can delete the original
if (_trackedPaths.TryGetValue(_currentlyHeldItem, out var path))
{
TrackPath(path);
}
NHLogger.Log($"Scene unloaded, preserved inactive held item {_currentlyHeldItem.name}");
// For some reason, the original will get destroyed no matter what do we make a copy
_currentlyHeldItem = MakePerfectCopy(_currentlyHeldItem).DontDestroyOnLoad();
}
// If warping with a vessel, make sure to also track the path to the advanced warp core (assuming the player actually removed it)
if (Main.Instance.CurrentStarSystem == "SolarSystem" && Main.Instance.IsWarpingFromVessel)
{
// Making sure its actually gone
var warpCoreSocket = GameObject.FindObjectOfType<TimeLoopCoreController>()._warpCoreSocket;
if (!warpCoreSocket.IsSocketOccupied() || warpCoreSocket.GetWarpCoreType() != WarpCoreType.Vessel)
{
TrackPath(ADVANCED_WARP_CORE);
}
}
_trackedPaths.Clear();
_isSystemReady = false;
}
@ -108,6 +126,10 @@ public static class HeldItemHandler
if (_pathOfItemTakenFromSystem.TryGetValue(Main.Instance.CurrentStarSystem, out var paths))
{
foreach (var path in paths)
{
Delay.FireInNUpdates(() =>
{
try
{
NHLogger.Log($"Removing item that was taken from this system at {path}");
var item = SearchUtilities.Find(path)?.GetComponent<OWItem>();
@ -115,9 +137,23 @@ public static class HeldItemHandler
if (item.GetComponentInParent<OWItemSocket>() is OWItemSocket socket)
{
socket.RemoveFromSocket();
// Time loop core controller doesn't have time to hook up its events yet so we call this manually
if (path == ADVANCED_WARP_CORE)
{
var controller = GameObject.FindObjectOfType<TimeLoopCoreController>();
controller.OpenCore();
controller.OnSocketableRemoved(item);
}
NHLogger.Log($"Unsocketed {item.name}");
}
GameObject.Destroy(item.gameObject);
}
catch (Exception e)
{
NHLogger.LogError($"Failed to remove item at {path}: {e}");
}
}, 2);
}
}
// Give whatever item we were previously holding