public void AddItemToInventory(GameItem item) { Inventory.Add(item); //if item is unique, we add it to grouped inventory with quantity of 1 if (item.IsUnique) { GroupedInventory.Add(new GroupedInventoryItem(item, 1)); } //if not unique else { //first check to see if it already exists in inventory if (!GroupedInventory.Any(gi => gi.Item.ItemTypeID == item.ItemTypeID)) { //if not, add it as groupedinventory item with quantity of 0, as we will increase quantity anyway always below GroupedInventory.Add(new GroupedInventoryItem(item, 0)); } GroupedInventory.First(gi => gi.Item.ItemTypeID == item.ItemTypeID).Quantity++; } OnPropertyChanged(nameof(Weapons)); OnPropertyChanged(nameof(Consumables)); OnPropertyChanged(nameof(HasConsumable)); }
public void RemoveItemFromInventory(GameItem item) { Inventory.Remove(item); GroupedInventoryItem groupedInventoryItemToRemove = item.IsUnique ? GroupedInventory.FirstOrDefault(gi => gi.Item == item) : // if true GroupedInventory.FirstOrDefault(gi => gi.Item.ItemTypeID == item.ItemTypeID); // if false // Conditional operator :? (ternary operator) => condition ? consequent : alternative // similar to if/else. It evaluates a Bool expression and returns the result of one of the two expressions (one is for = true, one is for =false) // source: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator if (groupedInventoryItemToRemove != null) { if (groupedInventoryItemToRemove.Quantity == 1) { GroupedInventory.Remove(groupedInventoryItemToRemove); } else { groupedInventoryItemToRemove.Quantity--; } } OnPropertyChanged(nameof(Weapons)); }
public void RemoveItemFromInventory(GameItem item) { Inventory.Remove(item); //gets first item from groupedinventory where the item matches what we've passed in //if unique, gets the exact item //if not unique, just the ID so that quests can still remove multiple GroupedInventoryItem groupedInventoryItemToRemove = item.IsUnique ? GroupedInventory.FirstOrDefault(gi => gi.Item == item) : GroupedInventory.FirstOrDefault(gi => gi.Item.ItemTypeID == item.ItemTypeID); //check we actually got something, should not be null but safety check if (groupedInventoryItemToRemove != null) { //if item's quantity is 1, just remove it if (groupedInventoryItemToRemove.Quantity == 1) { GroupedInventory.Remove(groupedInventoryItemToRemove); } //if quantity is not 1, just lower quantity by 1 else { groupedInventoryItemToRemove.Quantity--; } } OnPropertyChanged(nameof(Weapons)); OnPropertyChanged(nameof(Consumables)); OnPropertyChanged(nameof(HasConsumable)); }
public void RemoveItemFromInventory(GameItem item) { Inventory.Remove(item); //Checking to see if the item is unique or not GroupedInventoryItem groupedInventoryItemToRemove = item.IsUnique ? GroupedInventory.FirstOrDefault(gi => gi.Item == item) : GroupedInventory.FirstOrDefault(gi => gi.Item.ItemTypeID == item.ItemTypeID); if (groupedInventoryItemToRemove != null) { // Check to see if the item to remove from player's inventory isn't null if (groupedInventoryItemToRemove.Quantity == 1) { // If the item in question has a quantity of one, remove it from the screen GroupedInventory.Remove(groupedInventoryItemToRemove); } else { // Otherwise just decrease the quantity by one groupedInventoryItemToRemove.Quantity--; } } OnPropertyChanged(nameof(Weapons)); }
public void RemoveItemFromInventory(GameItem item) { Inventory.Remove(item); //get the first item from groupinventory where item id matches item id of item we want to remove //If item we want to remove is unique, we have to find that exact matching item //If not, there's no distinction between the individuzl items. //This is a ternary operator - if first part evaluates to true, the first statement is executed. GroupedInventoryItem groupedInventoryItemToRemove = item.IsUnique ? GroupedInventory.FirstOrDefault(gi => gi.Item == item) : GroupedInventory.FirstOrDefault(gi => gi.Item.ItemTypeID == item.ItemTypeID); if (groupedInventoryItemToRemove != null) //should never be null but good to check { //does object have quantity of 1? //if so, completely remove item from groupedinventory. if (groupedInventoryItemToRemove.Quantity == 1) { GroupedInventory.Remove(groupedInventoryItemToRemove); } else { //decrease quantity by 1 groupedInventoryItemToRemove.Quantity--; } } OnPropertyChanged(nameof(Weapons)); }
public void RemoveItemQuantity(ItemQuantity itemQuantity) { GameItem itemToRemove = ItemFactory.CreateGameItem(itemQuantity.ItemId); for (int i = 0; i < itemQuantity.Quantity; i++) { Inventory.Remove(itemToRemove); } GroupedInventory.FirstOrDefault(gi => gi.Item.ItemId == itemToRemove.ItemId).Quantity -= itemQuantity.Quantity; OnPropertyChanged(nameof(Weapons)); }
//methods public void AddItemToInventory(GameItem item) { Inventory.Add(item); if (item.IsUnique || !GroupedInventory.Any(gi => gi.Item.ItemId == item.ItemId)) { GroupedInventory.Add(new GroupedInventoryItem(item, 1)); } else { GroupedInventory.FirstOrDefault(gi => gi.Item.ItemId == item.ItemId).Quantity++; } OnPropertyChanged(nameof(Weapons)); }
public void RemoveItemFromInventory(GameItem item) { Inventory.Remove(item); GroupedInventoryItem itemToRemove = GroupedInventory.FirstOrDefault(gi => gi.Item.ItemId == item.ItemId); if (itemToRemove.Quantity <= 1) { GroupedInventory.Remove(itemToRemove); } else { itemToRemove.Quantity--; } OnPropertyChanged(nameof(Weapons)); }
public void AddItemToInventory(GameItem item) { Inventory.Add(item); if (item.IsUnique) { GroupedInventory.Add(new GroupedInventoryItem(item, 1)); } else { if (!GroupedInventory.Any(gi => gi.Item.ItemTypeID == item.ItemTypeID)) { GroupedInventory.Add(new GroupedInventoryItem(item, 0)); } GroupedInventory.First(gi => gi.Item.ItemTypeID == item.ItemTypeID).Quantity++; } OnPropertyChanged(nameof(Weapons)); }
public void RemoveItemFromInventory(GameItem item) { Inventory.Remove(item); GroupedInventoryItem groupedInventoryItemToRemove = item.IsUnique ? GroupedInventory.FirstOrDefault(gi => gi.Item == item):GroupedInventory.FirstOrDefault(gi => gi.Item.ItemTypeID == item.ItemTypeID); if (groupedInventoryItemToRemove != null) { if (groupedInventoryItemToRemove.Quantity == 1) { GroupedInventory.Remove(groupedInventoryItemToRemove); } else { groupedInventoryItemToRemove.Quantity--; } } OnPropertyChanged(nameof(Weapons)); }
public void RemoveItemFromInventory(GameItem item) // TODO: Overload with GroupedInventoryItem { Inventory.Remove(item); GroupedInventoryItem groupedInventoryItemToRemove = item.IsUnique ? GroupedInventory.FirstOrDefault(gi => gi.Item == item) : GroupedInventory.FirstOrDefault(gi => gi.Item.ItemTypeID == item.ItemTypeID); if (groupedInventoryItemToRemove.Quantity == 1) { GroupedInventory.Remove(groupedInventoryItemToRemove); } else { groupedInventoryItemToRemove.Quantity--; } OnPropertyChanged(nameof(Weapons)); OnPropertyChanged(nameof(Consumables)); OnPropertyChanged(nameof(HasConsumable)); }
public void AddItemToInventory(GameItem item) { Inventory.Add(item); if (item.IsUnique) { // If the game item is unique, it is added in GroupedInventory.Add(new GroupedInventoryItem(item, 1)); } else { if (!GroupedInventory.Any(gi => gi.Item.ItemTypeID == item.ItemTypeID)) { //if the game item isn't unique, it's added in too with a quantity of 0 GroupedInventory.Add(new GroupedInventoryItem(item, 0)); } // non-uniques have their quantity increased by one GroupedInventory.First(gi => gi.Item.ItemTypeID == item.ItemTypeID).Quantity++; } OnPropertyChanged(nameof(Weapons)); }
public void AddItemToInventory(GameItem item) { Inventory.Add(item); //if item is unique, add a new groupedinventory item with quantity 1. if (item.IsUnique) { GroupedInventory.Add(new GroupedInventoryItem(item, 1)); } else { //Check if this is the first one of this item that the player has if (!GroupedInventory.Any(gi => gi.Item.ItemTypeID == item.ItemTypeID)) { //quantity 0 because next line adds 1 to quantity GroupedInventory.Add(new GroupedInventoryItem(item, 0)); } GroupedInventory.First(gi => gi.Item.ItemTypeID == item.ItemTypeID).Quantity++; } OnPropertyChanged(nameof(Weapons)); }