Friday, December 25, 2009

First Game Publication

Season's Greetings! This year, I received a nice gift to complete 2009: the game I recently completed, Henway, was approved and published on the Indie Games area of Xbox LIVE Marketplace.

Henway: The goal is to cross the road without being killed; hence this game is based on real life.


During playtest and review, the feedback received on the XNA Creators Club was really positive and there were plenty of great ideas posted by fellow creators on the site.

Note: creators must have a Premium Membership before they can playtest and review Indie Games.

Here is a quick summary of features and updates that could potentially be added in a future release:
  • add more vehicles: trucks, buses, cyclists, motorcycles, tractors, emergency
  • add variety of squelching noises when the chicken gets run down
  • pause game when batteries removed from controller
  • use Xbox button images for quit and error popups
  • add exhaust particles for some of the vehicles
  • disable sign in guide when saving
  • disable the continue option

Tuesday, December 15, 2009

Template Method Design Pattern

In XNA, the Update() and Draw() methods are invoked, by default, 60 frames per second. During each frame, a general algorithm for both the Update() and Draw() methods could be applied in game code as follows:

Update AlgorithmDraw Algorithm
  • update input
  • update objects
  • update HUD (heads up display)
  • draw background
  • draw objects
  • draw HUD (heads up display)

Here we have defined a template for both the Update() and Draw() methods. A template is a method that defines an algorithm as a set of steps. One or more of these steps can be implemented by the main game code, or deferred to a subclass where appropriate. This ensures that the structure of the algorithm stays the same, while subclasses can provide some part of the implementation as required.

By definition, the Template Method design pattern defines the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

Let’s complete the discussion with a simple code sample.

First, create the skeleton of an algorithm for both the Update() and Draw() methods in the main game code:
// Template method for Update.
public void Update(Microsoft.Xna.Framework.GameTime gameTime)
{
 if (IsActive)
 {
  return;
 }

 UpdateInput(gameTime); 
 UpdateObjects(gameTime);
 UpdateHUD(gameTime);
}

// Template method for Draw.
public void Draw(Microsoft.Xna.Framework.GameTime gameTime)
{
 if (IsActive)
 {
  return;
 }

 DrawBackground(); 
 DrawObjects();
 DrawHUD();
}
Next, create the steps to be implemented by the template methods in the main game code:
public virtual void UpdateInput(GameTime gameTime) {}
public virtual void UpdateObjects(GameTime gameTime) {}
public virtual void UpdateHUD(GameTime gameTime) {}

public virtual void DrawBackground() {}
public virtual void DrawObjects() {}
public virtual void DrawHUD() {}
As this is a simple example, none of the steps will be deferred to subclasses; each step would execute the same game code each frame.

However, the Template Method can be used in conjunction with the State design pattern to redefine certain steps of the algorithm without changing the algorithm’s structure.

First, define an interface to be implemented by all game state objects. This interface contains all actions that each game state object will implement. In our example, the 2x actions Update() and Draw(), are now defined as template methods; this ensures that the structure of the Update() and Draw() algorithms stays the same while subclasses can provide some part of the implementation as required:
public class AbstractGameState
{   
 protected readonly Game game;   
  
 protected AbstractGameState(Game game)   
 {   
  this.game = game;   
 }   
 public void Update(GameTime gameTime)
 {
  if (IsActive)
  {
   return;
  }

 UpdateInput(gameTime); 
 UpdateObjects(gameTime);
 UpdateHUD(gameTime);
}

public void Draw(Microsoft.Xna.Framework.GameTime gameTime)
{
 if (IsActive)
 {
  return;
 }

 DrawBackground(); 
 DrawObjects();
 DrawHUD();
}
Next, construct one concrete implementation class for each state in the game. Each step in both the Update() and Draw() template methods can now be deferred to the concrete implementation class as required.

For example, imagine the Splash Screen game state concrete implementation class is responsible for detecting the Start button press and displaying the splash screen only:
public class SplashScreenState : AbstractGameState
{
 public override void UpdateInput(GameTime gameTime)
 {
  // Detect player press Start button.
 }

