Skip to content

Commit

Permalink
Add ShipDesignFromJson loader
Browse files Browse the repository at this point in the history
Migrate default ship design to use json
  • Loading branch information
behindcurtain3 committed Apr 1, 2024
1 parent 73c250e commit 3c1206b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 18 deletions.
20 changes: 20 additions & 0 deletions Pulsar4X/GameEngine/Data/basemod/shipDesigns/obennDropship.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "Ob'enn Dropship",
"armor": {
"id": "plastic-armor",
"thickness": 3
},
"components": [
{ "id": "default-design-passive-sensor-s50", "amount": 1 },
{ "id": "default-design-laser-weapon", "amount": 2 },
{ "id": "default-design-beam-fire-control", "amount": 1 },
{ "id": "default-design-cargo-hold-1t", "amount": 1 },
{ "id": "default-design-geo-surveyor", "amount": 1 },
{ "id": "default-design-gravitational-surveyor", "amount": 1 },
{ "id": "default-design-fuel-tank-1000", "amount": 2 },
{ "id": "default-design-alcubierre-500", "amount": 4 },
{ "id": "default-design-battery-2t", "amount": 3 },
{ "id": "default-design-reactor-15k", "amount": 1 },
{ "id": "default-design-merlin", "amount": 3 }
]
}
22 changes: 4 additions & 18 deletions Pulsar4X/GameEngine/Engine/Factories/DefaultStartFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ public static Entity DefaultHumans(Game game, string name)
DefaultMissileTube(factionEntity, factionDataStore);
MissileDesign250(factionEntity);
ShipSmallOrdnanceStore(factionEntity, factionDataStore);
DefaultGeoSurveyor(factionEntity, factionDataStore);
DefaultJPSurveyor(factionEntity, factionDataStore);
DefaultBatteryBank(factionEntity, factionDataStore);

colonyEntity.AddComponent(mineDesign);
colonyEntity.AddComponent(refinaryDesign);
Expand Down Expand Up @@ -337,25 +340,8 @@ public static ShipDesign DefaultShipDesign(Entity faction, FactionDataStore fact
{
if (_defaultShipDesign != null)
return _defaultShipDesign;
var factionInfo = faction.GetDataBlob<FactionInfoDB>();
List<(ComponentDesign, int)> components2 = new List<(ComponentDesign, int)>()
{
(ShipPassiveSensor(faction, factionDataStore), 1),
(DefaultSimpleLaser(faction, factionDataStore), 2),
(DefaultBFC(faction, factionDataStore), 1),
(ShipSmallCargo(faction, factionDataStore), 1),
(DefaultGeoSurveyor(faction, factionDataStore), 1),
(DefaultJPSurveyor(faction, factionDataStore), 1),
(DefaultFuelTank(faction, factionDataStore), 2),
(DefaultWarpDesign(faction, factionDataStore), 4),
(DefaultBatteryBank(faction, factionDataStore), 3),
(DefaultFisionReactor(faction, factionDataStore), 1),
(DefaultThrusterDesign(faction, factionDataStore), 3),

};
ArmorBlueprint plastic = factionDataStore.Armor["plastic-armor"]; //factionDataStore.ArmorTypes[new Guid("207af637-95a0-4b89-ac4a-6d66a81cfb2f")];
_defaultShipDesign = new ShipDesign(factionInfo, "Ob'enn Dropship", components2, (plastic, 3));
_defaultShipDesign.DamageProfileDB = new EntityDamageProfileDB(components2, _defaultShipDesign.Armor);
_defaultShipDesign = ShipDesignFromJson.Create(faction, factionDataStore, "Data/basemod/shipDesigns/obennDropship.json");
return _defaultShipDesign;
}

Expand Down
49 changes: 49 additions & 0 deletions Pulsar4X/GameEngine/Engine/Factories/ShipDesignFromJson.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json.Linq;
using Pulsar4X.Blueprints;
using Pulsar4X.Components;
using Pulsar4X.Datablobs;
using Pulsar4X.Engine.Designs;

namespace Pulsar4X.Engine.Factories;

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

var factionInfoDB = faction.GetDataBlob<FactionInfoDB>();
var shipComponents = new List<(ComponentDesign, int)>();

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

var components = (JArray?)rootJson["components"];
if(components != null)
{
foreach(var component in components)
{
var id = component["id"].ToString();
var amount = (int?)component["amount"] ?? 0;

shipComponents.Add((
factionInfoDB.InternalComponentDesigns[id],
amount
));
}
}

var armorId = rootJson["armor"]["id"].ToString();
var armorThickness = (int?)rootJson["armor"]["thickness"] ?? 1;

var armor = factionDataStore.Armor[armorId];
var design = new ShipDesign(factionInfoDB, designName, shipComponents, (armor, armorThickness))
{
DamageProfileDB = new EntityDamageProfileDB(shipComponents, (armor, armorThickness))
};

return design;
}
}

0 comments on commit 3c1206b

Please sign in to comment.