Example #1
0
        public void OnEndDrag(PointerEventData eventData)
        {
            List <RaycastResult> raycastResults = new List <RaycastResult>();

            EventSystem.current.RaycastAll(eventData, raycastResults);

            // try to find an slot
            foreach (RaycastResult raycast in raycastResults)
            {
                if (raycast.gameObject == gameObject)   // ignore self detection
                {
                    continue;
                }
                InventorySlot slot         = raycast.gameObject.GetComponent <InventorySlot>();
                CraftingSlot  craftingSlot = raycast.gameObject.GetComponent <CraftingSlot>();

                if (slot != null)
                {
                    Player.Instance.switchInventorySlots(int.Parse(originalParent.name), int.Parse(raycast.gameObject.transform.parent.name));
                    break;
                }
                else if (craftingSlot != null)
                {
                    craftingSlot.setCurrentItem(int.Parse(originalParent.name));
                    break;
                }
            }
            transform.position = initialPos;
            transform.SetParent(originalParent);
        }
Example #2
0
        public void showPossibleCrafting()
        {
            int  filledSlots = 0;
            bool found       = false;

            foreach (CraftingSlot slot in craftingSlots)
            {
                if (slot.currentItem != null)
                {
                    filledSlots++;
                }
            }

            foreach (Recipe recipe in Game.Instance.craftingRecipes)
            {
                // we can skip those recipes that dont match the amount of items
                if (filledSlots != recipe.ingredients.Length)
                {
                    continue;
                }

                foreach (Ingredient ingredient in recipe.ingredients)
                {
                    CraftingSlot slot = craftingSlots[ingredient.slot - 1];

                    if (slot.currentItem == null ||
                        ingredient.itemId != slot.currentItem.id ||
                        slot.currentItem.quantity < ingredient.quantity)
                    {
                        found = false;
                        break;
                    }
                    found = true;
                }

                if (found)
                {
                    craftingOutputSlot.currentRecipe = recipe;
                    craftingOutputSlot.setVisible(true);
                    return;
                }
            }

            craftingOutputSlot.setVisible(false);
        }