void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.tag == "Item") { if (stats.slotsUsed < 5) { stats.slots[stats.slotsUsed] = collision.GetComponent <Item>().weaponGiven; slotManager.AddItem(stats.slotsUsed, stats.slots[stats.slotsUsed].icon); stats.slotsUsed++; collision.GetComponent <Item>().SelfDestruct(); } } }
void OnMouseOver() { if (Input.GetKeyDown(KeyCode.E) && !Inventory.inventoryOpened && HealthManager.playerAlive) { foreach (Item i in Inventory.itemsFromJSON.items) { if (name.Split('(')[0].Trim() == i.spriteName) { if (SlotManager.AddItem(i.id, 1) == 0) { GameObject.FindGameObjectWithTag("MainCamera").GetComponent <Inventory>().ReloadHotbar(); Destroy(gameObject); } } } } }
bool AddRemovedBuildableBackToInventory(string buildableName) { foreach (Item i in Inventory.itemsFromJSON.items) { if (i.buildablePrefabName == buildableName.Split('(')[0].Trim()) { if (SlotManager.AddItem(i.id, 1) == 0) { return(true); } else { GameObject droppedItem; droppedItem = Instantiate(Resources.Load <GameObject>("Prefabs/ObjectsDropped/" + i.prefabName), GameObject.FindGameObjectWithTag("Player").transform.position + Camera.main.transform.forward * 0.2f, Quaternion.identity); droppedItem.GetComponent <Rigidbody>().AddForce(Camera.main.transform.forward * 160f); return(false); } } } return(false); }
static void CraftItem(Recipe recipe) //needs to be adjusted to new quantity system { bool canCraft = true; Dictionary <int, int> inventorySlotsWithQuantity = new Dictionary <int, int>(); Dictionary <int, int> hotbarSlotsWithQuantity = new Dictionary <int, int>(); foreach (IDQuantityPair iDQuantityPair in recipe.itemIds) { int remainingItems = iDQuantityPair.quantity; foreach (KeyValuePair <int, Item> item in Inventory.items) { if (remainingItems > 0 && item.Value != null && item.Value.id == iDQuantityPair.id) { inventorySlotsWithQuantity.Add(item.Key, item.Value.quantity - remainingItems <= 0 ? item.Value.quantity : remainingItems); remainingItems -= item.Value.quantity; if (remainingItems <= 0) { break; } } } foreach (KeyValuePair <int, Item> item in Inventory.hotbarItems) { if (remainingItems > 0 && item.Value != null && item.Value.id == iDQuantityPair.id) { hotbarSlotsWithQuantity.Add(item.Key, item.Value.quantity - remainingItems <= 0 ? item.Value.quantity : remainingItems); remainingItems -= item.Value.quantity; if (remainingItems <= 0) { break; } } } if (remainingItems > 0) { canCraft = false; break; } } if (canCraft) { foreach (KeyValuePair <int, int> inventorySlotWithQuantity in inventorySlotsWithQuantity) { if (Inventory.items[inventorySlotWithQuantity.Key].quantity - inventorySlotWithQuantity.Value <= 0) { Inventory.items[inventorySlotWithQuantity.Key] = null; } else { Inventory.items[inventorySlotWithQuantity.Key].quantity -= inventorySlotWithQuantity.Value; } } foreach (KeyValuePair <int, int> hotbarSlotWithQuantity in hotbarSlotsWithQuantity) { if (Inventory.hotbarItems[hotbarSlotWithQuantity.Key].quantity - hotbarSlotWithQuantity.Value <= 0) { Inventory.hotbarItems[hotbarSlotWithQuantity.Key] = null; } else { Inventory.hotbarItems[hotbarSlotWithQuantity.Key].quantity -= hotbarSlotWithQuantity.Value; } } int craftedItemsToDrop = SlotManager.AddItem(recipe.id, recipe.quantityMade); if (craftedItemsToDrop > 0) { for (int x = craftedItemsToDrop; x > 0; x--) { SlotManager.ThrowItemOut(recipe.id); } } GameObject.FindGameObjectWithTag("MainCamera").GetComponent <Inventory>().ReloadInventory(); GameObject.FindGameObjectWithTag("MainCamera").GetComponent <Inventory>().ReloadHotbar(); } }