示例#1
0
    //Monster of a function that equips a given item to this equipment holder
    public void Equip(int itemIndex, int EquipIndex)
    {
        //Reassert everything we already know!
        #if UNITY_EDITOR || DEVELOPMENT_BUILD
        Debug.Assert(CanEquip(itemIndex, EquipIndex), "Equip Error! Equipping something should require you to be able to equip it!");
        Debug.Assert(RequiresReequip(itemIndex, EquipIndex), "Equip Error! You shouldn't be trying to re-equip something to the same slot!");
        Debug.Assert(SlotsNeededToEquip(itemIndex, EquipIndex).Count == 0, "Equip Error! You must have freed all the needed slots before equipping something!");
        Debug.Assert(!inventory[itemIndex].held[0].equipable.isEquipped, "Equip Error! Someone should have unequipped the main item already.");
        #endif

        //Setup vars
        List <int> neededSlots = new List <int>();

        //Get item
        ItemStack     item  = inventory[itemIndex];
        EquipableItem equip = item.held[0].equipable;

        //Confirm that slot is open and primary
        EquipmentSlot main = equipmentSlots[EquipIndex];
        if (!main.type.Contains(equip.primarySlot))
        {
            //TODO: Console error!
            Debug.Log("<color=red>Item equipped to wrong type of primary slot!");
            //Debug.LogError("Item equipped to wrong type of primary slot!", equip);
            return;
        }

        neededSlots.Add(EquipIndex);

        //Confirm that secondary slots are available
        foreach (EquipSlotType t in equip.secondarySlots)
        {
            bool succeeded = false;
            //See if there is a free spot that is not already in the list
            for (int i = 0; i < equipmentSlots.Count; i++)
            {
                EquipmentSlot slot = equipmentSlots[i];
                if (slot.active)
                {
                    continue;              //This skip is completely okay now! Allowed because we know we've already freed all the slots we needed
                }
                if (slot.type.Contains(t) && !neededSlots.Contains(i))
                {
                    //Found a match!
                    succeeded = true;
                    neededSlots.Add(i);
                    break;
                }
            }


            //PARANOIA - Check that we didn't somehow fail to do the thing we've asserted twice now
            if (!succeeded)
            {
                //TODO: Log console message, then fail gracefully
                Debug.LogError("Equipment has failed to equip an item - This means that CanEquip and NeededSlots did not make good on their assertions");
                return;
            }
        }

        //Quick sanity check that we have all the slots (Which is main slot + number of secondary slots)
        #if UNITY_EDITOR || DEVELOPMENT_BUILD
        Debug.Assert(neededSlots.Count == equip.secondarySlots.Count + 1, "Counts did not align correctly!", this);
        #endif

        //Attach to all slots
        foreach (int i in neededSlots)
        {
            EquipmentSlot slot = equipmentSlots[i];

            #if UNITY_EDITOR || DEVELOPMENT_BUILD
            Debug.Assert(!slot.active, $"Slot {i} should be inactive, but is is still set active. Slot was either incorrectly added to list, or was was not unequipped correctly.");
            //Debug.Assert(slot.equipped == null, $"Slot still has an item in it! We're going to just override now, but that item was something?", slot.equipped.held[0]);
            #endif

            slot.equipped  = item;
            slot.active    = true;
            slot.removable = equip.removable;
        }

        //Fire off equip function
        equip.OnEquip(monster);
        //Done!
    }