Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Inventory Help


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Thekill473   Members   -  Reputation: 213

Like
0Likes
Like

Posted 24 July 2012 - 03:05 AM

Well I'm currently having a bit of trouble with my inventory. Ive got the data side of things down and packed, each character contains a bag class which holds itemIcons. And so far I've been able to get 1 inventory control working(Generalized inventory HUD) with drag and drop.

But my question is what do I do if i want multiple inventory controls where the player can drag and drop items between the two. Ive tried having the inventory controls output all the positions of their slots but then when I drag the inventory everything just moves out of place. anyways here is my current inventory code.

InventoryControl.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RpgLibary.ItemClasses;
using RpgLibary.TileEngine;
using RpgLibary.Collision_Classes;
using RpgLibary;
using RpgLibary.SpriteClasses;
namespace RpgLibary.Controls
{
	public class InventoryControl : Control
	{
		#region Fields
		bool draggable = true;		// Whether you can drag the inventory around or not.
		Bag bag;					  // Contains all the items
		List<Vector2> slotPositions;  // Contains all the positions of the slots
		Camera camera;
		Iconset   slotBackground;
		Texture2D inventoryTab;	
		#endregion
		#region Properties
		public Bag Bag
		{
			get { return bag; }
			set { bag = value; }
		}
		public bool Draggable
		{
			get { return draggable; }
			set { draggable = value; }
		}
		public Camera Camera
		{
			get { return camera; }
			set { camera = value; }
		}
		public List<Vector2> SlotPositions
		{
			get { return slotPositions; }
			set { slotPositions = value; }
		}
		#endregion
		#region Constructor
		public InventoryControl(Game gameRef, Bag _bag, Camera _camera, bool _draggable)
		{
			text = "hi";
			// Load graphics
			slotBackground = new Iconset(gameRef, @"GUI\General GUI\slot");
			inventoryTab   = gameRef.Content.Load<Texture2D>(@"GUI\Inventory GUI\inventoryTab");
			tabStop   = false;
			camera	= _camera ;
			draggable = _draggable;
			bag	   = _bag;
			position = new Vector2(20, 20);
			// Intialize the slots positions
			slotPositions = new List<Vector2>((int)(bag.Size.X * bag.Size.Y));
			#region Test Code
			WeaponData weapon = gameRef.Content.Load<WeaponData>(@"Game/Items/Weapon/sword");
			WeaponData mace = gameRef.Content.Load<WeaponData>(@"Game/Items/Weapon/mace");
			Weapon Weapon = new Weapon(weapon.Name, weapon.Type, weapon.Price, weapon.Weight, weapon.NumberHands, weapon.AttackValue, weapon.AttackModifier, weapon.DamageEffectData, weapon.AllowableClasses);
			Weapon Mace = new Weapon(mace.Name, mace.Type, mace.Price, mace.Weight, mace.NumberHands, mace.AttackValue, mace.AttackModifier, mace.DamageEffectData, mace.AllowableClasses);
			Iconset iconset = new Iconset(gameRef, @"ObjectSprites\" + Path.GetFileNameWithoutExtension(weapon.ImageName));
			ItemIcon WeaponItem = new ItemIcon(Weapon, iconset, weapon.IconSource);
			ItemIcon MaceItem = new ItemIcon(Mace, iconset, mace.IconSource);
			WeaponItem.SlotNumber = new Vector2(1, 1);
			MaceItem.SlotNumber = new Vector2(1, 2);
			Bag.AddItem(WeaponItem);
			Bag.AddItem(MaceItem);
			#endregion
			for (int x = 0; x < Bag.Size.X; x++)
			{
				for (int y = 0; y < Bag.Size.Y; y++)
				{
					// Create the Slots positions
					slotPositions.Add(new Vector2(x + position.X, y + position.Y));
				}
			}
		}
		public InventoryControl(ContentManager _content, Bag _bag, bool _draggable)
		{
			slotBackground = new Iconset(_content.Load<Texture2D>(@"GUI\Inventory GUI\inventorySlot"));
			inventoryTab   = _content.Load<Texture2D>(@"GUI\Inventory GUI\inventoryTab");
			tabStop  = false;
			draggable = _draggable;
			Bag	   = _bag;
			slotPositions = new List<Vector2>((int)(bag.Size.X * bag.Size.Y));
			#region Test Code
			WeaponData weapon = _content.Load<WeaponData>(@"Game/Items/Weapon/sword");
			WeaponData mace = _content.Load<WeaponData>(@"Game/Items/Weapon/mace");
			Weapon Weapon = new Weapon(weapon.Name, weapon.Type, weapon.Price, weapon.Weight, weapon.NumberHands, weapon.AttackValue, weapon.AttackModifier, weapon.DamageEffectData, weapon.AllowableClasses);
			Weapon Mace = new Weapon(mace.Name, mace.Type, mace.Price, mace.Weight, mace.NumberHands, mace.AttackValue, mace.AttackModifier, mace.DamageEffectData, mace.AllowableClasses);
			Iconset iconset = new Iconset(_content.Load<Texture2D>(@"ObjectSprites\" + Path.GetFileNameWithoutExtension(weapon.ImageName)));
			ItemIcon WeaponItem = new ItemIcon(Weapon, iconset, weapon.IconSource);
			ItemIcon MaceItem = new ItemIcon(Mace, iconset, mace.IconSource);
			WeaponItem.SlotNumber = new Vector2(1, 1);
			MaceItem.SlotNumber = new Vector2(1, 2);
			Bag.AddItem(WeaponItem);
			Bag.AddItem(MaceItem);
			#endregion
			for (int x = 0; x < Bag.Size.X; x++)
			{
				for (int y = 0; y < Bag.Size.Y; y++)
				{
					// Create the Slots positions
					slotPositions[y * (int)bag.Size.X + x] = new Vector2(x, y);
				}
			}
		}
		#endregion
		#region Methods
		public override void Update(GameTime gameTime)
		{
			foreach (Vector2 slotPos in SlotPositions)
			{
				//slotPos
			}
			#region Old Code
			/* Update the slots
			foreach (InventorySlot slot in slots)
			{
				if (slot != null)
					slot.Update(gameTime);
			}
			// Set the slots position
			for (int x = 0; x < Bag.Size.X; x++)
				for (int y = 0; y < Bag.Size.Y; y++)
				{
					slots[x, y].Sprite.Position = new Vector2(position.X + (x * 32), position.Y + 10 + (y * 32)); // Set the position.
				}
			HandleInput(PlayerIndex.One);
			// Update the icons
			foreach (ItemIcon i in Bag.Items)
			{
				if (i != null)
				{
					i.Update(gameTime, camera);
					// Foreach itemicon add all the slot positionsmin this inventory
					i.SlotPositions.Clear(); // will need to be added to the main gamplay loop later.
					foreach (InventorySlot slot in slots)
					{					  
						i.SlotPositions.Add(slot.Sprite.Position);
					}
				}
			}
			// If the control hasFocus follow the mouse
			if (hasFocus)
			{
				position = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
				foreach (ItemIcon i in bag.Items)
				{
					i.SlotPosition = position;
				}
			}*/
			#endregion
		}
		public override void Draw(SpriteBatch spriteBatch)
		{
			if (draggable == true)
				spriteBatch.Draw(inventoryTab, new Rectangle((int)position.X, (int)position.Y - 10, inventoryTab.Width, inventoryTab.Height), Color.White);
			#region Old Code
			/* Draw all the slots
			foreach (InventorySlot slot in slots)
			{
				if (slot != null)
					slot.Draw(spriteBatch);
			}
			// Draw the items
			foreach (ItemIcon i in Bag.Items)
				if (i != null)
					i.Draw(spriteBatch); */
			#endregion
		}
		public override void HandleInput(PlayerIndex playerIndex)
		{
			if (draggable == true)
			{
				if (InputHandler.MouseInUse == false)
				{
					Rectangle tabRect = new Rectangle((int)Position.X, (int)Position.Y, inventoryTab.Width, inventoryTab.Height);
					if (Mouse.GetState().X > tabRect.X && Mouse.GetState().X < tabRect.X + tabRect.Width && Mouse.GetState().Y > tabRect.Y && Mouse.GetState().Y < tabRect.Y + tabRect.Height) // If the mouse is within the tab
					{
						if (Mouse.GetState().LeftButton == ButtonState.Pressed) // If the mouse has clicked on the tab thens et it to true
						{
							hasFocus = true;
							InputHandler.MouseInUse = true;
						}
					}				  
				}
				//If the mousebutton is nolonger down releas the inventory.
				if (hasFocus && Mouse.GetState().LeftButton != ButtonState.Pressed)
				{
					hasFocus = false;
					InputHandler.MouseInUse = false;
				}
			}
		}
		#endregion
	}
}

ItemIcon.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RpgLibary;
using RpgLibary.ItemClasses;
using RpgLibary.SpriteClasses;
using RpgLibary.TileEngine;
namespace RpgLibary.ItemClasses
{
	public class ItemIcon
	{
		#region Fields
		BaseSprite sprite;
		BaseItem   item;
		int Amount;			   // The amount of the item there is
		Vector2 slotPosition;	// the position of the slot
		Vector2 slotNumber;	   // the number of the slot relative to its inventory
		List<Vector2> slotPositions = new List<Vector2>(); // Positions of all slots on the screen at the current time
		bool selected = false;	// If the sprite is active or not
		#endregion
		#region Properties
		public BaseSprite Sprite
		{
			get { return sprite; }
			set { sprite = value;  }
		}
		public BaseItem Item
		{
			get { return item; }
		}
		public bool Selected
		{
			get { return selected; }
		}
		public Vector2 SlotPosition
		{
			get { return slotPosition; }
			set { slotPosition = value; }
		}
		public Vector2 SlotNumber
		{
			get { return slotNumber; }
			set { slotNumber = value; }
		}
		public List<Vector2> SlotPositions
		{
			get { return slotPositions; }
			set { slotPositions = value; }
		}
		#endregion
		#region Constructor
		public ItemIcon(BaseItem item, Iconset iconset, Rectangle source)
		{
			this.item   = item;
			sprite = new BaseSprite(iconset.Texture, source);
		}
		#endregion
		#region Xna Methods
		public virtual void Update(GameTime gameTime, Camera camera)
		{
			sprite.Update(gameTime);
			if (selected == false)
			{
				sprite.Position = slotPosition;		
			}
			else
			{
				sprite.Position = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);			
			}
			HandleInput();
		}
		public virtual void Draw(SpriteBatch spriteBatch)
		{
			sprite.Draw(spriteBatch);
		}
		#endregion
		#region Methods
		private void HandleInput()
		{
			#region Draging Icons
			// If the mouse is over the sprite and the left button is down then set selected true and mouse in sue true.
			if (Mouse.GetState().X >= sprite.Position.X && Mouse.GetState().X <= sprite.Position.X + 32 && Mouse.GetState().Y >= sprite.Position.Y && Mouse.GetState().Y <= sprite.Position.Y + 32)
			{
				if (Mouse.GetState().LeftButton == ButtonState.Pressed)
				{
					if (InputHandler.MouseInUse == false)
					{
						InputHandler.MouseInUse = true;
						selected = true;
					}
				}			
			}
			if (selected == true)
			{
				if (Mouse.GetState().LeftButton != ButtonState.Pressed) // if the mouse os released then set selected to false
				{
					// Foreach slot position check if the cursor is over it and if so snap the sprite to that slot.
					foreach (Vector2 position in SlotPositions)
					{
						if (new Rectangle((int)position.X, (int)position.Y, 32, 32).Contains(new Point(Mouse.GetState().X, Mouse.GetState().Y)))
						{
							SlotPosition = position;
							// An event trigger will need to go here saying that the icon has found a slot so that we can notify the slot and tell it it is taken and also we'll need to see if the slotis taken. so bassicaly it would be a adat request event.
						}
					}
					InputHandler.MouseInUse = false;
					selected = false;
				}
			}
			#endregion
		}
		#endregion
	}
}

InventorySlot.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RpgLibary.SpriteClasses;
namespace RpgLibary.ItemClasses
{
	public class InventorySlot
	{
		#region Fields
		BaseSprite sprite; // The sprite for the inventory slot.
		bool	   taken;  // Whether the slot is taken or not.
		#endregion
		#region Properties
		public BaseSprite Sprite
		{
			get { return sprite; }
		}
		public bool Taken
		{
			get { return taken; }
			set { taken = value; }
		}
		#endregion
		#region Constructor
		public InventorySlot(Iconset iconset, Rectangle source)
		{
			taken = false;
			sprite = new BaseSprite(iconset.Texture, source);
		}
  
