/** * Get an object from the inventory at * the given position. * @param position The position **/ public GameObject GetObject(InventoryPosition position) { GameObject obj = inventory[position.x, position.y]; if (obj == null) { return(null); } inventory[position.x, position.y] = null; obj.SetActive(true); obj.transform.parent = null; return(obj); }
/** * Add an object to the inventory * Will add the object to the first open * location found. * * If this is a stackable item it will attempt * to stack it in each slot that is a stackable item. * If there is a remainder of items, it will place them * at the first emtpy slot * * @param obj The GameObject **/ public void AddObject(GameObject obj) { StackableInventoryItem stackableItem = obj.GetComponent <StackableInventoryItem>(); InventoryPosition defaultPosition = null; for (int x = 0; x < inventory.GetLength(0); x++) { for (int y = 0; y < inventory.GetLength(1); y++) { if (inventory[x, y] == null) { if (stackableItem == null) { AddObject(new InventoryPosition(x, y), obj); return; } else if (defaultPosition == null) { defaultPosition = new InventoryPosition(x, y); } } else { if (stackableItem != null) { StackableInventoryItem inventoryItem = inventory[x, y].GetComponent <StackableInventoryItem>(); if (inventoryItem != null) { StackableInventoryItem remainder = inventoryItem.AddStackableInventoryItems(stackableItem); if (remainder == null) { return; } } } } } } if (defaultPosition != null) { AddObject(defaultPosition, obj); } }
/** * Add an object at the specific position * @param InventoryPosition the position * @param obj The GameObject **/ public void AddObject(InventoryPosition position, GameObject obj) { inventory[position.x, position.y] = obj; obj.SetActive(false); obj.transform.parent = parent.transform; }