public int CompareTo(object obj) { InventoryListing otherListing = obj as InventoryListing; if (otherListing != null && itemIndex.Count > 0 && otherListing.itemIndex.Count > 0) { //Find name of this type of object string name = inventory.Items[itemIndex[0]].SingleItemDescription; //And for the other group string otherName = inventory.Items[otherListing.itemIndex[0]].SingleItemDescription; if (otherName == null) { return(1); } if (name == null) { return(-1); } return(name.CompareTo(otherName)); } else { throw new ArgumentException("Object is not an InventoryListing"); } }
/// <summary> /// Use the item group. Function is responsible for deleting the item if used up etc. Return true if item was used successfully and time should be advanced. /// </summary> /// <param name="selectedGroup"></param> internal bool UseItem(InventoryListing selectedGroup) { //For now, we use the first item in any stack only int itemIndex = selectedGroup.ItemIndex[0]; Item itemToUse = Inventory.Items[itemIndex]; //Check if this is a useable item IUseableItem useableItem = itemToUse as IUseableItem; if (useableItem == null) { Game.MessageQueue.AddMessage("Cannot use this type of item!"); LogFile.Log.LogEntry("Tried to use non-useable item: " + itemToUse.SingleItemDescription); return(false); } bool usedSuccessfully = useableItem.Use(this); if (useableItem.UsedUp) { //Remove item from inventory and don't drop on floor Inventory.RemoveItem(itemToUse); Game.Dungeon.RemoveItem(itemToUse); } return(usedSuccessfully); }
/// <summary> /// Player equips item. Returns true if item was equipped and time should advance /// </summary> private bool EquipItem() { //User selects which item to use int chosenIndex = PlayerChooseFromInventory(); //Player exited if (chosenIndex == -1) { return(false); } Inventory playerInventory = Game.Dungeon.Player.Inventory; InventoryListing selectedGroup = playerInventory.InventoryListing[chosenIndex]; bool usedSuccessfully = Game.Dungeon.Player.EquipItem(selectedGroup); return(usedSuccessfully); }
/// <summary> /// Update the listing groups /// </summary> public void RefreshInventoryListing() { //List of groups of similar items inventoryListing.Clear(); //Group similar items (based on type) into categories (InventoryListing) for (int i = 0; i < items.Count; i++) { Item item = items[i]; //Check if we have a similar item group already. If so, add the index of this item to that group //Equipped items are not stacked bool foundGroup = false; if (!item.IsEquipped) { foreach (InventoryListing group in inventoryListing) { //Check that we are the same type (and therefore sort of item) Type itemType = item.GetType(); //Look only at the first item in the group (stored by index). All the items in this group must have the same type if (items[group.ItemIndex[0]].GetType() == item.GetType() && !items[group.ItemIndex[0]].IsEquipped) { group.ItemIndex.Add(i); foundGroup = true; break; } } } //If there is no group, create a new one if (!foundGroup) { InventoryListing newGroup = new InventoryListing(this); newGroup.ItemIndex.Add(i); inventoryListing.Add(newGroup); } } }
/// <summary> /// Drop an item from inventory /// </summary> /// <returns></returns> private bool DropItem() { //User selects which item to use int chosenIndex = PlayerChooseFromInventory(); //Player exited if (chosenIndex == -1) { return(false); } Dungeon dungeon = Game.Dungeon; Player player = dungeon.Player; Inventory playerInventory = player.Inventory; InventoryListing selectedGroup = playerInventory.InventoryListing[chosenIndex]; Item selectedItem = playerInventory.Items[selectedGroup.ItemIndex[0]]; //Check there is no item here already if (dungeon.ItemAtSpace(player.LocationLevel, player.LocationMap) != null) { Game.MessageQueue.AddMessage("Can't drop - already an item here!"); return(false); } //Remove from player inventory playerInventory.RemoveItem(selectedItem); //Drop the item here selectedItem.InInventory = false; selectedItem.LocationLevel = player.LocationLevel; selectedItem.LocationMap = player.LocationMap; Game.MessageQueue.AddMessage(selectedItem.SingleItemDescription + " dropped."); return(true); }
/// <summary> /// Use the item group. Function is responsible for deleting the item if used up etc. Return true if item was used successfully and time should be advanced. /// </summary> /// <param name="selectedGroup"></param> internal bool UseItem(InventoryListing selectedGroup) { //For now, we use the first item in any stack only int itemIndex = selectedGroup.ItemIndex[0]; Item itemToUse = Inventory.Items[itemIndex]; //Check if this is a useable item IUseableItem useableItem = itemToUse as IUseableItem; if (useableItem == null) { Game.MessageQueue.AddMessage("Cannot use this type of item!"); LogFile.Log.LogEntry("Tried to use non-useable item: " + itemToUse.SingleItemDescription); return false; } bool usedSuccessfully = useableItem.Use(this); if (useableItem.UsedUp) { //Remove item from inventory and don't drop on floor Inventory.RemoveItem(itemToUse); Game.Dungeon.RemoveItem(itemToUse); } return usedSuccessfully; }
/// <summary> /// Equip an item. Item is removed from the main inventory. /// Returns true if item was used successfully. /// </summary> /// <param name="selectedGroup"></param> /// <returns></returns> public bool EquipItem(InventoryListing selectedGroup) { //Select the first item in the stack int itemIndex = selectedGroup.ItemIndex[0]; Item itemToUse = Inventory.Items[itemIndex]; //Check if this item is equippable IEquippableItem equippableItem = itemToUse as IEquippableItem; if (equippableItem == null) { LogFile.Log.LogEntryDebug("Can't equip item, not equippable: " + itemToUse.SingleItemDescription, LogDebugLevel.Medium); Game.MessageQueue.AddMessage("Can't equip " + itemToUse.SingleItemDescription); return false; } //Find all matching slots available on the player List<EquipmentSlot> itemPossibleSlots = equippableItem.EquipmentSlots; List<EquipmentSlotInfo> matchingEquipSlots = new List<EquipmentSlotInfo>(); foreach (EquipmentSlot slotType in itemPossibleSlots) { matchingEquipSlots.AddRange(this.EquipmentSlots.FindAll(x => x.slotType == slotType)); } //No suitable slots if (matchingEquipSlots.Count == 0) { LogFile.Log.LogEntryDebug("Can't equip item, no valid slots: " + itemToUse.SingleItemDescription, LogDebugLevel.Medium); Game.MessageQueue.AddMessage("Can't equip " + itemToUse.SingleItemDescription); return false; } //Look for first empty slot EquipmentSlotInfo freeSlot = matchingEquipSlots.Find(x => x.equippedItem == null); if (freeSlot == null) { //Not slots free, unequip first slot Item oldItem = matchingEquipSlots[0].equippedItem; IEquippableItem oldItemEquippable = oldItem as IEquippableItem; //Sanity check if (oldItemEquippable == null) { LogFile.Log.LogEntry("Currently equipped item is not equippable!: " + oldItem.SingleItemDescription); return false; } //Run unequip routine oldItemEquippable.UnEquip(this); oldItem.IsEquipped = false; //Can't do this right now, since not in inventory items appear on the floor //This slot is now free freeSlot = matchingEquipSlots[0]; } //We now have a free slot to equip in //Put new item in first relevant slot and run equipping routine matchingEquipSlots[0].equippedItem = itemToUse; equippableItem.Equip(this); itemToUse.IsEquipped = true; //Update the inventory listing since equipping an item changes its stackability Inventory.RefreshInventoryListing(); //Message the user LogFile.Log.LogEntryDebug("Item equipped: " + itemToUse.SingleItemDescription, LogDebugLevel.Low); Game.MessageQueue.AddMessage(itemToUse.SingleItemDescription + " equipped in " + StringEquivalent.EquipmentSlots[matchingEquipSlots[0].slotType]); return true; }
/// <summary> /// Update the listing groups /// </summary> public void RefreshInventoryListing() { //INVENTORY (non-equippable) //List of groups of similar items inventoryListing.Clear(); //Group similar items (based on type) into categories (InventoryListing) for (int i = 0; i < items.Count; i++) { Item item = items[i]; //Equipped items are not displayed if (item.IsEquipped) { continue; } //Check if we have a similar item group already. If so, add the index of this item to that group bool foundGroup = false; foreach (InventoryListing group in inventoryListing) { //Check that we are the same type (and therefore sort of item) Type itemType = item.GetType(); //Look only at the first item in the group (stored by index). All the items in this group must have the same type if (items[group.ItemIndex[0]].GetType() == item.GetType() && !items[group.ItemIndex[0]].IsEquipped) { group.ItemIndex.Add(i); foundGroup = true; break; } } //If there is no group, create a new one if (!foundGroup) { InventoryListing newGroup = new InventoryListing(this); newGroup.ItemIndex.Add(i); inventoryListing.Add(newGroup); } } //Sort the inventory listing alphabetically. This keeps the list roughly in the same order for the player inventoryListing.Sort(); //EQUIPPABLE (non-removable) equipmentListing.Clear(); for (int i = 0; i < items.Count; i++) { Item item = items[i]; //Non-equipped items are not displayed if (!item.IsEquipped) { continue; } //No stacking for equipment InventoryListing newGroup = new InventoryListing(this); newGroup.ItemIndex.Add(i); equipmentListing.Add(newGroup); } }
/// <summary> /// Use the item group. Function is responsible for deleting the item if used up etc. Return true if item was used successfully and time should be advanced. /// </summary> /// <param name="selectedGroup"></param> internal bool UseItem(InventoryListing selectedGroup) { //For now, we use the first item in any stack only int itemIndex = selectedGroup.ItemIndex[0]; Item itemToUse = Inventory.Items[itemIndex]; //Check if this is a useable item IUseableItem useableItem = itemToUse as IUseableItem; if (useableItem == null) { Game.MessageQueue.AddMessage("Cannot use this type of item!"); LogFile.Log.LogEntry("Tried to use non-useable item: " + itemToUse.SingleItemDescription); return false; } bool usedSuccessfully = useableItem.Use(this); if (useableItem.UsedUp) { //Remove item from inventory and don't drop on floor //Goes back into the global list and will be respawned at town //Inventory.RemoveItem(itemToUse); //This permanently deletes it from the game //Game.Dungeon.RemoveItem(itemToUse); //If the line above is commented, the item will be returned to town. Will want to un-use it in this case //Only ditch the non-equippable items IEquippableItem equipItem = useableItem as IEquippableItem; if (equipItem == null) { Inventory.RemoveItem(itemToUse); } //useableItem.UsedUp = false; } return usedSuccessfully; }
/// <summary> /// Equip an item. Item is removed from the main inventory. /// Returns true if item was used successfully. /// </summary> /// <param name="selectedGroup"></param> /// <returns></returns> public bool EquipItem(InventoryListing selectedGroup) { //Select the first item in the stack int itemIndex = selectedGroup.ItemIndex[0]; Item itemToUse = Inventory.Items[itemIndex]; //Check if this item is equippable IEquippableItem equippableItem = itemToUse as IEquippableItem; if (equippableItem == null) { LogFile.Log.LogEntryDebug("Can't equip item, not equippable: " + itemToUse.SingleItemDescription, LogDebugLevel.Medium); Game.MessageQueue.AddMessage("Can't equip " + itemToUse.SingleItemDescription); return(false); } //Find all matching slots available on the player List <EquipmentSlot> itemPossibleSlots = equippableItem.EquipmentSlots; List <EquipmentSlotInfo> matchingEquipSlots = new List <EquipmentSlotInfo>(); foreach (EquipmentSlot slotType in itemPossibleSlots) { matchingEquipSlots.AddRange(this.EquipmentSlots.FindAll(x => x.slotType == slotType)); } //No suitable slots if (matchingEquipSlots.Count == 0) { LogFile.Log.LogEntryDebug("Can't equip item, no valid slots: " + itemToUse.SingleItemDescription, LogDebugLevel.Medium); Game.MessageQueue.AddMessage("Can't equip " + itemToUse.SingleItemDescription); return(false); } //Look for first empty slot EquipmentSlotInfo freeSlot = matchingEquipSlots.Find(x => x.equippedItem == null); if (freeSlot == null) { //Not slots free, unequip first slot Item oldItem = matchingEquipSlots[0].equippedItem; IEquippableItem oldItemEquippable = oldItem as IEquippableItem; //Sanity check if (oldItemEquippable == null) { LogFile.Log.LogEntry("Currently equipped item is not equippable!: " + oldItem.SingleItemDescription); return(false); } //Run unequip routine oldItemEquippable.UnEquip(this); oldItem.IsEquipped = false; //Can't do this right now, since not in inventory items appear on the floor //This slot is now free freeSlot = matchingEquipSlots[0]; } //We now have a free slot to equip in //Put new item in first relevant slot and run equipping routine matchingEquipSlots[0].equippedItem = itemToUse; equippableItem.Equip(this); itemToUse.IsEquipped = true; //Update the inventory listing since equipping an item changes its stackability Inventory.RefreshInventoryListing(); //Message the user LogFile.Log.LogEntryDebug("Item equipped: " + itemToUse.SingleItemDescription, LogDebugLevel.Low); Game.MessageQueue.AddMessage(itemToUse.SingleItemDescription + " equipped in " + StringEquivalent.EquipmentSlots[matchingEquipSlots[0].slotType]); return(true); }