ItemInventories don't have own ID's anymore but rely on owner ID, relaying reliablemessages through server
This commit is contained in:
@@ -25,15 +25,14 @@ namespace Barotrauma
|
||||
private Vector2[] slotPositions;
|
||||
|
||||
public CharacterInventory(int capacity, Character character)
|
||||
: base(capacity)
|
||||
: base(character, capacity)
|
||||
{
|
||||
this.character = character;
|
||||
|
||||
if (icons == null) icons = TextureLoader.FromFile("Content/UI/inventoryIcons.png");
|
||||
|
||||
slotPositions = new Vector2[limbSlots.Length];
|
||||
|
||||
|
||||
|
||||
int rectWidth = 40, rectHeight = 40;
|
||||
int spacing = 10;
|
||||
for (int i = 0; i < slotPositions.Length; i++)
|
||||
@@ -123,16 +122,19 @@ namespace Barotrauma
|
||||
if (allowedSlots.HasFlag(limbSlots[i]) && items[i]!=null) return false;
|
||||
}
|
||||
|
||||
bool placed = false;
|
||||
for (int i = 0; i < capacity; i++)
|
||||
{
|
||||
if (allowedSlots.HasFlag(limbSlots[i]) && items[i] == null)
|
||||
{
|
||||
PutItem(item, i, createNetworkEvent);
|
||||
PutItem(item, i, createNetworkEvent, !placed);
|
||||
item.Equip(character);
|
||||
return true;
|
||||
placed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (placed) return true;
|
||||
|
||||
if (allowedSlots.HasFlag(LimbSlot.BothHands)) TryPutItem(item, 3, createNetworkEvent);
|
||||
|
||||
return false;
|
||||
@@ -158,7 +160,7 @@ namespace Barotrauma
|
||||
Inventory otherInventory = items[i].inventory;
|
||||
if (otherInventory!=null)
|
||||
{
|
||||
new Networking.NetworkEvent(Networking.NetworkEventType.InventoryUpdate, otherInventory.ID, true);
|
||||
new Networking.NetworkEvent(Networking.NetworkEventType.InventoryUpdate, otherInventory.Owner.ID, true);
|
||||
}
|
||||
|
||||
combined = true;
|
||||
|
||||
@@ -89,7 +89,7 @@ namespace Barotrauma.Items.Components
|
||||
public ItemContainer(Item item, XElement element)
|
||||
: base (item, element)
|
||||
{
|
||||
inventory = new ItemInventory(this, capacity, hudPos, slotsPerRow);
|
||||
inventory = new ItemInventory(item, this, capacity, hudPos, slotsPerRow);
|
||||
containableItems = new List<RelatedItem>();
|
||||
|
||||
foreach (XElement subElement in element.Elements())
|
||||
|
||||
@@ -8,11 +8,13 @@ using System;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
class Inventory : Entity
|
||||
class Inventory
|
||||
{
|
||||
public static Item draggingItem;
|
||||
public static Item doubleClickedItem;
|
||||
|
||||
public readonly Entity Owner;
|
||||
|
||||
private int slotsPerRow;
|
||||
|
||||
public int SlotsPerRow
|
||||
@@ -39,10 +41,12 @@ namespace Barotrauma
|
||||
|
||||
public Item[] items;
|
||||
|
||||
public Inventory(int capacity, Vector2? centerPos = null, int slotsPerRow=5)
|
||||
public Inventory(Entity owner, int capacity, Vector2? centerPos = null, int slotsPerRow=5)
|
||||
{
|
||||
this.capacity = capacity;
|
||||
|
||||
this.Owner = owner;
|
||||
|
||||
this.slotsPerRow = slotsPerRow;
|
||||
|
||||
items = new Item[capacity];
|
||||
@@ -95,6 +99,7 @@ namespace Barotrauma
|
||||
|
||||
public virtual bool TryPutItem(Item item, int i, bool createNetworkEvent = true)
|
||||
{
|
||||
if (Owner == null) return false;
|
||||
if (CanBePut(item,i))
|
||||
{
|
||||
PutItem(item, i, createNetworkEvent);
|
||||
@@ -108,6 +113,8 @@ namespace Barotrauma
|
||||
|
||||
protected void PutItem(Item item, int i, bool createNetworkEvent, bool removeItem = true)
|
||||
{
|
||||
if (Owner == null) return;
|
||||
|
||||
if (item.inventory != null && removeItem)
|
||||
{
|
||||
item.Drop();
|
||||
@@ -121,7 +128,7 @@ namespace Barotrauma
|
||||
item.body.Enabled = false;
|
||||
}
|
||||
|
||||
if (createNetworkEvent) new NetworkEvent(NetworkEventType.InventoryUpdate, ID, true);
|
||||
if (createNetworkEvent) new NetworkEvent(NetworkEventType.InventoryUpdate, Owner.ID, true);
|
||||
}
|
||||
|
||||
public void RemoveItem(Item item)
|
||||
@@ -136,7 +143,6 @@ namespace Barotrauma
|
||||
|
||||
protected virtual void DropItem(Item item)
|
||||
{
|
||||
|
||||
item.Drop(null, false);
|
||||
return;
|
||||
}
|
||||
@@ -185,8 +191,11 @@ namespace Barotrauma
|
||||
}
|
||||
else
|
||||
{
|
||||
int[] data = { draggingItem.ID, -1 };
|
||||
new NetworkEvent(NetworkEventType.InventoryUpdate, ID, true, data);
|
||||
if (Owner!=null)
|
||||
{
|
||||
int[] data = { draggingItem.ID, -1 };
|
||||
new NetworkEvent(NetworkEventType.InventoryUpdate, Owner.ID, true, data);
|
||||
}
|
||||
|
||||
DropItem(draggingItem);
|
||||
}
|
||||
@@ -279,7 +288,7 @@ namespace Barotrauma
|
||||
spriteBatch.DrawString(GUI.Font, (int)item.Condition + " %", new Vector2(rect.X + rect.Width / 2, rect.Y + rect.Height / 2), Color.Red);
|
||||
}
|
||||
|
||||
public override bool FillNetworkData(NetworkEventType type, NetOutgoingMessage message, object data)
|
||||
public bool FillNetworkData(NetworkEventType type, NetOutgoingMessage message, object data)
|
||||
{
|
||||
for (int i = 0; i<capacity; i++)
|
||||
{
|
||||
@@ -289,7 +298,7 @@ namespace Barotrauma
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void ReadNetworkData(NetworkEventType type, NetIncomingMessage message)
|
||||
public void ReadNetworkData(NetworkEventType type, NetIncomingMessage message)
|
||||
{
|
||||
int[] newItemIDs = new int[capacity];
|
||||
|
||||
@@ -315,7 +324,7 @@ namespace Barotrauma
|
||||
continue;
|
||||
}
|
||||
|
||||
Item item = FindEntityByID(newItemIDs[i]) as Item;
|
||||
Item item = Entity.FindEntityByID(newItemIDs[i]) as Item;
|
||||
if (item == null) continue;
|
||||
|
||||
TryPutItem(item, i, false);
|
||||
|
||||
@@ -1169,6 +1169,10 @@ namespace Barotrauma
|
||||
message.Write(body.SimPosition.Y);
|
||||
}
|
||||
break;
|
||||
case NetworkEventType.InventoryUpdate:
|
||||
var itemContainer = GetComponent<ItemContainer>();
|
||||
if (itemContainer == null || itemContainer.inventory == null) return false;
|
||||
return itemContainer.inventory.FillNetworkData(NetworkEventType.InventoryUpdate, message, data);
|
||||
case NetworkEventType.UpdateComponent:
|
||||
|
||||
int componentIndex = (int)data;
|
||||
@@ -1233,6 +1237,11 @@ namespace Barotrauma
|
||||
SetTransform(newSimPos, body.Rotation);
|
||||
Drop(null, false);
|
||||
break;
|
||||
case NetworkEventType.InventoryUpdate:
|
||||
var itemContainer = GetComponent<ItemContainer>();
|
||||
if (itemContainer == null || itemContainer.inventory == null) return;
|
||||
itemContainer.inventory.ReadNetworkData(NetworkEventType.DropItem, message);
|
||||
break;
|
||||
case NetworkEventType.UpdateComponent:
|
||||
int componentIndex = message.ReadByte();
|
||||
if (componentIndex < 0 || componentIndex > components.Count - 1) return;
|
||||
|
||||
@@ -7,8 +7,8 @@ namespace Barotrauma
|
||||
{
|
||||
ItemContainer container;
|
||||
|
||||
public ItemInventory(ItemContainer container, int capacity, Vector2? centerPos = null, int slotsPerRow = 5)
|
||||
: base(capacity, centerPos, slotsPerRow)
|
||||
public ItemInventory(Item owner, ItemContainer container, int capacity, Vector2? centerPos = null, int slotsPerRow = 5)
|
||||
: base(owner, capacity, centerPos, slotsPerRow)
|
||||
{
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user