Ejemplo n.º 1
0
    public void SwitchItemSlot(UI_INV_SLOT a, UI_INV_SLOT b)
    {
        if (a.isHotkey && !b.isHotkey)
        {
            ItemEntity itemA = hotbar[a.pos.x];
            ItemEntity itemB = inventory[b.pos.x, b.pos.y];

            hotbar[a.pos.x]             = itemB;
            inventory[b.pos.x, b.pos.y] = itemA;

            if (a.pos.x == currSelectedIndex)
            {
                UpdateCurrItem();
            }
        }

        else if (!a.isHotkey && b.isHotkey)
        {
            ItemEntity itemB = hotbar[b.pos.x];
            ItemEntity itemA = inventory[a.pos.x, a.pos.y];

            inventory[a.pos.x, a.pos.y] = itemB;
            hotbar[b.pos.x]             = itemA;

            if (b.pos.x == currSelectedIndex)
            {
                UpdateCurrItem();
            }
        }
        else if (!a.isHotkey && !b.isHotkey)
        {
            ItemEntity itemA = inventory[a.pos.x, a.pos.y];
            ItemEntity itemB = inventory[b.pos.x, b.pos.y];

            inventory[b.pos.x, b.pos.y] = itemA;
            inventory[a.pos.x, a.pos.y] = itemB;
        }
        else if (a.isHotkey && b.isHotkey)
        {
            ItemEntity itemA = hotbar[a.pos.x];
            ItemEntity itemB = hotbar[b.pos.x];

            hotbar[b.pos.x] = itemA;
            hotbar[a.pos.x] = itemB;

            if (a.pos.x == currSelectedIndex)
            {
                UpdateCurrItem();
            }
            if (b.pos.x == currSelectedIndex)
            {
                UpdateCurrItem();
            }
        }
    }
Ejemplo n.º 2
0
    public void OnEndDrag(PointerEventData eventData)
    {
        draggedItem = null;

        canvasGroup.blocksRaycasts = true;
        transform.position         = startPos;

        bool succ = canvasGroup.IsRaycastLocationValid(eventData.position, GameObject.FindGameObjectWithTag("MainCamera").GetComponent <Camera>());

        if (succ)
        {
            UI_INV_SLOT this_slot    = this.gameObject.GetComponentInParent <UI_INV_SLOT>();
            UI_INV_SLOT dragged_slot = eventData.pointerCurrentRaycast.gameObject.GetComponentInParent <UI_INV_SLOT>();

            if (this_slot == null)
            {
                return;
            }
            if (dragged_slot == null)
            {
                return;
            }

            GameObject inv = dragged_slot.gameObject.transform.parent.gameObject;

            InventoryHandler invHandler = GameObject.FindGameObjectWithTag("Player").GetComponent <InventoryHandler>();

            inventory_init _Init     = inv.GetComponent <inventory_init>();
            Vector2        right_pos = new Vector2(-1, -1);
            foreach (var entry in _Init.slots)
            {
                GameObject  key  = entry.Value;
                UI_INV_SLOT slot = key.GetComponentInParent <UI_INV_SLOT>();
                Vector2Int  pos  = slot.pos;

                if (pos == dragged_slot.pos)
                {
                    right_pos = pos;

                    invHandler.SwitchItemSlot(this_slot, slot);
                    break;
                }
            }
        }
    }
Ejemplo n.º 3
0
    // Use this for initialization
    private void Start()
    {
        fileDatabase = GameObject.FindGameObjectWithTag("ModLoader").GetComponent <FileDatabase>();
        gameDatabase = GameObject.FindGameObjectWithTag("ModLoader").GetComponent <GameDatabase>();
        inventory    = GameObject.FindGameObjectWithTag("Player").GetComponent <InventoryHandler>();

        GridLayoutGroup grid = GetComponent <GridLayoutGroup>();

        grid.cellSize = UI_CONSTS.INV_SLOT_SIZE;
        grid.spacing  = new Vector2(UI_CONSTS.INV_SLOT_OFFSET, UI_CONSTS.INV_SLOT_OFFSET);

        for (int x = 0; x < UI_CONSTS.INV_SIZE.x; x++)
        {
            for (int y = 0; y < UI_CONSTS.INV_SIZE.y; y++)
            {
                GameObject go = Instantiate(slot_prefab);
                go.name = "Slot_" + x;

                go.transform.parent = this.transform;
                UI_INV_SLOT slot = go.AddComponent <UI_INV_SLOT>();
                slot.pos      = new Vector2Int(x, y);
                slot.isHotkey = false;
                slots.Add(new Vector2(x, y), go);
            }
        }

        for (int x = 0; x < UI_CONSTS.INV_SIZE.x; x++)
        {
            GameObject go = Instantiate(slot_prefab);
            int        y  = UI_CONSTS.INV_SIZE.y + 1;
            go.name = "HotKey_Slot_" + x;

            go.transform.parent = this.transform;
            UI_INV_SLOT slot = go.AddComponent <UI_INV_SLOT>();
            slot.pos      = new Vector2Int(x, y);
            slot.isHotkey = true;
            slots.Add(new Vector2(x, y), go);
        }
    }