I'm trying to write a SpriteManager
class in XNA, but I need access to the Game
variables. For example, the Game.Content
property would be very helpful in loading textures, and Game.GraphicsDevice.Viewport
would be helpful in getting the window size.
For now, I have the following class:
public MainGame : DrawableGameComponent
{
public SpriteManager<Enemy> EnemyManager { get; get; }
public SpriteManager<Powerup> PowerupManager { get; set; }
...
}
I can think of three ways of getting access to these variables in the SpriteManager
class:
Make my sprite manager class derive off of
GameComponent
. This would be ideal, except that my sprite manager is used inMainGame
, which is aGameComponent
.Use
((Game) Game1)
to access the variables. However, doing this kind of cast is ugly and breaks encapsulation.Pass everything down from
MainGame
. For example,EnemyManager
needs to load a random enemy type each time it spawns an enemy, so instead of loading the textures inMainGame
, passEnemyManager
theGame.Content
property and let it handle texture loading. However, I'm not sure if this is the best way of handling the situation. I can't place my finger on it, but it just seems 'wrong' in some way.
I could use some advice on the best way of achieving this.