/// <summary> /// Adds a given number of the given Item type to this Inventory /// </summary> /// <param name="type">The Item type to add</param> /// <param name="quantity">The number of items to add</param> public void addItem(Item type, int quantity) { if (quantity < 0 || type.Equals(Item.Nothing)) return; //bad arguments if (this.items[(int)type] == 0) { this.order[numTypes++] = (int)type; } this.items[(int)type] += quantity; total += quantity; // push event InventoryEventArgs args = new InventoryEventArgs(); args.type = type; args.quantity = quantity; pushEvent(args); }
private void pushEvent(InventoryEventArgs e) { if (InventoryChanged != null) { InventoryChanged(this, e); } }
/// <summary> /// Listens for inventory changed events and updates the entries. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void updateInventory(object sender, InventoryEventArgs e) { Item item = e.type; int quantity = e.quantity; if (Hero.instance.inventory.numItem(item) == 0) // no more items of this type { this.entries.Remove(allEntries[(int)item]); this.loadEntries(); // !! this automatically resizes the list of entries on the panel } else if (Hero.instance.inventory.numItem(item) == quantity) { ItemEntry itemEntry = allEntries[(int)item]; this.loadEntries(itemEntry); } }
/// <summary> /// Removes a given number of the given item type from this Inventory /// </summary> /// <param name="type">The Item type to remove</param> /// <param name="quantity">The number of items to remove</param> public void removeItem(Item type, int quantity) { if (quantity < 0 || type.Equals(Item.Nothing)) return; //bad arguments if (this.items[(int)type] <= quantity) { int i = 0; for (; order[i] != (int)type; i++) ; Array.Copy(order, i + 1, order, i, numTypes - 1 - i); order[numTypes - 1] = -1; numTypes--; // O(n) operation quantity = this.items[(int)type]; //remove all items of this type } this.items[(int)type] -= quantity; this.total -= quantity; // push event InventoryEventArgs args = new InventoryEventArgs(); args.type = type; args.quantity = -quantity; pushEvent(args); }
private void inventoryEventListener(object sender, InventoryEventArgs e) { if (e.quantity > 0) { this.pushNotification("Received " + SunsetUtils.enumToString<Item>(e.type) + "(" + e.quantity + ")"); } }