HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

MVC in XNA game development - rudimentary MVC GameEngine

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
rudimentarymvcxnagameenginegamedevelopment

Problem

I've read a lot about using MVC in game development and it seems to be a good way (speaking of testing, code reusing, etc ...)

I created a XNA project and tried to implement MVC. But I'm not sure if I'm doing it right.

I seperated the game into three blocks:

  • Renderers (the "View")



  • GameObjects and GameLogic (the "Model")



  • Controllers



On top of these components is a GameEngine object (created by the XNA game class) which instantiates all of the components and handles the draw and update methods (game class calls GameEngine.Draw and GameEngine.Update).

```
public class GameEngine
{
private readonly HUDRenderer m_HUDRenderer;
private readonly Game m_game;
private readonly List m_lstRenderer = new List();

private readonly Player m_player;
private readonly PlayerRenderer m_playerRenderer;
private readonly World m_world;
private readonly WorldController m_worldController;

private readonly WorldRenderer m_worldRenderer;

public GameEngine(Game game)
{
m_game = game;

m_player = new Player();
m_world = new World(m_player);

// Controller
m_worldController = new WorldController(m_world);

// Views
m_worldRenderer = new WorldRenderer(m_world);
m_lstRenderer.Add(m_worldRenderer);

m_playerRenderer = new PlayerRenderer(m_world);
m_lstRenderer.Add(m_playerRenderer);

m_HUDRenderer = new HUDRenderer(m_world);
m_lstRenderer.Add(m_HUDRenderer);
}

public void Update(GameTime gameTime)
{
m_worldController.Update(gameTime);
}

public void Draw(GameTime gameTime, DrawContext drawContext)
{
drawContext.Begin();

// ########## ADD ALL DRAW METHODS ##########

foreach (IRenderer renderer in m_lstRenderer)
{
renderer.Render(gameTime, drawContext);
}

// ########## END OF ADD ALL DRAW METHODS ##########

drawContext.End();
}

///

Solution

To answer your questions, 1) I would make the World a singleton, therefore both eliminating the need for quite a few params and the need to put it as a static field somewhere. However, make sure that retrieving the instance is a lightweight method, because it will happen a lot.
As for the game logic, I would say it depends. For objects which have only one or three instances, I would put the logic in the class, but for more numerous things, put that in a seperate object.

Context

StackExchange Code Review Q#16460, answer score: 2

Revisions (0)

No revisions yet.