Skip to content

Commit

Permalink
Add json loader for Component Designs
Browse files Browse the repository at this point in the history
Migrate the conventional rocket engine designs to use the new loader
  • Loading branch information
behindcurtain3 committed Apr 1, 2024
1 parent 1cb4f51 commit 1cba9dd
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 43 deletions.
7 changes: 7 additions & 0 deletions Pulsar4X/GameEngine/Data/basemod/componentDesigns/f1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "F1",
"templateId": "conventional-engine",
"attributes": [
{ "key": "Size", "value": 50 }
]
}
7 changes: 7 additions & 0 deletions Pulsar4X/GameEngine/Data/basemod/componentDesigns/merlin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "Merlin",
"templateId": "conventional-engine",
"attributes": [
{ "key": "Size", "value": 30 }
]
}
8 changes: 8 additions & 0 deletions Pulsar4X/GameEngine/Data/basemod/componentDesigns/raptor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "Raptor-Vac",
"templateId": "conventional-engine",
"attributes": [
{ "key": "Size", "value": 15 },
{ "key": "Fuel Type", "value": "methalox" }
]
}
8 changes: 8 additions & 0 deletions Pulsar4X/GameEngine/Data/basemod/componentDesigns/rs-25.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "RS-25",
"templateId": "conventional-engine",
"attributes": [
{ "key": "Size", "value": 100 },
{ "key": "Fuel Type", "value": "hydrolox" }
]
}
49 changes: 49 additions & 0 deletions Pulsar4X/GameEngine/Engine/Factories/ComponentDesignFromJson.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.IO;
using Newtonsoft.Json.Linq;
using Pulsar4X.Blueprints;
using Pulsar4X.Components;
using Pulsar4X.Datablobs;

namespace Pulsar4X.Engine.Factories;

public static class ComponentDesignFromJson
{
public static ComponentDesign Create(Game game, Entity faction, FactionDataStore factionDataStore, string filePath)
{
string fileContents = File.ReadAllText(filePath);
var rootJson = JObject.Parse(fileContents);

var templateName = rootJson["templateId"].ToString();
var designName = rootJson["name"].ToString();

ComponentDesign design;
var blueprint = factionDataStore.ComponentTemplates[templateName];
var designer = new ComponentDesigner(blueprint, factionDataStore, faction.GetDataBlob<FactionTechDB>()){
Name = designName
};

var attributes = (JArray?)rootJson["attributes"];
foreach(var attribute in attributes)
{
var key = attribute["key"].ToString();
var valueType = attribute["value"];
if(valueType.Type == JTokenType.Integer)
{
designer.ComponentDesignAttributes[key].SetValueFromInput((int?)attribute["value"] ?? 0);
}
else if(valueType.Type == JTokenType.Float)
{
designer.ComponentDesignAttributes[key].SetValueFromInput((double?)attribute["value"] ?? 0.0);
}
else
{
designer.ComponentDesignAttributes[key].SetValueFromString(attribute["value"].ToString());
}
}

design = designer.CreateDesign(faction);
factionDataStore.IncrementTechLevel(design.TechID);

return design;
}
}
48 changes: 5 additions & 43 deletions Pulsar4X/GameEngine/Engine/Factories/DefaultStartFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Pulsar4X.Engine.Orders;
using Pulsar4X.Atb;
using Pulsar4X.Extensions;
using Pulsar4X.Engine.Factories;

namespace Pulsar4X.Engine
{
Expand Down Expand Up @@ -661,17 +662,8 @@ public static ComponentDesign DefaultThrusterDesign(Game game, Entity faction, F
if (_merlin != null)
return _merlin;

ComponentDesigner engineDesigner;

ComponentTemplateBlueprint engineSD = factionDataStore.ComponentTemplates["conventional-engine"];
engineDesigner = new ComponentDesigner(engineSD, factionDataStore, faction.GetDataBlob<FactionTechDB>());
engineDesigner.ComponentDesignAttributes["Size"].SetValueFromInput(30);
engineDesigner.Name = "Merlin";
//engineDesignDB.ComponentDesignAbilities[1].SetValueFromInput

_merlin = engineDesigner.CreateDesign(faction);
_merlin = ComponentDesignFromJson.Create(game, faction, factionDataStore, "Data/basemod/componentDesigns/merlin.json");

factionDataStore.IncrementTechLevel(_merlin.TechID);
return _merlin;
}

Expand All @@ -680,17 +672,7 @@ public static ComponentDesign F1ThrusterDesign(Game game, Entity faction, Factio
if (_f1 != null)
return _f1;

ComponentDesigner engineDesigner;

ComponentTemplateBlueprint engineSD = factionDataStore.ComponentTemplates["conventional-engine"];
engineDesigner = new ComponentDesigner(engineSD, factionDataStore, faction.GetDataBlob<FactionTechDB>());
engineDesigner.ComponentDesignAttributes["Size"].SetValueFromInput(50);
engineDesigner.Name = "F1";
//engineDesignDB.ComponentDesignAbilities[1].SetValueFromInput

_f1 = engineDesigner.CreateDesign(faction);

factionDataStore.IncrementTechLevel(_f1.TechID);
_f1 = ComponentDesignFromJson.Create(game, faction, factionDataStore, "Data/basemod/componentDesigns/f1.json");
return _f1;
}

Expand All @@ -705,36 +687,16 @@ public static ComponentDesign RaptorThrusterDesign(Game game, Entity faction, Fa
if (_raptor != null)
return _raptor;

ComponentDesigner engineDesigner;

ComponentTemplateBlueprint engineSD = factionDataStore.ComponentTemplates["conventional-engine"];
engineDesigner = new ComponentDesigner(engineSD, factionDataStore, faction.GetDataBlob<FactionTechDB>());
engineDesigner.ComponentDesignAttributes["Size"].SetValueFromInput(15);
engineDesigner.ComponentDesignAttributes["Fuel Type"].SetValueFromString("methalox");
engineDesigner.Name = "Raptor-Vac";
//engineDesignDB.ComponentDesignAbilities[1].SetValueFromInput

_raptor = engineDesigner.CreateDesign(faction);
_raptor = ComponentDesignFromJson.Create(game, faction, factionDataStore, "Data/basemod/componentDesigns/raptor.json");

factionDataStore.IncrementTechLevel(_raptor.TechID);
return _raptor;
}

public static ComponentDesign RS25ThrusterDesign(Game game, Entity faction, FactionDataStore factionDataStore)
{
if (_rs25 != null)
return _rs25;
ComponentDesigner engineDesigner;
ComponentTemplateBlueprint engineSD = factionDataStore.ComponentTemplates["conventional-engine"];
engineDesigner = new ComponentDesigner(engineSD, factionDataStore, faction.GetDataBlob<FactionTechDB>());
engineDesigner.ComponentDesignAttributes["Size"].SetValueFromInput(100);
engineDesigner.ComponentDesignAttributes["Fuel Type"].SetValueFromString("hydrolox");
engineDesigner.Name = "RS-25";
//engineDesignDB.ComponentDesignAbilities[1].SetValueFromInput

_rs25 = engineDesigner.CreateDesign(faction);

factionDataStore.IncrementTechLevel(_rs25.TechID);
_rs25 = ComponentDesignFromJson.Create(game, faction, factionDataStore, "Data/basemod/componentDesigns/rs-25.json");
return _rs25;
}

Expand Down

0 comments on commit 1cba9dd

Please sign in to comment.