 public override void DrawHUD (GameTime gameTime)
 {
  // Draw splash screen
 }
}
Note: the remaining steps in both the Update() and Draw() template methods are invoked automatically by the algorithms contained in the base game state class.

To summarize, the Template Method design pattern offers algorithm encapsulation so that subclasses can hook themselves right into a computation anytime they want.

Tuesday, December 1, 2009

Command Design Pattern

The Command design pattern encapsulates a command request as an object. In game code, an example of a command request could be a simple move command: e.g. move sprite left, right, up, down.

The Command design pattern is used to express a command request, including the method call and all of its required parameters, into a command object. The command object may then be executed immediately, queued for later use or reused to support undoable actions.

Note: the command object does not contain the functionality that is to be executed; only the information required to perform an action. The functionality is contained within a receiver object. This removes the direct link between the command object and the functionality to promote loose coupling. Finally, neither the command object nor the receiver is responsible for determining the execution of the command request; this is controlled using an invoker.

In our move sprite example above, the move command object encapsulates the method invocation to update the position of the sprite. The receiver object is the sprite itself. The invoker is input detection from the player’s controller to determine the execution of the move command request.

Initially, the thought of using the Command design pattern to move a sprite seems overkill. In game code, this can easily be accomplished using the following excerpt:
protected override void Update(Microsoft.Xna.Framework.GameTime gameTime)
{
 Single velocityX = GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X;
 Single velocityY = GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Y;
 if (velocityX == 0 && velocityY == 0)  
 {  
  return;  
 }  

 sprite.Velocity = new Vector2(velocityX, velocityY);
 sprite.Update();
}
However, by implementing the Command design pattern to move a sprite we are now able to record each move command, add to a list of move command objects, and persist to playback at a later time. This can be useful, for example, to implement a demo mode in a game. In fact, this is exactly how the game Henway employs a demo mode in the title screen. Let’s check it out:

First, define an interface to be implemented by each command object. Typically, this interface contains a single Execute() method but can be extended to support Undo() operations:
public interface ICommand
{
 void Execute();
}
Next, construct a concrete implementation class for each command object in the game. In our example there is simply one command object: MoveCommand.
public struct MoveCommand : ICommand
{
 private readonly Sprite sprite;
 private readonly Vector2 velocity;

 public MoveCommand(Sprite sprite, Vector2 velocity): this()
 {
  this.sprite = sprite;
  this.velocity = velocity;
 }

 public void Execute()
 {
  sprite.Velocity = velocity;
  sprite.Update();
 }
}
Next, construct a list of command objects. This list will record each command object as it’s created in game code and will be used to playback at a later time.
public IList<ICommand> CommandsSave { get; set; }
Note: you will also need to construct a list of integers that record the number of frames that elapse between each command object being recorded; this is required for playback mode.
public IList<Int32> CommandsDelta { get; set; }
Next, update the main game class Update() method: replace the simple sprite update above with game code that now:
  • constructs a new command object;
  • sets all required parameters;
  • executes the command;
  • records the command for later use;
protected override void Update(Microsoft.Xna.Framework.GameTime gameTime)
{
 updateFrame++;
   
 Single velocityX = GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X;
 Single velocityY = GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Y;
 if (velocityX == 0 && velocityY == 0)
 {
  return;
 }

 Vector2 velocity = new Vector2(velocityX, velocityY);
 ICommand command = new MoveCommand(sprite, velocity);
 command.Execute();

 CommandsSave.Add(command);
 CommandsDelta.Add(updateFrame);

 updateFrame = 0;
}
Finally, after all command objects have been recorded through the revised Update() method, the list of frame deltas and the list of command objects can be formatted and saved:
private void SaveCommands()
{
 for (Int32 index = 0; index < CommandsSave.Count; index++)
 {
  Int32 frame = CommandsDelta[index];

  MoveCommand command = CommandsSave[index];
  Single velocityX = ((MoveCommand)command).Velocity.X;
  Single velocityY = ((MoveCommand)command).Velocity.Y;

  String format = String.Format("{0},{1},{2}",
   frame,
   velocityX,
   velocityY,
   );

  // Persist command object data.
 }
}
Now the list of frame deltas and the list of command objects can be loaded at a later time, for example, during the title screen:
public ICommand[] Commands;
public Int32[] Frames;

private void LoadCommands()
{
 IList<String> lines = GetCommandData();

 Int32 maxCommand = lines.Count;
 Commands = new ICommand[maxCommand];
 Frames = new Int32[maxCommand];

 for (Int32 index = 0; index < maxCommand; index++)
 {
  String line = lines[index];
  String[] values = line.Split(new[] { ',' });

  Frames [index] = Convert.ToInt32(values[0]);

  Single velocityX = Convert.ToSingle(values[1]);
  Single velocityY = Convert.ToSingle(values[2]);
  Vector2 velocity = new Vector2(velocityX, velocityY);

  Commands[index] = new MoveCommand(sprite, velocity);
 } 
}
And the list of frame deltas and the list of command objects can be played back as a demo mode:
private Int32 frame = 0;
private Int32 index = 0;
private Int32 maxCommand = Commands.GetLength(0);

protected override void Update(Microsoft.Xna.Framework.GameTime gameTime)
{
 frame++;
 if (frame >= Frames[index])
 {
  frame = 0;
  Commands[index].Execute();

  index++;
  if (index >= maxCommand)
  {
   // All commands executed – stop playback.
  }
 }  
}
To summarize, the Command design pattern can be very useful in game development: by encapsulating method invocation, game code can crystallize pieces of computation so that the object invoking the computation doesn’t need to worry about how to do things; it just uses the crystallized method to get its work done.