public void moveItemCellTo(Inventory inventoryToAddTo, Vector2 dropPoint, InventoryCell inventoryCell) { if (inventoryCell.items.Count > 0) { //Drop to ground Game1.currentMap.entityAddQueue.Add(new Drop(inventoryCell.items.ToArray(), new RigidBody(Drop.massesOfPlayer), dropPoint)); inventoryCell.items.Clear(); OnInventoryChanged.Invoke(this, null); } }
public Inventory(string inventoryName, int[] inventorySize, int drawAreaWidth, int drawAreaHeight, int xOffset = 0, int yOffset = 0, bool selectable = true) { this.inventoryName = inventoryName; inventoryCells = new InventoryCell[inventorySize[0], inventorySize[1]]; for (int i = 0; i < inventoryCells.GetLength(0); i++) { for (int j = 0; j < inventoryCells.GetLength(1); j++) { inventoryCells[i, j] = new InventoryCell(new Rectangle()); } } OnInventoryChanged = new EventHandler((object sender, EventArgs e) => { }); this.selectable = selectable; initializeInventoryForArea(drawAreaWidth, drawAreaHeight, xOffset, yOffset); }
public bool mouseInteraction(MouseState mouseState, MouseState pastMouseState, KeyboardState keyboardState, ref List <Item> itemsHeld) { foreach (InventoryCell b in inventoryCells) { if (b.rectangle.Contains(mouseState.X, mouseState.Y)) { //Interaction is with this inventory cell if (keyboardState.IsKeyDown(Keys.LeftShift)) { if (selectable) { //Cell selection if (pastMouseState.LeftButton == ButtonState.Pressed && mouseState.LeftButton == ButtonState.Released) { //Cell is selected if (selectedCell == b) { selectedCell.useItem(InventoryCell.useEventType.unequipItem); selectedCell = null; } else { if (selectedCell != null) { selectedCell.useItem(InventoryCell.useEventType.unequipItem); } selectedCell = b; selectedCell.useItem(InventoryCell.useEventType.equipItem); } return(true); } } } else { //Item movement if (pastMouseState.LeftButton == ButtonState.Pressed && mouseState.LeftButton == ButtonState.Released) { //Either swap, deposit, or grab items if (b.items.Count > 0 && itemsHeld.Count > 0 && b.items[0].itemType == itemsHeld[0].itemType) { //Item types are the same. Add to slot b._items.AddRange(itemsHeld); itemsHeld.Clear(); } else { List <Item> itemHeldBuffer = itemsHeld; itemsHeld = b.items; b._items = itemHeldBuffer; } //Call inventory changed event OnInventoryChanged.Invoke(this, null); return(true); } else if (pastMouseState.RightButton == ButtonState.Pressed && mouseState.RightButton == ButtonState.Released) { //Add one of items held to the inventory cell if it contains nothing or the same type if (itemsHeld.Count > 0 && (b.items.Count == 0 || b.items[0].itemType == itemsHeld[0].itemType)) { //Can add one b.items.Add(itemsHeld[0]); itemsHeld.Remove(itemsHeld[0]); OnInventoryChanged.Invoke(this, null); return(true); } else if (itemsHeld.Count == 0 && b.items.Count > 0) { //Split items in cell into held int amountToTake = b.items.Count / 2; for (int i = 0; i < amountToTake; i++) { itemsHeld.Add(b.items[0]); b._items.Remove(b._items[0]); } if (amountToTake > 0) { OnInventoryChanged.Invoke(this, null); return(true); } } } } } } return(false); }
public void drawGUI(SpriteBatch spriteBatch, int screenWidth, int screenHeight, MouseState mouseState) { if (inventoryMenuOpen) { //Draw inventories playerInventory.drawInventory(spriteBatch); if (otherInventory != null) { otherInventory.drawInventory(spriteBatch); } //Draw crafting button craftButton.draw(spriteBatch); //Draw craftable item if not null if (craftWindowOpen && craftableItem != null) { InventoryCell craftOutcomeCell = new InventoryCell(new Rectangle(craftButton.rectangle.X + ((craftButton.rectangle.Width - craftInventory.cellSize) / 2), craftButton.rectangle.Bottom + GUIItemPadding, craftInventory.cellSize - (GUIItemPadding * 2), craftInventory.cellSize - (GUIItemPadding * 2))); craftOutcomeCell.addItem(new Item(craftableItem), craftableItem.amountMadeFromCraft); craftOutcomeCell.drawInventoryCell(craftOutcomeCell.rectangle, false, spriteBatch, Inventory.inventoryFont); if (craftOutcomeCell.rectangle.Contains(mouseState.X, mouseState.Y)) { craftOutcomeCell.drawItemLabel(new Vector2(mouseState.X, mouseState.Y), spriteBatch); } } //Draw items held if not null if (itemsHeld.Count > 0) { Rectangle drawRec = new Rectangle(mouseState.X - (playerInventory.cellSize / 2), mouseState.Y - (playerInventory.cellSize / 2), playerInventory.cellSize, playerInventory.cellSize); spriteBatch.Draw(itemsHeld[0].itemType.itemTexture, new Rectangle(drawRec.X + (int)(Inventory.itemInCellPadding * drawRec.Width), drawRec.Y + (int)(Inventory.itemInCellPadding * drawRec.Width), drawRec.Width - ((int)(Inventory.itemInCellPadding * drawRec.Width) * 2), drawRec.Height - ((int)(Inventory.itemInCellPadding * drawRec.Width) * 2)), itemsHeld[0].itemType.drawTint); if (itemsHeld.Count > 1) { spriteBatch.DrawString(Inventory.inventoryFont, itemsHeld.Count.ToString(), new Vector2(drawRec.X - Inventory.inventoryFont.MeasureString(itemsHeld.Count.ToString()).X + drawRec.Width - (int)((Inventory.itemInCellPadding * drawRec.Width) * 1.5F), drawRec.Y - Inventory.inventoryFont.MeasureString(itemsHeld.Count.ToString()).Y + drawRec.Height - (int)(Inventory.itemInCellPadding * drawRec.Width)), Color.White); } } foreach (InventoryCell b in playerInventory.inventoryCells) { if (b.rectangle.Contains(mouseState.X, mouseState.Y)) { b.drawItemLabel(new Vector2(mouseState.X, mouseState.Y), spriteBatch); } } if (otherInventory != null) { foreach (InventoryCell b in otherInventory.inventoryCells) { if (b.rectangle.Contains(mouseState.X, mouseState.Y)) { b.drawItemLabel(new Vector2(mouseState.X, mouseState.Y), spriteBatch); } } } } else { //Draw selected item if an item is selected if (playerInventory.selectedCell != null) { Rectangle drawRectangle = new Rectangle(GUIItemPadding, screenHeight - GUIItemPadding - (playerInventory.selectedCell.rectangle.Height * 2), playerInventory.selectedCell.rectangle.Width * 2, playerInventory.selectedCell.rectangle.Height * 2); playerInventory.selectedCell.drawInventoryCell(drawRectangle, false, spriteBatch, GUI.GUIFont); if (drawRectangle.Contains(mouseState.X, mouseState.Y)) { playerInventory.selectedCell.drawItemLabel(new Vector2(mouseState.X, mouseState.Y), spriteBatch); } } } }