Ejemplo n.º 1
0
    public void GenerateSlots()
    {
        slots.Clear();
        int childAmount = UiItemsContainer.childCount;

        for (int i = 0; i < childAmount; i++)
        {
            DestroyImmediate(UiItemsContainer.GetChild(0).gameObject);
        }
        for (int i = 0; i < slotAmount; i++)
        {
            UiSlotInfo temp = Instantiate(slotPrefab, UiItemsContainer).GetComponent <UiSlotInfo>();
            temp.item = emptyItem;
            temp.Setup();
            slots.Add(temp);
        }
    }
Ejemplo n.º 2
0
    public bool AddItemToSlot(Item item, UiSlotInfo slot)
    {
        if (item.IsStackable)
        {
            if (slot.item.ItemName == item.ItemName)
            {
                if (slot.item.CurrentStack + item.CurrentStack <= slot.item.MaxStack)
                {
                    slot.item.CurrentStack += item.CurrentStack;
                    AddedItem();
                    return(true);
                }
            }
        }

        if (slot.item == emptyItem)
        {
            slot.item = item.DeepCopy();
            AddedItem();
            return(true);
        }

        return(false);
    }
Ejemplo n.º 3
0
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.I))
        {
            AddItem(testItem);
        }
        int input = GetInput();

        if (input != 999)
        {
            AddItemToSlot(testItem, input);
            if (input < slots.Count)
            {
                ItemSelection.position = slots[input].GetComponent <RectTransform>().position;
                selectedSlot           = slots[input];
            }
        }

        if (Input.GetMouseButtonDown(0))
        {
            /*if(Physics.Raycast(Cam.ScreenPointToRay(Input.mousePosition), out RaycastHit hit))
             * {
             *  if(hit.transform.GetComponent<ItemInfo>())
             *  {
             *      PickUpItem(hit.transform.GetComponent<ItemInfo>());
             *  }
             *  print(hit.transform.name);
             * }*/

            if (!startedDragging)
            {
                for (int i = 0; i < slots.Count; i++)
                {
                    RectTransform slotTransform = slots[i].GetComponent <RectTransform>();
                    if (isInsideBox(slotTransform.position, slotTransform.sizeDelta * canvas.scaleFactor, Input.mousePosition))
                    {
                        if (slots[i].item != emptyItem)
                        {
                            startedDragging = true;

                            clickedItem   = slots[i].item.DeepCopy();
                            clickedSlot   = slots[i];
                            slots[i].item = emptyItem;
                        }
                    }
                }
            }
            else
            {
                OnDrop();
                startedDragging = false;
                onDragOnce      = false;
            }
        }

        if (Input.GetMouseButtonDown(1))
        {
            if (!startedDragging)
            {
                for (int i = 0; i < slots.Count; i++)
                {
                    RectTransform slotTransform = slots[i].GetComponent <RectTransform>();
                    if (isInsideBox(slotTransform.position, slotTransform.sizeDelta * canvas.scaleFactor, Input.mousePosition))
                    {
                        if (slots[i].item != emptyItem)
                        {
                            startedDragging = true;

                            clickedItem = slots[i].item.DeepCopy();
                            clickedSlot = slots[i];
                            clickedItem.CurrentStack = 1;

                            if (slots[i].item.CurrentStack > 1)
                            {
                                slots[i].item.CurrentStack--;
                            }
                            else
                            {
                                slots[i].item = emptyItem;
                            }
                        }
                    }
                }
            }
            else
            {
                OnDrop();
                startedDragging = false;
                onDragOnce      = false;
            }
        }

        if (startedDragging)
        {
            OnDrag();
        }
    }