Пример #1
0
    private void CreateUIItemSlot(int index, Inventory.InventorySlot invSlot)
    {
        UIItemSlot slot = Instantiate(itemSlotExample, contentView).GetComponent <UIItemSlot>();

        slot.Set(invSlot);
        slot.name = "[" + invSlot.item.name + "]";
        slot.gameObject.SetActive(true);
    }
Пример #2
0
    public void Update()
    {
        if (!isActive)
        {
            return;
        }

        //if active drag the transform with the mouse
        var screenpoint = Input.mousePosition;

        // aperently mouse position is screen point
        this.transform.position = screenpoint;
        //this should work

        if (Input.GetButtonUp("Interact"))
        //release
        {
            isActive = false;

            //use drop position as raycast position
            pointer.position = transform.position;

            //snap back
            this.transform.position = snapback;
            List <RaycastResult> res = new List <RaycastResult>();

            CameraReference.Instance.canvas.GetComponent <GraphicRaycaster>().Raycast(pointer, res);

            GameObject gameObject   = null;
            bool       hitSlot      = false;
            bool       hitInventory = false;

            foreach (RaycastResult result in res)
            {
                if (result.gameObject.transform.CompareTag("ItemSlot"))
                {
                    gameObject = result.gameObject;
                    hitSlot    = true;
                }
                if (result.gameObject.transform.CompareTag("Inventory"))
                {
                    hitInventory = true;
                }
            }

            if (hitSlot)
            {
                //place the item in inventory
                Inventory.Swap(this.transform, gameObject.transform);
            }
            else if (!hitInventory)
            {
                //try to drop this item.
                Inventory.InventorySlot slot = Inventory.Instance.FindSlot(this.transform);
                Inventory.Drop(slot, CameraReference.Instance.camera.ScreenToWorldPoint(Input.mousePosition));
            }
        }
    }
Пример #3
0
 public void UpdateUI(Inventory.InventorySlot slot)
 {
     UpdateUI();
 }
Пример #4
0
 public void Set(Inventory.InventorySlot inventorySlot)
 {
     this.inventorySlot = inventorySlot;
     nameLabel.text     = inventorySlot.item.name;
     amountInput.text   = inventorySlot.amount.ToString();
 }
        void MakeItemButton(SelectableElement element, Inventory.InventorySlot slot, Inventory forInventory, Inventory linkedInventory, int maxButtons, int uiIndex, int otherIndex)
        {
            string display = slot.item.itemName + " ( x" + slot.count + " )";

            MakeButton(element, display, new object[] { slot.item, forInventory, linkedInventory, maxButtons, uiIndex, otherIndex });
        }
Пример #6
0
    private void RefreshInventoryItems()
    {
        foreach (Transform child in itemSlotContainer)
        {
            if (child == itemSlotTemplate)
            {
                continue;
            }
            Destroy(child.gameObject);
        }

        int   x = 0;
        int   y = 0;
        float itemSlotCellSize = 54f;

        foreach (Inventory.InventorySlot inventorySlot in inventory.GetInventorySlotArray())
        {
            Item item = inventorySlot.GetItem();

            RectTransform itemSlotRectTransform = Instantiate(itemSlotTemplate, itemSlotContainer).GetComponent <RectTransform>();
            itemSlotRectTransform.gameObject.SetActive(true);

            itemSlotRectTransform.GetComponent <Button_UI>().ClickFunc           = () => {
                // Use item
                //inventory.UseItem(item);
            };
            itemSlotRectTransform.GetComponent <Button_UI>().MouseRightClickFunc = () => {
                // Split item
                if (item.IsStackable())
                {
                    // Is Stackable
                    if (item.amount > 2)
                    {
                        // Can split
                        int splitAmount = Mathf.FloorToInt(item.amount / 2f);
                        item.amount -= splitAmount;
                        Item duplicateItem = new Item {
                            itemType = item.itemType, amount = splitAmount
                        };
                        inventory.AddItem(duplicateItem);
                    }
                }

                // Drop item
                //Item duplicateItem = new Item { itemType = item.itemType, amount = item.amount };
                //inventory.RemoveItem(item);
                //ItemWorld.DropItem(player.GetPosition(), duplicateItem);
            };

            itemSlotRectTransform.anchoredPosition = new Vector2(x * itemSlotCellSize, -y * itemSlotCellSize);

            if (!inventorySlot.IsEmpty())
            {
                // Not Empty, has Item
                Transform uiItemTransform = Instantiate(pfUI_Item, itemSlotContainer);
                uiItemTransform.GetComponent <RectTransform>().anchoredPosition = itemSlotRectTransform.anchoredPosition;
                UI_Item uiItem = uiItemTransform.GetComponent <UI_Item>();
                uiItem.SetItem(item);
            }

            Inventory.InventorySlot tmpInventorySlot = inventorySlot;

            UI_ItemSlot uiItemSlot = itemSlotRectTransform.GetComponent <UI_ItemSlot>();
            uiItemSlot.SetOnDropAction(() => {
                // Dropped on this UI Item Slot
                Item draggedItem = UI_ItemDrag.Instance.GetItem();
                draggedItem.RemoveFromItemHolder();
                inventory.AddItem(draggedItem, tmpInventorySlot);
            });

            /*
             * TextMeshProUGUI uiText = itemSlotRectTransform.Find("amountText").GetComponent<TextMeshProUGUI>();
             * if (inventorySlot.IsEmpty()) {
             *  // Empty
             *  uiText.SetText("");
             * } else {
             *  if (item.amount > 1) {
             *      uiText.SetText(item.amount.ToString());
             *  } else {
             *      uiText.SetText("");
             *  }
             * }
             */

            x++;
            int itemRowMax = 7;
            if (x >= itemRowMax)
            {
                x = 0;
                y++;
            }
        }
    }