Beispiel #1
0
        private void UpdateItems()
        {
            Dictionary <int, InventoryUIItem> remainingItems = new Dictionary <int, InventoryUIItem>(this.currentItems);

            foreach (KeyValuePair <int, int> entry in InventoryManager.Instance.playerInventory.items)
            {
                Item currentItem       = InventoryManager.Instance.itemsCatalogue[entry.Key];
                int  currentItemAmount = InventoryManager.Instance.playerInventory.items[currentItem.uuid];
                if (currentItemAmount <= 0)
                {
                    continue;
                }

                if (this.currentItems != null && this.currentItems.ContainsKey(currentItem.uuid))
                {
                    this.currentItems[currentItem.uuid].UpdateUI(currentItem, currentItemAmount);
                    remainingItems.Remove(currentItem.uuid);
                }
                else
                {
                    GameObject itemUIPrefab = this.itemUIPrefab;
                    if (itemUIPrefab == null)
                    {
                        string error     = "No inventory item UI prefab found. Fill the required field at {0}";
                        string errorPath = "GameCreator/Preferences and head to Inventory -> Settings";
                        Debug.LogErrorFormat(error, errorPath);
                        return;
                    }

                    GameObject      itemUIAsset = Instantiate(itemUIPrefab, this.itemsContaineScrollRect.content);
                    InventoryUIItem itemUI      = itemUIAsset.GetComponent <InventoryUIItem>();
                    itemUI.Setup(currentItem, currentItemAmount);
                    this.currentItems.Add(currentItem.uuid, itemUI);
                }
            }

            foreach (KeyValuePair <int, InventoryUIItem> entry in remainingItems)
            {
                this.currentItems.Remove(entry.Key);
                Destroy(entry.Value.gameObject);
            }
        }
        public void OnDragMove(BaseEventData eventData)
        {
            PointerEventData pointerData = ((PointerEventData)eventData);

            if (pointerData == null)
            {
                return;
            }

            if (DATABASE_INVENTORY != null &&
                DATABASE_INVENTORY.inventorySettings.onDragGrabItem && this.item.sprite != null)
            {
                InventoryUIManager.OnDragItem(this.item.sprite, true);
            }

            if (pointerData.pointerCurrentRaycast.gameObject == null)
            {
                return;
            }

            GameObject      target = pointerData.pointerCurrentRaycast.gameObject;
            InventoryUIItem other  = target.GetComponent <InventoryUIItem>();

            if (other == null)
            {
                return;
            }

            Button otherButton = pointerData.pointerCurrentRaycast.gameObject.GetComponentInChildren <Button>();

            if (otherButton != null)
            {
                this.HighlightButton(this.button, true);
                this.HighlightButton(otherButton, true);
            }

            eventData.Use();
        }
        public void OnDragEnd(BaseEventData eventData)
        {
            if (DATABASE_INVENTORY != null && DATABASE_INVENTORY.inventorySettings.cursorDrag != null)
            {
                Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
            }

            InventoryUIManager.OnDragItem(null, false);
            PointerEventData pointerData = ((PointerEventData)eventData);

            if (pointerData == null)
            {
                return;
            }
            if (pointerData.pointerCurrentRaycast.gameObject == null)
            {
                return;
            }

            GameObject      target       = pointerData.pointerCurrentRaycast.gameObject;
            IgniterDropItem targetDrop   = target.GetComponent <IgniterDropItem>();
            InventoryUIItem targetItemUI = target.GetComponent <InventoryUIItem>();

            if (targetDrop != null)
            {
                targetDrop.OnDrop(this.item);
                eventData.Use();
            }
            else if (targetItemUI == null)
            {
                if (DATABASE_INVENTORY.inventorySettings.canDropItems && this.item.prefab != null)
                {
                    Vector3 position  = pointerData.pointerCurrentRaycast.worldPosition + (Vector3.up * 0.1f);
                    Vector3 direction = position - HookPlayer.Instance.transform.position;

                    if (direction.magnitude > DATABASE_INVENTORY.inventorySettings.dropItemMaxDistance)
                    {
                        position = (
                            HookPlayer.Instance.transform.position +
                            direction.normalized * DATABASE_INVENTORY.inventorySettings.dropItemMaxDistance
                            );
                    }

                    Instantiate(this.item.prefab, position, Quaternion.identity);
                    InventoryManager.Instance.SubstractItemFromInventory(this.item.uuid, 1);
                }

                eventData.Use();
            }
            else if (this.item.uuid != targetItemUI.item.uuid)
            {
                Button otherButton = pointerData.pointerCurrentRaycast.gameObject.GetComponentInChildren <Button>();
                if (otherButton != null)
                {
                    this.HighlightButton(this.button, false);
                    this.HighlightButton(otherButton, false);
                }

                InventoryManager.Instance.UseRecipe(this.item.uuid, targetItemUI.item.uuid);
                eventData.Use();
            }
        }