Пример #1
0
 private void OnDestroy()
 {
     if (indexable != null)
     {
         indexable.GetContainer().UnsetOnChanged(HandleContainerOnChanged);
     }
 }
Пример #2
0
        // Start is called before the first frame update
        void Start()
        {
            indexable = GetComponentInParent <IIndexable <Item> >();

            indexable.GetContainer().SetOnChanged(HandleContainerOnChanged);

            CheckContainer();
        }
Пример #3
0
        //public void Init(int index, Item item, int quantity, float baseWidth, float baseHeight)
        public void StartDragging(GameObject sender, GameObject currentRaycast)
        {
            // Store the source
            source = sender.GetComponent <IIndexable <Item> >();

            // Get the image component
            Image image = GetComponent <Image>();

            // Set icon and resize
            Item item = (source.GetContainer() as IContainer <Item>).GetElement(source.GetIndex());

            image.sprite = item.Icon;
            (transform as RectTransform).sizeDelta = new Vector2(baseWidth * item.SlotShape.x, baseHeight * item.SlotShape.y);
            (transform as RectTransform).pivot     = new Vector2(0f, 1f);
        }
Пример #4
0
        public void StopDragging(GameObject sender, GameObject currentRaycast)
        {
            if (currentRaycast == null)
            {
                ExitDragMode();
                return;
            }

            // Try move to another slot
            IIndexable <Item> dest = currentRaycast.GetComponent <IIndexable <Item> >();

            if (dest != null)
            {
                // Get destination data
                IContainer <Item> dstContainer = dest.GetContainer();
                int  dstIdx  = dest.GetIndex();
                Item dstItem = dstContainer.GetElement(dstIdx);

                // Get source data
                IContainer <Item> srcContainer = source.GetContainer();
                int  srcIdx  = source.GetIndex();
                Item srcItem = srcContainer.GetElement(source.GetIndex());


                if (srcContainer == dstContainer)              /*********************** Same container ( ex. from inventory to inventory ) *********************/
                {
                    if (dstItem == null || dstItem == srcItem) // Destination is empty or contains the same item
                    {
                        int  srcQ        = srcContainer.GetQuantity(srcIdx);
                        int  dstFreeRoom = srcContainer.GetFreeRoom(dstIdx, srcItem);
                        bool doNothing   = false;

                        // If there is no room or I'm not moving the item at all then do nothing
                        bool multiSlotSupport = new List <System.Type>(srcContainer.GetType().GetInterfaces()).Contains(typeof(IBigSlotContainer));
                        if ((dstFreeRoom <= 0) || ((dstIdx == srcIdx || (multiSlotSupport && (srcContainer as IBigSlotContainer).GetRootIndex(srcIdx) == (srcContainer as IBigSlotContainer).GetRootIndex(dstIdx)))))
                        {
                            doNothing = false;
                        }

                        if (!doNothing)
                        {
                            int max = Mathf.Min(dstFreeRoom, srcQ);
                            Debug.Log("Max:" + max);
                            if (max > 1 && Input.GetKey(KeyCode.LeftControl))
                            {
                                CounterSliderUI.Instance.Show(1, max /*srcContainer.GetQuantity(srcIdx)*/, (int a) =>
                                {
                                    // Move items
                                    srcContainer.Move(srcIdx, dstIdx, a);
                                }, () => { });
                            }
                            else
                            {
                                max = 1;
                                srcContainer.Move(srcIdx, dstIdx, max);
                            }
                        }
                    }
                }
                else /********** Different containers *****************/
                {
                    if (dstItem == null || dstItem == srcItem) // Destination is empty or contains the same item
                    {
                        int  srcQ        = srcContainer.GetQuantity(srcIdx);
                        int  dstFreeRoom = dstContainer.GetFreeRoom(dstIdx, srcItem);
                        bool doNothing   = false;

                        if (dstFreeRoom <= 0)
                        {
                            doNothing = true;
                        }

                        if (!doNothing)
                        {
                            int max = Mathf.Min(dstFreeRoom, srcQ);
                            Debug.Log("Max:" + max);
                            if (max > 1 && Input.GetKey(KeyCode.LeftControl))
                            {
                                CounterSliderUI.Instance.Show(1, max /*srcContainer.GetQuantity(srcIdx)*/, (int a) =>
                                {
                                    // Move items
                                    int q = dstContainer.Insert(dstIdx, srcItem, max);
                                    if (q > 0)
                                    {
                                        srcContainer.Remove(srcIdx, q);
                                    }
                                }, () => { });
                            }
                            else
                            {
                                max = 1;
                                // Move items
                                int q = dstContainer.Insert(dstIdx, srcItem, max);
                                if (q > 0)
                                {
                                    srcContainer.Remove(srcIdx, q);
                                }
                            }
                        }
                    }
                    else // Different items between different containers
                    {
                        int  srcQ        = srcContainer.GetQuantity(srcIdx);
                        int  dstFreeRoom = dstContainer.GetFreeRoom(dstIdx, srcItem);
                        bool doNothing   = false;

                        if (!doNothing)
                        {
                            int max = Mathf.Min(dstFreeRoom, srcQ);
                            Debug.Log("Max:" + max);
                            if (max > 1 && Input.GetKey(KeyCode.LeftControl))
                            {
                                CounterSliderUI.Instance.Show(1, max /*srcContainer.GetQuantity(srcIdx)*/, (int a) =>
                                {
                                    // Move items
                                    TrySwitch(srcIdx, dstIdx, srcItem, dstItem, max, srcContainer, dstContainer);
                                }, () => { });
                            }
                            else
                            {
                                max = 1;
                                TrySwitch(srcIdx, dstIdx, srcItem, dstItem, max, srcContainer, dstContainer);
                            }
                        }
                    }
                }
            }

            ExitDragMode();
        }