public void ClearSlot(TopDownUIItemSlot slot)
 {
     if (slot != null)
     {
         if (slot.itemInSlot != null)
         {
             slot.itemInSlot.slotOfThisItem = null;
             slot.itemInSlot = null;
         }
         slot.itemIcon.sprite  = null;
         slot.itemIcon.enabled = false;
     }
 }
 /// <summary>
 /// Moves an item to first free inventory slot.
 /// </summary>
 /// <param name="item">Item to move.</param>
 public void MoveItemToInventory(TopDownUIItemSlot item)
 {
     for (int i = 0; i < slots.Length; i++)
     {
         if (slots[i].itemInSlot == null)
         {
             items.Add(item.itemInSlot);
             slots[i].AddItemToSlot(item.itemInSlot);
             if (item.slottedInQuick != null)
             {
                 slots[i].slottedInQuick          = item.slottedInQuick;
                 item.slottedInQuick.originalSlot = slots[i];
                 item.slottedInQuick = null;
             }
             return;
         }
     }
 }
    public void Update()
    {
        if (holdingItemSlot.GetComponent <CanvasGroup>().alpha == 1f)
        {
            Vector2 tmp     = Input.mousePosition;
            Vector2 namePos = new Vector2(tmp.x + holdingSlotOffset.x, tmp.y + holdingSlotOffset.y);
            holdingItemSlot.transform.position = namePos;
        }

        if (previousSlot != null)
        {
            if (TopDownUIManager.instance.checkUi.IsPointerOverUIObject() == false)
            {
                if (Input.GetKeyDown(TopDownInputManager.instance.interactKey))
                {
                    if (previousSlot.slotType == SlotType.Inventory)
                    {
                        slots[0].ClearSlot(previousSlot);
                        holdingItem = null;
                        holdingItemSlot.GetComponent <TopDownUIHoldingItemSlot>().itemIconImage.sprite = null;
                        holdingItemSlot.GetComponent <CanvasGroup>().alpha = 0f;
                        previousSlot = null;
                        StartCoroutine(ClickedOutOfUiTimer());
                        //PLAY DESTROY ITEM SOUND
                    }
                    else if (previousSlot.slotType == SlotType.Quickslot)
                    {
                        previousSlot.GetComponent <TopDownUIQuickSlot>().originalSlot.slottedInQuick = null;
                        slots[0].ClearSlot(previousSlot);
                        holdingItem = null;
                        holdingItemSlot.GetComponent <TopDownUIHoldingItemSlot>().itemIconImage.sprite = null;
                        holdingItemSlot.GetComponent <CanvasGroup>().alpha = 0f;
                        previousSlot = null;
                        StartCoroutine(ClickedOutOfUiTimer());
                    }
                }
            }
        }
    }
    public void Update()
    {
        if (itemInSlot != null)
        {
            if (slotActivateKey.ToString().Contains("Alpha"))
            {
                string tmp  = slotActivateKey.ToString();
                string tmp1 = tmp.Replace("Alpha", "");
                keyText.text = tmp1;
            }

            if (Input.GetKeyDown(slotActivateKey))
            {
                if (itemInSlot.weaponHoldingType == WeaponHoldingType.TwoHanded)
                {
                    if (TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[3].itemInSlot != null)
                    {
                        TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[3].UseSlottedItem();
                        print(TopDownUIInventory.instance.currentEquipmentManager.gameObject.name);

                        TopDownUIInventory.instance.MoveItemToInventory(TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[3]);
                        ClearSlot(TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[3]);
                    }
                }

                //If item we are trying to equip is shield, we want to check if there is a two handed weapon equipped and to deequip it
                if (itemInSlot.itemType == ItemType.Shield)
                {
                    if (TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[2].itemInSlot != null && TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[2].itemInSlot.weaponHoldingType == WeaponHoldingType.TwoHanded)
                    {
                        TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[2].UseSlottedItem();
                        print(TopDownUIInventory.instance.currentEquipmentManager.gameObject.name);

                        TopDownUIInventory.instance.MoveItemToInventory(TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[2]);
                        ClearSlot(TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[2]);
                    }
                }

                if (TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[(int)itemInSlot.itemType] != null)
                {
                    if (TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[(int)itemInSlot.itemType].itemInSlot == null)
                    {
                        TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[(int)itemInSlot.itemType].AddItemToSlot(itemInSlot);
                        TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[(int)itemInSlot.itemType].slottedInQuick = this;
                        TopDownUIInventory.instance.RemoveItem(originalSlot.itemInSlot);
                        originalSlot.slottedInQuick = null;
                        ClearSlot(originalSlot);
                        originalSlot = TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[(int)itemInSlot.itemType];
                    }
                    else if (TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[(int)itemInSlot.itemType].itemInSlot == itemInSlot)
                    {
                        TopDownUIInventory.instance.MoveItemToInventory(TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[(int)itemInSlot.itemType]);
                        ClearSlot(TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[(int)itemInSlot.itemType]);
                    }
                    else if (TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[(int)itemInSlot.itemType].itemInSlot != itemInSlot)
                    {
                        //first we need to unequip old item and move it to inventory
                        //then equip new item
                        TopDownUIInventory.instance.MoveItemToInventory(TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[(int)itemInSlot.itemType]);
                        TopDownUIInventory.instance.currentEquipmentManager.UnequipItem(TopDownUIInventory.instance.currentEquipmentManager.currentEquipment[(int)itemInSlot.itemType]);
                        ClearSlot(TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[(int)itemInSlot.itemType]);

                        TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[(int)itemInSlot.itemType].AddItemToSlot(itemInSlot);
                        TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[(int)itemInSlot.itemType].slottedInQuick = this;
                        TopDownUIInventory.instance.RemoveItem(originalSlot.itemInSlot);

                        originalSlot.slottedInQuick = null;
                        ClearSlot(originalSlot);
                        originalSlot = TopDownUIInventory.instance.currentEquipmentSlots.equipmentSlots[(int)itemInSlot.itemType];
                    }
                }
                else
                {
                    if (itemInSlot.itemType == ItemType.Scroll)
                    {
                        if (TopDownAudioManager.instance.spellUseAudio != null)
                        {
                            Instantiate(TopDownAudioManager.instance.spellUseAudio, Vector3.zero, Quaternion.identity);
                        }

                        TopDownCharacterManager.instance.controllingCharacter.GetComponent <TopDownRpgSpellcaster>().activeSpell   = itemInSlot;
                        TopDownCharacterManager.instance.controllingCharacter.GetComponent <TopDownRpgSpellcaster>().spellItemSlot = originalSlot;
                        TopDownCharacterManager.instance.controllingCharacter.GetComponent <TopDownRpgSpellcaster>().castingSpell  = true;

                        if (TopDownUIManager.instance.charInfoPanel.inventoryActive)
                        {
                            if (TopDownUIManager.instance.inventory.GetComponent <CanvasGroup>().alpha > 0)
                            {
                                TopDownUIManager.instance.SetUIState(TopDownUIManager.instance.inventory);
                            }
                        }
                        if (TopDownUIManager.instance.charInfoPanel.questLogActive)
                        {
                            if (TopDownUIManager.instance.questLog.GetComponent <CanvasGroup>().alpha > 0)
                            {
                                TopDownUIManager.instance.SetUIState(TopDownUIManager.instance.questLog);
                            }
                        }
                    }
                }


                UseSlottedItem();
                //print(TopDownUIInventory.instance.currentEquipmentManager.gameObject.name);
                //We need to move item from inventory slot to equipment slot
            }
        }
        else
        {
            keyText.text = string.Empty;
        }
    }
    public IEnumerator CastSpell()
    {
        if (spellItemSlot != null)
        {
            if (spellItemSlot.slottedInQuick != null)
            {
                spellItemSlot.slottedInQuick.ClearSlot(spellItemSlot.slottedInQuick);
                spellItemSlot.slottedInQuick = null;
            }

            spellItemSlot.ClearSlot(spellItemSlot);
        }

        animator.Play(activeSpell.castingSpellAnimation);

        yield return(new WaitForSeconds(activeSpell.animationTriggerTime));

        if (activeSpell.spellCastSfx != null)
        {
            Instantiate(activeSpell.spellCastSfx, Vector3.zero, Quaternion.identity);
        }

        if (activeSpell.spellType == SpellType.CastOnEnemy)
        {
            GameObject fx = Instantiate(activeSpell.spellFx as GameObject);
            fx.transform.SetParent(transform);
            fx.transform.localPosition = new Vector3(0f, gameObject.GetComponent <CapsuleCollider>().center.y, 0f);
            fx.transform.SetParent(null);

            fx.GetComponent <Rigidbody>().velocity = (target.transform.position - transform.position).normalized * speed;

            if (fx.GetComponent <TopDownRpgSpellCollision>() == null)
            {
                TopDownRpgSpellCollision spellCol = fx.AddComponent <TopDownRpgSpellCollision>();
                spellCol.thisSpell     = activeSpell;
                spellCol.thisSpellType = activeSpell.spellType;
            }
        }
        else if (activeSpell.spellType == SpellType.CastOnAlly)
        {
            GameObject fx = Instantiate(activeSpell.spellFx as GameObject);
            fx.transform.SetParent(transform);
            fx.transform.localPosition = new Vector3(0f, gameObject.GetComponent <CapsuleCollider>().center.y, 0f);
            fx.transform.SetParent(null);

            fx.GetComponent <Rigidbody>().velocity = (target.transform.position - transform.position).normalized * speed;

            if (fx.GetComponent <TopDownRpgSpellCollision>() == null)
            {
                TopDownRpgSpellCollision spellCol = fx.AddComponent <TopDownRpgSpellCollision>();
                spellCol.thisSpell     = activeSpell;
                spellCol.thisSpellType = activeSpell.spellType;
            }
        }

        castingSpell = false;

        tdcInteract.RemoveFocus();

        previousTarget = target;
        target         = null;

        tdcCard.energy -= activeSpell.castingCost;

        activeSpell   = null;
        spellItemSlot = null;
    }