Esempio n. 1
0
    public void Initialize(Item newItem, int count, ItemSlotHandler handler)
    {
        myHandler = handler;

        if (newItem != null)
        {
            myItem     = newItem;
            this.count = count;
        }

        if (myItem != null)
        {
            nameText.text = myItem.name;
            if (count > 1)
            {
                countText.text = "x" + count;
            }
            else
            {
                countText.text = "";
            }


            if (myItem is WeaponItem)
            {
                WeaponItem item = myItem as WeaponItem;

                if (item.weaponClass == WeaponClass.Primary)
                {
                    GetComponent <Image>().color = Utilities.PRIMARY_ITEM_COLOR;
                }
                if (item.weaponClass == WeaponClass.Secondary)
                {
                    GetComponent <Image>().color = Utilities.SECONDARY_ITEM_COLOR;
                }
                if (item.weaponClass == WeaponClass.Tertiary)
                {
                    GetComponent <Image>().color = Utilities.TERTIARY_ITEM_COLOR;
                }
            }
            else if (myItem is Accessory)
            {
                Accessory item = myItem as Accessory;

                GetComponent <Image>().color = Utilities.UPGRADE_ITEM_COLOR;
            }
        }
    }
    public void TransferItemAutomatically()
    {
        GameObject targetPanelInventorySlots = null;

        // check if its in the inventory or anywhere else, and depending on that were to put it now

        if (gameObject.transform.parent.parent.parent.gameObject.name == "PanelInventory")
        {
            // the clicked item is in the inventory, therefore transfer item in the autoTransferTargetParent's slots
            // which should be set according to the opened canvas

            targetPanelInventorySlots = autoTransferTargetParent.gameObject;
        }
        else
        {
            // the clicked item is not in the inventory but in a itemslot somewhere else. it shall be transferred to the inventory.

            foreach (Transform child in GameObject.Find("PanelInventory").transform)
            {
                if (child.gameObject.name == "PanelInventorySlots")
                {
                    targetPanelInventorySlots = child.gameObject;
                }
            }
        }



        ItemSlotHandler targetItemSlot     = null;
        bool            itemMergeSlotFound = false;

        //  step 1: iterate through all fitting slots and see if there's any one which already has the correct ingredient inside so they can be merged,


        //check if the item to be moved has exactly 1 ingredient

        if (gameObject.GetComponent <InventoryItemHandler>().ingredientTypes.Count == 1)
        {
            foreach (Transform child in targetPanelInventorySlots.transform)
            {
                //check if slot really is a slot
                if (child.GetComponent <ItemSlotHandler>() != null)
                {
                    //check if the slot has an item
                    if (child.transform.childCount == 1)
                    {
                        // check if the item in the slot has exactly 1 ingredient
                        if (child.transform.GetChild(0).gameObject.GetComponent <InventoryItemHandler>().ingredientTypes.Count == 1)
                        {
                            //check if the ingredients match
                            if (child.transform.GetChild(0).gameObject.GetComponent <InventoryItemHandler>().ingredientTypes[0] == gameObject.GetComponent <InventoryItemHandler>().ingredientTypes[0])
                            {
                                //check that the item in slot is not the exact same item we want to reposition
                                if (child.transform.GetChild(0) != gameObject.transform)
                                {
                                    targetItemSlot     = child.gameObject.gameObject.GetComponent <ItemSlotHandler>();
                                    itemMergeSlotFound = true;

                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }

        // step 2: iterate through all fitting slots and see if there is a free slot (if there is no slot for merging)
        if (!itemMergeSlotFound)
        {
            foreach (Transform child in targetPanelInventorySlots.transform)
            {
                //check if thr loop comes across the origin slot before it reaches any free slot --> in that case stop right there and don't switch anything.
                // this only works before we do not even reach this loop if we found something appicable for a merge. That would have a higher priority.

                if (child.transform == gameObject.transform.parent)
                {
                    return;
                }

                //check if slot is free
                if (child.transform.childCount == 0)
                {
                    //check if slot really is a slot
                    if (child.GetComponent <ItemSlotHandler>() != null)
                    {
                        targetItemSlot = child.gameObject.gameObject.GetComponent <ItemSlotHandler>();
                        break;
                    }
                }
            }
        }



        // step 3: move item into target slot
        if (targetItemSlot != null)
        {
            Debug.Log("target Itemslot for instant transfer set");


            targetItemSlot.PutItemIntoSlot(gameObject.GetComponent <InventoryItemHandler>());
        }
        else
        {
            Debug.Log("no applicable itemslot for instant transfer found");
        }
    }
Esempio n. 3
0
 public void ChangeHandler(ItemSlotHandler newHandler)
 {
     myHandler = newHandler;
 }