Saving Loadouts

Added entity save option to the inventory panel dropdown
This commit is contained in:
Reinier Vens 2021-01-18 02:35:56 +00:00
commit b4432b7aca
18 changed files with 192 additions and 103 deletions

View file

@ -10,25 +10,48 @@ using Unity.Mathematics;
public class EntityPack
{
[Key(0)]
public EquippableItem Hull;
public string Name;
[Key(1)]
public EquippableItem Hull;
[Key(2)]
public (int2 position, EquippableItem item)[] Equipment;
[Key(3)]
public (int2 position, EquippableItem item)[] CargoBays;
[Key(4)]
public (int2 position, EquippableItem item)[] DockingBays;
[Key(5)]
public Dictionary<int2, PersistentBehaviorData[]> PersistedBehaviors;
[Key(6)]
public float[,] Temperature;
[Key(7)]
public float[,] Armor;
[Key(8)]
public bool2[,] Conductivity;
[Key(9)]
public Dictionary<int, float> HardpointArmor;
[Key(10)]
public int[] DockingBayAssignments;
[Key(11)]
public (int2 position, ItemInstance item)[][] CargoContents;
[Key(12)]
public (int2 position, ItemInstance item)[][] DockingBayContents;
[Key(13)]
public EntityPack[] Children;
public static EntityPack Serialize(Entity entity)
public static EntityPack Pack(Entity entity)
{
var pack = new EntityPack();
@ -41,7 +64,9 @@ public class EntityPack
.Where(b=>b is IPersistentBehavior)
.Cast<IPersistentBehavior>()})
.ToDictionary(x=> x.equippable.Position, x=>x.behaviors.Select(b => b.Store()).ToArray());
pack.Hull = entity.Hull;
pack.Name = entity.Name;
pack.Equipment = entity.Equipment.Select(e => (e.Position, e.EquippableItem)).ToArray();
pack.CargoBays = entity.CargoBays.Select(e => (e.Position, e.EquippableItem)).ToArray();
pack.DockingBays = entity.DockingBays.Select(e => (e.Position, e.EquippableItem)).ToArray();
@ -50,20 +75,21 @@ public class EntityPack
pack.DockingBayContents = entity.DockingBays.Select(b => b.Cargo.Select(i => (i.Value, i.Key)).ToArray()).ToArray();
pack.Armor = entity.Armor;
pack.Temperature = entity.Temperature;
pack.Conductivity = entity.HullConductivity;
var hullData = entity.ItemManager.GetData(entity.Hull) as HullData;
pack.HardpointArmor = entity.HardpointArmor.ToDictionary(x => hullData.Hardpoints.IndexOf(x.Key), x => x.Value);
pack.Children = entity.Children.Select(Serialize).ToArray();
pack.Children = entity.Children.Select(Pack).ToArray();
return pack;
}
public static Ship Deserialize(ItemManager itemManager, Zone zone, EntityPack pack)
public static Ship Unpack(ItemManager itemManager, Zone zone, EntityPack pack)
{
var entity = new Ship(itemManager, zone, pack.Hull);
Restore(itemManager, zone, pack, entity);
return entity;
}
public static OrbitalEntity Deserialize(ItemManager itemManager, Zone zone, EntityPack pack, Guid orbit)
public static OrbitalEntity Unpack(ItemManager itemManager, Zone zone, EntityPack pack, Guid orbit)
{
var entity = new OrbitalEntity(itemManager, zone, pack.Hull, orbit);
Restore(itemManager, zone, pack, entity);
@ -72,9 +98,10 @@ public class EntityPack
public static void Restore(ItemManager itemManager, Zone zone, EntityPack pack, Entity entity)
{
entity.Name = pack.Name;
entity.Children = pack.Children.Select(c =>
{
var child = Deserialize(itemManager, zone, c);
var child = Unpack(itemManager, zone, c);
child.Parent = entity;
return (Entity) child;
}).ToList();
@ -109,6 +136,7 @@ public class EntityPack
entity.Temperature = pack.Temperature;
entity.Armor = pack.Armor;
var hullData = entity.ItemManager.GetData(entity.Hull) as HullData;
entity.HullConductivity = pack.Conductivity;
entity.HardpointArmor = pack.HardpointArmor.ToDictionary(x => hullData.Hardpoints[x.Key], x => x.Value);
}
}

View file

@ -87,6 +87,47 @@ public class Int2Formatter : MessagePack.Formatters.IMessagePackFormatter<int2>
}
}
public class Bool2Formatter : MessagePack.Formatters.IMessagePackFormatter<bool2>
{
public void Serialize(ref MessagePackWriter writer, bool2 value, MessagePackSerializerOptions options)
{
writer.WriteArrayHeader(2);
writer.Write(value.x);
writer.Write(value.y);
}
public bool2 Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
if (reader.IsNil)
{
throw new InvalidOperationException("typecode is null, struct not supported");
}
var length = reader.ReadArrayHeader();
var x = default(bool);
var y = default(bool);
for (int i = 0; i < length; i++)
{
var key = i;
switch (key)
{
case 0:
x = reader.ReadBoolean();
break;
case 1:
y = reader.ReadBoolean();
break;
default:
reader.Skip();
break;
}
}
var result = bool2(x, y);
return result;
}
}
public class Float3Formatter : MessagePack.Formatters.IMessagePackFormatter<float3>
{
public void Serialize(ref MessagePackWriter writer, float3 value, MessagePackSerializerOptions options)

View file

@ -38,6 +38,7 @@ internal static class MathResolverResolverGetFormatterHelper
// standard
{ typeof(float2), new Float2Formatter() },
{ typeof(int2), new Int2Formatter() },
{ typeof(bool2), new Bool2Formatter() },
{ typeof(float3), new Float3Formatter() },
{ typeof(float4), new Float4Formatter() },
{ typeof(float2?), new StaticNullableFormatter<float2>(new Float2Formatter()) },