Game Development Reference
In-Depth Information
The header of the constructor then becomes
public
GameWorld(ContentManager Content)
In the
Painter
class, we then define a member variable
gameWorld
that will contain an
instance of the
GameWorld
class. In the
Painter
program, there will only be a single
game world. And we will be accessing that instance later on in many other classes.
Let us therefore find a way to easily access this instance. One thing that we can do
is to make it static. That way, we will not need an actual
Game
object to access the
game world instance. This leaves us with only a few member variables in the
Painter
class:
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
InputHelper inputHelper;
static
GameWorld gameWorld;
The
graphics
variable contains the graphics device manager as usual, there is a
spriteBatch
variable that is used for drawing sprites, the
inputHelper
object gives a
few convenient methods and properties for player input handling, and the
gameWorld
variable contains the game world. This variable is assigned a value in the
LoadContent
method of the
Painter3
class. This cannot be done earlier since the
GameWorld
object
needs to load sprites. This assignment is given as follows:
gameWorld =
new
GameWorld(Content);
Then, we can add a
static property
to the
Painter
class to access this game world.
This property is given as follows:
public static
GameWorld GameWorld
{
get
{
return
gameWorld; }
}
Now, whenever we want to access the game world, we can get it with the expression
Painter.GameWorld
. We do not need an actual object. This is very useful, because the
GameWorld
object is going to provide the means for game objects to
interact with
each other
, as we will see in later chapters.
7.5.3 Managing the Game Objects
There are basically three things that the
gameWorld
object should do to manage the
game objects: make sure that the game objects handle player input, make sure that
they are updated, and finally draw them on the screen. For now, the only game ob-