IGameActionManager interface for simplifying input management.

While reading all the latest WP7 buzz, I got to thinking that I am going to need some way of managing input between devices so as not to have a billion #if WINDOWS_PHONE or #if XBOX preprocessor statements all over my game logic. The solution is a pretty simple yet elegant abstracted IGameActionManager interface.

Here is the interface I have defined so far:

public interface IGameActionManager
	bool IsJumping();
	bool IsMovingRight();
	bool IsMovingLeft();
	bool IsMovingUp();
	bool IsMovingDown();
	bool IsIdle();
	bool IsFiring();
	void GetNewState();

Each function corresponds to a typical game action, and the GetNewState() function gets the state for all input devices associated with the implented class itself. Knowing nothing of the implementation, other than which one you want, your game logic would look something like this:

IGameActionManager actionManager = new WindowsGameActionManager();

// somewhere in your update method:
if (actionManager.IsJumping())
	// Do your jump logic here
if (actionManager.IsMovingLeft())
	// Do your move left logic here

That eliminates code like:

#if !XBOX
KeyboardState keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.Space) && previousKeyboardState.IsKeyUp(Keys.Space))
GamePadState gamePadState = GamePad.GetState();
if (gamePadState.DPad.Up == ButtonState.Pressed)
	// Do your jump logic here

The result is less clutter in your game code itself, but more importantly, your code is more game related. You are now worrying about things like “Is the player trying to jump” instead of “is the player pressing the key that I designated to jump?”

I’m still working out exactly how to handle previous state code within the class, but for now you would probably have to declare a previous IGameActionManager object and get a new state at the appropriate time. The code is still cleaner than worrying about actual key presses throughout still, though.

Here is a windows implementation example for the IGameActionManager:

public class WindowsGameActionManager : IGameActionManager
	KeyboardState keyboardState;
	GamePadState gamepadState;

	public WindowsGameActionManager()

	#region IGameActionManager Members

	public bool IsJumping()
		return keyboardState.IsKeyDown(Keys.Space) ||
		gamepadState.DPad.Up == ButtonState.Pressed;

	public bool IsMovingRight()
		return (keyboardState.IsKeyDown(Keys.Right) &&
		keyboardState.IsKeyUp(Keys.Left)) ||
		(gamepadState.DPad.Right == ButtonState.Pressed &&
		gamepadState.DPad.Left == ButtonState.Released);

	public bool IsMovingLeft()
		return (keyboardState.IsKeyDown(Keys.Left) &&
		keyboardState.IsKeyUp(Keys.Right)) ||
		(gamepadState.DPad.Left == ButtonState.Pressed &&
		gamepadState.DPad.Right == ButtonState.Released);

	public bool IsIdle()
		return !IsMovingDown() && !IsMovingLeft() && !IsMovingRight() && !IsMovingUp();

	public bool IsFiring()
		return keyboardState.IsKeyDown(Keys.RightControl) ||
		gamepadState.Triggers.Right > 0.0f;

	public bool IsMovingUp()
		return (keyboardState.IsKeyDown(Keys.Up) && keyboardState.IsKeyUp(Keys.Down)) ||
		(gamepadState.DPad.Up == ButtonState.Pressed && gamepadState.DPad.Down == ButtonState.Released);

	public bool IsMovingDown()
		return (keyboardState.IsKeyDown(Keys.Down) && keyboardState.IsKeyUp(Keys.Up)) ||
		(gamepadState.DPad.Down == ButtonState.Pressed && gamepadState.DPad.Up == ButtonState.Released);

	public void GetNewState()
		keyboardState = Keyboard.GetState();
		gamepadState = GamePad.GetState(PlayerIndex.One);


Please drop a comment if you find this useful, or send me a tweet: @DomenicDatti

XNA and Silverlight coming to Windows Mobile!

I purchased an HTC Ozone (upgradable to WM6.5), and a couple months later the Droid came to Verizon. Needless to say, I was upset, but there was nothing I could do about it.

Enter Microsoft:

That’s right… XNA and Silverlight are coming to Windows Mobile 7. Before now, I was just counting the days until my current Verizon contract was up so that I could get my hands on a Droid. That would’ve meant learning a whole new set of development tools, and possibly a new language altogether (I’m not too sure what development for the Android OS is all about). Now, though, my current .NET & XNA skills should be transferable!

A quote from the Engadget article:
…[Charlie] Kindel boldly proclaims that “If you are Silverlight or XNA developer today you’re gonna be really happy.”

I sure am.

I can’t wait to see what all the great XNA devs come up with for the gaming side of things, and it should be pretty interesting to see what type of apps are made using Silverlight. The possibilities are endless and exciting.

My only hope is that they build upon the successes of the Xbox Live Indie Games platform and allow independent game developers to distribute their games and make some cash. Every other similar phone has a market place right now, and to not include that capability would be a devastating mistake on Microsoft’s part. Without the ability to buy a phone, search for fun apps & games, buy, and play with them immediately, I don’t believe that anyone could look at all their options (e.g. iPhone and Droid) and choose a “crippled” Windows Mobile 7 phone over them.
Update: http://twitter.com/wp7dev/status/10005356682 there will be a marketplace. WOOT!

All in all, it’s an extremely exciting announcement, and I hope that everyone sees the potential like I do.

I leave you with a demo of the Windows Mobile 7 phone: