Nico's digital footprint

I grew up in the nineties, that makes me awesome by default

Weekly update on my XNA project

by Nico

My first week back on the job is finished, that means I’ve had about 5 hours of time to work on my lunch game. Progress started of pretty small because of some enemy placement issues that showed up after I changed the complete control scheme. After loosing about an hour of time trying to fix it, I’ve decided to revert back to the virtual thumbsticks. About half an hour and some Mercurial commands later I was back to the original controls, the enemy movement behavior also reverted back to normal.

Day 2 I decided it was about time to make the enemy move, the character itself was moving up and down but without any animation, he was just sliding up and down. My animations are done using a spritesheet, the eight walking directions each have a line in the spritesheet. In the code itself I have a rectangle that has the width and height of one texture on the sheet, using the Update method I move the rectangle after x amount of time. This is the piece of code that creates the animation

protected Rectangle SourceRectRunning;
protected readonly TimeSpan RunRate = TimeSpan.FromSeconds(0.1);
protected TimeSpan RunTimer;

public override void Update(GameTime gameTime)
{
    RunTimer -= gameTime.ElapsedGameTime;

    if (RunTimer <= TimeSpan.Zero)
    {
        if (CharState == State.Running)
        {
            if (SourceRectRunning.X >= TextureWidth * 7)
            {
                SourceRectRunning.X = 0;
                return;
            }

            SourceRectRunning.X = SourceRectRunning.X + (int)TextureWidth;
        }

        RunTimer = RunRate;
    }
}

 

The CharState is just en enum that I’ve build. The enum looks like this

public enum State
{
    Running,
    Standing,
    Spotted,
    Walking
}

This makes it very easy for me to call the right code when the player starts moving or when he’s spotted by the enemy.

I was very happy of how the animation looked like, so I figured it was about time to do something about the collision detection. This is something I’ve implemented quite some time ago but it never really worked how it should. The way collision detection is working in my app is a pretty nifty one. I found it in the demo code of the tile engine that I’m using.

A map build with Tiled consist of several layers, I have a layer that contains only the floor textures and a second layer that contains stuff like trees, walls and other non-interactable objects. The demo project contained with the WP7 Tiled engine has a method that can get the color of a certain tile on a certain layer. I take the player’s position, add a few pixels to it and determine the color on that location of the Objects layer, if the color is NULL then there’s no object in the way. Let me show you the code

Color? collColor = _level.Map.GetColorAt("Objects", _player.WorldPosition + 
    (VirtualThumbsticks.LeftThumbstick * 5));

if (collColor == null)
{
    //player can move
}

The GetColorAt method looks like this

public Color? GetColorAt(string layerName, Vector2 position)
{
    var l = GetLayer(layerName);

    TileLayer tileLayer = l as TileLayer;

    position.X = (int)position.X;
    position.Y = (int)position.Y;

    Vector2 tilePosition = new Vector2((int)(position.X / TileWidth), (int)(position.Y/TileHeight));

    Tile collisionTile = tileLayer.Tiles[(int)tilePosition.X, (int)tilePosition.Y];

    if (collisionTile == null)
        return null;

    if (collisionTile.CollisionData != null)
    {
        int positionOnTileX = ((int)position.X - (((int)position.X / TileWidth) * TileWidth));
        int positionOnTileY = ((int)position.Y - (((int)position.Y / TileHeight) * TileHeight));
        positionOnTileX = (int)MathHelper.Clamp(positionOnTileX, 0, TileWidth);
        positionOnTileY = (int)MathHelper.Clamp(positionOnTileY, 0, TileHeight);

        int pixelCheckX = (collisionTile.Source.X) + positionOnTileX;
        int pixelCheckY = (collisionTile.Source.Y) + positionOnTileY;

        return collisionTile.CollisionData[(pixelCheckY * collisionTile.Texture.Width) + pixelCheckX];
    }
    else
    {
        return null;
    }
}

As I’ve said, this piece of code comes from the Tiled engine I’ve found on this site.

The problem I was having before was that I was passing the player’s coordinate on the screen to the method instead of the player’s position on the map. It does work much better now but currently I’m passing the coordinate of the upper left corner of the texture (XNA default) so it does need some fine-tuning.

I’m thinking next week to do some more work on the level editor or maybe start working on interactable objects. I’ll write another post next week, so stay tuned or follow me on Twitter to get daily updates.


Tags:

.Net | Game development | WP7 | XNA

blog comments powered by Disqus
  Log in

About the author

Hi,

My name is Nico, I’m an MVP Windows Platform Development living in Belgium.
I’m currently employed as a .NET consultant at RealDolmen, one of Belgium’s leading IT single source providers.

I'm also founding member and board member of the Belgian Metro App Developer Network, a user group focussed on Windows 8 and Windows Phone development. If you're in Belgium feel free to drop by if we're doing an event. http://www.madn.be

Since June 2012 I'm a proud member of Microsoft's Extended Experts Team Belgium. And in February 2013 I became a member of DZone's Most Valuable Bloggers family.

In 2013 I became a book author and wrote "Windows 8 app projects, XAML & C# edition".

In 2014 I received the MVP award for the very first time.

I hope to get feedback from my readers either through comments, mail (nico_vermeir@hotmail.com), twitter, facebook, …

 

mvp

 

 

MeetLogo

 

MVBLogo

mybook

 

Month List