diff --git a/NewHorizons/Components/ChangeStarSystemVolume.cs b/NewHorizons/Components/ChangeStarSystemVolume.cs index 326809fc..76bd3375 100644 --- a/NewHorizons/Components/ChangeStarSystemVolume.cs +++ b/NewHorizons/Components/ChangeStarSystemVolume.cs @@ -24,7 +24,6 @@ namespace NewHorizons.Components public override void VanishPlayer(OWRigidbody playerBody, RelativeLocationData entryLocation) { Main.Instance.ChangeCurrentStarSystem(TargetSolarSystem, PlayerState.AtFlightConsole()); - //Main.Instance.ChangeCurrentStarSystem(TargetSolarSystem, false); } } } diff --git a/NewHorizons/Components/ShipWarpController.cs b/NewHorizons/Components/ShipWarpController.cs index aa17b48b..0d969394 100644 --- a/NewHorizons/Components/ShipWarpController.cs +++ b/NewHorizons/Components/ShipWarpController.cs @@ -125,7 +125,7 @@ namespace NewHorizons.Components Logger.Log("Player died in a warp drive accident, reviving them"); // Means the player was killed meaning they weren't teleported in Player.getResources()._currentHealth = 100f; - if(!PlayerState.AtFlightConsole()) Teleportation.teleportPlayerToShip(); + if (!PlayerState.AtFlightConsole()) TeleportToShip(); } } @@ -147,7 +147,7 @@ namespace NewHorizons.Components Logger.Log("Starting warp-in effect"); _oneShotSource.PlayOneShot(global::AudioType.VesselSingularityCollapse, 1f); Locator.GetDeathManager()._invincible = true; - if (Main.Instance.CurrentStarSystem.Equals("SolarSystem")) Teleportation.teleportPlayerToShip(); + if (Main.Instance.CurrentStarSystem.Equals("SolarSystem")) TeleportToShip(); _whitehole.Create(); _waitingToBeSeated = true; if (_wearingSuit && !Locator.GetPlayerController()._isWearingSuit) @@ -156,6 +156,12 @@ namespace NewHorizons.Components } } + private void TeleportToShip() + { + var playerSpawner = GameObject.FindObjectOfType(); + playerSpawner.DebugWarp(playerSpawner.GetSpawnPoint(SpawnLocation.Ship)); + } + public void FinishWarpIn() { Logger.Log("Finishing warp"); diff --git a/NewHorizons/External/StarSystemConfig.cs b/NewHorizons/External/StarSystemConfig.cs new file mode 100644 index 00000000..01928b83 --- /dev/null +++ b/NewHorizons/External/StarSystemConfig.cs @@ -0,0 +1,56 @@ +using NewHorizons.Utility; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NewHorizons.External +{ + public class StarSystemConfig + { + public bool canEnterViaWarpDrive = true; + public bool startHere = false; + public string factRequiredForWarp; + public NomaiCoordinates coords; + + public class NomaiCoordinates + { + public int[] x; + public int[] y; + public int[] z; + } + + public StarSystemConfig(Dictionary dict) + { + if (dict == null) return; + + foreach (var item in dict) + { + var property = typeof(PlanetConfig).GetProperty(item.Key, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); + if (property == null) + property = typeof(PlanetConfig).GetProperty(item.Key.ToCamelCase(), System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); + if (property == null) + property = typeof(PlanetConfig).GetProperty(item.Key.ToTitleCase(), System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); + + if (property != null) + { + if (property.PropertyType.BaseType == typeof(Module)) + { + if (property.GetValue(this) == null) + { + var module = Activator.CreateInstance(property.PropertyType); + property.SetValue(this, module); + } + ((Module)property.GetValue(this)).Build(item.Value as Dictionary); + } + else + { + property.SetValue(this, Convert.ChangeType(item.Value, property.PropertyType)); + } + } + else Logger.LogError($"{item.Key} {item.Value} is not valid. Is your config formatted correctly?"); + } + } + } +} diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index 29d4b03b..eef9c4dd 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -34,12 +34,14 @@ namespace NewHorizons public static AssetBundle ShaderBundle; public static Main Instance { get; private set; } + public static Dictionary SystemDict = new Dictionary(); public static Dictionary> BodyDict = new Dictionary>(); public static List NextPassBodies = new List(); public static Dictionary AssetBundles = new Dictionary(); public static float FurthestOrbit { get; set; } = 50000f; public StarLightController StarLightController { get; private set; } + private string _defaultStarSystem = "SolarSystem"; private string _currentStarSystem = "SolarSystem"; public string CurrentStarSystem { get { return Instance._currentStarSystem; } } @@ -63,6 +65,7 @@ namespace NewHorizons GlobalMessenger.AddListener("PlayerDeath", OnDeath); ShaderBundle = Main.Instance.ModHelper.Assets.LoadBundle("AssetBundle/shader"); BodyDict["SolarSystem"] = new List(); + SystemDict["SolarSystem"] = new NewHorizonsSystem("SolarSystem", new StarSystemConfig(null), this.ModHelper); Tools.Patches.Apply(); Tools.WarpDrivePatches.Apply(); @@ -79,7 +82,6 @@ namespace NewHorizons Logger.LogWarning("Couldn't find planets folder"); } - UnityEngine.Random.InitState((int)DateTime.Now.Ticks); Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => OnSceneLoaded(SceneManager.GetActiveScene(), LoadSceneMode.Single)); } @@ -101,7 +103,7 @@ namespace NewHorizons if (scene.name != "SolarSystem") { // Reset back to original solar system after going to main menu. - _currentStarSystem = "SolarSystem"; + _currentStarSystem = _defaultStarSystem; return; } @@ -111,11 +113,31 @@ namespace NewHorizons NewHorizonsData.Load(); - // Make the warp controller if there are multiple star systems - if (BodyDict.Keys.Count > 1) + // By default we dont have it + HasWarpDrive = false; + + // Lets us warp home if we want + if (_currentStarSystem != "SolarSystem") { HasWarpDrive = true; + } + else + { + // Make the warp controller if there are multiple star systems + foreach (NewHorizonsSystem system in SystemDict.Values) + { + Logger.Log($"System {system}, {system.Config.canEnterViaWarpDrive}"); + if (system.Config.canEnterViaWarpDrive && system.Spawn?.ShipSpawnPoint != null) + { + HasWarpDrive = true; + break; + } + } + } + if (HasWarpDrive == true) + { + Logger.Log("Setting up warp drive"); _shipWarpController = GameObject.Find("Ship_Body").AddComponent(); Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => StarChartHandler.Init()); @@ -221,6 +243,7 @@ namespace NewHorizons Logger.Log($"Is the player warping in? {IsWarping}"); if (IsWarping && _shipWarpController) Instance.ModHelper.Events.Unity.FireInNUpdates(() => _shipWarpController.WarpIn(WearingSuit), 1); + else Instance.ModHelper.Events.Unity.FireInNUpdates(() => GameObject.FindObjectOfType().DebugWarp(SystemDict[_currentStarSystem].SpawnPoint), 1); IsWarping = false; } @@ -335,6 +358,10 @@ namespace NewHorizons if(body != null) { + // Wanna track the spawn point of each system + if (body.Config.Spawn != null) SystemDict[body.Config.StarSystem].Spawn = body.Config.Spawn; + + // Add the new planet to the planet dictionary BodyDict[body.Config.StarSystem].Add(body); } } @@ -348,7 +375,24 @@ namespace NewHorizons var config = mod.ModHelper.Storage.Load(relativeDirectory); Logger.Log($"Loaded {config.Name}"); if (config.Base.CenterOfSolarSystem) config.Orbit.IsStatic = true; - if (!BodyDict.ContainsKey(config.StarSystem)) BodyDict.Add(config.StarSystem, new List()); + if (!BodyDict.ContainsKey(config.StarSystem)) + { + // See if theres a star system config + var starSystemConfig = mod.ModHelper.Storage.Load($"{config.StarSystem}.json"); + if (starSystemConfig == null) starSystemConfig = new StarSystemConfig(null); + else Logger.Log($"Loaded system config for {config.StarSystem}"); + + // Since we only load stuff the first time we can do this now + if (starSystemConfig.startHere) + { + _defaultStarSystem = config.StarSystem; + _currentStarSystem = config.StarSystem; + } + + SystemDict.Add(config.StarSystem, new NewHorizonsSystem(config.StarSystem, starSystemConfig, mod.ModHelper)); + + BodyDict.Add(config.StarSystem, new List()); + } body = new NewHorizonsBody(config, mod.ModHelper); } @@ -524,6 +568,7 @@ namespace NewHorizons // Spawning on other planets is a bit hacky so we do it last if (body.Config.Spawn != null) { + Logger.Log("Doing spawn point thing"); SpawnPointBuilder.Make(go, body.Config.Spawn, owRigidBody); } diff --git a/NewHorizons/Utility/NewHorizonsSystem.cs b/NewHorizons/Utility/NewHorizonsSystem.cs new file mode 100644 index 00000000..c55fc4d0 --- /dev/null +++ b/NewHorizons/Utility/NewHorizonsSystem.cs @@ -0,0 +1,26 @@ +using NewHorizons.External; +using OWML.Common; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NewHorizons.Utility +{ + public class NewHorizonsSystem + { + public NewHorizonsSystem(string name, StarSystemConfig config, IModHelper mod) + { + Name = name; + Config = config; + Mod = mod; + } + + public string Name; + public SpawnModule Spawn = null; + public SpawnPoint SpawnPoint = null; + public StarSystemConfig Config; + public IModHelper Mod; + } +}