Inventory Events

Added an event system allowing the UI to subscribe to changes in entity cargo, hardpoints or on specific items
Fixed bugs in colony tab caused by recent refactoring
This commit is contained in:
hyperion51 2020-06-06 03:21:03 +01:00
commit 39ce6809c0
11 changed files with 370 additions and 249 deletions

View file

@ -47,14 +47,11 @@ public class Factory : IBehavior, IPersistentBehavior
_blueprint = value;
RetoolingTime = ToolingTime;
_retooling = true;
OnProductionUpdate?.Invoke();
OnProductionUpdate = null;
Item.Change();
ItemName = Context.Cache.Get<BlueprintData>(value).Name;
}
}
}
public event Action OnProductionUpdate;
private FactoryData _data;
@ -87,7 +84,7 @@ public class Factory : IBehavior, IPersistentBehavior
if (_retooling)
{
_retooling = false;
OnProductionUpdate?.Invoke();
Item.Change();
}
if (ItemUnderConstruction != Guid.Empty)
@ -106,6 +103,7 @@ public class Factory : IBehavior, IPersistentBehavior
Entity.AddCargo(craftedItemInstance);
Entity.IncompleteCargo.Remove(ItemUnderConstruction);
ItemUnderConstruction = Guid.Empty;
Item.Change();
}
return true;
@ -117,7 +115,8 @@ public class Factory : IBehavior, IPersistentBehavior
(pow(Context.Random.NextFloat(), blueprint.RandomExponent) +
pow(Context.Random.NextFloat(), blueprint.RandomExponent)) / 2, ItemName);
OnProductionUpdate?.Invoke();
if(ItemUnderConstruction!=Guid.Empty)
Item.Change();
return false;
}

View file

@ -99,13 +99,14 @@ public class MiningController : ControllerBase, IBehavior, IPersistentBehavior,
foreach (var item in _entity.Cargo
.Select(id => _context.Cache.Get<ItemInstance>(id))
.Where(item => item is SimpleCommodity)
.Cast<SimpleCommodity>())
.Cast<SimpleCommodity>()
.ToArray())
{
int supportedQuantity = (int) ((homeEntity.Capacity - homeEntity.OccupiedCapacity) / item.ItemData.Size);
var newItem = _entity.RemoveCargo(item, supportedQuantity);
int quantity = min((int) ((homeEntity.Capacity - homeEntity.OccupiedCapacity) / item.ItemData.Size), item.Quantity);
var newItem = _entity.RemoveCargo(item, quantity);
homeEntity.AddCargo(newItem);
if (supportedQuantity < item.Quantity)
if (quantity < item.Quantity)
{
homeEntity.SetMessage("Colony is out of cargo space. Closing Mining Task.");
FinishTask();

View file

@ -75,15 +75,25 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR
// [IgnoreMember] public Dictionary<Gear, EquippableItemData> GearData;
// [IgnoreMember] public Dictionary<Gear, List<IBehavior>> ItemBehaviors;
private bool _inventoryChanged;
[IgnoreMember] public float Mass { get; private set; }
[IgnoreMember] public float OccupiedCapacity { get; private set; }
[IgnoreMember] public float ThermalMass { get; private set; }
[IgnoreMember] public float Visibility => VisibilitySources.Values.Sum();
public event Action OnInventoryUpdate;
public class InventoryEvent : IDynamicProperties
{
public event Action OnChanged;
public void Change()
{
OnChanged?.Invoke();
}
}
private bool _cargoChanged;
public InventoryEvent CargoEvent = new InventoryEvent();
private bool _gearChanged;
public InventoryEvent GearEvent = new InventoryEvent();
public float Capacity
{
get
@ -161,6 +171,8 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR
return;
}
hardpoint.ItemData = hardpoint.Gear.ItemData;
hardpoint.Behaviors = hardpoint.Gear.ItemData.Behaviors
.Select(bd => bd.CreateInstance(Context, this, hardpoint.Gear))
.ToArray();
@ -184,11 +196,12 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR
{
Gear.Remove(hardpoint.Gear.ID);
Cargo.Add(hardpoint.Gear.ID);
_cargoChanged = true;
}
hardpoint.Gear = gear;
HydrateHardpoint(hardpoint);
_inventoryChanged = true;
_gearChanged = true;
}
public bool Equip(Guid gearID, bool force = false)
@ -226,7 +239,7 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR
Mass += item.Mass;
ThermalMass += item.ThermalMass;
OccupiedCapacity += item.Size;
_inventoryChanged = true;
_cargoChanged = true;
return item;
}
@ -238,7 +251,7 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR
Mass -= item.Mass;
ThermalMass -= item.ThermalMass;
OccupiedCapacity -= item.Size;
_inventoryChanged = true;
_cargoChanged = true;
return item;
}
@ -260,12 +273,13 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR
var existingCommodity = Context.Cache.Get<SimpleCommodity>(existingCommodityID);
existingCommodity.Quantity += item.Quantity;
Context.Cache.Delete(item);
_cargoChanged = true;
return existingCommodity;
}
// No match was found, simply add the item instance to the cargo
Cargo.Add(item.ID);
_inventoryChanged = true;
_cargoChanged = true;
return item;
}
@ -294,7 +308,7 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR
ThermalMass -= item.ThermalMass;
OccupiedCapacity -= item.Size;
_inventoryChanged = true;
_cargoChanged = true;
return item;
}
@ -442,11 +456,6 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR
Cargo.Select(i => Context.Cache.Get<ItemInstance>(i)).Sum(ii => ii.Size);
}
public void ClearInventoryListeners()
{
OnInventoryUpdate = null;
}
public T GetBehavior<T>() where T : class, IBehavior
{
foreach (var hardpoint in Hardpoints)
@ -605,6 +614,17 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR
// foreach (var axis in ItemBehaviors[staleItem].Where(b => b is IAnalogBehavior).Cast<IAnalogBehavior>())
// Axes[axis] = 0;
// }
if (_cargoChanged)
{
CargoEvent.Change();
_cargoChanged = false;
}
if (_gearChanged)
{
GearEvent.Change();
_gearChanged = false;
}
}
public void SetMessage(string message)

View file

@ -0,0 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public interface IDynamicProperties
{
event Action OnChanged;
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9188eb1236c6f6f42b4c2d04b3111867
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -55,9 +55,16 @@ public class SimpleCommodity : ItemInstance
}
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
public class Gear : CraftedItemInstance
public class Gear : CraftedItemInstance, IDynamicProperties
{
[JsonProperty("durability"), Key(7)] public float Durability;
public EquippableItemData ItemData => Context.GetData(this);
public event Action OnChanged;
public void Change()
{
OnChanged?.Invoke();
}
}