public bool PlaceInventory(GameInventory inventory) { if (inventory == null) { Inventory = null; return(true); } if (Inventory != null) { // There's already inventory here. Maybe we can combine a stack? if (Inventory.Type != inventory.Type) { UnityDebugger.Debugger.LogError("Tile", "Trying to assign inventory to a tile that already has some of a different type."); return(false); } int numToMove = inventory.StackSize; if (Inventory.StackSize + numToMove > Inventory.MaxStackSize) { numToMove = Inventory.MaxStackSize - Inventory.StackSize; } Inventory.StackSize += numToMove; inventory.StackSize -= numToMove; return(true); } // At this point, we know that our current inventory is actually // null. Now we can't just do a direct assignment, because // the inventory manager needs to know that the old stack is now // empty and has to be removed from the previous lists. Inventory = inventory.Clone(); Inventory.Tile = this; inventory.StackSize = 0; return(true); }
public bool PlaceInventory(GameCharacter character, GameInventory sourceInventory, int amount = -1) { amount = amount < 0 ? sourceInventory.StackSize : Math.Min(amount, sourceInventory.StackSize); sourceInventory.ReleaseClaim(character); if (character.Inventory == null) { character.Inventory = sourceInventory.Clone(); character.Inventory.StackSize = 0; if (Inventories.ContainsKey(character.Inventory.Type) == false) { Inventories[character.Inventory.Type] = new List <GameInventory>(); } Inventories[character.Inventory.Type].Add(character.Inventory); } else if (character.Inventory.Type != sourceInventory.Type) { UnityDebugger.Debugger.LogError(InventoryManagerLogChanel, "Character is trying to pick up a mismatched inventory object type."); return(false); } character.Inventory.StackSize += amount; if (character.Inventory.MaxStackSize < character.Inventory.StackSize) { sourceInventory.StackSize = character.Inventory.StackSize - character.Inventory.MaxStackSize; character.Inventory.StackSize = character.Inventory.MaxStackSize; } else { sourceInventory.StackSize -= amount; } CleanupInventory(sourceInventory); return(true); }