/// <summary> /// Replaces a item /// </summary> /// <param name="name"></param> public InventoryItem_Base ReplaceItem(InventoryItem_Base item, int slot) { if (!(item is InventoryItem_Equipment)) { return(null); } InventoryItem_Equipment itemAsEquip = item as InventoryItem_Equipment; if (!CheckIfSlotAcceptsItem(itemAsEquip, slot)) { return(null); } if (CheckIfItemAlreadyInEquip(itemAsEquip)) { return(null); } var oldSlot = Slots[slot].FirstItem == null ? new InventoryItem_Base() { Name = "empty" } : Slots[slot].FirstItem.Clone() as InventoryItem_Base; Slots[slot].Remove(item); Slots[slot].AddItem(item); if (ItemAdded != null) { ItemAdded(this, new InventoryEventsArgs(item)); } return(oldSlot); }
public static void MoveFromInventoryToEquipment(Transform inv, Transform equip) { var slotIdInv = Int32.Parse(inv.name.Split('(')[1].Split(')')[0]); //Naming convention of slot is mandatory: f.e. Slot (1) var slotIdEquip = Int32.Parse(equip.name.Split('(')[1].Split(')')[0]); //Naming convention of slot is mandatory: f.e. Slot (1) InventoryItem_Base itemFromEquip = null; if (Inventory.Instance.Slots[slotIdInv].FirstItem != null) { itemFromEquip = Equipment.Instance.ReplaceItem(Inventory.Instance.Slots[slotIdInv].FirstItem.Clone() as InventoryItem_Base, slotIdEquip); } else { itemFromEquip = Equipment.Instance.RemoveItem(slotIdEquip); } if (itemFromEquip != null && itemFromEquip.Name == "empty")//Slot was empty { Inventory.Instance.RemoveItem(slotIdInv); return; } else if (itemFromEquip != null) { Inventory.Instance.RemoveItem(slotIdInv); Inventory.Instance.AddItem(itemFromEquip.Name, slotIdInv); } else { Debug.Log("The slot did not accept this item"); } }
/// <summary> /// Add a item to the inventory /// </summary> /// <param name="name"></param> public void AddItem(string name) { InventoryItem_Base item = null; item = GetItem(name); if (item == null) { Debug.Log("The item " + name + "does not exist in available items."); } InventorySlot freeSlot = FindStackableSlot(item); if (freeSlot == null) { freeSlot = FindNextEmptySlot(); } if (freeSlot != null) { freeSlot.AddItem(item); if (ItemAdded != null) { ItemAdded(this, new InventoryEventsArgs(item)); } } }
public Transform GetMatchingSlot(InventoryItem_Base item) { int fittingSlot = -1; for (int slot = 0; slot < SLOTS; slot++) { if (CheckIfSlotAcceptsItem(item as InventoryItem_Equipment, slot)) { //Save first slot for item change if (fittingSlot == -1) { fittingSlot = slot; } //First slot which is empty if (Slots[slot].FirstItem == null) { fittingSlot = slot; break; } } } return(Transform.GetChild(fittingSlot)); }
/// <summary> /// Event is called when a Item is selected in the Inventory /// </summary> private void OnItemSelected() { InventoryItem_Base item = Slots.Where(s => s.Id == currentSelectedSlot).First().FirstItem; if (ItemSelected != null) { ItemSelected(this, new InventoryEventsArgs(item)); } }
/// <summary> /// Finds the next stackable slot for an item /// </summary> /// <param name="item"></param> /// <returns></returns> private InventorySlot FindStackableSlot(InventoryItem_Base item) { foreach (InventorySlot slot in Slots) { if (slot.IsStackable(item)) { return(slot); } } return(null); }
/// <summary> /// Use the current selected item /// </summary> public void UseSelectedItem() { InventoryItem_Base item = null; if (currentSelectedSlot != -1) { item = RemoveItem(currentSelectedSlot); } if (item != null && ItemUsed != null) { ItemUsed(this, new InventoryEventsArgs(item)); } }
/// <summary> /// Gets the InventoryItem of an type /// </summary> /// <param name="type"></param> /// <returns></returns> private InventoryItem_Base GetItem(string type) { InventoryItem_Base foundItem = null; try { foundItem = (InventoryItem_Base)AvailableItems.First(it => it.Name == type).Clone(); } catch (Exception ex) { Debug.Log("Item not found: " + type); throw ex; } return(foundItem); }
/// <summary> /// Check if a Item could be added or if stack already reached max size /// </summary> /// <param name="item"></param> /// <returns></returns> public bool IsStackable(InventoryItem_Base item) { if (IsEmpty || item.StackCount == ItemStack.Count) { return(false); } InventoryItem_Base first = ItemStack.Peek(); if (first.Name == item.Name) { return(true); } return(false); }
/// <summary> /// Remove a item from the stack /// </summary> /// <param name="item"></param> /// <returns></returns> public bool Remove(InventoryItem_Base item) { if (IsEmpty) { return(false); } InventoryItem_Base first = ItemStack.Peek(); if (first.Name == item.Name) { ItemStack.Pop(); return(true); } return(false); }
/// <summary> /// Set the slot to the slot information /// </summary> /// <param name="item"></param> public void SetSlot(InventoryItem_Base item) { if (item == null || item.Slot == null) { return; } Transform slot = transform.GetChild(item.Slot.Id); Transform imageTransform = slot.Find("Border").Find("ItemImage"); Transform textTransform = slot.Find("Border").Find("Text"); Text HoverText = slot.Find("HoverMenu").Find("Text").GetComponent <Text>(); Image image = imageTransform.GetComponent <Image>(); Text txtCount = textTransform.GetComponent <Text>(); int itemCount = item.Slot.Count; image.enabled = true; image.sprite = Resources.Load <Sprite>(item.Name + "Icon"); HoverText.text = item.GetHoverMenue(); if (itemCount == 0) //No more items in slot { image.enabled = false; image.sprite = null; HoverText.text = ""; } else if (itemCount == 1) //First or last item in slot { if (OverideText) { txtCount.text = ""; } } else { if (OverideText) { txtCount.text = itemCount.ToString(); } } }
/// <summary> /// Add item on positon /// </summary> /// <param name="name"></param> public void AddItem(string name, int id) { InventoryItem_Base item = null; item = GetItem(name); if (item == null) { Debug.Log("The item " + name + "does not exist in available items."); } InventorySlot freeSlot = Slots[id]; if (freeSlot != null) { freeSlot.AddItem(item); if (ItemAdded != null) { ItemAdded(this, new InventoryEventsArgs(item)); } } }
/// <summary> /// Checks which Focus was set in the UI and adds the found random item to the inventory. /// </summary> public void Scavenge() { var benefits = Equipment.Instance.EquipmentBenefits; FocusType searchFocus = FocusType.None; if (drinkToggle.isOn) { searchFocus = FocusType.Drink; } else if (foodToggle.isOn) { searchFocus = FocusType.Food; } else if (healthToggle.isOn) { searchFocus = FocusType.Health; } else if (equipToggle.isOn) { searchFocus = FocusType.Equipment; } if (!Player.playerInstance.Scavange(searchFocus == FocusType.None ? 1 : 2)) { Debug.Log("No AP for Scavange available"); return; } for (int i = 0; i < Equipment.Instance.EquipmentBenefits.ScavengeneItemMultiplier + 1; i++) { InventoryItem_Base foundItem = getRandomItem(searchFocus); Inventory.Instance.AddItem(foundItem.Name); } }
public InventoryEventsArgs(InventoryItem_Base item) { Item = item; }
/// <summary> /// Adds a item to the stack /// </summary> /// <param name="item"></param> public void AddItem(InventoryItem_Base item) { item.Slot = this; ItemStack.Push(item); }