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


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

Basic Tile Engine Tutorial – Creating our player class

If you haven’t completed the previous tutorial yet, be sure you do so. You can find it here. Also, if you want to download the code for all the way through this tutorial, you can find it here.

In this tutorial, we finally get around to the fun stuff and get the base for an actual working game. This tutorial will cover adding our player class, animating the player, etc.

First, create a new folder in your content project and name it peeps. You can really name it whatever you want, but the code I have written points to “peeps”. Download the below image and place it in the peeps folder.

Now that we have our sprite sheet, let’s start the tutorial.

Continue reading


2 Comments

Basic Tile Engine Tutorial – Creating our player class base

Update As with the other tutorials. I have whipped up a new solution for everything that we have covered so far and placed it here. Please feel free to download and have a working demo. I have also cleaned up the code a bit from the tutorial. Going forward, I will try to invest the time needed to clean up the code as I write the tutorials.

This is the third tutorial in our tile engine series. This tutorial will cover starting our player class to make our game playable! Please be sure that you have followed the previous tutorial located here first. This is probably the one component that if I had to do everything over again, I would have done it different. Revisiting this, I likely would have implemented a physics engine such as farseer.

-Start by adding a new class to your project and name it animatedSprite.
-As usual, add all the xna namespaces
Continue reading


1 Comment

Basic Tile Engine Tutorial – Creating our map class

(Update) Just like our first tutorial, I have been recreating this tutorial from scratch to give you a code example. You can download this example here. Also, I have added another method that was missing from the original post named addNewSprite. I’ve also taken the time to really clean up the code and make it more readable.

This is the second part of our basic tile engine tutorial. This tutorial will cover the major programming concepts needed to create a platformer type game. In the previous tutorial, we covered creating our tile class which will be used for our individual tiles. You can find this tutorial if you missed it.

Now that we have our basic tile sprite class, we can start building out our class that will hold our map. Start by creating a new class and naming it Level.cs.

-Include the usual stuff. These are all the namespaces that are needed by default in an XNA game.

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Xml;
using System.IO;

Continue reading


Leave a comment

Basic Tile Engine Tutorial – Creating our tile class

Since I originally wrote this article, I have rolled up a new XNA solution that contains only a simple screen management solution and this class. You can find it here.

This tutorial series will follow all of the major steps needed to make a platform type of tile engine. I apologize that I am rusty on my XNA, all of this code is being pulled from projects that I have not worked on in several months. In many places, the code can be improved, which we will do a little bit of in future tutorials (we will convert this engine into more of an RPG type of tile engine). We will not go through all of the exact steps required to build the project, but will cover the major pieces. When finished, you should be able to write a game similar to the image below. I apologize if some of my explanations are not clear as I am not currently coding in XNA, but I will try to explain the code the best that I can.

-The first thing we need to do is create a new class. If you have never added a new class in visual studio before, right click on your project name or a specific folder and choose “add” and then “class.” Name this class TileSprite.cs.

-Include the usual stuff in your namespace section.

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Xml;
using System.IO;

-Add the following properties:
Continue reading