Beispiel #1
0
        private static void StackAction(IStackable obj, List <byte> output)
        {
            var bytes = obj.Stack();

            output.AddRange(Encoding.UTF8.GetBytes(obj.GetType().FullName !+';'));
            output.AddRange(bytes);
        }
Beispiel #2
0
        private void GetPlace() //Процедура определения новой позиции карты либо оставление ее на прежнем месте
        {
            ContactFilter2D filter = new ContactFilter2D();

            filter.NoFilter();
            List <Collider2D> colliders = new List <Collider2D>();

            Physics2D.OverlapBox(transform.position, transform.localScale, 0, filter, colliders);

            float      distance  = 10f;
            IStackable stackable = null;

            foreach (var collider in colliders)
            {
                if (collider.transform != transform)
                {
                    if (distance > (collider.transform.position - transform.position).magnitude)
                    {
                        if (collider.TryGetComponent(out IStackable stackableTemp))
                        {
                            stackable = stackableTemp;
                        }

                        distance = (collider.transform.position - transform.position).magnitude;
                    }
                }
            }

            if (stackable != null)
            {
                if (stackable.Stack(this))
                {
                    OnChangedParent?.Invoke(stackable);
                    Position = transform.localPosition;
                    return;
                }
            }

            transform.localPosition = Position;
        }
Beispiel #3
0
        public void OnDrop(PointerEventData eventData)
        {
            if (UIItem.DraggedItem == null)
            {
                return;
            }
            if (UIItem.DraggedItem.Item == null)
            {
                return;
            }

            if (HaveItem == false)
            {
                AddUIItem(UIItem.DraggedItem);

                if (IsCrossInventory)
                {
                    Debug.Log("EMPTY CROSS PANEL");

                    // Remove item from inventory that its coming from
                    EventMessenger.Instance.Raise(new EventRemoveItemFromInventory(UIItem.DraggedItemStartSlot.InventoryUUID, UIItem.DraggedItem.Item, false));

                    UIItem.DraggedItem.Item.BaseData.InventoryUUID = InventoryUUID;

                    // IMPORTANT SET THE INVENTORY
                    // IMPORTANT SET THE INVENTORY
                    //this.ThisUIItem.Item.Inventory = ThisUIItem.Item.Inventory;

                    // Add Item to the new inventory
                    EventMessenger.Instance.Raise(new EventAddItemToInventory(InventoryUUID, UIItem.DraggedItem.Item, false));

                    this.ThisUIItem.transform.SetParent(UIItem.DraggedItemStartSlot.transform);
                }
                else
                {
                    Debug.Log("EMPTY SAME PANEL");
                    // IMPORTANT SET THE INVENTORY
                    // IMPORTANT SET THE INVENTORY
                    //UIItem.DraggedItem.Item.Inventory = ThisUIItem.Item.Inventory;
                    this.ThisUIItem.transform.SetParent(UIItem.DraggedItemStartSlot.transform);
                }
            }
            else
            {
                // If the item in the slot is stackable check here too and dont return to original position
                // if we have an item in the slot where this slot is being dragged return to original position

                // Set the start slot ItemContainer to this slot item container
                UIItem.DraggedItemStartSlot.ThisUIItem = this.ThisUIItem;

                // Here I can change where to check for the stackable interface
                // could be in the item itself (ItemContainer.Item)
                // or it could be in the data (ItemContainer.Item.BaseData)
                IStackable stackInterface = ThisUIItem.Item as IStackable;
                if (stackInterface != null && stackInterface.IsStackable)
                {
                    StackResult stackResult = stackInterface.Stack(UIItem.DraggedItem.Item as BaseItem);
                    if (stackResult != null)
                    {
                        if ((stackResult.item.BaseData as IStackableData).Quantity <= 0)
                        {
                            //Debug.Log("ITEMS WERE STACKED RESULT QUANTITY IS <= 0");

                            // Set the slot ItemContainer that the item is being dragged from to null
                            UIItem.DraggedItemStartSlot.ThisUIItem = ThisUIItem;

                            ThisUIItem.UpdateQuantity();
                            UIItem.DraggedItem.UpdateQuantity();

                            if (IsCrossInventory)
                            {
                                Debug.Log("STACK WITH DESTROY CROSS PANEL");
                                // --> EventMessenger.Instance.Raise(new EventUIInventoryItemChanged(InventoryPanel.Inventory.UniqueUUID, InventoryItem.Item.BaseData));
                            }
                            else
                            {
                                Debug.Log("STACK WITH DESTROY SAME PANEL");

                                UIItem.DraggedItem.transform.SetParent(UIItem.DraggedItemStartSlot.transform);
                                // --> EventMessenger.Instance.Raise(new EventUIInventoryItemChanged(InventoryPanel.Inventory.UniqueUUID, InventoryItem.Item.BaseData));
                            }
                        }
                        else
                        {
                            Debug.Log("ITEMS WERE STACKED BOTH ITEMS STILL HAVE QUANTITY");
                            // right item (dragged item) exchanged the quantities but
                            // it still have quantity put it back where it came from
                            UIItem.DraggedItem.transform.SetParent(UIItem.DraggedItemStartSlot.transform);

                            // update the left (item in this slot) item quantity
                            ThisUIItem.UpdateQuantity();

                            // update right item (dragged item) quantity
                            UIItem.DraggedItem.UpdateQuantity();
                        }
                    }
                    else
                    {
                        // stack failed for some reason maybe the items were not stackable
                        // or the left item (item in the slot) was full. Do a switch;
                        Debug.Log("STACK IS NULL DO A SWITCH ON THE ITEMS");
                        SwapNonStackable();
                    }
                }
                else
                {
                    Debug.LogError("Item [" + ThisUIItem.Item.BaseData.Name + "] Does not implement the IStackable interface. Or the item is not marked as stackable " + "[" + ThisUIItem.GetType() + "]");
                    //Debug.Log("ITEM COREDATA IS MARKED AS NON STACKABLE SWAP ITEMS");
                    SwapNonStackable();
                }

                ThisUIItem.UpdateSlotInfo();

                // When stacking an item the inventory item of tmpItemStartSlot will
                // be null. Lets check that and only update the slot info if its not null
                if (UIItem.DraggedItemStartSlot.ThisUIItem != null)
                {
                    UIItem.DraggedItemStartSlot.ThisUIItem.UpdateSlotInfo();
                }
            }
        }