/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { KeyboardState keyboardState = Keyboard.GetState(); MouseState mouseState = Mouse.GetState(); player.GetCurrentLocation().Update(keyboardState, mouseState, camera); if (keyboardState.IsKeyDown(Keys.Escape) && !previousKeyboardState.IsKeyDown(Keys.Escape) && menuList.Count > 0) { menuList.RemoveAt(menuList.Count - 1); } for (int i = 0; i < menuList.Count; i++) { menuList[i].Update(mouseState); } time++; if (time > 2000) { time = 0; } previousKeyboardState = keyboardState; playerInfoBar.Update(); base.Update(gameTime); }
override public void Update(MouseState mouseState) { Rectangle topMenu = new Rectangle(container.X, container.Y, container.Width, 40); if (mouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton != ButtonState.Pressed && topMenu.Contains(mouseState.Position)) { dragging = true; } if (dragging) { if (mouseState.LeftButton != ButtonState.Pressed) { dragging = false; } else { container.X += mouseState.X - previousMouseState.X; container.Y += mouseState.Y - previousMouseState.Y; } for (int i = 0; i < inventoryItems.Count; i++) { inventoryItems[i].SetLocation(new Vector2(container.X + 20 + ((i % 5) * 50), container.Y + 50 + ((i / 5) * 50))); } } if (!draggingItem) { selectedItem = null; } bool select = false; foreach (InventoryItem item in inventoryItems) { item.SetHovering(false, mouseState); if (item.GetHitBox().Contains(mouseState.Position)) { item.SetHovering(true, mouseState); if (mouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton != ButtonState.Pressed) { selectedItem = item; draggingItem = true; select = true; break; } if (mouseState.RightButton == ButtonState.Pressed && previousMouseState.RightButton != ButtonState.Pressed) { selectedItem = item; select = true; player.GetEquipmentMenu().Equip(item); break; } } } if (select) { RemoveFromInventory(selectedItem); if (!draggingItem) { selectedItem = null; } } if (draggingItem) { selectedItem.SetLocation(new Vector2(mouseState.Position.X - 15, mouseState.Position.Y - 15)); if (mouseState.LeftButton != ButtonState.Pressed) { if (container.Contains(mouseState.Position)) { AddToInventory(selectedItem.GetItem(), false, 0); } else { Item drop = selectedItem.GetItem(); drop.ResetPickUpCounter(); drop.SetLocation(player.location); player.GetCurrentLocation().AddItem(drop); } selectedItem = null; draggingItem = false; } } previousMouseState = mouseState; }