		#endregion
		#region Xna Methods
		public virtual void Update(GameTime gameTime)
		{
			sprite.Update(gameTime);
		}
		public virtual void Draw(SpriteBatch spriteBatch)
		{
			sprite.Draw(spriteBatch);
		}
		#endregion
		#region Methods
		#endregion
	}
}

Hud.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RpgLibary.CharacterClasses;
using RpgLibary.Controls;
using RpgLibary.ItemClasses;
using RpgLibary.ItemClasses.InventoryClasses;
using RpgLibary.TileEngine;
using RpgLibary.SpriteClasses;
namespace RpgLibary
{
	/* NOTE:
	 * This class will manage the hud of the gameplay screen and will align all hus controls
	 * into line with the playerss camera
	*/
	public class Hud
	{
		#region Fields
		ControlManager controlManager;
		Game		   gameRef;
		// Controls
		Bar HealthBar;
		Bar StaminaBar;
		Bar ManaBar;	  
		#region Inventory Managment Fields
		// Textures
		Iconset slotTexture;
		InventoryControl inventory;
		InventoryControl inventory2;
		List<InventoryControl> inventories = new List<InventoryControl>();
		List<InventorySlot> inventorySlots = new List<InventorySlot>(); // Will hold all the inventory slots.
		#endregion
		#endregion
		#region Properties
		#endregion
		#region Constructor
		public Hud(Game _gameRef, Camera _camera, Entity _entity)
		{
			gameRef = _gameRef;
			controlManager = new ControlManager(_gameRef.Content.Load<SpriteFont>(@"Fonts\SegoeUIMono_S17"));
			CreateControls(_camera, _entity);
			slotTexture = new Iconset(_gameRef, @"GUI\General GUI\slot");
		}
		#endregion
		#region Xna Methods
		public void LoadContent()
		{
			#region Inventory Management
			foreach (InventoryControl inv in inventories)
			{
				foreach (Vector2 slotPos in inv.SlotPositions)
				{
					InventorySlot slot = new InventorySlot(slotTexture, new Rectangle(0, 0, 32, 32));
					slot.Sprite.Position = slotPos * 32;
					inventorySlots.Add(slot);
				}
			}
			#endregion
		}
		public void Update(GameTime gameTime, Camera _camera, Entity _player)
		{		  
			// Keep code here so dont have to rewrite repositioning code for inventory
			//Inventory.Position  = (Player.Camera.Position + new Vector2(Inventory.OriginalPosition.X, Inventory.OriginalPosition.Y)) * (1 / Player.Camera.Zoom);
			// Varible update the Health, Mana, and stamina bars
			HealthBar.vUpdate(_player.Health.CurrentValue, _player.Health.MaximumValue);
			StaminaBar.vUpdate(_player.Stamina.CurrentValue, _player.Stamina.MaximumValue);
			ManaBar.vUpdate(_player.Mana.CurrentValue, _player.Mana.MaximumValue);
			#region Inventory Management
			foreach (InventoryControl inv in inventories)
			{
				// Update positions
			  
				// Update inventories
				inv.Update(gameTime);
			}
			#endregion
			controlManager.Update(gameTime, PlayerIndex.One);
		}
		public void Draw(SpriteBatch spriteBatch)
		{
			controlManager.Draw(spriteBatch);
			foreach (InventorySlot s in inventorySlots)
			{
				s.Draw(spriteBatch);
			}
		}
		#endregion
		#region Methods
		public void CreateControls(Camera _camera, Entity _player)
		{
			// Textures
			Texture2D BarBackgroundTexture = gameRef.Content.Load<Texture2D>(@"GUI\General GUI\barBackground");
			Texture2D BarFillTexture	   = gameRef.Content.Load<Texture2D>(@"GUI\General GUI\barFill");
			Texture2D BarCoverTexture	  = gameRef.Content.Load<Texture2D>(@"GUI\General GUI\barCover");
			// Controls
			HealthBar = new Bar(_player.Health.CurrentValue, _player.Health.MaximumValue, BarBackgroundTexture, BarFillTexture, BarCoverTexture, Color.Red);
			HealthBar.Position  = new Vector2(0, 0);					
			controlManager.Add(HealthBar);
			StaminaBar = new Bar(_player.Stamina.CurrentValue, _player.Stamina.MaximumValue, BarBackgroundTexture, BarFillTexture, BarCoverTexture, Color.Yellow);
			StaminaBar.Position = new Vector2(0, 39);
			controlManager.Add(StaminaBar);
			ManaBar = new Bar(_player.Mana.CurrentValue, _player.Mana.MaximumValue, BarBackgroundTexture, BarFillTexture, BarCoverTexture, Color.Blue);
			ManaBar.Position = new Vector2(0, 78);
			controlManager.Add(ManaBar);
			BagData bagData = new BagData();
			bagData.Name = "PlayerInventory";
			bagData.Size = new Vector2(5, 5);
			inventory = new InventoryControl(gameRef, new Bag(bagData), _camera, true);
			inventory.Position = new Vector2(0, 0);
			inventories.Add(inventory);
			inventory2 = new InventoryControl(gameRef, new Bag(bagData), _camera, false);
			inventory.Position = new Vector2(0, 0);
		   // inventories.Add(inventory2);
		}
		#endregion
	}
}

If you need anymore information about the system or any more code files just ask.

Thanks for reading

Thekill473

Sponsor:

#2 Faelenor   Members   -  Reputation: 396

Like
0Likes
Like

Posted 24 July 2012 - 02:07 PM

I don't think anyone has time to go through all your code to analyse it. If you want help, you should try to explain what you're trying to do in your code instead of posting it. You may even find the answer while doing it. Also, did you use the debugger to try to find why your coordinates are wrong?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS