Saturday, June 18, 2011

How to add a Basic Inventory

Hey guys it's been a great week here, I have a couple of new things added to my game and I thought I would share them with you. I admit that I have been spending a lot of time this week playing Terraria but I was still able to get some good progress made on my game. The two things I have added this update are

  1. A basic way to add different blocks into the game (still has a long way to go before I'll call it good)
  2. Very basic player inventory showing the block last mined (which I'm going to explain with code in this post)
Now I'm sure a lot of you have read, or been told, or know just from personal experience that when implementing something new into a game, or any software really, it's usually a great idea to create the most basic form of whatever it is you are trying to create. This way there is something to build on and improve on, as opposed to it remaining just an idea inside of your head.

The player inventory is a perfect example of this (in my mind). Instead of trying to create an elaborate inventory system from the start (since I have never made one before), I decided to simplify it and make my inventory (for now) consist of one block (the last block that the player 'mined'). This way I don't have to worry (yet) about managing a list of different items (which I don't even have in the game yet). The way that I do this is simply to add a:
 public Block Inventory;  
In my Player class. This allows me to add other lines whenever I click the left mouse button or right mouse button that (for example) look like this:
 Inventory = TileLayer.blocksDictionary[TileLayer.GetBlock(mouseCell.X, mouseCell.Y).Name + "SingleTop"];  
For the case of left mouse click (which adds the block the player clicked on to the inventory). When the player clicks the right mouse button, I simply have pass the block in the player's inventory to the method that I use to place a block:
 TileLayer.PlaceBlock(mouseCell.X,  
                  mouseCell.Y,  
                  Inventory);  
This very simplistic version of a player inventory is my starting place to expand upon later and add more functionality to it.

And that's the basics of adding in a player inventory. The only trick left to do is display it on the screen (which I do in the Game1.cs file). Basically, I added an itemBackground Texture2D variable with a rounded transparent rectangle as the item background texture. After that I simply told the spriteBatch to draw the item Background and the player's inventory's texture at the top right corner of the screen, thereby displaying a pretty cool new feature in the game. Here's some of the drawing code for a reference:
 spriteBatch.Draw(  
         itemBkgd,  
         hudLocation + new Vector2(titleSafeArea.Width - 42, 10.0f),  
         new Color(48, 48, 48, 100));  
 if (tileLayer.Player.Inventory != null) {  
    spriteBatch.Draw(  
           tileLayer.Player.Inventory.Texture,  
           hudLocation + new Vector2(titleSafeArea.Width - 42 + 16, 26.0f),  
           tileLayer.Player.Inventory.SourceRectangle,  
           Color.White,  
           0.0f,  
           new Vector2(8, 8),  
           1.0f,  
           SpriteEffects.None,  
           0.0f);  
 }  
I hope this has helped you understand that you don't have to have the biggest and best anything when you are first developing it. For most things, it's better to start off with the very basics and add to it as you go along. This helps keep you motivated as well as encouraged to put more things into your game.

And just in case you thought I wasn't going to show the "finished" product:


Check back soon for more updates to my game. On a side note, if you have an idea for a name for this game, leave me a comment because I think it's about time I started calling it something other than 'my game'. Thanks everyone!

3 comments:

  1. This is looking great!
    Just one question, I don't suppose you could help me with making my engine? I am trying to achieve the same as you (merging nickgravelyn's tile engine and the Platform Starter Kit), but I'm having some trouble. I don't suppose you could drop me an email, or something? It would be great to see how you do it, and I would love to see if there's anything I could help you with, because I really love your blog, and your game.

    Terraria is awesome! :P

    ReplyDelete
  2. Sure I don't mind helping out where I can. Feel free to email me (check my profile for my email address) and I'll see what I can do.

    ReplyDelete
  3. Great, thanks.
    Also, some name ideas:

    SimplyRPG
    Terrain RPG
    Cavern Constructor
    World Explorer

    Just a few ideas.

    ReplyDelete