Game Development Reference
In-Depth Information
Note The overuse of inheritance can lead to severe headaches and very ugly code architecture.
Do not use it just for the sake of using it. The simple class hierarchy just used is OK, but you
shouldn't let it go a lot deeper (for example, by extending Cannon ). There are alternative
representations of game objects that do away with all inheritance by composition. For your
purposes, simple inheritance is more than enough, though. If you are interested in other
representations, search for “composites� or “mixins� on the Web.
The Spatial Hash Grid
Our cannon will be bounded by a rectangle of 1Ă—1 m, the cannonball will have a bounding
rectangle of 0.2Ă—0.2 m, and the targets will each have a bounding rectangle of 0.5Ă—0.5 m. The
bounding rectangles are centered on each object's position to make our life a little easier.
When our cannon example starts up, we can simply place a number of targets at random
positions. Here's how we can set up the objects in our world:
Cannon cannon = new Cannon(0, 0, 1, 1);
DynamicGameObject ball = new DynamicGameObject(0, 0, 0.2f, 0.2f);
GameObject[] targets = new GameObject[NUM_TARGETS];
for ( int i = 0; i < NUM_TARGETS; i++) {
targets[i] = new GameObject(( float )Math. random () * WORLD_WIDTH,
( float )Math. random () * WORLD_HEIGHT,
0.5f, 0.5f);
}
The constants WORLD_WIDTH and WORLD_HEIGHT define the size of our game world. Everything
should happen inside the rectangle bounded by (0,0) and ( WORLD_WIDTH , WORLD_HEIGHT ).
Figure 8-17 shows a little mock-up of the game world so far.
Figure 8-17. A mock-up of your game world
 
Search WWH ::




Custom Search