コード例 #1
0
        public void Update()
        {
            if (IsHovered && Input.GetKeyDown(KeyCode.G))
            {
                if (SlotsObject.Slots[Y, X] != null)
                {
                    Debug.Log("DROP");
                    // TODO: Item can bug into other blogs when dropped right next to them, because position is absolute
                    Vector3 newPos = new Vector3(SlotsObject.Owner.transform.position.x + 0.5f,
                                                 SlotsObject.Owner.transform.position.y + 0.5f);
                    GameObject current = Instantiate(InventoryItem.ItemDefinition.Prefab, newPos, Quaternion.identity);
                    current.transform.parent = PrefabRepository.Instance.Map.transform.GetChild(2);
                    ItemInstance itemInstance = new ItemInstance(InventoryItem.ItemDefinition.ItemType, current);
                    current.GetComponent <ItemBehaviour>().Instance = itemInstance;
                    PrefabRepository.Instance.World.Items.Add(itemInstance);
                    if (SlotsObject.Slots[Y, X].Count - 1 < 1)
                    {
                        SlotsObject.Slots[Y, X] = null;
                    }
                    else
                    {
                        SlotsObject.Slots[Y, X].Count--;
                    }
                    Destroy(gameObject);

                    SaveChangesInventoryItemBar(SlotsObject.Owner);

                    SlotsObject.DeleteGui();
                    SlotsObject.DrawUi();
                }
            }
        }
コード例 #2
0
        public void OnEndDrag(PointerEventData eventData)
        {
            Debug.Log("Drag-end event triggered");
            if (SlotsObject.CurrentDraggingItem != null)
            {
                GameObject.Destroy(SlotsObject.CurrentDraggingImageItem);
                SlotsObject.CurrentDraggingImageItem = null;

                SlotsObject curSlotObject = null;
                if (SlotsObject.Owner.Inventory.SelectedSlotPosition != null)
                {
                    curSlotObject = SlotsObject.Owner.Inventory;
                }
                else if (SlotsObject.Owner.ItemBar.SelectedSlotPosition != null)
                {
                    curSlotObject = SlotsObject.Owner.ItemBar;
                }

                if (curSlotObject == null)
                {
                    SlotsObject.Slots[Y, X] = SlotsObject.CurrentDraggingItem;
                    SlotsObject.DeleteGui();
                    SlotsObject.DrawUi();
                    Debug.Log("Reset dragging");
                }
                else
                {
                    InventoryItem help = curSlotObject.Slots[curSlotObject.SelectedSlotPosition.Value.y,
                                                             curSlotObject.SelectedSlotPosition.Value.x];
                    if (help != null &&
                        help.ItemDefinition.ItemType == SlotsObject.CurrentDraggingItem.ItemDefinition.ItemType &&
                        help.Count + SlotsObject.CurrentDraggingItem.Count <= ItemDefinition.MaxItemCount)
                    {
                        curSlotObject.Slots[curSlotObject.SelectedSlotPosition.Value.y,
                                            curSlotObject.SelectedSlotPosition.Value.x].Count +=
                            SlotsObject.CurrentDraggingItem.Count;
                    }
                    else if (help != null)
                    {
                        // help.Count + SlotsObject.CurrentDraggingItem.Count <
                        SlotsObject.Slots[SlotsObject.DraggingStartPosition.y,
                                          SlotsObject.DraggingStartPosition.x] = help;
                        SlotsObject.DeleteGui();
                        SlotsObject.DrawUi();
                        curSlotObject.Slots[curSlotObject.SelectedSlotPosition.Value.y,
                                            curSlotObject.SelectedSlotPosition.Value.x] =
                            SlotsObject.CurrentDraggingItem;
                    }
                    else
                    {
                        curSlotObject.Slots[curSlotObject.SelectedSlotPosition.Value.y,
                                            curSlotObject.SelectedSlotPosition.Value.x] =
                            SlotsObject.CurrentDraggingItem;
                    }
                    curSlotObject.DeleteGui();
                    curSlotObject.DrawUi();

                    var itemBar = curSlotObject as ItemBar;
                    if (itemBar != null)
                    {
                        itemBar.UpdateSelectedItemGameObject();
                    }

                    SaveChangesInventoryItemBar(SlotsObject.Owner);
                    Debug.Log("END DRAG - " + curSlotObject.SelectedSlotPosition.Value.y + "/" +
                              curSlotObject.SelectedSlotPosition.Value.x);
                }
            }
        }