2020-04-09 10:33:16 +01:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
2020-04-29 23:36:00 +01:00
|
|
|
using JsonKnownTypes;
|
|
|
|
|
using MessagePack;
|
|
|
|
|
using Newtonsoft.Json;
|
2020-12-10 23:20:38 +00:00
|
|
|
using UniRx;
|
2020-04-09 10:33:16 +01:00
|
|
|
using Unity.Mathematics;
|
|
|
|
|
using static Unity.Mathematics.math;
|
2020-12-10 23:20:38 +00:00
|
|
|
using int2 = Unity.Mathematics.int2;
|
2020-04-09 10:33:16 +01:00
|
|
|
|
2020-04-29 23:36:00 +01:00
|
|
|
[MessagePackObject,
|
2020-05-04 00:07:36 +01:00
|
|
|
Union(0, typeof(Ship)),
|
|
|
|
|
Union(1, typeof(OrbitalEntity)),
|
2020-04-29 23:36:00 +01:00
|
|
|
JsonObject(MemberSerialization.OptIn), JsonConverter(typeof(JsonKnownTypesConverter<Entity>))]
|
2020-12-10 23:20:38 +00:00
|
|
|
public abstract class Entity
|
2020-04-09 10:33:16 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
public Zone Zone;
|
|
|
|
|
public EquippableItem Hull;
|
2020-06-01 03:11:04 +01:00
|
|
|
|
|
|
|
|
public float2 Position;
|
|
|
|
|
public float2 Direction = float2(0,1);
|
|
|
|
|
public float2 Velocity;
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
public float[,] Temperature;
|
|
|
|
|
public float Energy;
|
2020-06-05 04:16:33 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
public readonly ReactiveCollection<EquippedItem> EquippedItems = new ReactiveCollection<EquippedItem>();
|
|
|
|
|
public readonly ReactiveCollection<EquippedCargoBay> EquippedCargoBays = new ReactiveCollection<EquippedCargoBay>();
|
|
|
|
|
|
|
|
|
|
public Entity Parent;
|
|
|
|
|
public List<Entity> Children = new List<Entity>();
|
2020-06-01 03:11:04 +01:00
|
|
|
|
|
|
|
|
public string Name;
|
|
|
|
|
|
|
|
|
|
public int Population;
|
|
|
|
|
public Dictionary<Guid, float> Personality = new Dictionary<Guid, float>();
|
2020-05-06 04:56:36 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
public readonly Dictionary<string, float> Messages = new Dictionary<string, float>();
|
|
|
|
|
public readonly Dictionary<object, float> VisibilitySources = new Dictionary<object, float>();
|
2020-06-02 07:01:03 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
private List<IPopulationAssignment> PopulationAssignments = new List<IPopulationAssignment>();
|
|
|
|
|
|
2020-06-11 02:55:11 +01:00
|
|
|
|
2020-07-03 01:33:42 +01:00
|
|
|
private bool _active;
|
2020-12-10 23:20:38 +00:00
|
|
|
private EquippedItem[,] _gearOccupancy;
|
|
|
|
|
private EquippedItem[,] _thermalOccupancy;
|
|
|
|
|
private HardpointData[,] _hardpoints;
|
|
|
|
|
|
|
|
|
|
public ItemManager ItemManager { get; }
|
|
|
|
|
public int AssignedPopulation => PopulationAssignments.Sum(pa => pa.AssignedPopulation);
|
|
|
|
|
public float Mass { get; private set; }
|
|
|
|
|
public float Visibility => VisibilitySources.Values.Sum();
|
|
|
|
|
|
2020-07-03 01:33:42 +01:00
|
|
|
public bool Active
|
|
|
|
|
{
|
|
|
|
|
get => _active;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_active = value;
|
|
|
|
|
if (_active)
|
|
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
foreach (var hardpoint in EquippedItems)
|
|
|
|
|
if(hardpoint.EquippableItem!=null)
|
2020-07-03 01:33:42 +01:00
|
|
|
foreach (var behavior in hardpoint.Behaviors)
|
|
|
|
|
{
|
|
|
|
|
if(behavior is IInitializableBehavior initializableBehavior)
|
|
|
|
|
initializableBehavior.Initialize();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-25 03:05:30 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
public Entity(ItemManager itemManager, Zone zone, EquippableItem hull)
|
2020-04-09 10:33:16 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
ItemManager = itemManager;
|
2020-05-07 03:53:54 +01:00
|
|
|
Zone = zone;
|
2020-04-23 03:31:17 +01:00
|
|
|
Hull = hull;
|
2020-06-05 04:16:33 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
private void MapShip()
|
2020-06-05 04:16:33 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
var hullData = ItemManager.GetData(Hull) as HullData;
|
|
|
|
|
_gearOccupancy = new EquippedItem[hullData.Shape.Width, hullData.Shape.Height];
|
|
|
|
|
_thermalOccupancy = new EquippedItem[hullData.Shape.Width, hullData.Shape.Height];
|
|
|
|
|
_hardpoints = new HardpointData[hullData.Shape.Width, hullData.Shape.Height];
|
|
|
|
|
foreach (var hardpoint in hullData.Hardpoints)
|
2020-06-05 04:16:33 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
foreach (var hardpointCoord in hardpoint.Shape.Coordinates)
|
|
|
|
|
{
|
|
|
|
|
var hullCoord = hardpoint.Position + hardpointCoord;
|
|
|
|
|
_hardpoints[hullCoord.x, hullCoord.y] = hardpoint;
|
|
|
|
|
}
|
2020-07-03 01:33:42 +01:00
|
|
|
}
|
2020-05-06 04:56:36 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
public void Hydrate(EquippedItem equippedItem)
|
2020-05-06 04:56:36 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
if (equippedItem.EquippableItem == null)
|
2020-06-05 04:16:33 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
ItemManager.Log("Attempted to hydrate EquippedGear with no gear on it! This should be impossible!");
|
2020-06-05 04:16:33 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
var gearData = ItemManager.GetData(equippedItem.EquippableItem);
|
2020-06-06 03:21:03 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
equippedItem.Behaviors = gearData.Behaviors
|
|
|
|
|
.Select(bd => bd.CreateInstance(ItemManager, this, equippedItem))
|
2020-06-05 04:16:33 +01:00
|
|
|
.ToArray();
|
2020-04-09 10:33:16 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
equippedItem.BehaviorGroups = equippedItem.Behaviors
|
2020-06-05 04:16:33 +01:00
|
|
|
.GroupBy(b => b.Data.Group)
|
|
|
|
|
.OrderBy(g=>g.Key)
|
|
|
|
|
.Select(g=>new BehaviorGroup {
|
|
|
|
|
Behaviors = g.ToArray(),
|
|
|
|
|
//Axis = (IAnalogBehavior) g.FirstOrDefault(b=>b is IAnalogBehavior),
|
|
|
|
|
Switch = (Switch) g.FirstOrDefault(b=>b is Switch),
|
|
|
|
|
Trigger = (Trigger) g.FirstOrDefault(b=>b is Trigger)
|
|
|
|
|
})
|
|
|
|
|
.ToArray();
|
2020-06-11 02:55:11 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
foreach (var behavior in equippedItem.Behaviors)
|
2020-07-03 01:33:42 +01:00
|
|
|
{
|
|
|
|
|
if(behavior is IPopulationAssignment populationAssignment)
|
|
|
|
|
PopulationAssignments.Add(populationAssignment);
|
|
|
|
|
}
|
2020-06-05 04:16:33 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
public EquippedCargoBay FindItemInCargo(Guid itemDataID)
|
2020-06-05 04:16:33 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
return EquippedCargoBays.FirstOrDefault(c => c.ItemsOfType.ContainsKey(itemDataID));
|
2020-06-10 02:45:10 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
// Attempts to move a given number of items of the given type to the target Entity
|
|
|
|
|
// Returns the number of items successfully transferred
|
|
|
|
|
public int TryTransferItems(Entity target, Guid itemDataID, int quantity)
|
2020-06-10 02:45:10 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
int quantityTransferred = 0;
|
|
|
|
|
while (quantityTransferred < quantity)
|
|
|
|
|
{
|
|
|
|
|
EquippedCargoBay originInventory = EquippedCargoBays.FirstOrDefault(c => c.ItemsOfType.ContainsKey(itemDataID));
|
|
|
|
|
|
|
|
|
|
if (originInventory == null) break;
|
|
|
|
|
|
|
|
|
|
var itemInstance = originInventory.ItemsOfType[itemDataID][0];
|
|
|
|
|
|
|
|
|
|
if (itemInstance is SimpleCommodity simpleCommodity)
|
|
|
|
|
{
|
|
|
|
|
var targetQuantity = min(simpleCommodity.Quantity, quantity - quantityTransferred);
|
|
|
|
|
if (!target.EquippedCargoBays.Any(c => originInventory.TryTransferItem(c, simpleCommodity, targetQuantity)))
|
|
|
|
|
{
|
|
|
|
|
quantityTransferred += targetQuantity - simpleCommodity.Quantity;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
quantityTransferred += targetQuantity;
|
|
|
|
|
}
|
|
|
|
|
else if (itemInstance is CraftedItemInstance craftedItemInstance)
|
|
|
|
|
{
|
|
|
|
|
if (!target.EquippedCargoBays.Any(c => originInventory.TryTransferItem(c, craftedItemInstance)))
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
quantityTransferred++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-10 02:45:10 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
return quantityTransferred;
|
2020-06-05 04:16:33 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
public EquippableItem Unequip(EquippedItem item)
|
2020-06-05 04:16:33 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
if (item.EquippableItem == null)
|
|
|
|
|
{
|
|
|
|
|
ItemManager.Log("Attempted to remove equipped item with no equippable item on it! This should be impossible!");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-06-05 04:16:33 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
if (item is EquippedCargoBay cargoBay)
|
|
|
|
|
{
|
|
|
|
|
if(cargoBay.Cargo.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
ItemManager.Log("Attempted to remove cargo bay that is not empty! Please check first before doing this!");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-06-05 04:16:33 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
EquippedCargoBays.Remove(cargoBay);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EquippedItems.Remove(item);
|
|
|
|
|
|
|
|
|
|
var itemData = ItemManager.GetData(item.EquippableItem);
|
|
|
|
|
foreach (var i in itemData.Shape.Coordinates)
|
|
|
|
|
{
|
|
|
|
|
var itemCoord = item.Position + itemData.Shape.Rotate(i, item.EquippableItem.Rotation);
|
|
|
|
|
(itemData.HardpointType == HardpointType.Thermal ? _thermalOccupancy : _gearOccupancy)[itemCoord.x, itemCoord.y] = null;
|
|
|
|
|
}
|
2020-06-05 04:16:33 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
return item.EquippableItem;
|
2020-06-05 04:16:33 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
// Check whether the given item will fit when its origin is placed at the given coordinate
|
|
|
|
|
private bool ItemFits(EquippableItemData itemData, HullData hullData, EquippableItem item, int2 hullCoord)
|
2020-06-05 04:16:33 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
// Items without specific hardpoints on the ship can be freely rotated and placed anywhere
|
|
|
|
|
if (itemData.HardpointType == HardpointType.Tool || itemData.HardpointType == HardpointType.Thermal)
|
|
|
|
|
{
|
|
|
|
|
// Check every cell of the item's shape
|
|
|
|
|
foreach (var i in itemData.Shape.Coordinates)
|
|
|
|
|
{
|
|
|
|
|
// If there is any gear already occupying that space, it won't fit
|
|
|
|
|
// Thermal items have their own layer and do not collide with gear
|
|
|
|
|
var itemCoord = hullCoord + itemData.Shape.Rotate(i, item.Rotation);
|
|
|
|
|
if (!hullData.InteriorCells[itemCoord] || (itemData.HardpointType == HardpointType.Thermal ? _thermalOccupancy : _gearOccupancy)[itemCoord.x, itemCoord.y] != null) return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var hardpoint = _hardpoints[hullCoord.x, hullCoord.y];
|
|
|
|
|
|
|
|
|
|
// If there's no hardpoint there, it won't fit
|
|
|
|
|
if (hardpoint == null) return false;
|
|
|
|
|
|
|
|
|
|
// If the hardpoint type doesn't match the item, it won't fit
|
|
|
|
|
if (hardpoint.Type != itemData.HardpointType) return false;
|
|
|
|
|
|
|
|
|
|
// Check every cell of the item's shape
|
|
|
|
|
foreach (var i in itemData.Shape.Coordinates)
|
|
|
|
|
{
|
|
|
|
|
// If the hardpoint does not have a matching cell, it wont fit
|
|
|
|
|
if (!hardpoint.Shape[i]) return false;
|
|
|
|
|
|
|
|
|
|
// If there is any gear already occupying that space, it won't fit
|
2020-12-12 00:02:00 +00:00
|
|
|
// Items placed in hardpoints are automatically aligned to hardpoint rotation
|
|
|
|
|
var itemCoord = hardpoint.Position + itemData.Shape.Rotate(i, hardpoint.Rotation);
|
2020-12-10 23:20:38 +00:00
|
|
|
if (_gearOccupancy[itemCoord.x, itemCoord.y] != null) return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2020-06-05 04:16:33 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
// Check whether the given item will fit when its origin is placed at the given coordinate on the hull
|
|
|
|
|
public bool ItemFits(EquippableItem item, int2 hullCoord)
|
2020-06-05 04:16:33 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
var itemData = ItemManager.GetData(item);
|
|
|
|
|
var hullData = ItemManager.GetData(Hull) as HullData;
|
|
|
|
|
return ItemFits(itemData, hullData, item, hullCoord);
|
2020-06-05 04:16:33 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
public bool TryFindSpace(EquippableItem item, out int2 hullCoord)
|
2020-06-05 04:16:33 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
var itemData = ItemManager.GetData(item);
|
|
|
|
|
var hullData = ItemManager.GetData(Hull) as HullData;
|
2020-05-11 01:17:10 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
// Tools and thermal equipment can be installed anywhere on the ship
|
|
|
|
|
// Search the whole ship for somewhere the item will fit
|
|
|
|
|
if (itemData.HardpointType == HardpointType.Tool || itemData.HardpointType == HardpointType.Thermal)
|
|
|
|
|
{
|
|
|
|
|
foreach (var hullCoord2 in hullData.InteriorCells.Coordinates)
|
|
|
|
|
{
|
|
|
|
|
if (ItemFits(itemData, hullData, item, hullCoord2))
|
|
|
|
|
{
|
|
|
|
|
hullCoord = hullCoord2;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-06 04:56:36 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
// Everything else has to be equipped onto a hardpoint of the same type
|
|
|
|
|
// Search the ship for an empty hardpoint that matches the type and shape of the item
|
|
|
|
|
else
|
2020-06-05 04:16:33 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
foreach (var hardpoint in hullData.Hardpoints)
|
|
|
|
|
{
|
|
|
|
|
if(hardpoint.Type == itemData.HardpointType)
|
|
|
|
|
{
|
|
|
|
|
foreach (var hardpointCoord in hardpoint.Shape.Coordinates)
|
|
|
|
|
{
|
|
|
|
|
var hullCoord2 = hardpoint.Position + hardpointCoord;
|
|
|
|
|
if (ItemFits(itemData, hullData, item, hullCoord2))
|
|
|
|
|
{
|
|
|
|
|
hullCoord = hullCoord2;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-05 04:16:33 +01:00
|
|
|
}
|
2020-04-28 01:50:50 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
hullCoord = int2.zero;
|
|
|
|
|
return false;
|
2020-04-28 01:50:50 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
// Try to equip the given item anywhere it will fit, returns true when the item was successfully equipped
|
|
|
|
|
public bool TryEquip(EquippableItem item) => TryFindSpace(item, out var hullCoord) && TryEquip(item, hullCoord);
|
|
|
|
|
|
|
|
|
|
// Try to equip the given item to the given location
|
|
|
|
|
public bool TryEquip(EquippableItem item, int2 hullCoord)
|
2020-06-02 07:01:03 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
var itemData = ItemManager.GetData(item);
|
|
|
|
|
var hullData = ItemManager.GetData(Hull) as HullData;
|
|
|
|
|
|
|
|
|
|
if (!ItemFits(itemData, hullData, item, hullCoord)) return false;
|
2020-06-05 04:16:33 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
if (itemData.HardpointType == HardpointType.Tool || itemData.HardpointType == HardpointType.Thermal)
|
|
|
|
|
{
|
|
|
|
|
EquippedItem equippedItem;
|
|
|
|
|
if(itemData is CargoBayData)
|
|
|
|
|
{
|
|
|
|
|
equippedItem = new EquippedCargoBay(ItemManager, item, hullCoord);
|
|
|
|
|
EquippedCargoBays.Add((EquippedCargoBay) equippedItem);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
equippedItem = new EquippedItem(ItemManager, item, hullCoord);
|
|
|
|
|
EquippedItems.Add(equippedItem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var i in itemData.Shape.Coordinates)
|
|
|
|
|
{
|
|
|
|
|
var occupiedCoord = hullCoord + itemData.Shape.Rotate(i, item.Rotation);
|
|
|
|
|
(itemData.HardpointType == HardpointType.Thermal ? _thermalOccupancy : _gearOccupancy)[occupiedCoord.x, occupiedCoord.y] = equippedItem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Hydrate(equippedItem);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-06-02 07:01:03 +01:00
|
|
|
else
|
2020-06-05 04:16:33 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
var equippedItem = new EquippedItem(ItemManager, item, hullCoord);
|
|
|
|
|
EquippedItems.Add(equippedItem);
|
|
|
|
|
|
|
|
|
|
foreach (var i in itemData.Shape.Coordinates)
|
2020-06-05 04:16:33 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
var occupiedCoord = hullCoord + i;
|
|
|
|
|
(itemData.HardpointType == HardpointType.Thermal ? _thermalOccupancy : _gearOccupancy)[occupiedCoord.x, occupiedCoord.y] = equippedItem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Hydrate(equippedItem);
|
|
|
|
|
return true;
|
2020-06-05 04:16:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddChild(Entity entity)
|
|
|
|
|
{
|
|
|
|
|
Mass += entity.Mass;
|
2020-12-10 23:20:38 +00:00
|
|
|
Children.Add(entity);
|
2020-06-05 04:16:33 +01:00
|
|
|
}
|
2020-06-02 07:01:03 +01:00
|
|
|
|
2020-06-05 04:16:33 +01:00
|
|
|
public void RemoveChild(Entity entity)
|
|
|
|
|
{
|
|
|
|
|
Mass -= entity.Mass;
|
2020-12-10 23:20:38 +00:00
|
|
|
Children.Remove(entity);
|
2020-06-02 07:01:03 +01:00
|
|
|
}
|
2020-12-01 00:16:11 +00:00
|
|
|
|
|
|
|
|
public void SetParent(Entity parent)
|
|
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
Parent = parent;
|
2020-12-01 00:16:11 +00:00
|
|
|
parent.AddChild(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveParent()
|
|
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
if (Parent == null)
|
2020-12-01 00:16:11 +00:00
|
|
|
return;
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
Parent.RemoveChild(this);
|
|
|
|
|
Parent = null;
|
2020-12-01 00:16:11 +00:00
|
|
|
}
|
2020-06-02 07:01:03 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
public T GetBehavior<T>() where T : class, IBehavior
|
2020-06-02 07:01:03 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
foreach (var equippedItem in EquippedItems)
|
|
|
|
|
foreach (var behavior in equippedItem.Behaviors)
|
|
|
|
|
if (behavior is T b)
|
|
|
|
|
return b;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-06-05 04:16:33 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
public IEnumerable<T> GetBehaviors<T>() where T : class, IBehavior
|
|
|
|
|
{
|
|
|
|
|
foreach (var equippedItem in EquippedItems)
|
|
|
|
|
foreach (var behavior in equippedItem.Behaviors)
|
|
|
|
|
if (behavior is T b)
|
|
|
|
|
yield return b;
|
|
|
|
|
}
|
2020-06-05 04:16:33 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
public IEnumerable<T> GetBehaviorData<T>() where T : BehaviorData
|
|
|
|
|
{
|
|
|
|
|
foreach (var equippedItem in EquippedItems)
|
|
|
|
|
foreach (var behavior in equippedItem.Behaviors)
|
|
|
|
|
if (behavior.Data is T b)
|
|
|
|
|
yield return b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Switch GetSwitch<T>() where T : class, IBehavior
|
|
|
|
|
{
|
|
|
|
|
foreach (var equippedItem in EquippedItems)
|
|
|
|
|
foreach (var group in equippedItem.BehaviorGroups)
|
|
|
|
|
foreach(var behavior in group.Behaviors)
|
|
|
|
|
if (behavior is T)
|
|
|
|
|
return group.Switch;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Trigger GetTrigger<T>() where T : class, IBehavior
|
|
|
|
|
{
|
|
|
|
|
foreach (var equippedItem in EquippedItems)
|
|
|
|
|
foreach (var group in equippedItem.BehaviorGroups)
|
|
|
|
|
foreach(var behavior in group.Behaviors)
|
|
|
|
|
if (behavior is T)
|
|
|
|
|
return group.Trigger;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void Update(float delta)
|
|
|
|
|
{
|
|
|
|
|
if (Active)
|
2020-06-05 04:16:33 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
foreach (var equippedItem in EquippedItems)
|
2020-06-02 07:01:03 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
equippedItem.Update(delta);
|
2020-06-02 07:01:03 +01:00
|
|
|
}
|
2020-12-10 23:20:38 +00:00
|
|
|
|
|
|
|
|
foreach (var message in Messages.Keys.ToArray())
|
2020-06-02 07:01:03 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
Messages[message] = Messages[message] - delta;
|
|
|
|
|
if (Messages[message] < 0)
|
|
|
|
|
Messages.Remove(message);
|
2020-06-02 07:01:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
if (Parent != null)
|
2020-06-05 04:16:33 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
Position = Parent.Position;
|
|
|
|
|
Velocity = Parent.Velocity;
|
2020-06-02 07:01:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
public void SetMessage(string message)
|
2020-04-28 01:50:50 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
Messages[message] = ItemManager.GameplaySettings.MessageDuration;
|
2020-06-02 07:01:03 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
// public void OnBeforeSerialize()
|
|
|
|
|
// {
|
|
|
|
|
// // Filter item behavior collections by those with any persistent behaviors
|
|
|
|
|
// // For each item create an object containing the item ID and a list of persistent behaviors
|
|
|
|
|
// // Then turn that into a dictionary mapping from item ID to an array of every behaviors persistent data
|
|
|
|
|
// PersistedBehaviors = EquippedItems
|
|
|
|
|
// .Where(item => item.Behaviors.Any(b=>b is IPersistentBehavior))
|
|
|
|
|
// .Select(item => new {equippable=item, behaviors = item.Behaviors
|
|
|
|
|
// .Where(b=>b is IPersistentBehavior)
|
|
|
|
|
// .Cast<IPersistentBehavior>()})
|
|
|
|
|
// .ToDictionary(x=> x.equippable.Position, x=>x.behaviors.Select(b => b.Store()).ToArray());
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// public void OnAfterDeserialize()
|
|
|
|
|
// {
|
|
|
|
|
// foreach(var item in EquippedItems)
|
|
|
|
|
// Hydrate(item);
|
|
|
|
|
//
|
|
|
|
|
// // 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
|
|
|
|
|
// foreach (var persistentBehaviorData in EquippedItems
|
|
|
|
|
// .Where(item => PersistedBehaviors.ContainsKey(item.Position))
|
|
|
|
|
// .SelectMany(item => item.Behaviors
|
|
|
|
|
// .Where(b=> b is IPersistentBehavior)
|
|
|
|
|
// .Cast<IPersistentBehavior>()
|
|
|
|
|
// .Zip(PersistedBehaviors[item.Position], (behavior, data) => new{behavior, data})))
|
|
|
|
|
// persistentBehaviorData.behavior.Restore(persistentBehaviorData.data);
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
|
|
|
|
|
public class EquippedItem
|
|
|
|
|
{
|
|
|
|
|
[JsonProperty("item"), Key(0)] public EquippableItem EquippableItem;
|
|
|
|
|
|
|
|
|
|
[JsonProperty("position"), Key(1)] public int2 Position;
|
|
|
|
|
|
|
|
|
|
[JsonProperty("temperature"), Key(2)] public float Temperature;
|
|
|
|
|
|
|
|
|
|
[IgnoreMember]
|
|
|
|
|
public IBehavior[] Behaviors;
|
|
|
|
|
|
|
|
|
|
[IgnoreMember]
|
|
|
|
|
public BehaviorGroup[] BehaviorGroups;
|
|
|
|
|
|
|
|
|
|
protected readonly ItemManager _itemManager;
|
|
|
|
|
private readonly EquippableItemData _data;
|
|
|
|
|
|
|
|
|
|
public EquippedItem(ItemManager itemManager, EquippableItem item, int2 position)
|
2020-04-09 10:33:16 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
_itemManager = itemManager;
|
|
|
|
|
_data = _itemManager.GetData(item);
|
|
|
|
|
EquippableItem = item;
|
|
|
|
|
Position = position;
|
2020-04-09 10:33:16 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
public void Update(float delta)
|
2020-05-11 01:17:10 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
if(Temperature < _data.MinimumTemperature || Temperature > _data.MaximumTemperature)
|
|
|
|
|
{
|
|
|
|
|
EquippableItem.Durability -= delta;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var group in BehaviorGroups)
|
|
|
|
|
{
|
|
|
|
|
foreach (var behavior in group.Behaviors)
|
|
|
|
|
{
|
|
|
|
|
if (!behavior.Update(delta))
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var behavior in Behaviors.Where(b => b is IAlwaysUpdatedBehavior).Cast<IAlwaysUpdatedBehavior>())
|
|
|
|
|
behavior.AlwaysUpdate(delta);
|
2020-05-11 01:17:10 +01:00
|
|
|
}
|
2020-12-10 23:20:38 +00:00
|
|
|
}
|
2020-05-11 01:17:10 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
public class EquippedCargoBay : EquippedItem
|
|
|
|
|
{
|
|
|
|
|
public readonly ReactiveDictionary<ItemInstance, int2> Cargo = new ReactiveDictionary<ItemInstance, int2>();
|
|
|
|
|
|
|
|
|
|
public readonly ItemInstance[,] Occupancy;
|
|
|
|
|
|
|
|
|
|
public readonly Dictionary<Guid, List<ItemInstance>> ItemsOfType = new Dictionary<Guid, List<ItemInstance>>();
|
|
|
|
|
|
|
|
|
|
private readonly CargoBayData _cargoData;
|
|
|
|
|
|
|
|
|
|
public float Mass { get; private set; }
|
|
|
|
|
public float ThermalMass { get; private set; }
|
|
|
|
|
|
|
|
|
|
public EquippedCargoBay(ItemManager itemManager, EquippableItem item, int2 position) : base(itemManager, item, position)
|
2020-05-11 01:17:10 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
_cargoData = _itemManager.GetData(EquippableItem) as CargoBayData;
|
|
|
|
|
|
|
|
|
|
Mass = _cargoData.Mass;
|
|
|
|
|
ThermalMass = _cargoData.Mass * _cargoData.SpecificHeat;
|
|
|
|
|
|
|
|
|
|
Occupancy = new ItemInstance[_cargoData.InteriorShape.Width,_cargoData.InteriorShape.Height];
|
2020-05-11 01:17:10 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
// Check whether the given item will fit when its origin is placed at the given coordinate
|
|
|
|
|
private bool ItemFits(ItemInstance item, int2 cargoCoord)
|
2020-05-28 04:50:40 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
var itemData = _itemManager.GetData(item);
|
|
|
|
|
// Check every cell of the item's shape
|
|
|
|
|
foreach (var i in itemData.Shape.Coordinates)
|
|
|
|
|
{
|
|
|
|
|
// If there is an item already occupying that space, it won't fit
|
|
|
|
|
var itemCargoCoord = cargoCoord + itemData.Shape.Rotate(i, item.Rotation);
|
|
|
|
|
if (Occupancy[itemCargoCoord.x, itemCargoCoord.y] != null) return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2020-05-28 04:50:40 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
// Tries to find a place to put the given items in the inventory
|
|
|
|
|
// Will attempt to fill existing item stacks first
|
|
|
|
|
// Returns true only when ALL of the items have places to go
|
|
|
|
|
public bool TryFindSpace(SimpleCommodity item, out List<int2> positions)
|
2020-05-28 04:50:40 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
positions = new List<int2>();
|
|
|
|
|
var itemData = _itemManager.GetData(item);
|
|
|
|
|
var remainingQuantity = item.Quantity;
|
|
|
|
|
|
|
|
|
|
// For simple commodities, search for existing item stacks to add to
|
|
|
|
|
foreach (var cargoItem in Cargo.Keys)
|
|
|
|
|
{
|
|
|
|
|
if (item.Data != cargoItem.Data) continue;
|
|
|
|
|
|
|
|
|
|
var cargoCommodity = (SimpleCommodity) cargoItem;
|
|
|
|
|
if (cargoCommodity.Quantity >= itemData.MaxStack) continue;
|
|
|
|
|
|
|
|
|
|
// Subtract remaining space in existing stack from remaining quantity
|
|
|
|
|
remainingQuantity -= min(itemData.MaxStack - cargoCommodity.Quantity, remainingQuantity);
|
|
|
|
|
positions.Add(Cargo[cargoItem]);
|
|
|
|
|
|
|
|
|
|
// If we've moved all of the items into existing stacks, no need to search for empty space!
|
|
|
|
|
if (remainingQuantity == 0) return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Search all the space in the cargo bay for an empty space where the item fits
|
|
|
|
|
foreach (var cargoCoord in _cargoData.InteriorShape.Coordinates)
|
|
|
|
|
{
|
|
|
|
|
if (ItemFits(item, cargoCoord))
|
|
|
|
|
{
|
|
|
|
|
positions.Add(cargoCoord);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2020-05-28 04:50:40 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
// Attempts to store all of the given item anywhere in the inventory
|
|
|
|
|
// Will attempt to fill existing item stacks first
|
|
|
|
|
// Returns true only when ALL of the items are successfully stored
|
|
|
|
|
public bool TryStore(SimpleCommodity item)
|
2020-04-09 10:33:16 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
TryFindSpace(item, out var positions);
|
|
|
|
|
foreach (var position in positions)
|
2020-05-26 01:12:06 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
if (TryStore(item, position)) return true;
|
2020-05-26 01:12:06 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
return false;
|
2020-04-09 10:33:16 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
// Try to store the given commodity at the given position
|
|
|
|
|
// If there's a stack at the given position it will be added to
|
|
|
|
|
// Returns true only when ALL of the items are successfully stored
|
|
|
|
|
public bool TryStore(SimpleCommodity item, int2 cargoCoord)
|
2020-04-09 10:33:16 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
var itemData = _itemManager.GetData(item);
|
|
|
|
|
if (ItemFits(item, cargoCoord))
|
2020-06-05 04:16:33 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
foreach (var p in itemData.Shape.Coordinates)
|
2020-06-12 01:57:15 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
var pos = cargoCoord + itemData.Shape.Rotate(p, item.Rotation);
|
|
|
|
|
Occupancy[pos.x, pos.y] = item;
|
|
|
|
|
}
|
|
|
|
|
Cargo[item] = cargoCoord;
|
2020-06-05 04:16:33 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
if(!ItemsOfType.ContainsKey(item.Data))
|
|
|
|
|
ItemsOfType[item.Data] = new List<ItemInstance>();
|
|
|
|
|
ItemsOfType[item.Data].Add(item);
|
|
|
|
|
}
|
|
|
|
|
else if (Occupancy[cargoCoord.x, cargoCoord.y] is SimpleCommodity cargoCommodity && cargoCommodity.Data == item.Data)
|
|
|
|
|
{
|
|
|
|
|
if (cargoCommodity.Quantity + item.Quantity <= itemData.MaxStack)
|
|
|
|
|
{
|
|
|
|
|
cargoCommodity.Quantity += item.Quantity;
|
2020-06-12 01:57:15 +01:00
|
|
|
}
|
2020-12-10 23:20:38 +00:00
|
|
|
else
|
2020-06-12 01:57:15 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
var quantityTransferred = itemData.MaxStack - cargoCommodity.Quantity;
|
|
|
|
|
item.Quantity -= quantityTransferred;
|
|
|
|
|
cargoCommodity.Quantity = itemData.MaxStack;
|
|
|
|
|
|
|
|
|
|
Mass += itemData.Mass * quantityTransferred;
|
|
|
|
|
ThermalMass += itemData.Mass * itemData.SpecificHeat * quantityTransferred;
|
|
|
|
|
return false;
|
2020-06-12 01:57:15 +01:00
|
|
|
}
|
2020-12-10 23:20:38 +00:00
|
|
|
}
|
|
|
|
|
else return false;
|
|
|
|
|
|
|
|
|
|
Mass += _itemManager.GetMass(item);
|
|
|
|
|
ThermalMass += _itemManager.GetThermalMass(item);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-06-12 01:57:15 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
// Searches the cargo bay for a position where the item will fit, returns true when found
|
|
|
|
|
public bool TryFindSpace(CraftedItemInstance item, out int2 position)
|
|
|
|
|
{
|
|
|
|
|
// Search all the space in the cargo bay for an empty space where the item fits
|
|
|
|
|
foreach (var cargoCoord in _cargoData.InteriorShape.Coordinates)
|
|
|
|
|
{
|
|
|
|
|
if (ItemFits(item, cargoCoord))
|
2020-06-12 01:57:15 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
position = cargoCoord;
|
|
|
|
|
return true;
|
2020-06-12 01:57:15 +01:00
|
|
|
}
|
2020-06-05 04:16:33 +01:00
|
|
|
}
|
2020-05-04 00:07:36 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
position = int2.zero;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try to store the given item anywhere it will fit, returns true when the item was successfully stored
|
|
|
|
|
public bool TryStore(CraftedItemInstance item) => TryFindSpace(item, out var position) && TryStore(item, position);
|
|
|
|
|
|
|
|
|
|
// Try to store the given item at the given position, returns true when the item was successfully stored
|
|
|
|
|
public bool TryStore(CraftedItemInstance item, int2 cargoCoord)
|
|
|
|
|
{
|
|
|
|
|
if (!ItemFits(item, cargoCoord)) return false;
|
|
|
|
|
|
|
|
|
|
var itemData = _itemManager.GetData(item);
|
|
|
|
|
foreach (var p in itemData.Shape.Coordinates)
|
2020-05-04 00:07:36 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
var pos = cargoCoord + itemData.Shape.Rotate(p, item.Rotation);
|
|
|
|
|
Occupancy[pos.x, pos.y] = item;
|
2020-05-04 00:07:36 +01:00
|
|
|
}
|
2020-12-10 23:20:38 +00:00
|
|
|
Cargo[item] = cargoCoord;
|
|
|
|
|
|
|
|
|
|
if(!ItemsOfType.ContainsKey(item.Data))
|
|
|
|
|
ItemsOfType[item.Data] = new List<ItemInstance>();
|
|
|
|
|
ItemsOfType[item.Data].Add(item);
|
|
|
|
|
|
|
|
|
|
Mass += _itemManager.GetMass(item);
|
|
|
|
|
ThermalMass += _itemManager.GetThermalMass(item);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-05-11 01:17:10 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
public SimpleCommodity Remove(SimpleCommodity item, int quantity)
|
|
|
|
|
{
|
|
|
|
|
if (!Cargo.ContainsKey(item))
|
2020-06-06 03:21:03 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
_itemManager.Log("Attempted to remove item from a cargo bay that it wasn't even in! Something went wrong here!");
|
|
|
|
|
return null;
|
2020-06-06 03:21:03 +01:00
|
|
|
}
|
2020-12-10 23:20:38 +00:00
|
|
|
var itemData = _itemManager.GetData(item);
|
|
|
|
|
if(quantity >= item.Quantity)
|
2020-06-06 03:21:03 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
foreach (var pos in itemData.Shape.Coordinates.Select(p => Cargo[item] + itemData.Shape.Rotate(p, item.Rotation)))
|
|
|
|
|
Occupancy[pos.x, pos.y] = null;
|
|
|
|
|
|
|
|
|
|
Cargo.Remove(item);
|
|
|
|
|
ItemsOfType[item.Data].Remove(item);
|
|
|
|
|
if (!ItemsOfType[item.Data].Any())
|
|
|
|
|
ItemsOfType.Remove(item.Data);
|
|
|
|
|
|
|
|
|
|
Mass -= _itemManager.GetMass(item);
|
|
|
|
|
ThermalMass -= _itemManager.GetThermalMass(item);
|
|
|
|
|
|
|
|
|
|
return item;
|
2020-06-06 03:21:03 +01:00
|
|
|
}
|
2020-04-29 23:36:00 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
item.Quantity -= quantity;
|
|
|
|
|
Mass -= itemData.Mass * quantity;
|
|
|
|
|
ThermalMass -= itemData.Mass * itemData.SpecificHeat * quantity;
|
|
|
|
|
return new SimpleCommodity{Data = item.Data, Quantity = quantity, Rotation = item.Rotation};
|
2020-05-29 23:06:35 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
public void Remove(CraftedItemInstance item)
|
2020-04-29 23:36:00 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
if (!Cargo.ContainsKey(item))
|
|
|
|
|
{
|
|
|
|
|
_itemManager.Log("Attempted to remove item from a cargo bay that it wasn't even in! Something went wrong here!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var itemData = _itemManager.GetData(item);
|
|
|
|
|
foreach (var pos in itemData.Shape.Coordinates.Select(p => Cargo[item] + itemData.Shape.Rotate(p, item.Rotation)))
|
|
|
|
|
Occupancy[pos.x, pos.y] = null;
|
|
|
|
|
|
|
|
|
|
Cargo.Remove(item);
|
|
|
|
|
ItemsOfType[item.Data].Remove(item);
|
|
|
|
|
if (!ItemsOfType[item.Data].Any())
|
|
|
|
|
ItemsOfType.Remove(item.Data);
|
|
|
|
|
|
|
|
|
|
Mass -= _itemManager.GetMass(item);
|
|
|
|
|
ThermalMass -= _itemManager.GetThermalMass(item);
|
2020-04-29 23:36:00 +01:00
|
|
|
}
|
2020-12-10 23:20:38 +00:00
|
|
|
|
|
|
|
|
public bool TryTransferItem(EquippedCargoBay target, SimpleCommodity item, int quantity)
|
2020-04-29 23:36:00 +01:00
|
|
|
{
|
2020-12-10 23:20:38 +00:00
|
|
|
if (!Cargo.ContainsKey(item))
|
|
|
|
|
{
|
|
|
|
|
_itemManager.Log("Attempted to remove item from a cargo bay that it wasn't even in! Something went wrong here!");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-04-30 02:54:31 +01:00
|
|
|
|
2020-12-10 23:20:38 +00:00
|
|
|
var oldPos = Cargo[item];
|
|
|
|
|
var newItem = Remove(item, quantity);
|
|
|
|
|
|
|
|
|
|
if (target.TryStore(item)) return true;
|
|
|
|
|
|
|
|
|
|
// Failed to transfer full quantity, move the remaining items back to their old slot
|
|
|
|
|
TryStore(newItem, oldPos);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TryTransferItem(EquippedCargoBay target, CraftedItemInstance item)
|
|
|
|
|
{
|
|
|
|
|
if (!Cargo.ContainsKey(item))
|
|
|
|
|
{
|
|
|
|
|
_itemManager.Log("Attempted to remove item from a cargo bay that it wasn't even in! Something went wrong here!");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!target.TryStore(item)) return false;
|
|
|
|
|
Remove(item);
|
|
|
|
|
return true;
|
2020-04-29 23:36:00 +01:00
|
|
|
}
|
2020-06-02 07:01:03 +01:00
|
|
|
}
|
|
|
|
|
|
2020-06-05 04:16:33 +01:00
|
|
|
public class BehaviorGroup
|
2020-06-02 07:01:03 +01:00
|
|
|
{
|
2020-06-05 04:16:33 +01:00
|
|
|
public IBehavior[] Behaviors;
|
|
|
|
|
public Trigger Trigger;
|
|
|
|
|
public Switch Switch;
|
|
|
|
|
//public IAnalogBehavior Axis;
|
2020-04-09 10:33:16 +01:00
|
|
|
}
|