Game Development Reference
In-Depth Information
Texture2D background = Content.Load<Texture2D>("background");
We have to make a software design decision about where the sprites belonging to the
game should be loaded. In the
Painter
game, some sprites (such as the background
sprite) were loaded in the
GameWorld
class, others were loaded in the classes that
defined the objects related to those sprites, such as
Ball
or
Cannon
. The disadvantage
of loading the sprites in different classes is that we need to pass around the content
manager in order to be able to call the
Load<Texture2D>
method everywhere. A way
to avoid that would be to simply load all the sprites when the game starts, and then
pass these sprites as parameters to the different objects that are created. In the
Painter
game, the logical place to do this is the
GameWorld
class:
Texture2D cannonBarrel = Content.Load<Texture2D>("spr_cannon_barrel");
Texture2D cannonColorRed = Content.Load<Texture2D>("spr_cannon_red");
Texture2D cannonColorGreen = Content.Load<Texture2D>("spr_cannon_green");
Texture2D cannonColorBlue = Content.Load<Texture2D>("spr_cannon_blue");
Texture2D ballColorRed = Content.Load<Texture2D>("spr_ball_red");
Texture2D ballColorGreen = Content.Load<Texture2D>("spr_ball_green");
Texture2D ballColorBlue = Content.Load<Texture2D>("spr_ball_blue");
Texture2D canColorRed = Content.Load<Texture2D>("spr_can_red");
Texture2D canColorGreen = Content.Load<Texture2D>("spr_can_green");
Texture2D canColorBlue = Content.Load<Texture2D>("spr_can_blue");
cannon =
new
Cannon(cannonBarrel, cannonColorred, cannonColorGreen,
cannonColorBlue);
ball =
new
Ball(ballColorRed, ballColorGreen, ballColorBlue, ballShot);
can1 =
new
PaintCan(canColorRed, canColorGreen, canColorBlue, 450.0f,
Color.Red, collectPoints);
can2 =
new
PaintCan(canColorRed, canColorGreen, canColorBlue, 575.0f,
Color.Green, collectPoints);
can3 =
new
PaintCan(canColorRed, canColorGreen, canColorBlue, 700.0f,
Color.Blue, collectPoints);
However, a disadvantage of loading the sprites in the
GameWorld
instance is that the
game world has to know which assets its game objects use. It is much more logical
that the game objects
themselves
load their sprites. But of course, then we would
have to pass around the content manager everywhere again. As a solution to this
problem, we will introduce a new class, the
AssetManager
class.
15.2.2 The Asset Manager
The goal of the asset manager is to keep track of any assets that have been loaded,
and make them available to game objects that want to use these assets. The idea is
that the asset manager provides a simple way to access assets used in the game. The
asset manager needs a method that load and returns an asset, given an identifier. In