jump to navigation

Game actor state management April 14, 2010

Posted by Jesse in : Game Development , 1 comment so far

I could use a little advice.

Slowly but surely, my new game is starting to take shape. Recently, however, I’ve run into a bit of problem.

In Being, controlling the state of a game actor (both physics and animation) was very straightforward. After all, there were only a few options. Standing still, walking, jumping, and reacting to taking damage. There was no special work to transition between those states, and they were all very clearly defined so there was never any ambiguity about the status of the player.

I started out with a switch statement. Here’s a trimmed down version:

switch(state)
{
	case State.Idle:
		if(playerHit())
		{
			state = State.Hit;
			velocity.Y = HIT_VELOCITY;
			playAnimation("Hit");
			playSound("Ouch");
		}
		else if(playerJumped())
		{
			state = State.InAir;
			velocity.Y = JUMP_VELOCITY;
			playAnimation("Jump");
		}
		else if(playerLeft())
		{
			state = State.Walking;
			direction = Direction.Left;
			velocity.X = -WALK_VELOCITY;
			playAnimation("Walk");
		}
		else if(playerLeft())
		{
			state = State.Walking;
			direction = Direction.Right;
			velocity.X = WALK_VELOCITY;
			playAnimation("Walk");
		}
		break;
	case State.InAir:
		...
}

You can see, for even something as simple as this basic player, this becomes a very long chunk of code quickly. I read an article showing how I might break each state up into a class. You’d have a basic ActorState class, and derive from that to have an IdleState, InAirState, WalkingState, etc. This would simplify a few things, since any transition effects of changing state would be handled in the OnEnter method of the correct class.

An example of that:

class WalkingState : ActorState
{
	public override void OnEnter()
	{
		playAnimation("Walk");
	}

	public override string Update(GameTime gameTime)
	{
		if(playerRight())
		{
			velocity.X = WALK_VELOCITY;
			direction = Direction.Right;
			return "Walking";
		}
		else if (playerLeft())
		{
			...
		}

		...

		return "Idle";
	}
}

Well, that works, and certainly looks a little cleaner to me. But now I’ve got a whole mess of classes in a really big file, or I can stretch the player’s state code over a whole mess of little files. And if I do that for every Actor in the game, it would be a huge nightmare to find anything if I needed to make a change.

And now my big problem. While simple, my new game is significantly more complicated than Being. Currently the player can stand idle, walk, jump, double jump, and throw weapons. I’d also like to add wall jumping and possibly melee attacks. This pile of options leaves room for all sorts of ambiguity, as well as a bunch of transition issues. My code is starting to become unwieldy and impossible to maintain. I know it’s time to refactor, but I’m not really sure how to make it better.

And this is still a pretty simple game, all things considered. I can’t imagine how complicated things are in a modern big budget game.

Since every game needs this sort of system, this is a pretty well solved problem, right? I welcome all advice anyone wants to share.

Tiled Map Editor (Part 3) April 1, 2010

Posted by Jesse in : Game Development , 6 comments

Update: Due to recent updates of the TiledLib project, the inline code below will no longer work without a few modifications. I may update it at some point in the future, but even in the current form, the concepts are still valid.

Since my last post, Nick has uploaded the TiledLib project to CodePlex. There have been a bunch of pretty cool updates. You should check it out!

I’ve been having a blast playing around with a new game using Tiled as my level editor, and it’s been a great opportunity for me to play around C# features I’ve never had much use for, but wanted to learn about. In my last post, I showed how we could use reflection to find classes that matched the types in our Tiled map objects, and automatically generate them. In this article, I’ll take that a step farther, and do the same thing to automatically fill out the properties of that class.

(more…)

Tiled Map Editor (Part 2) March 17, 2010

Posted by Jesse in : Game Development , 2 comments

If you haven’t already, read part one first.

Now that we can load Tiled maps into our game, what can we do with the data? In his article, Nick mentioned that you can create layers dedicated to rectangles with attached metadata. In the editor, these are called object layers. (In the code, the type is MapObjectLayer, containing a collection of type MapObject.)

