Saving Loadouts
Added entity save option to the inventory panel dropdown
This commit is contained in:
parent
8d6770ab15
commit
b4432b7aca
18 changed files with 192 additions and 103 deletions
|
|
@ -45,17 +45,20 @@ public class ActionGameManager : MonoBehaviour
|
|||
|
||||
// private CinemachineFramingTransposer _transposer;
|
||||
// private CinemachineComposer _composer;
|
||||
|
||||
private DirectoryInfo _filePath;
|
||||
private DirectoryInfo _presetPath;
|
||||
private bool _editMode;
|
||||
private float _time;
|
||||
private AetheriaInput _input;
|
||||
private int _zoomLevelIndex;
|
||||
public List<EntityPack> Presets { get; set; } = new List<EntityPack>();
|
||||
|
||||
// private ShipInput _shipInput;
|
||||
private float2 _shipYawPitch;
|
||||
private float3 _viewDirection;
|
||||
private (HardpointData[] hardpoints, Transform[] barrels, PlaceUIElementWorldspace crosshair)[] ArticulationGroups;
|
||||
private (TargetLock targetLock, PlaceUIElementWorldspace indicator, Rotate spin)[] LockingIndicators;
|
||||
private (HardpointData[] hardpoints, Transform[] barrels, PlaceUIElementWorldspace crosshair)[] _articulationGroups;
|
||||
private (TargetLock targetLock, PlaceUIElementWorldspace indicator, Rotate spin)[] _lockingIndicators;
|
||||
public List<Ship> PlayerShips { get; } = new List<Ship>();
|
||||
public EquippedDockingBay DockingBay { get; private set; }
|
||||
|
||||
|
|
@ -71,6 +74,11 @@ public class ActionGameManager : MonoBehaviour
|
|||
(float2(0, -1), "Rear")
|
||||
};
|
||||
|
||||
public void SavePreset(EntityPack pack)
|
||||
{
|
||||
File.WriteAllBytes(Path.Combine(_presetPath.FullName, $"{pack.Name}.preset"), MessagePackSerializer.Serialize(pack));
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
// _transposer = DockCamera.GetCinemachineComponent<CinemachineFramingTransposer>();
|
||||
|
|
@ -79,10 +87,14 @@ public class ActionGameManager : MonoBehaviour
|
|||
|
||||
ConsoleController.MessageReceiver = this;
|
||||
_filePath = new DirectoryInfo(Application.dataPath).Parent.CreateSubdirectory("GameData");
|
||||
_presetPath = _filePath.CreateSubdirectory("Presets");
|
||||
ItemData = new DatabaseCache();
|
||||
ItemData.Load(_filePath.FullName);
|
||||
ItemManager = new ItemManager(ItemData, Settings.GameplaySettings, Debug.Log);
|
||||
|
||||
Presets.AddRange(_presetPath.EnumerateFiles("*.preset")
|
||||
.Select(fi => MessagePackSerializer.Deserialize<EntityPack>(File.ReadAllBytes(fi.FullName))));
|
||||
|
||||
var zoneFile = Path.Combine(_filePath.FullName, "Home.zone");
|
||||
|
||||
// If the game has already been run, there will be a Home file containing a ZonePack; if not, generate one
|
||||
|
|
@ -535,7 +547,7 @@ public class ActionGameManager : MonoBehaviour
|
|||
Cursor.lockState = CursorLockMode.None;
|
||||
_input.Player.Disable();
|
||||
GameplayUI.SetActive(false);
|
||||
if(LockingIndicators!=null) foreach(var (_, indicator, _) in LockingIndicators)
|
||||
if(_lockingIndicators!=null) foreach(var (_, indicator, _) in _lockingIndicators)
|
||||
indicator.GetComponent<Prototype>().ReturnToPool();
|
||||
}
|
||||
}
|
||||
|
|
@ -573,7 +585,7 @@ public class ActionGameManager : MonoBehaviour
|
|||
FollowCamera.enabled = true;
|
||||
FollowCamera.LookAt = SectorRenderer.EntityInstances[CurrentShip].LookAtPoint;
|
||||
FollowCamera.Follow = SectorRenderer.EntityInstances[CurrentShip].Transform;
|
||||
ArticulationGroups = CurrentShip.Equipment
|
||||
_articulationGroups = CurrentShip.Equipment
|
||||
.Where(item => item.Behaviors.Any(x => x.Data is WeaponData && !(x.Data is LauncherData)))
|
||||
.GroupBy(item => SectorRenderer.EntityInstances[CurrentShip]
|
||||
.GetBarrel(CurrentShip.Hardpoints[item.Position.x, item.Position.y])
|
||||
|
|
@ -588,10 +600,10 @@ public class ActionGameManager : MonoBehaviour
|
|||
|
||||
foreach (var crosshair in Crosshairs)
|
||||
crosshair.gameObject.SetActive(false);
|
||||
foreach (var group in ArticulationGroups)
|
||||
foreach (var group in _articulationGroups)
|
||||
group.crosshair.gameObject.SetActive(true);
|
||||
|
||||
LockingIndicators = CurrentShip.GetBehaviors<TargetLock>().Select(x =>
|
||||
_lockingIndicators = CurrentShip.GetBehaviors<TargetLock>().Select(x =>
|
||||
{
|
||||
var i = LockIndicator.Instantiate<PlaceUIElementWorldspace>();
|
||||
return (x, i, i.GetComponent<Rotate>());
|
||||
|
|
@ -677,7 +689,7 @@ public class ActionGameManager : MonoBehaviour
|
|||
if (CurrentShip.Target.Value != null)
|
||||
TargetIndicator.Target = CurrentShip.Target.Value.Position;
|
||||
var distance = length((float3)ViewDot.Target - CurrentShip.Position);
|
||||
foreach (var (_, barrels, crosshair) in ArticulationGroups)
|
||||
foreach (var (_, barrels, crosshair) in _articulationGroups)
|
||||
{
|
||||
var averagePosition = Vector3.zero;
|
||||
foreach (var barrel in barrels)
|
||||
|
|
@ -685,7 +697,7 @@ public class ActionGameManager : MonoBehaviour
|
|||
averagePosition /= barrels.Length;
|
||||
crosshair.Target = averagePosition;
|
||||
}
|
||||
foreach (var (targetLock, indicator, spin) in LockingIndicators)
|
||||
foreach (var (targetLock, indicator, spin) in _lockingIndicators)
|
||||
{
|
||||
var showLockingIndicator = targetLock.Lock > .01f && CurrentShip.Target.Value != null;
|
||||
indicator.gameObject.SetActive(showLockingIndicator);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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()) },
|
||||
|
|
|
|||
|
|
@ -124,6 +124,12 @@ public class InventoryPanel : MonoBehaviour
|
|||
|
||||
if(GameManager.DockingBay!=null && _displayedCargo!=GameManager.DockingBay)
|
||||
ContextMenu.AddOption(GameManager.DockingBay.Name, () => Display(GameManager.DockingBay));
|
||||
|
||||
ContextMenu.AddOption("Create Preset",
|
||||
() =>
|
||||
{
|
||||
GameManager.SavePreset(EntityPack.Pack(_displayedEntity));
|
||||
});
|
||||
|
||||
ContextMenu.Show();
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue