public static void deselectItem(Character characterSheet, MapGenerator mapGenerator, Unit u) { Vector3 mousePos = Input.mousePosition; mousePos.y = Screen.height - mousePos.y; Tile t = (mapGenerator != null && u != null ? mapGenerator.tiles[(int)u.position.x, (int)-u.position.y] : null); foreach (InventorySlot slot in inventorySlots) { Rect r = UnitGUI.getInventorySlotRect(slot); if (r.Contains(mousePos)) { Vector2 v2 = getIndexOfSlot(slot); Vector2 v = v2 - selectedCell; Debug.Log(v); if (characterSheet.characterSheet.inventory.canInsertItemInSlot(selectedItem, v)) { if (selectedItemWasInSlot == InventorySlot.None) { t.removeItem(selectedItem, 1); u.useMinor(MinorType.Loot, false, false); } characterSheet.characterSheet.inventory.insertItemInSlot(selectedItem, v); selectedItem = null; return; } else { InventoryItemSlot invSlot = characterSheet.characterSheet.inventory.inventory[Inventory.getIndexForSlot(v2)]; Item invSlotItem = invSlot.getItem(); if (invSlotItem != null && characterSheet.characterSheet.inventory.itemCanStackWith(invSlotItem, selectedItem)) { if (selectedItemWasInSlot == InventorySlot.None) { t.removeItem(selectedItem, 1); u.useMinor(MinorType.Loot, false, false); } characterSheet.characterSheet.inventory.stackItemWith(invSlotItem, selectedItem); selectedItem = null; return; } } break; } } /* if (GameGUI.looting && !(mousePos.x < groundX || mousePos.y < groundY || mousePos.x > groundX + groundWidth || mousePos.y > groundY + groundHeight)) { * if (selectedItemWasInSlot!=InventorySlot.None && selectedItem!=null) { * while (selectedItem.stackSize() > 1) t.addItem(selectedItem.popStack()); * t.addItem(selectedItem); * u.useMinor(MinorType.Loot, false, false); * // characterSheet.characterSheet.inventory.removeItemFromSlot(getInventorySlotPos(selectedItemWasInSlot)); * } * } * else if (selectedItemWasInSlot!=InventorySlot.None) { * if (characterSheet.characterSheet.inventory.canInsertItemInSlot(selectedItem, getIndexOfSlot(selectedItemWasInSlot))) { * characterSheet.characterSheet.inventory.insertItemInSlot(selectedItem, getIndexOfSlot(selectedItemWasInSlot)); * } * else { * int slot1 = getLinearIndexFromIndex(getIndexOfSlot(selectedItemWasInSlot)); * if (slot1 > -1 && characterSheet.characterSheet.inventory.itemCanStackWith(characterSheet.characterSheet.inventory.inventory[slot1].getItem(),selectedItem)) { * characterSheet.characterSheet.inventory.stackItemWith(characterSheet.characterSheet.inventory.inventory[slot1].getItem(),selectedItem); * } * } * }*/ selectedItem = null; }
public static void selectItem(Character characterSheet, MapGenerator mapGenerator, Unit u) { Vector3 mousePos = Input.mousePosition; mousePos.y = Screen.height - mousePos.y; foreach (InventorySlot slot in inventorySlots) { Rect r = UnitGUI.getInventorySlotRect(slot); if (r.Contains(mousePos)) { Vector2 v = getIndexOfSlot(slot); // Debug.Log(v); int ind = getLinearIndexFromIndex(v); InventoryItemSlot sl = characterSheet.characterSheet.inventory.inventory[ind]; InventoryItemSlot slR = sl.itemSlot; if (slR == null) { break; } // Item i = slR.item; Vector2 itemSlot = Inventory.getSlotForIndex(ind); ItemReturn ir = characterSheet.characterSheet.inventory.removeItemFromSlot(itemSlot); selectedItem = ir.item; if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) { if (selectedItem.stackSize() > 1) { characterSheet.characterSheet.inventory.insertItemInSlot(selectedItem, itemSlot - ir.slot); selectedItem = selectedItem.popStack(); } } selectedCell = ir.slot; selectedMousePos = mousePos; // selectedItemPos = getInventorySlotPos(); selectedItemPos = UnitGUI.getInventorySlotPos(inventorySlots[slR.index]); selectedItemWasInSlot = inventorySlots[slR.index]; break; } } // if (!GameGUI.looting || mousePos.x < groundX || mousePos.y < groundY || mousePos.x > groundX + groundWidth || mousePos.y > groundY + groundHeight) return; Vector2 scrollOff = UnitGUI.groundScrollPosition; float div = 20.0f; float y = div + UnitGUI.groundY - scrollOff.y; float mid = UnitGUI.groundX + UnitGUI.groundWidth / 2.0f + scrollOff.x; // mousePos.y += groundScrollPosition.y; selectedItem = null; if (mapGenerator != null) { List <Item> groundItems = mapGenerator.tiles[(int)u.position.x, (int)-u.position.y].getReachableItems(); foreach (Item i in groundItems) { if (i.inventoryTexture == null) { continue; } // Debug.Log(mousePos.x + " " + mousePos.y + " " + mid + " " + y); Vector2 size = i.getSize(); float x = mid - size.x * inventoryCellSize / 2.0f; Rect r = new Rect(x, y, size.x * inventoryCellSize, size.y * UnitGUI.inventoryCellSize); if (r.Contains(mousePos)) { // Debug.Log(i); selectedCell = new Vector2((int)((mousePos.x - x) / inventoryCellSize), (int)((mousePos.y - y) / inventoryCellSize)); foreach (Vector2 cell in i.getShape()) { if (cell.x == selectedCell.x && cell.y == selectedCell.y) { selectedItemPos = new Vector2(x, y); selectedMousePos = mousePos; selectedItem = i; selectedItemWasInSlot = InventorySlot.None; } } Debug.Log(selectedCell); if (selectedItem != null) { break; } } y += size.y * UnitGUI.inventoryCellSize + div; } } }