(more…)

Tiled Map Editor (Part 1)

Posted by Jesse in : Game Development , 1 comment so far

A couple of weeks ago Nick Gravelyn posted a really great article explaining why he had given up on writing his custom level editor in favor of using the Tiled Map Editor. Along with the article, he uploaded some source code for reading Tiled maps into an XNA Game Studio based game. Since I’ve been procrastinating lately on my next game, I downloaded Tiled, grabbed Nick’s code and started tinkering.

Using some of my existing code, I’ve got a basic platformer up and running in just a few hours of work. If you’re interested in writing a tile based game, I’d recommend checking it out. However, I have a couple of note on using Tiled and Nick’s code:

  1. Layer names must be unique.
  2. Internally, the layers are stored in a Dictionary, with the layer name as the key. If you happen to have two layers with the same name (for example they might be named “New Layer” if you failed to rename them) you will get a thrown exception.

  3. Don’t use a tile size that doesn’t match.
  4. For some reason, when you create a tileset, Tiled allows you to specify a size different from the size you used when you created your map. The result in Tiled looks somewhat strange to me, and the example renderer in Nick’s code doesn’t handle it in quite the same way. I think this is an odd feature anyway, so my advice is to just don’t do it unless you really need to.

  5. Be aware of file path issues.
  6. Nick included the ability to set a custom tileset path in the map processor, which is really handy. Unfortunately, Tiled stores the tileset path using relative paths, which can confuse Path.Combine. If you keep the map and tileset in the same folder, you can ignore this problem.

    Alternatively, you can slightly modify the map processor. In the file TmxProcessor.cs, change line 27 from:

    string path = string.IsNullOrEmpty(TileSetDirectory) ?
        tileSet.Image : Path.Combine(TileSetDirectory,
        tileSet.Image);
    

    to:

    string path = string.IsNullOrEmpty(TileSetDirectory) ?
        tileSet.Image : Path.Combine(TileSetDirectory,
        Path.GetFileName(tileSet.Image));
    

That’s it for now. In part two, I’m going to take a look at how you might go about creating game objects from the object layers in your Tiled map.

Line Segment Intersection (Update) February 23, 2010

Posted by Jesse in : Game Development , add a comment

I’m not sure why, but lately there’s been a lot of interest in the post I wrote about line segment intersection. I get emails about it pretty regularly, and that post has the most comments of anything I’ve written here. Today, a comment was posted explaining that there’s a flaw in my code. The code cannot tell when two line segments are coincident, but not overlapping. There’s a post on Stack Overflow which demonstrates a solution to this problem.

(more…)

Art stuffs October 22, 2009

Posted by Jesse in : Game Development , 3 comments

Three posts in one day? What’s going on here? :P

A little while back, Nick Gravelyn posted about his experiments into pixel art. As it happens, that’s something that I’ve been messing with lately myself.

Meet Blobby:

Yeah, I know he’s rough. It’s programmer art, but I’m working at it. :)

I have two different 70% code complete games just sitting around waiting for art. I can’t afford to hire an artist. (I have a four month old daughter. I’m broke.) So I’ve been doodling alot. I got some art books, and I’ve read countless tutorials. There’s all kinds of neat stuff available online. Check out the demo video for one of the Don Bluth dvds.

I’m probably never going to be an artist. But maybe if I keep at it, I can have good enough programmer art to get the job done while I save up to hire a real artist.

A good month for Being

Posted by Jesse in : Being, Game Development , add a comment

Every few days I still take a look at the sales data for Being to see if anybody is buying it. In the last few months, sales had dropped off to about one sale every day or two. Earlier this month, however, I logged in to find I’d sold nineteen copies on October 11th. I assumed it was some sort of fluke, but then on the 12th another ten copies sold. This left me wondering, “What in the world is going on here?”

I guessed that the game had been reviewed by a magazine or website, and that’s what had driven the traffic, but after much web searching I couldn’t find anything. (Let this be a warning to other developers. Do not use names for your game that are impossible to Google for.) It turns out that Being had been added to the “IGN.com Top Picks” on the Xbox Indie Games section of the 360 dashboard. Sweet! As far as I can tell, there’s no website or blog to go along with their picks, so I just grabbed a shot of the tv with my digital camera to add to my collection of press stuff.

