Cash Money
Player credits now tracked by game manager Buying items subtracts credits Right clicking on trade menu items allows player to buy multiple Inventory panel dropdown now allows instantiating saved loadouts
This commit is contained in:
parent
b4432b7aca
commit
48abee4847
9 changed files with 239 additions and 51 deletions
|
|
@ -40,6 +40,7 @@ public class ActionGameManager : MonoBehaviour
|
|||
public InventoryPanel TargetShipPanel;
|
||||
public ConfirmationDialog ConfirmationDialog;
|
||||
public float2 Sensitivity;
|
||||
public int Credits = 15000000;
|
||||
|
||||
//public PlayerInput Input;
|
||||
|
||||
|
|
@ -52,7 +53,7 @@ public class ActionGameManager : MonoBehaviour
|
|||
private float _time;
|
||||
private AetheriaInput _input;
|
||||
private int _zoomLevelIndex;
|
||||
public List<EntityPack> Presets { get; set; } = new List<EntityPack>();
|
||||
public List<EntityPack> Loadouts { get; set; } = new List<EntityPack>();
|
||||
|
||||
// private ShipInput _shipInput;
|
||||
private float2 _shipYawPitch;
|
||||
|
|
@ -74,7 +75,7 @@ public class ActionGameManager : MonoBehaviour
|
|||
(float2(0, -1), "Rear")
|
||||
};
|
||||
|
||||
public void SavePreset(EntityPack pack)
|
||||
public void SaveLoadout(EntityPack pack)
|
||||
{
|
||||
File.WriteAllBytes(Path.Combine(_presetPath.FullName, $"{pack.Name}.preset"), MessagePackSerializer.Serialize(pack));
|
||||
}
|
||||
|
|
@ -92,7 +93,7 @@ public class ActionGameManager : MonoBehaviour
|
|||
ItemData.Load(_filePath.FullName);
|
||||
ItemManager = new ItemManager(ItemData, Settings.GameplaySettings, Debug.Log);
|
||||
|
||||
Presets.AddRange(_presetPath.EnumerateFiles("*.preset")
|
||||
Loadouts.AddRange(_presetPath.EnumerateFiles("*.preset")
|
||||
.Select(fi => MessagePackSerializer.Deserialize<EntityPack>(File.ReadAllBytes(fi.FullName))));
|
||||
|
||||
var zoneFile = Path.Combine(_filePath.FullName, "Home.zone");
|
||||
|
|
|
|||
|
|
@ -51,6 +51,8 @@ public class EntityPack
|
|||
[Key(13)]
|
||||
public EntityPack[] Children;
|
||||
|
||||
private int _price;
|
||||
|
||||
public static EntityPack Pack(Entity entity)
|
||||
{
|
||||
var pack = new EntityPack();
|
||||
|
|
@ -82,32 +84,32 @@ public class EntityPack
|
|||
return pack;
|
||||
}
|
||||
|
||||
public static Ship Unpack(ItemManager itemManager, Zone zone, EntityPack pack)
|
||||
public static Ship Unpack(ItemManager itemManager, Zone zone, EntityPack pack, bool instantiate = false)
|
||||
{
|
||||
var entity = new Ship(itemManager, zone, pack.Hull);
|
||||
Restore(itemManager, zone, pack, entity);
|
||||
var entity = new Ship(itemManager, zone, instantiate ? (EquippableItem) itemManager.Instantiate(pack.Hull) : pack.Hull);
|
||||
Restore(itemManager, zone, pack, entity, instantiate);
|
||||
return entity;
|
||||
}
|
||||
|
||||
public static OrbitalEntity Unpack(ItemManager itemManager, Zone zone, EntityPack pack, Guid orbit)
|
||||
public static OrbitalEntity Unpack(ItemManager itemManager, Zone zone, EntityPack pack, Guid orbit, bool instantiate = false)
|
||||
{
|
||||
var entity = new OrbitalEntity(itemManager, zone, pack.Hull, orbit);
|
||||
Restore(itemManager, zone, pack, entity);
|
||||
var entity = new OrbitalEntity(itemManager, zone, instantiate ? (EquippableItem) itemManager.Instantiate(pack.Hull) : pack.Hull, orbit);
|
||||
Restore(itemManager, zone, pack, entity, instantiate);
|
||||
return entity;
|
||||
}
|
||||
|
||||
public static void Restore(ItemManager itemManager, Zone zone, EntityPack pack, Entity entity)
|
||||
public static void Restore(ItemManager itemManager, Zone zone, EntityPack pack, Entity entity, bool instantiate = false)
|
||||
{
|
||||
entity.Name = pack.Name;
|
||||
entity.Children = pack.Children.Select(c =>
|
||||
{
|
||||
var child = Unpack(itemManager, zone, c);
|
||||
var child = Unpack(itemManager, zone, c, instantiate);
|
||||
child.Parent = entity;
|
||||
return (Entity) child;
|
||||
}).ToList();
|
||||
foreach (var (position, item) in pack.Equipment) entity.TryEquip(item, position);
|
||||
foreach (var (position, item) in pack.CargoBays) entity.TryEquip(item, position);
|
||||
foreach (var (position, item) in pack.DockingBays) entity.TryEquip(item, position);
|
||||
foreach (var (position, item) in pack.Equipment) entity.TryEquip(instantiate ? (EquippableItem) itemManager.Instantiate(item) : item, position);
|
||||
foreach (var (position, item) in pack.CargoBays) entity.TryEquip(instantiate ? (EquippableItem) itemManager.Instantiate(item) : item, position);
|
||||
foreach (var (position, item) in pack.DockingBays) entity.TryEquip(instantiate ? (EquippableItem) itemManager.Instantiate(item) : item, position);
|
||||
|
||||
for (var i = 0; i < pack.DockingBayAssignments.Length; i++)
|
||||
{
|
||||
|
|
@ -117,11 +119,11 @@ public class EntityPack
|
|||
|
||||
for (var bayIndex = 0; bayIndex < pack.CargoContents.Length; bayIndex++)
|
||||
foreach (var (position, item) in pack.CargoContents[bayIndex])
|
||||
entity.CargoBays[bayIndex].TryStore(item, position);
|
||||
entity.CargoBays[bayIndex].TryStore(instantiate ? itemManager.Instantiate(item) : item, position);
|
||||
|
||||
for (var bayIndex = 0; bayIndex < pack.DockingBayContents.Length; bayIndex++)
|
||||
foreach (var (position, item) in pack.DockingBayContents[bayIndex])
|
||||
entity.DockingBays[bayIndex].TryStore(item, position);
|
||||
entity.DockingBays[bayIndex].TryStore(instantiate ? itemManager.Instantiate(item) : item, position);
|
||||
|
||||
// Iterate only over the behaviors of items which contain persistent data
|
||||
// Filter the behaviors for each item to get the persistent ones, then cast them and combine with the persisted data array for that item
|
||||
|
|
@ -139,4 +141,52 @@ public class EntityPack
|
|||
entity.HullConductivity = pack.Conductivity;
|
||||
entity.HardpointArmor = pack.HardpointArmor.ToDictionary(x => hullData.Hardpoints[x.Key], x => x.Value);
|
||||
}
|
||||
|
||||
public int Price(ItemManager itemManager)
|
||||
{
|
||||
if (_price == 0)
|
||||
{
|
||||
var hullData = itemManager.GetData(Hull);
|
||||
_price = hullData.Price;
|
||||
|
||||
foreach (var (_, item) in Equipment)
|
||||
{
|
||||
var itemData = itemManager.GetData(item);
|
||||
_price += itemData.Price;
|
||||
}
|
||||
foreach (var (_, item) in CargoBays)
|
||||
{
|
||||
var itemData = itemManager.GetData(item);
|
||||
_price += itemData.Price;
|
||||
}
|
||||
foreach (var (_, item) in DockingBays)
|
||||
{
|
||||
var itemData = itemManager.GetData(item);
|
||||
_price += itemData.Price;
|
||||
}
|
||||
|
||||
for (var bayIndex = 0; bayIndex < CargoContents.Length; bayIndex++)
|
||||
foreach (var (_, item) in CargoContents[bayIndex])
|
||||
{
|
||||
var itemData = itemManager.GetData(item);
|
||||
if (item is SimpleCommodity s)
|
||||
_price += itemData.Price * s.Quantity;
|
||||
else
|
||||
_price += itemData.Price;
|
||||
}
|
||||
|
||||
for (var bayIndex = 0; bayIndex < DockingBayContents.Length; bayIndex++)
|
||||
foreach (var (_, item) in DockingBayContents[bayIndex])
|
||||
{
|
||||
var itemData = itemManager.GetData(item);
|
||||
if (item is SimpleCommodity s)
|
||||
_price += itemData.Price * s.Quantity;
|
||||
else
|
||||
_price += itemData.Price;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return _price;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -447,6 +447,16 @@ public class ItemManager
|
|||
_logger("Attempted to create Simple Commodity instance using missing or incorrect item id");
|
||||
return null;
|
||||
}
|
||||
|
||||
public ItemInstance Instantiate(ItemInstance item)
|
||||
{
|
||||
var data = GetData(item);
|
||||
if(data is CraftedItemData c)
|
||||
return CreateInstance(c, 0, 1);
|
||||
if (item is SimpleCommodity s)
|
||||
return CreateInstance(data as SimpleCommodityData, s.Quantity);
|
||||
return null;
|
||||
}
|
||||
|
||||
public CraftedItemInstance CreateInstance(CraftedItemData item, float qualityMin, float qualityMax)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -7,11 +7,14 @@ using System.Runtime.Remoting.Contexts;
|
|||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
|
||||
public class TradeMenu : MonoBehaviour
|
||||
{
|
||||
public ActionGameManager GameManager;
|
||||
public ContextMenu ContextMenu;
|
||||
public ConfirmationDialog Dialog;
|
||||
public Button NewFilterButton;
|
||||
public Prototype FilterPrototype;
|
||||
public SizeFilter MinimumSizeFilter;
|
||||
|
|
@ -178,24 +181,18 @@ public class TradeMenu : MonoBehaviour
|
|||
columns.Add(("Type", 2,
|
||||
data => () =>
|
||||
{
|
||||
if (data is SimpleCommodityData s)
|
||||
return Enum.GetName(typeof(SimpleCommodityCategory), s.Category);
|
||||
if(data is CompoundCommodityData c)
|
||||
return Enum.GetName(typeof(CompoundCommodityCategory), c.Category);
|
||||
if(data is EquippableItemData e)
|
||||
return Enum.GetName(typeof(HardpointType), e.HardpointType);
|
||||
if (data is SimpleCommodityData s) return Enum.GetName(typeof(SimpleCommodityCategory), s.Category);
|
||||
if(data is CompoundCommodityData c) return Enum.GetName(typeof(CompoundCommodityCategory), c.Category);
|
||||
if(data is EquippableItemData e) return Enum.GetName(typeof(HardpointType), e.HardpointType);
|
||||
return "None";
|
||||
},
|
||||
data =>
|
||||
{
|
||||
if (data is SimpleCommodityData s)
|
||||
return (int) s.Category;
|
||||
if (data is SimpleCommodityData s) return (int) s.Category;
|
||||
var offset = Enum.GetValues(typeof(SimpleCommodityCategory)).Length;
|
||||
if(data is CompoundCommodityData c)
|
||||
return (int) c.Category + offset;
|
||||
if(data is CompoundCommodityData c) return (int) c.Category + offset;
|
||||
offset += Enum.GetValues(typeof(CompoundCommodityCategory)).Length;
|
||||
if(data is EquippableItemData e)
|
||||
return (int) e.HardpointType + offset;
|
||||
if(data is EquippableItemData e) return (int) e.HardpointType + offset;
|
||||
return 0;
|
||||
}));
|
||||
columns.Add(("Mass", 1,
|
||||
|
|
@ -306,21 +303,134 @@ public class TradeMenu : MonoBehaviour
|
|||
},
|
||||
OnDoubleClick = () =>
|
||||
{
|
||||
if (i is HullData h)
|
||||
switch (i)
|
||||
{
|
||||
var ship = new Ship(GameManager.ItemManager, GameManager.Zone, GameManager.ItemManager.CreateInstance(h, .1f, 1) as EquippableItem);
|
||||
ship.SetParent(GameManager.CurrentShip.Parent);
|
||||
GameManager.PlayerShips.Add(ship);
|
||||
case HullData h:
|
||||
Buy(h);
|
||||
break;
|
||||
case CraftedItemData c:
|
||||
Buy(c,1);
|
||||
break;
|
||||
case SimpleCommodityData s:
|
||||
Buy(s, 1);
|
||||
break;
|
||||
}
|
||||
else if(i is CraftedItemData c)
|
||||
_targetCargo.TryStore(GameManager.ItemManager.CreateInstance(c, .1f, 1));
|
||||
else if(i is SimpleCommodityData s)
|
||||
_targetCargo.TryStore(GameManager.ItemManager.CreateInstance(s, 1));
|
||||
|
||||
Populate();
|
||||
},
|
||||
OnRightClick = () =>
|
||||
{
|
||||
if (!(i is HullData))
|
||||
{
|
||||
ContextMenu.Clear();
|
||||
ContextMenu.AddOption("Buy Quantity",
|
||||
() =>
|
||||
{
|
||||
int quantity = 1;
|
||||
Dialog.Clear();
|
||||
Dialog.Title.text = $"Buying {i.Name}";
|
||||
Dialog.AddField("Quantity", () => quantity, q => quantity = min(q, GameManager.Credits/i.Price));
|
||||
Dialog.Show(() =>
|
||||
{
|
||||
switch (i)
|
||||
{
|
||||
case CraftedItemData c:
|
||||
Buy(c,quantity);
|
||||
break;
|
||||
case SimpleCommodityData s:
|
||||
Buy(s, quantity);
|
||||
break;
|
||||
}
|
||||
|
||||
Populate();
|
||||
});
|
||||
});
|
||||
ContextMenu.Show();
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
private void Buy(HullData data)
|
||||
{
|
||||
if (data.Price < GameManager.Credits)
|
||||
{
|
||||
var ship = new Ship(GameManager.ItemManager, GameManager.Zone, GameManager.ItemManager.CreateInstance(data, .1f, 1) as EquippableItem);
|
||||
ship.SetParent(GameManager.CurrentShip.Parent);
|
||||
GameManager.PlayerShips.Add(ship);
|
||||
|
||||
GameManager.Credits -= data.Price;
|
||||
}
|
||||
else
|
||||
{
|
||||
Dialog.Clear();
|
||||
Dialog.Title.text = "Unable to buy: Insufficient Credits!";
|
||||
Dialog.Show();
|
||||
}
|
||||
}
|
||||
|
||||
private void Buy(CraftedItemData data, int quantity)
|
||||
{
|
||||
for (int i = 0; i < quantity; i++)
|
||||
{
|
||||
if (data.Price < GameManager.Credits)
|
||||
{
|
||||
if (_targetCargo.TryStore(GameManager.ItemManager.CreateInstance(data, .1f, 1)))
|
||||
{
|
||||
GameManager.Credits -= data.Price;
|
||||
}
|
||||
else
|
||||
{
|
||||
Dialog.Clear();
|
||||
Dialog.Title.text = "Unable to buy: Insufficient Cargo Space!";
|
||||
Dialog.Show();
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Dialog.Clear();
|
||||
Dialog.Title.text = "Unable to buy: Insufficient Credits!";
|
||||
Dialog.Show();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Buy(SimpleCommodityData data, int quantity)
|
||||
{
|
||||
int lots = quantity / data.MaxStack;
|
||||
int remaining = quantity;
|
||||
for (int i = 0; i < lots; i++)
|
||||
{
|
||||
int q = min(remaining, data.MaxStack);
|
||||
if (q * data.Price < GameManager.Credits)
|
||||
{
|
||||
if (_targetCargo.TryStore(GameManager.ItemManager.CreateInstance(data, q)))
|
||||
{
|
||||
GameManager.Credits -= q * data.Price;
|
||||
remaining -= q;
|
||||
}
|
||||
else
|
||||
{
|
||||
Dialog.Clear();
|
||||
Dialog.Title.text = "Unable to buy: Insufficient Cargo Space!";
|
||||
Dialog.Show();
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Dialog.Clear();
|
||||
Dialog.Title.text = "Unable to buy: Insufficient Credits!";
|
||||
Dialog.Show();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
FoldoutButton.onClick.AddListener(() =>
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ public class ConfirmationDialog : PropertiesPanel
|
|||
private Action _onCancel;
|
||||
private Action _onConfirm;
|
||||
|
||||
private new void Start()
|
||||
private void Start()
|
||||
{
|
||||
Cancel.onClick.AddListener(() =>
|
||||
{
|
||||
|
|
@ -62,7 +62,7 @@ public class ConfirmationDialog : PropertiesPanel
|
|||
_onCancel = onCancel;
|
||||
Cancel.gameObject.SetActive(onCancel!=null);
|
||||
|
||||
ButtonGroup.SetActive(onConfirm!=null && onCancel!=null);
|
||||
ButtonGroup.SetActive(onConfirm!=null || onCancel!=null);
|
||||
|
||||
transform.position = Mouse.current.position.ReadValue();
|
||||
CancelClickCatcher.gameObject.SetActive(true);
|
||||
|
|
|
|||
|
|
@ -65,11 +65,11 @@ public class ContextMenu : MonoBehaviour
|
|||
optionButton.Label.text = text;
|
||||
optionButton.Button.onClick.AddListener(() =>
|
||||
{
|
||||
action();
|
||||
if(Parent!=null)
|
||||
Parent.End();
|
||||
else
|
||||
End();
|
||||
action();
|
||||
});
|
||||
optionButton.Hover.OnEnter += data =>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -11,9 +11,9 @@ using UniRx;
|
|||
using UniRx.Triggers;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.Experimental.Rendering;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
using int2 = Unity.Mathematics.int2;
|
||||
|
||||
|
|
@ -125,12 +125,27 @@ public class InventoryPanel : MonoBehaviour
|
|||
if(GameManager.DockingBay!=null && _displayedCargo!=GameManager.DockingBay)
|
||||
ContextMenu.AddOption(GameManager.DockingBay.Name, () => Display(GameManager.DockingBay));
|
||||
|
||||
ContextMenu.AddOption("Create Preset",
|
||||
ContextMenu.AddOption("Save Loadout",
|
||||
() =>
|
||||
{
|
||||
GameManager.SavePreset(EntityPack.Pack(_displayedEntity));
|
||||
GameManager.SaveLoadout(EntityPack.Pack(_displayedEntity));
|
||||
});
|
||||
|
||||
if (GameManager.Loadouts.Any())
|
||||
{
|
||||
ContextMenu.AddDropdown("Restore Loadout",
|
||||
GameManager.Loadouts.Select<EntityPack, (string text, Action action, bool enabled)>(pack =>
|
||||
(
|
||||
$"{pack.Name} - {pack.Price(GameManager.ItemManager):n0}", () =>
|
||||
{
|
||||
var ship = EntityPack.Unpack(GameManager.ItemManager, GameManager.CurrentShip.Zone, pack, true);
|
||||
ship.SetParent(GameManager.CurrentShip.Parent);
|
||||
GameManager.PlayerShips.Add(ship);
|
||||
GameManager.Credits -= pack.Price(GameManager.ItemManager);
|
||||
}, pack.Price(GameManager.ItemManager) < GameManager.Credits
|
||||
)));
|
||||
}
|
||||
|
||||
ContextMenu.Show();
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue