Josh-CO Dev

Solving the worlds problems one line of code at a time.

XNA – Basic Inventory Tutorial, Harvesting

Leave a comment


Yet another from the archives. I apologize that I’ve still been unable to get part 2 of the inventory system. It’s kind of needed for this to work, but I’m sure some of you can put something together with it.

To further build on our tutorials for our basic inventory system, let’s add a basic system to harvest our resources. In my code, I have added a new folder named crafting which I intend to use for anything even remotely crafting related. I then created a new class called Harvesting.cs. Here is the code:

using System;
using FarmGame.Inventory;
using FarmGame.TileEngine;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace FarmGame.Crafting
{
    static class Harvesting
    {
        public static void Harvest(GameObject go, ref PlayerInventory CurrentInventory, ref List objects, ContentManager cm)
        {
            if (go.thisType == GameObject.ObjectType.rock)
               HarvestRock(go, ref CurrentInventory, ref objects, cm);
            if (go.thisType == GameObject.ObjectType.bush)
                HarvestBerryBush(go, ref CurrentInventory, ref objects, cm);
            if (go.thisType == GameObject.ObjectType.tree)
                HarvestTree(go, ref CurrentInventory, ref objects, cm);
        }

        private static void HarvestTree(GameObject g, ref PlayerInventory inventory, ref List objects, ContentManager cm)
        {
            objects.Remove(g);

            inventory.AddItemToInventory(new InventoryItem("wood", 5, cm.Load(@"Objects\wood"), InventoryItem.InventoryType.craft));
        }

        private static void HarvestBerryBush(GameObject g, ref PlayerInventory inventory, ref List objects, ContentManager cm)
        {
            objects.Remove(g);

            inventory.AddItemToInventory(new InventoryItem("berry", 5, cm.Load(@"Objects\berries"), InventoryItem.InventoryType.craft));
        }

        private static void HarvestRock(GameObject g, ref PlayerInventory inventory, ref List objects, ContentManager cm)
        {
            objects.Remove(g);

            inventory.AddItemToInventory(new InventoryItem("stone", 2, cm.Load(@"Objects\stones"), InventoryItem.InventoryType.craft));
        }
    }
}

As per usual, the code should be simple and easy to understand. The one difference here compared to most of my other code is that this is a static class. To put it simply, this means that you do not need to declare a new instance of the class before you call a method, it is loaded in memory when the game launches. Simply just call Harvesting.Harvest, pass in the object you are trying to harvest and the player ineventory. I’m also passing in the list of game objects so that we can handle all object manipulation (such as removing the object from the game) directly from this class.

For my implementation, I am checking for a space key press when it is checking all of my game objects. Here is the relevant code:

if (tileRect.Intersects(Player.fowardBox()))
                {
                    bool harvestable;

                    box.Position = new Vector2(tileRect.X + 5, tileRect.Y + 5);

                    //box.textHandler.Output = "Press space to mine this rock.";
                    box.textHandler.Output = GameObjectDialogue.GetOutput(Inventory.EquippedItem, go, out harvestable);
                    dialogBoxNeeded = true;

                    //check for action key
                    if (keyState.IsKeyDown(Keys.Space) && !prevKeyState.IsKeyDown(Keys.Space))
                    {
                        //add additional code here to return items 
                        if (harvestable)
                        {
                            Harvesting.Harvest(go, ref Inventory, ref GameObjects,CM);                    //this.GameObjects.Remove(go);
                            return;
                        }
                    }
                }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s