Question

I'm building my first inventory and I want it to work like any other basic inventory that is:

  • When you pick up something it will add it to your inventory that you later on can equip.
  • It will look and work similar to Realm of the mad god's inventory.

I got some of the basic stuff done like:

  • Drawing inventory slots and when you mouse over they change texture.
  • Move an item/object around with your mouse.

and that's about it.

So now I'm stuck the part that involves how to handle items, I know that there are several ways to handle items but I don't know what would be the easiest way to do it in the current code that I'm using.

If anyone could point me in the right direction I would be very greatful and it would save me a lot of time!

Here's the code btw:

class InventorySlots
{
    Texture2D box;
    Texture2D blackbox;
    Texture2D empty;
    const int offSet = 100;
    Rectangle[,] Inventoryslots = new Rectangle[6, 4];
    Rectangle testrect; // Rect for moving item
    bool isMoving;

    public void LoadContent(ContentManager Content)
    {
        box = Content.Load<Texture2D>("Box");
        blackbox = Content.Load<Texture2D>("boxselected");
        testrect = new Rectangle(10, 20, box.Width, box.Height);//Set up my test rect
        empty = box;

        for (int x = 0; x < 6; x++)
        {
            for (int y = 0; y < 4; y++)
            {
                Inventoryslots[x, y] = new Rectangle((x * box.Width) + offSet, // Setup my inventory slots
                     (y * box.Height) + offSet, box.Width, box.Height);
            }
        }
    }

    public void Update(GameTime gameTime)
    {
        if ((ButtonState.Pressed == Mouse.GetState().LeftButton))
        {
            if (testrect.Intersects(new Rectangle(Game1.mousePosition.X, Game1.mousePosition.Y, 0, 0)))
            {
                isMoving = true;
            }
        }
        else
        {
            isMoving = false;
        }
        if (isMoving)
        {
            testrect = new Rectangle(Game1.mousePosition.X - testrect.Width / 2, Game1.mousePosition.Y - testrect.Height / 2, testrect.Width, testrect.Height);
        }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        for (int x = 0; x < 6; x++)
        {
            for (int y = 0; y < 4; y++)
            {
                if (Inventoryslots[x, y].Contains(Game1.mousePosition))
                {
                    empty = box;
                }
                else if (!Inventoryslots[x, y].Contains(Game1.mousePosition))
                    empty = blackbox;

                spriteBatch.Draw(empty, Inventoryslots[x, y], Color.White);                   
            }
        }

        spriteBatch.Draw(box, testrect, Color.White);//Draw my test item that i can move around
    }
}
Was it helpful?

Solution

The code you have demonstrates a lack of understanding of Object Oriented Principles, everything in your game should be an object or an interface.

public class Item 
{
    private Texture2D texture; 

    private Vector2 position; 
    public Vector2 Position { get {return position; } } 

    private Rectangle Bounds; 

    public bool Collides(Vector2 position)
    {
         Bounds = new Rectangle(this.position.X, this.position.Y, 
                  this.texture.width, this.texture.height); 

         if (Bounds.Intersects(position))
             return true; 
         else return false; 
    }
}

using the principles of Object Oriented Programming, you would make a specific item derive from the item base class, like so:

public class Sword : Item 
{
     public Sword() { } 
}

the idea behind OOP is that everything in your code is expressed literally, base classes contain properties and functions that encapsulate all of their derived children. So Sword would inherit position, Bounds, texture, so on and so forth.

This is smart because it allows you to program things once and then re-use that code for each item.

I'm not going to write your code for you, but start by defining your items like this:

private Item[,] Inventoryslots = new Item[6, 4]; 

I'm going to keep this to a minimum because I don't have a lot of time, but if I could point you in a direction it would be to learn more about Object Oriented Principles in the C# computer programming language. Google that and you're golden.

OTHER TIPS

inventory is quite huge part of game. you can items to list, example writen from head.

Public Class Item
     Public ID as integer // id of item (apple=1, hammer=2)
     Public Position as vector2 // position in your inventory
     Public Equiped as boolean = false;
End Class

Public Class Items
     Inherit List of(Item)

     Sub Add(id)
         dim Item as new Item
         Item.ID = id
         // find first free slot and apply position to Item.Position
         me.add(Item)
     End Sub

     Sub Remove(id)
         me.removeAll(function(c) c.id = id)
     End Sub

     Sub Relocate(id, newPostion)
         // example of looping
         For Each Item as Item in Me
             If Item.ID = id Then
                 Item.Position = newPosition
             End If
         Next
     End Sub

     Sub Equip(id)
         Dim Item as Item = me.find(function(c) c.id=id)
         Item.Equiped = true;
     End Sub

End Class

You can create enumerator like this:

Enum InventoryItems
    1 = Apple
    2 = Sword
End

You declare main vairable: Public Inventory as New Items. To add new item using enumerator: Inventory.Add(InventoryItems.Apple) but then add function must accept enums.

it's not that complicated once when you understand how it works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top