Skip to content

Commit

Permalink
Add LoadSystemFromJson to StarSystemFactory, entire system can now be…
Browse files Browse the repository at this point in the history
… loaded from json
  • Loading branch information
behindcurtain3 committed Mar 31, 2024
1 parent ee3090f commit 1cb4f51
Show file tree
Hide file tree
Showing 35 changed files with 105 additions and 2 deletions.
13 changes: 13 additions & 0 deletions Pulsar4X/GameEngine/Data/basemod/sol/sol.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "Sol",
"info": {
"mass": 1.98855E30,
"radius": 696342,
"age": 4.6E9,
"class": "G",
"temperature": 5778,
"luminosity": 1,
"luminosityClass": "V",
"spectralType": "G"
}
}
39 changes: 39 additions & 0 deletions Pulsar4X/GameEngine/Data/basemod/sol/systemInfo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "Sol",
"stars": [
"sol.json"
],
"bodies": [
"mercury.json",
"venus.json",
"earth.json",
"mars.json",
"jupiter/jupiter.json",
"saturn/saturn.json",
"uranus.json",
"neptune.json",
"ceres.json",
"eris.json",
"halleyscomet.json",
"haumea.json",
"luna.json",
"makemake.json",
"pluto.json",
"jupiter/01-io.json",
"jupiter/02-europa.json",
"jupiter/03-ganymede.json",
"jupiter/04-callisto.json",
"jupiter/amalthea.json",
"jupiter/elara.json",
"jupiter/himalia.json",
"jupiter/lysithea.json",
"jupiter/pasiphae.json",
"jupiter/sinope.json",
"saturn/dione.json",
"saturn/enceladus.json",
"saturn/mimas.json",
"saturn/rhea.json",
"saturn/tethys.json",
"saturn/titan.json"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static Entity DefaultHumans(Game game, string name)

//var log = StaticRefLib.EventLog;
StarSystemFactory starfac = new StarSystemFactory(game);
StarSystem solSys = starfac.CreateSol(game);
StarSystem solSys = starfac.LoadSystemFromJson(game, "Data/basemod/sol/");
//sol.ManagerSubpulses.Init(sol);
Entity solStar = solSys.GetAllEntitiesWithDataBlob<StarInfoDB>().First();
Entity earth = NameLookup.GetFirstEntityWithName(solSys, "Earth"); //should be fourth entity created
Expand Down
53 changes: 52 additions & 1 deletion Pulsar4X/GameEngine/Engine/Factories/StarSystemFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using Pulsar4X.Engine.Sensors;
using Pulsar4X.Engine.Sol;
using Pulsar4X.Engine.Factories;
using System.IO;
using Newtonsoft.Json.Linq;

namespace Pulsar4X.Engine
{
Expand Down Expand Up @@ -194,7 +196,7 @@ public StarSystem CreateSol(Game game)
// WIP Function...
StarSystem sol = new StarSystem();
sol.Initialize(game, "Sol", -1);
Entity sun = _starFactory.CreateStar(sol, UniversalConstants.Units.SolarMassInKG, UniversalConstants.Units.SolarRadiusInAu, 4.6E9, "G", 5778, 1, SpectralType.G, "Sol");
var sun = StarFromJsonFactory.Create(sol, GalaxyGen.Settings, "Data/basemod/bodies/sol.json");

// Planets and their moons
var mercury = SystemBodyFromJsonFactory.Create(game, sol, sun, GalaxyGen.Settings.J2000, new SensorProfileDB(), "Data/basemod/bodies/mercury.json");
Expand Down Expand Up @@ -304,6 +306,55 @@ public StarSystem CreateSol(Game game)
return sol;
}

public StarSystem LoadSystemFromJson(Game game, string folder)
{
string fileContents = File.ReadAllText(Path.Combine(folder, "systemInfo.json"));
var rootJson = JObject.Parse(fileContents);
var systemName = rootJson["name"].ToString();
var rngSeed = (int?)rootJson["seed"] ?? -1;

StarSystem system = new StarSystem();
system.Initialize(game, systemName, rngSeed);

var stars = (JArray?)rootJson["stars"];
Entity? rootStar = null;
foreach(var starFileName in stars)
{
var star = StarFromJsonFactory.Create(system, GalaxyGen.Settings, Path.Combine(folder, starFileName.ToString()));
if(rootStar == null)
rootStar = star;
}

if(rootStar != null)
{
var bodies = (JArray?)rootJson["bodies"];
foreach(var bodyFileName in bodies)
{
var body = SystemBodyFromJsonFactory.Create(
game,
system,
rootStar,
GalaxyGen.Settings.J2000,
new SensorProfileDB(),
Path.Combine(folder, bodyFileName.ToString()));
_systemBodyFactory.MineralGeneration(game.StartingGameData.Minerals.Values.ToList(), system, body);
}
}

JPSurveyFactory.GenerateJPSurveyPoints(system);

// Go through all the created entities and set them to be neutral
foreach(var entity in system.GetAllEntites())
{
entity.FactionOwnerID = Game.NeutralFactionId;
}

game.GameMasterFaction.GetDataBlob<FactionInfoDB>().KnownSystems.Add(system.Guid);

return system;
}


#region TestSystems

/// <summary>
Expand Down

0 comments on commit 1cb4f51

Please sign in to comment.