// Updates the quantity of a single item in the inventory. If the item // of itemId being updated is not yet in the inventory, it is added. // See "AddItem(...)" for more information on conditions under which // things go awry. public void UpdateItem(int itemId, int quantity) { if (occupiedSpaces.ContainsKey(itemId)) { InventoryGUIItem item = GetInventoryItem((int)occupiedSpaces[itemId]); item.SetQuantity(quantity); } else { AddItem(itemId, quantity); } }
// Deletes an item of itemId from the inventory. The space it occupied // is marked as available for a future item to use. The item no longer // appears in the graphical interface. public void DeleteItem(int itemId) { if (occupiedSpaces.ContainsKey(itemId)) { int spaceNumber = (int)occupiedSpaces[itemId]; // Reset the GUI item. InventoryGUIItem item = GetInventoryItem(spaceNumber); item.ClearItem(); // Update available spaces, occupied spaces. availableSpaces.Add(spaceNumber); occupiedSpaces.Remove(itemId); } }
// Generates the grid of placeholders that make up the graphical // inventory interface. Each placeholder is a prefab which contains // everything required to ultimately be an item. private void GenerateInventoryPlaceholders() { int i = 0; for (int y = inventoryItemsVertical - 1; y >= 0; y--) { for (int x = 0; x < inventoryItemsHorizontal; x++) { InventoryGUIItem inventoryItem = (InventoryGUIItem)Instantiate(inventoryItemCloneable); inventoryItem.transform.parent = transform; inventoryItem.transform.localPosition = GetInventoryItemPosition(x, y); inventoryItem.inventory = inventory; inventoryItem.itemManager = itemManager; items.Add(inventoryItem); availableSpaces.Add(i++); } } }
void Update() { if (lastJoystickMovement < 0) { if (Input.GetAxisRaw("Joystick UI X") > 0.3) // Right { if (joystickSelectedItem < ((inventoryItemsHorizontal - 1) % inventoryItemsHorizontal)) { // Set current item to a "not hovered" state. items[joystickSelectedItem].SetNotHoveredByJoystick(); // Set next item to a "hovered" state. joystickSelectedItem++; while (!items[joystickSelectedItem].ItemIsSet() && joystickSelectedItem > 0) { joystickSelectedItem--; } InventoryGUIItem item = items[joystickSelectedItem]; item.SetHoveredByJoystick(); lastJoystickMovement = timeBetweenJoystickMovements; } } if (Input.GetAxisRaw("Joystick UI X") < -0.3) // Left { if (joystickSelectedItem > 0) { // Set current item to a "not hovered" state. items[joystickSelectedItem].SetNotHoveredByJoystick(); // Set next item to a "hovered" state. joystickSelectedItem--; InventoryGUIItem item = items[joystickSelectedItem]; item.SetHoveredByJoystick(); lastJoystickMovement = timeBetweenJoystickMovements; } } if (Input.GetAxisRaw("Joystick UI Y") < -0.3) // Up { if (joystickSelectedItem > (inventoryItemsHorizontal - 1)) { // Set current item to a "not hovered" state. items[joystickSelectedItem].SetNotHoveredByJoystick(); // Set next item to a "hovered" state. joystickSelectedItem -= inventoryItemsHorizontal; while (!items[joystickSelectedItem].ItemIsSet() && joystickSelectedItem > 0) { joystickSelectedItem--; } InventoryGUIItem item = items[joystickSelectedItem]; item.SetHoveredByJoystick(); lastJoystickMovement = timeBetweenJoystickMovements; } } if (Input.GetAxisRaw("Joystick UI Y") > 0.3) // Down { if (joystickSelectedItem / inventoryItemsHorizontal < (inventoryItemsVertical - 1)) { // Set current item to a "not hovered" state. items[joystickSelectedItem].SetNotHoveredByJoystick(); // Set next item to a "hovered" state. joystickSelectedItem += inventoryItemsHorizontal; while (!items[joystickSelectedItem].ItemIsSet() && joystickSelectedItem > 0) { joystickSelectedItem--; } InventoryGUIItem item = items[joystickSelectedItem]; item.SetHoveredByJoystick(); lastJoystickMovement = timeBetweenJoystickMovements; } } } else { lastJoystickMovement -= Time.deltaTime; } }