void LoadDefaultEquipment() { foreach (ScriptableItem item in inventory) { ScriptableEquipment equipment = item as ScriptableEquipment; if (equipment != null) { equipment.Equip(this.gameObject); } } }
private void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "Player") { foreach (ScriptableItem item in itemsToPick) { other.GetComponent <CharacterInventory>().Add(item); ScriptableEquipment equipment = item as ScriptableEquipment; if (equipment != null) { if (equipOnPickup) { equipment.Equip(other.gameObject); } } } isCollected = true; Deactivate(); } }
public void Draw() { // Clean panel first foreach (Transform child in slotPanel.transform) { Destroy(child.gameObject); } // Get all the equipment slots in the equipment panel and remove all sprites initially // Since we can't have any equipped slot sprites in the beginning of the draw foreach (Transform equipSlot in equipmentPanel.transform) { Transform go = equipSlot.gameObject.transform; if (go.childCount <= 0) { continue; } Image img = go.GetChild(2).gameObject.GetComponent <Image>(); if (img) { img.sprite = null; } } // Store reference for performance List <ScriptableItem> visited = new List <ScriptableItem>(); List <GameObject> visitedButtons = new List <GameObject>(); foreach (ScriptableItem item in inventory) { GameObject itemButton = null; int visitedIndex = item.stackable ? visited.FindIndex(entry => entry.name == item.name) : -1; // Check if visited contains this item if (visitedIndex != -1) { GameObject stackCounter = FindNestedGameObjectByTag(visitedButtons[visitedIndex], UI_INVENTORY_STACK); itemButton = visitedButtons[visitedIndex]; // Count all existing items so far int count = inventory.FindAll(inventoryItem => inventoryItem.name == visited[visitedIndex].name).Count; if (stackCounter != null) { stackCounter.GetComponent <Text>().text = "x" + count; } } else { // Register item in visited list for stackable cases visited.Add(item); // Instantiate Item Button itemButton = Instantiate(itemButtonPrefab); visitedButtons.Add(itemButton); } // Icon imagery logic GameObject sprite = FindNestedGameObjectByTag(itemButton, UI_INVENTORY_IMAGE); if (sprite != null) { sprite.GetComponent <Image>().sprite = item.itemSprite; } // Parent item button to panel itemButton.transform.SetParent(slotPanel.transform); Button itemBtn = itemButton.GetComponent <Button>(); itemBtn.onClick.RemoveAllListeners(); if (item.itemType == ItemEnum.ITEM) { itemBtn.onClick.AddListener(() => { item.Consume(inventoryOwner); // Refresh inventory panel Draw(); }); } if (item.itemType == ItemEnum.EQUIPMENT || item.itemType == ItemEnum.WEAPON) { var equipment = new ScriptableEquipment(); if (item.itemType == ItemEnum.EQUIPMENT) { equipment = (ScriptableEquipment)item; } if (item.itemType == ItemEnum.WEAPON) { equipment = (ScriptableWeapon)item; } bool itemIsEquipped = currentCharacterEquipmentSlot.GetSlot(equipment.bodyPart)?.equipment == equipment; // Look for sprite gameobject to update its sprite GetSlotButton(equipment.bodyPart).transform.GetChild(2).GetComponent <Image>().sprite = itemIsEquipped ? equipment.itemSprite : null; FindNestedGameObjectByTag(itemButton, UI_INVENTORY_EQUIPPED).GetComponent <Image>().enabled = itemIsEquipped; // Add item data to draggable slot itemBtn.GetComponent <ItemData>().itemOwner = this.inventoryOwner; itemBtn.GetComponent <ItemData>().inventoryUI = this.gameObject; itemBtn.GetComponent <ItemData>().equippedItem = equipment; itemBtn.onClick.AddListener(() => { Slot slot = currentCharacterEquipmentSlot.GetSlot(equipment.bodyPart); FindNestedGameObjectByTag(itemButton, UI_INVENTORY_EQUIPPED).GetComponent <Image>().enabled = false; // Item is already equipped? if (slot?.equipment == equipment) { equipment.Unequip(inventoryOwner); Draw(); return; } // Unequip current slot before attempting to equip new item if (slot?.equipment != null) { slot.equipment.Unequip(inventoryOwner); } // Slot is free. Equip item equipment.Equip(inventoryOwner); FindNestedGameObjectByTag(itemButton, UI_INVENTORY_EQUIPPED).GetComponent <Image>().enabled = true; // Update UI to update UI_INVENTORY_EQUIPPED borders for active items Draw(); }); } } }