Add default star system override option

This commit is contained in:
Nick 2022-05-13 16:30:37 -04:00
parent 87c859728e
commit d5ee5ff33a

View File

@ -35,9 +35,10 @@ namespace NewHorizons
public static Main Instance { get; private set; } public static Main Instance { get; private set; }
// Settings // Settings
public static bool Debug; public static bool Debug { get; private set; }
private static bool _useCustomTitleScreen; private static bool _useCustomTitleScreen;
private static bool _wasConfigured = false; private static bool _wasConfigured = false;
private static string _defaultSystemOverride;
public static Dictionary<string, NewHorizonsSystem> SystemDict = new Dictionary<string, NewHorizonsSystem>(); public static Dictionary<string, NewHorizonsSystem> SystemDict = new Dictionary<string, NewHorizonsSystem>();
public static Dictionary<string, List<NewHorizonsBody>> BodyDict = new Dictionary<string, List<NewHorizonsBody>>(); public static Dictionary<string, List<NewHorizonsBody>> BodyDict = new Dictionary<string, List<NewHorizonsBody>>();
@ -84,6 +85,8 @@ namespace NewHorizons
DebugReload.UpdateReloadButton(); DebugReload.UpdateReloadButton();
Logger.UpdateLogLevel(Debug ? Logger.LogType.Log : Logger.LogType.Error); Logger.UpdateLogLevel(Debug ? Logger.LogType.Log : Logger.LogType.Error);
_defaultSystemOverride = config.GetSettingsValue<string>("Default System Override");
var wasUsingCustomTitleScreen = _useCustomTitleScreen; var wasUsingCustomTitleScreen = _useCustomTitleScreen;
_useCustomTitleScreen = config.GetSettingsValue<bool>("Custom title screen"); _useCustomTitleScreen = config.GetSettingsValue<bool>("Custom title screen");
// Reload the title screen if this was updated on it // Reload the title screen if this was updated on it
@ -111,11 +114,14 @@ namespace NewHorizons
destroyStockPlanets = false destroyStockPlanets = false
} }
}; };
foreach (AssetBundle bundle in AssetBundles.Values)
foreach (var pair in AssetBundles)
{ {
bundle.Unload(true); if (pair.Value == null) Logger.LogError($"The asset bundle for {pair.Key} was null when trying to unload");
else pair.Value.Unload(true);
} }
AssetBundles.Clear(); AssetBundles.Clear();
if (!resetTranslation) return; if (!resetTranslation) return;
TranslationHandler.ClearTables(); TranslationHandler.ClearTables();
TextTranslation.Get().SetLanguage(TextTranslation.Get().GetLanguage()); TextTranslation.Get().SetLanguage(TextTranslation.Get().GetLanguage());
@ -136,9 +142,9 @@ namespace NewHorizons
GlobalMessenger<DeathType>.AddListener("PlayerDeath", OnDeath); GlobalMessenger<DeathType>.AddListener("PlayerDeath", OnDeath);
GlobalMessenger.AddListener("WakeUp", new Callback(OnWakeUp)); GlobalMessenger.AddListener("WakeUp", new Callback(OnWakeUp));
ShaderBundle = Main.Instance.ModHelper.Assets.LoadBundle("AssetBundle/shader"); ShaderBundle = Main.Instance.ModHelper.Assets.LoadBundle("AssetBundle/shader");
ResetConfigs(resetTranslation: false); ResetConfigs(resetTranslation: false);
Logger.Log("Begin load of config files...", Logger.LogType.Log); Logger.Log("Begin load of config files...", Logger.LogType.Log);
try try
@ -153,7 +159,7 @@ namespace NewHorizons
Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => OnSceneLoaded(SceneManager.GetActiveScene(), LoadSceneMode.Single)); Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => OnSceneLoaded(SceneManager.GetActiveScene(), LoadSceneMode.Single));
Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => _firstLoad = false); Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => _firstLoad = false);
Instance.ModHelper.Menus.PauseMenu.OnInit += DebugReload.InitializePauseMenu; Instance.ModHelper.Menus.PauseMenu.OnInit += DebugReload.InitializePauseMenu;
} }
public void OnDestroy() public void OnDestroy()
{ {
@ -182,22 +188,22 @@ namespace NewHorizons
Logger.Log($"Scene Loaded: {scene.name} {mode}"); Logger.Log($"Scene Loaded: {scene.name} {mode}");
// Set time loop stuff if its enabled and if we're warping to a new place // Set time loop stuff if its enabled and if we're warping to a new place
if(_isChangingStarSystem && (SystemDict[_currentStarSystem].Config.enableTimeLoop || _currentStarSystem == "SolarSystem") && SecondsLeftInLoop > 0f) if (_isChangingStarSystem && (SystemDict[_currentStarSystem].Config.enableTimeLoop || _currentStarSystem == "SolarSystem") && SecondsLeftInLoop > 0f)
{ {
TimeLoop.SetSecondsRemaining(SecondsLeftInLoop); TimeLoop.SetSecondsRemaining(SecondsLeftInLoop);
// Prevent the OPC from firing // Prevent the OPC from firing
var launchController = GameObject.FindObjectOfType<OrbitalProbeLaunchController>(); var launchController = GameObject.FindObjectOfType<OrbitalProbeLaunchController>();
if(launchController != null) if (launchController != null)
{ {
GlobalMessenger<int>.RemoveListener("StartOfTimeLoop", launchController.OnStartOfTimeLoop); GlobalMessenger<int>.RemoveListener("StartOfTimeLoop", launchController.OnStartOfTimeLoop);
foreach(var fakeDebris in launchController._fakeDebrisBodies) foreach (var fakeDebris in launchController._fakeDebrisBodies)
{ {
fakeDebris.gameObject.SetActive(false); fakeDebris.gameObject.SetActive(false);
} }
launchController.enabled = false; launchController.enabled = false;
} }
var nomaiProbe = GameObject.Find("NomaiProbe_Body"); var nomaiProbe = GameObject.Find("NomaiProbe_Body");
if(nomaiProbe != null) nomaiProbe.gameObject.SetActive(false); if (nomaiProbe != null) nomaiProbe.gameObject.SetActive(false);
} }
// Reset this // Reset this
@ -210,21 +216,21 @@ namespace NewHorizons
TitleSceneHandler.DisplayBodyOnTitleScreen(BodyDict.Values.ToList().SelectMany(x => x).ToList()); TitleSceneHandler.DisplayBodyOnTitleScreen(BodyDict.Values.ToList().SelectMany(x => x).ToList());
} }
if(scene.name == "EyeOfTheUniverse" && IsWarping) if (scene.name == "EyeOfTheUniverse" && IsWarping)
{ {
if(_ship != null) SceneManager.MoveGameObjectToScene(_ship, SceneManager.GetActiveScene()); if (_ship != null) SceneManager.MoveGameObjectToScene(_ship, SceneManager.GetActiveScene());
_ship.transform.position = new Vector3(50, 0, 0); _ship.transform.position = new Vector3(50, 0, 0);
_ship.SetActive(true); _ship.SetActive(true);
} }
if(scene.name == "SolarSystem") if (scene.name == "SolarSystem")
{ {
foreach(var body in GameObject.FindObjectsOfType<AstroObject>()) foreach (var body in GameObject.FindObjectsOfType<AstroObject>())
{ {
Logger.Log($"{body.name}, {body.transform.rotation}"); Logger.Log($"{body.name}, {body.transform.rotation}");
} }
if(_ship != null) if (_ship != null)
{ {
_ship = GameObject.Find("Ship_Body").InstantiateInactive(); _ship = GameObject.Find("Ship_Body").InstantiateInactive();
DontDestroyOnLoad(_ship); DontDestroyOnLoad(_ship);
@ -252,14 +258,14 @@ namespace NewHorizons
if (IsWarping && _shipWarpController) if (IsWarping && _shipWarpController)
{ {
Instance.ModHelper.Events.Unity.RunWhen( Instance.ModHelper.Events.Unity.RunWhen(
() => IsSystemReady, () => IsSystemReady,
() => _shipWarpController.WarpIn(WearingSuit) () => _shipWarpController.WarpIn(WearingSuit)
); );
} }
else else
{ {
Instance.ModHelper.Events.Unity.RunWhen( Instance.ModHelper.Events.Unity.RunWhen(
() => IsSystemReady, () => IsSystemReady,
() => FindObjectOfType<PlayerSpawner>().DebugWarp(SystemDict[_currentStarSystem].SpawnPoint) () => FindObjectOfType<PlayerSpawner>().DebugWarp(SystemDict[_currentStarSystem].SpawnPoint)
); );
} }
@ -274,7 +280,16 @@ namespace NewHorizons
else else
{ {
// Reset back to original solar system after going to main menu. // Reset back to original solar system after going to main menu.
_currentStarSystem = _defaultStarSystem; // If the override is a valid system then we go there
if (SystemDict.Keys.Contains(_defaultSystemOverride))
{
_currentStarSystem = _defaultSystemOverride;
IsWarping = true; // always do this else sometimes the spawn gets messed up
}
else
{
_currentStarSystem = _defaultStarSystem;
}
} }
} }
@ -309,6 +324,12 @@ namespace NewHorizons
var relativePath = file.Replace(folder, ""); var relativePath = file.Replace(folder, "");
var starSystemConfig = mod.ModHelper.Storage.Load<StarSystemConfig>(relativePath); var starSystemConfig = mod.ModHelper.Storage.Load<StarSystemConfig>(relativePath);
if (starSystemConfig.startHere)
{
// We always want to allow mods to overwrite setting the main SolarSystem as default but not the other way around
if (name != "SolarSystem") SetDefaultSystem(name);
}
var system = new NewHorizonsSystem(name, starSystemConfig, mod); var system = new NewHorizonsSystem(name, starSystemConfig, mod);
SystemDict[name] = system; SystemDict[name] = system;
} }
@ -336,7 +357,7 @@ namespace NewHorizons
LoadTranslations(folder, mod); LoadTranslations(folder, mod);
} }
} }
catch(Exception ex) catch (Exception ex)
{ {
Logger.LogError($"{ex.Message}, {ex.StackTrace}"); Logger.LogError($"{ex.Message}, {ex.StackTrace}");
} }
@ -380,15 +401,13 @@ namespace NewHorizons
if (config.Base.CenterOfSolarSystem) config.Orbit.IsStatic = true; if (config.Base.CenterOfSolarSystem) config.Orbit.IsStatic = true;
if (!SystemDict.ContainsKey(config.StarSystem)) if (!SystemDict.ContainsKey(config.StarSystem))
{ {
// See if theres a star system config // Since we didn't load it earlier there shouldn't be a star system config
var starSystemConfig = mod.ModHelper.Storage.Load<StarSystemConfig>($"systems/{config.StarSystem}.json"); var starSystemConfig = mod.ModHelper.Storage.Load<StarSystemConfig>($"systems/{config.StarSystem}.json");
if (starSystemConfig == null) starSystemConfig = new StarSystemConfig(null); if (starSystemConfig == null) starSystemConfig = new StarSystemConfig(null);
else Logger.Log($"Loaded system config for {config.StarSystem}"); else Logger.LogWarning($"Loaded system config for {config.StarSystem}. Why wasn't this loaded earlier?");
var system = new NewHorizonsSystem(config.StarSystem, starSystemConfig, mod); var system = new NewHorizonsSystem(config.StarSystem, starSystemConfig, mod);
if (system.Config.startHere) SetDefaultSystem(system.Name);
SystemDict.Add(config.StarSystem, system); SystemDict.Add(config.StarSystem, system);
BodyDict.Add(config.StarSystem, new List<NewHorizonsBody>()); BodyDict.Add(config.StarSystem, new List<NewHorizonsBody>());
@ -420,7 +439,7 @@ namespace NewHorizons
OnChangeStarSystem?.Invoke(newStarSystem); OnChangeStarSystem?.Invoke(newStarSystem);
Logger.Log($"Warping to {newStarSystem}"); Logger.Log($"Warping to {newStarSystem}");
if(warp && _shipWarpController) _shipWarpController.WarpOut(); if (warp && _shipWarpController) _shipWarpController.WarpOut();
_currentStarSystem = newStarSystem; _currentStarSystem = newStarSystem;
_isChangingStarSystem = true; _isChangingStarSystem = true;
IsWarping = warp; IsWarping = warp;
@ -429,7 +448,7 @@ namespace NewHorizons
// We kill them so they don't move as much // We kill them so they don't move as much
Locator.GetDeathManager().KillPlayer(DeathType.Meditation); Locator.GetDeathManager().KillPlayer(DeathType.Meditation);
if(newStarSystem == "EyeOfTheUniverse") if (newStarSystem == "EyeOfTheUniverse")
{ {
PlayerData.SaveWarpedToTheEye(TimeLoop.GetSecondsRemaining()); PlayerData.SaveWarpedToTheEye(TimeLoop.GetSecondsRemaining());
LoadManager.LoadSceneAsync(OWScene.EyeOfTheUniverse, true, LoadManager.FadeType.ToBlack, 0.1f, true); LoadManager.LoadSceneAsync(OWScene.EyeOfTheUniverse, true, LoadManager.FadeType.ToBlack, 0.1f, true);
@ -446,7 +465,17 @@ namespace NewHorizons
// We reset the solar system on death (unless we just killed the player) // We reset the solar system on death (unless we just killed the player)
if (!_isChangingStarSystem) if (!_isChangingStarSystem)
{ {
_currentStarSystem = _defaultStarSystem; // If the override is a valid system then we go there
if (SystemDict.Keys.Contains(_defaultSystemOverride))
{
_currentStarSystem = _defaultSystemOverride;
IsWarping = true; // always do this else sometimes the spawn gets messed up
}
else
{
_currentStarSystem = _defaultStarSystem;
}
IsWarping = false; IsWarping = false;
} }
} }