Josh-CO Dev

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


Leave a comment

XNA – Basic Enemy AI

Here’s another from the archives!

One thing I was surprised to find in XNA, was just how simple it really ended up being to add some incredibly basic enemy AI and pathfinding. In my game, the enemies are dumped onto a platform and then are bound by all the same rules of gravity as the player. Initially, they all suffered from Super Mario Bros syndrome where they would just walk off the platform and die. The solution was surprisingly simple: read which way the enemy is facing, project out a separate bounding rectangle, and then go through all of the tiles in our map and see if there is an intersection. If there is not, then the enemy is at the end of the platform and needs to turn around. All of this is called from the update method of our enemy. Here is the relevant code:

public void PathFinding(GameTime gt)
{
int maxX = this.SpriteWidth; // *2;
bool endOfPathFound = true;



Rectangle pathFindingRect = new Rectangle();

if(direction == Direction.Right)
pathFindingRect = new Rectangle(((int)Position.X - this.SpriteWidth/2) + maxX, (int)(Position.Y -this.SpriteHeight) +1,
this.SpriteWidth, this.SpriteHeight);
else if(direction == Direction.Left)
pathFindingRect = new Rectangle(((int)Position.X - this.SpriteWidth / 2) - maxX, (int)(Position.Y - this.SpriteHeight) + 1,
this.SpriteWidth, this.SpriteHeight);

foreach (TileSprite ts in level.Map)
{
if (ts.Landable)
{
if (pathFindingRect.Intersects(ts.BoundingBox))
{
endOfPathFound = false;
}
}
}

if (endOfPathFound)
{
if (direction == Direction.Left)
direction = Direction.Right;
else if (direction == Direction.Right)
direction = Direction.Left;
}
}
Advertisements


Leave a comment

XNA – Sprite Sheet Animation

Dug this article out from the old archives and thought I would post it here. Context might not make sense, but the basic code should work all the same. You can tie this into the tile engine tutorial.

Haven’t had too much time to do any programming this week, but I did just convert my player animation engine to animate a sprite based on a sprite sheet rather than a horizontal imageset. Here is the relevant code.

I realize that this needs to be more object oriented, but I’ll get there.
This is added to the animated sprite class:

public int YFrame
        {
            get { return yFrame; }
            set { yFrame = value; }
        }
        int yFrame = 0;

All the rest of the code is in the player class

private void AnimateLeft(GameTime gt)
        {
            if (currentKBState != previousKBState)
            {
                //CurrentFrame = 5;
                CurrentFrame = 0;
                YFrame = 1;
            }

            Timer += (float)gt.ElapsedGameTime.TotalMilliseconds;

            if (Timer > Interval)
            {
                CurrentFrame++;

                //if (CurrentFrame > 7)
                    //CurrentFrame = 4;
                if (CurrentFrame > 2)
                    CurrentFrame = 0;

                Timer = 0f;
            }
        }

        private void AnimateRight(GameTime gt)
        {
            if (currentKBState != previousKBState)
            {
                //CurrentFrame = 9;
                CurrentFrame = 0;
                YFrame = 2;
            }

            Timer += (float)gt.ElapsedGameTime.TotalMilliseconds;

            if (Timer > Interval)
            {
                CurrentFrame++;

                //if (CurrentFrame > 11)
                   //CurrentFrame = 8;
                if (CurrentFrame > 2)
                    CurrentFrame = 0;

                Timer = 0f;
            }
        }

SourceRect = new Rectangle(CurrentFrame * SpriteWidth, YFrame * SpriteHeight, SpriteWidth, SpriteHeight);

And that’s it. Just use the code that we had from the previous articles, and the animation will now pull from the sprite sheet.


Leave a comment

C# Nullable datatypes – How to set a variable to accept nulls

This is a nifty little trick that I almost forgot about until today. Often times you need to pass a null value into a non-nullable datatype, such as an int. If you don’t know a trick, this can be pretty difficult. Here’s the trick:

int? varName;

Simple eh? Just add a question mark to the end of the datatype. This will force it to become a nullable datatype.


Leave a comment

Why not to use the “delete before publishing feature” in Visual Studio

So, I learned an important lesson yesterday, I narrowly avoided wiping the entire site so far. Not that I have a whole lot of content yet, but it would have been a bit heartbreaking to say the least. I am working on a personal project that involves setting up a subdomain on this site that will host a silverlight application. My web project is set to delete all files and replace them when I perform a publish. Well, I am troubleshooting some issues with a WCF service and posted some files in my root directory. A couple of mistakes later and I accidently tried to publish my project to the root drive rather than the folder that houses my subdomain. Many of my folders and all of my files were wiped from the root directory. Thankfully, I happened to have a backup of most of the files and most all wordpress content is housed in the database. Talk about a rough lesson to learn!


Leave a comment

.NET – Querying data from a binding source

Rather than write code that is chatty with the database, it is possible to pull all the data that your user could ever need from a table or procedure and store it in a dataset. Normally you would then drop in a binding source to link up your datagrid and the dataset. In some cases, you may want to search the data in the grid to find a particular piece of data. Maybe you want to give your users the ability to enter a value in a search box and navigate directly to that record. Using the binding source, this is as simple as two lines of code.

Here it is:

int itemFound = bindingSource.Find(”Field”, Convert.ToInt32 (ValueToFind));

bindingSource.Position = itemFound;


Leave a comment

.NET – Filtering on a BindingSource

So here is another cool trick that I picked up recently. It is incredibly simple to filter on a binding source. Using this code, you can pull all the data back into a DataGrid, other other tool, and then let the users filter the data using the binding source. Thus, you can delimit the data without making subsequent calls to the database.

Here’s the code:

BingingSource.Filter = “FieldName= “Variable + “AND FieldName2= ” + Variable2;


Leave a comment

XNA – Simple Collision Detection

Here is some incredibly simple code to check for collisions. The method is accepting an x and a y coordinate. The for loop then loops through each object on the screen and checks first to see if the x value is within the x-range of the object. If that check returns true, it performs the same check against the y-range. If all checks are true, we remove the object from the list so it will no longer be drawn and increase the score.

Continue reading