November 15, 2012

Design Patterns in Game Programming (II): Strategy

Here are some use cases for the strategy design pattern in game programming, all of them leading to a more dynamic game setup, making configuration easier and decoupling the game core from implementation details of various algorithms.

Paint Strategy
In Java2d define an interface to render images with two implementations:
  • simply call Graphics#drawImage()
  • use java.awt.image.BufferStrategy
Win Rule
A game can be played with different rules to actually win the game, like:
  • LAST_MAN - the last survior wins
  • HIGHSCORE - the highest score wins
  • TIMESPAN - highest score wins after playing for a fixed time
  • INFINITY - play forever, useful for development and testing
The game has one current win rule object which is called on a regular basis to find out whether the game should be finished. In case the win rule is fullfilled, a list of the winners is returned.

Path Finding
Call the path finder only through an PathFinder interface to be able to easily swap between different algorithm at runtime like A* or Dijkstra.

Field Of View
Call the field of view calculation through an interface, thus making it easy to change the algorithm at runtime according to defined game performance options or machine specs. Add mock implementations to support testing.

Collision Checking
Isolate collision check algorithms in concrete strategy implementations.

Moving a sprite from point a to point b is possible in different ways:
  • straight
  • by following a tile path
  • by following calculated spline nodes

No comments:

Post a Comment