Well, it’s been nearly two weeks since then, and I’m happy to report that Being has seen a tremendous spike in downloads. So far this month, I’m up to 1076 trial downloads, and 137 sales. I’ve almost had as many downloads in the last 11 days as all of the last quarter. Crazy!

The additional downloads have raised the game’s position on the “most popular” charts. Here’s a screenshot I just grabbed from Retronator. (Click the images for a bigger view. The layout of the blog isn’t really suited to wide images, and these versions are pretty hard to read. Sorry.)

Here is a chart I plotted showing Being’s sales stats since launch. There are a few interesting dates to notice. There’s a huge spike around November 21, 2008, which corresponds to the initial launch of the XNA Community Games. The next spike is around July 21, 2009, which is around the time of the rename to Xbox Indie Games, the addition of several new countries, and the price drop to one dollar. And finally, around October 12, 2009, you can see the spike that has me so excited. Whee!

Just for fun, here’s a chart showing the trial downloads. All of the same spikes are visible, but what’s really interesting to me is that the initial spike is so huge it makes everything else look tiny. It’s hard for me to believe that nearly 1400 people downloaded the trial in a single day, but I’ve got the data to prove it. Crazy.

And finally, last quarter’s sales have been tallied, and Being has now earned us over $1000. (That’s after Microsoft’s cut, but before taxes.) That’s pretty amazing for a game that I was expecting to get about 50 sales total.

Being – The Final Level September 9, 2009

Posted by Jesse in : Being, Game Development , add a comment

I recently received an email asking me for tips on beating the last level of Being, and since it’s a rather common occurrence, I put together a Youtube video of me playing through it. I can get through it almost every try, but I’ll admit it took me three tries to get through without getting hit. I had a couple of close calls with the bees, but pulled it off.

The framerate looks a little odd, and I’m not sure why. Might just be a compression artifact, or because Youtube doesn’t like that the video was recorded at 60 frames per second.

I’ve embedded it below, but the blog format is a bit narrow, so it looks a bit small. Click here to watch it quite a bit bigger over on Youtube.

A little news August 12, 2009

Posted by Jesse in : Being, Game Development , add a comment

I found a great review of Being yesterday. The author really enjoyed the game, and went into great detail about it.

The rating system is now in place on the 360 dashboard. If you enjoyed Being, please take a second to go leave a rating. To find it, on your xbox 360, go to Game Marketplace, Xbox Indie Games, Browse Games, All games. Then scroll down to find Being. Press X to bring up the rating box, then press A to add stars. If you haven’t yet tried it, you can download it to your xbox on this page.

Finally, work slowly continues on the next game. I have a good deal of the basic framework code in place, and an artist is working on some concept sketches for the look of the main characters. Hopefully I’ll be able to share something soon.

Being sales data April 2, 2009

Posted by Jesse in : Being, Game Development , 2 comments

It’s been awhile since I’ve written anything. I’ve gotten really into playing Street Fighter 4, and haven’t felt the urge to write video games lately. I do have something interesting to share, though.

Microsoft gave us access to the sales data for Community Games recently. As of March 30th, 2009, the trial version of Being has been downloaded 9084 times. Out of those, 485 people decided it was worth their money and purchased it. From the data, I had a rush early on, but now I get a sale every day or two.

Here’s a breakdown by country:

Canada: 33 (6.8%)
France: 4 (0.8%)
Italy: 4 (0.8%)
Spain: 5 (1.0%)
United Kingdom: 66 (13.6%)
United States: 373 (76.9%)
Total: 485

For my first game, I have to say I’m totally stoked by these numbers. This isn’t the sort of sales I would need to start a business, but that wasn’t the goal here.

If you haven’t yet given the game a go, you can download the demo to your Xbox 360 right here. If you try it, please tell me what you think.

I’ve got an idea for a new game. It’s time to get back to work!