void LoadDefaultEquipment() { foreach (ScriptableItem item in inventory) { ScriptableEquipment equipment = item as ScriptableEquipment; if (equipment != null) { equipment.Equip(this.gameObject); } } }
public void Remove(ScriptableItem itemToRemove) { ScriptableEquipment s = itemToRemove as ScriptableEquipment; if (s != null) { // If is a weapon or armour, before destroying, attempt to unequip s.Unequip(this.gameObject); } int index = inventory.FindLastIndex(i => i == itemToRemove); inventory.RemoveAt(index); }
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(); }); } } }
public void EquipOnSlot(BodyPart bodyPart, ScriptableEquipment equipment) { switch (bodyPart) { case BodyPart.Hair: hair = null; hair = new Slot(equipment); break; case BodyPart.Head: head = null; head = new Slot(equipment); break; case BodyPart.Torso: torso = null; torso = new Slot(equipment); break; case BodyPart.ArmUpperRight: rightUpperArm = null; rightUpperArm = new Slot(equipment); break; case BodyPart.ArmLowerRight: rightLowerArm = null; rightLowerArm = new Slot(equipment); break; case BodyPart.RightHand: rightHand = null; rightHand = new Slot(equipment); break; case BodyPart.ArmUpperLeft: leftUpperArm = null; leftUpperArm = new Slot(equipment); break; case BodyPart.ArmLowerLeft: leftLowerArm = null; leftLowerArm = new Slot(equipment); break; case BodyPart.LeftHand: leftHand = null; leftHand = new Slot(equipment); break; case BodyPart.Hips: hips = null; hips = new Slot(equipment); break; case BodyPart.RightLeg: rightLeg = null; rightLeg = new Slot(equipment); break; case BodyPart.LeftLeg: leftLeg = null; leftLeg = new Slot(equipment); break; case BodyPart.RightHandWeapon: rightWeapon = null; rightWeapon = new Slot(equipment); break; default: break; } }
private void LoadSlots(SaveableSlot[] savedSlots) { hair = null; head = null; torso = null; leftUpperArm = null; leftLowerArm = null; leftHand = null; rightUpperArm = null; rightLowerArm = null; rightHand = null; hips = null; leftLeg = null; rightLeg = null; leftWeapon = null; rightWeapon = null; // We need to access the character's inventory so we can access each instance of each equipped item CharacterInventory characterInventory = this.gameObject.GetComponent <CharacterInventory>(); if (characterInventory == null) { Debug.LogError("No character inventory found. Were you trying to load slots on the wrong character?"); } // Hair string hairEquipment = savedSlots.Where(x => x.slotKey == "hair").First().currentEquipment; if (!string.IsNullOrEmpty(hairEquipment)) { ScriptableEquipment hairEquipmentInstance = characterInventory.FindByItemName(hairEquipment) as ScriptableEquipment; if (hairEquipment != null) { hair = new Slot(hairEquipmentInstance); hair.equipment.Equip(this.gameObject); } } // Head string headEquipment = savedSlots.Where(x => x.slotKey == "head").First().currentEquipment; if (!string.IsNullOrEmpty(headEquipment)) { ScriptableEquipment headEquipmentInstance = characterInventory.FindByItemName(headEquipment) as ScriptableEquipment; if (headEquipment != null) { head = new Slot(headEquipmentInstance); head.equipment.Equip(this.gameObject); } } // Torso string torsoEquipment = savedSlots.Where(x => x.slotKey == "torso").First().currentEquipment; if (!string.IsNullOrEmpty(torsoEquipment)) { ScriptableEquipment torsoEquipmentInstance = characterInventory.FindByItemName(torsoEquipment) as ScriptableEquipment; if (torsoEquipment != null) { torso = new Slot(torsoEquipmentInstance); torso.equipment.Equip(this.gameObject); } } // Left Upper Arm string leftUpperArmEquipment = savedSlots.Where(x => x.slotKey == "leftUpperArm").First().currentEquipment; if (!string.IsNullOrEmpty(leftUpperArmEquipment)) { ScriptableEquipment leftUpperArmEquipmentInstance = characterInventory.FindByItemName(leftUpperArmEquipment) as ScriptableEquipment; if (leftUpperArmEquipmentInstance != null) { leftUpperArm = new Slot(leftUpperArmEquipmentInstance); leftUpperArm.equipment.Equip(this.gameObject); } } // Left Lower Arm string leftLowerArmEquipment = savedSlots.Where(x => x.slotKey == "leftLowerArm").First().currentEquipment; if (!string.IsNullOrEmpty(leftLowerArmEquipment)) { ScriptableEquipment leftLowerArmEquipmentInstance = characterInventory.FindByItemName(leftLowerArmEquipment) as ScriptableEquipment; if (leftLowerArmEquipmentInstance != null) { leftLowerArm = new Slot(leftLowerArmEquipmentInstance); leftLowerArm.equipment.Equip(this.gameObject); } } // Left Hand string leftHandEquipment = savedSlots.Where(x => x.slotKey == "leftHand").First().currentEquipment; if (!string.IsNullOrEmpty(leftHandEquipment)) { ScriptableEquipment leftHandEquipmentInstance = characterInventory.FindByItemName(leftHandEquipment) as ScriptableEquipment; if (leftHandEquipmentInstance != null) { leftHand = new Slot(leftHandEquipmentInstance); leftHand.equipment.Equip(this.gameObject); } } // Right Upper Arm string rightUpperArmEquipment = savedSlots.Where(x => x.slotKey == "rightUpperArm").First().currentEquipment; if (!string.IsNullOrEmpty(rightUpperArmEquipment)) { ScriptableEquipment rightUpperArmEquipmentInstance = characterInventory.FindByItemName(rightUpperArmEquipment) as ScriptableEquipment; if (rightUpperArmEquipmentInstance != null) { rightUpperArm = new Slot(rightUpperArmEquipmentInstance); rightUpperArm.equipment.Equip(this.gameObject); } } // Right Lower Arm string rightLowerArmEquipment = savedSlots.Where(x => x.slotKey == "rightLowerArm").First().currentEquipment; if (!string.IsNullOrEmpty(rightLowerArmEquipment)) { ScriptableEquipment rightLowerArmEquipmentInstance = characterInventory.FindByItemName(rightLowerArmEquipment) as ScriptableEquipment; if (rightLowerArmEquipmentInstance != null) { rightLowerArm = new Slot(rightLowerArmEquipmentInstance); rightLowerArm.equipment.Equip(this.gameObject); } } // Right Hand string rightHandEquipment = savedSlots.Where(x => x.slotKey == "rightHand").First().currentEquipment; if (!string.IsNullOrEmpty(rightHandEquipment)) { ScriptableEquipment rightHandEquipmentInstance = characterInventory.FindByItemName(rightHandEquipment) as ScriptableEquipment; if (rightHandEquipmentInstance != null) { rightHand = new Slot(rightHandEquipmentInstance); rightHand.equipment.Equip(this.gameObject); } } // Hips string hipsEquipment = savedSlots.Where(x => x.slotKey == "hips").First().currentEquipment; if (!string.IsNullOrEmpty(hipsEquipment)) { ScriptableEquipment hipsEquipmentInstance = characterInventory.FindByItemName(hipsEquipment) as ScriptableEquipment; if (hipsEquipmentInstance != null) { hips = new Slot(hipsEquipmentInstance); hips.equipment.Equip(this.gameObject); } } // Left Leg string leftLegEquipment = savedSlots.Where(x => x.slotKey == "leftLeg").First().currentEquipment; if (!string.IsNullOrEmpty(leftLegEquipment)) { ScriptableEquipment leftLegEquipmentInstance = characterInventory.FindByItemName(leftLegEquipment) as ScriptableEquipment; if (leftLegEquipmentInstance != null) { leftLeg = new Slot(leftLegEquipmentInstance); leftLeg.equipment.Equip(this.gameObject); } } // Right Leg string rightLegEquipment = savedSlots.Where(x => x.slotKey == "rightLeg").First().currentEquipment; if (!string.IsNullOrEmpty(rightLegEquipment)) { ScriptableEquipment rightLegEquipmentInstance = characterInventory.FindByItemName(rightLegEquipment) as ScriptableEquipment; if (rightLegEquipmentInstance != null) { rightLeg = new Slot(rightLegEquipmentInstance); rightLeg.equipment.Equip(this.gameObject); } } // Left Weapon string leftWeaponEquipment = savedSlots.Where(x => x.slotKey == "leftWeapon").First().currentEquipment; if (!string.IsNullOrEmpty(leftWeaponEquipment)) { ScriptableEquipment leftWeaponEquipmentInstance = characterInventory.FindByItemName(leftWeaponEquipment) as ScriptableEquipment; if (leftWeaponEquipmentInstance != null) { leftWeapon = new Slot(leftWeaponEquipmentInstance); leftWeapon.equipment.Equip(this.gameObject); } } // Right Weapon string rightWeaponEquipment = savedSlots.Where(x => x.slotKey == "rightWeapon").First().currentEquipment; if (!string.IsNullOrEmpty(rightWeaponEquipment)) { ScriptableEquipment rightWeaponEquipmentInstance = characterInventory.FindByItemName(rightWeaponEquipment) as ScriptableEquipment; if (rightWeaponEquipmentInstance != null) { rightWeapon = new Slot(rightWeaponEquipmentInstance); rightWeapon.equipment.Equip(this.gameObject); } } }
public Slot(ScriptableEquipment equipment) { this.equipment = equipment; }