Josh-CO Dev

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

XNA – Object or Character Dialog

Leave a comment


Another one from the archives. This one is a goodie! Enjoy!

One thing that was important to me in my latest game was to have actual dialog show on the screen. When the player interacts with objects, I want a box to display on the screen to give them immediate feedback. Granted, my implementation makes some basic assumptions and is quite simple, it is working and scaling quite well for me. Here is a basic implementation:

using System;
using FarmGame.Inventory;
using FarmGame.TileEngine;

namespace FarmGame.Dialogue
{
    static class GameObjectDialogue
    {

        static public string GetOutput(InventoryItem item, GameObject go, out bool harvestable)
        {
            harvestable = false;

            if (go.thisType == GameObject.ObjectType.rock)
                return GetRockOutput(item, out harvestable);
            if (go.thisType == GameObject.ObjectType.bush)
                return GetBushOutput(item, out harvestable);
            if (go.thisType == GameObject.ObjectType.tree)
                return GetTreeOutput(item, out harvestable);
            if (go.thisType == GameObject.ObjectType.house)
                return GetHouseOutput(out harvestable);
            
            return string.Empty;
        }

        private static string GetHouseOutput(out bool harvestable)
        {
            harvestable = false;
            return "This is your house.";    
        }

        private static string GetTreeOutput(InventoryItem ii, out bool harvestable)
        {
            if (ii.ItemName == "axe")
            {
                harvestable = true;
                return "Press space to chop this tree.";
            }
            else
            {
                harvestable = false;
                return "You must have the axe equipped.";
            }
        }

        static public string GetRockOutput(InventoryItem ii, out bool harvestable)
        {
            if (ii.ItemName == "pickaxe")
            {
                harvestable = true;
                return "Press space to mine this rock.";
            }
            else
            {
                harvestable = false;
                return "You must have the pickaxe equipped.";
            }
        }

        static public string GetBushOutput(InventoryItem ii, out bool harvestable)
        {
            if (ii.ItemName == "scythe")
            {
                harvestable = true;
                return "Press space to harvest berries.";
            }
            else
            {
                harvestable = false;
                return "You must have the scythe equipped.";
            }
        }
    }
}

I won’t get too into the details as it should be self explanatory. In this case, you are passing in the equipped item and the object you are interacting with. It will then return if the object is harvestable. In my case, I have rocks, bushes, trees, and a house.

Calling it from your code is equally as simple. I have this in the update method of my level class:

            bool dialogBoxNeeded = false;

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;
                        }
                    }
                }

                box.DrawBox = dialogBoxNeeded;

Basically, the player has a seperate bounding box that projects in front of them. We are checking this bounding box to see if it interacts with a game object. If so, we note the position for our box, determine the text, and flag the code as needing a dialog box.

Now we need to call the draw method:

            if (this.box.DrawBox)
                this.box.Draw(sb);

I’m not going to step through it, but here is the box class:

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Text;
using Microsoft.Xna.Framework.Content;

namespace FarmGame.UI_Elements
{
    class Box
    {
        public Text textHandler { get; set; }
        public Texture2D Texture { get; set; }
        public Vector2 Position { get; set; }
        public bool DrawBox { get; set; }

        //private int TextureWidth { get; set; }
        //private int TextureHeight { get; set; }

        private string Output;
        //StringBuilder sb;

        public Box(ContentManager cm)
        {
            this.Texture = cm.Load(@"UI\popuptext");

            textHandler = new Text(cm, @"Fonts\boxSpriteFont");
            textHandler.TextColor = Color.White;
        }

        public Box(ContentManager cm, string output, Vector2 coords)
        {
            this.Texture = cm.Load(@"UI\popuptext");
            this.Output = output;
            this.Position = coords;

            textHandler = new Text(cm, @"Fonts\boxSpriteFont");
            textHandler.TextColor = Color.White;
        }

        public void Draw(SpriteBatch sb)
        {
            sb.Draw(this.Texture, this.Position, null, Color.White );

            textHandler.FontPos = new Vector2(Position.X + 15, Position.Y + 15);
            textHandler.DrawText(sb);
        }
    }
}
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