I am trying to determine the best way to implement the "update" system or engine for a simple XNA game.
Description of situation
I have a few classes, lets call them:
- Player [will be an array/list/collection of 1-4 local players]
- Enemy [will consist of all enemy characters in the given "zone"]
- Future [not implemented, may not be implemented for this game, but may be needed in future]
- Future
I'm trying to decide what the best way to manage this will be, I'm going back and forth between a couple options.
First Approach
In the Update()
method of my game, write code like this:
void Update(GameTime gameTime)
{
var ctlInput = GetControllerInput();
for(int i = 0; i < 4; i++)
{
if(Player[i] != null) { Player[i].Update(gameTime, ctlInput); }
}
for(int i = 0; i < Enemy.Count; i++)
{
Enemy[i].Update(gameTime, Player);
}
}
In this version, I would add "FrameStatus" fields to the Player class, which would look like bool IsShooting
and Vector2 TargetedLocation
etc. This also means that each class would be responsible for knowing how to update itself in the game world.
In my eyes, the advantages of this approach are:
- No new classes.
- Classes become self-documenting
- Easy to extend and add additional properties
but the disadvantages are:
- Order of code in Game.Update is very important
- Tight coupling of Player and Enemy classes
Second Approach
Implement a type of UpdateManager class, which accepts the user input, and it alone updates the Player and Enemy classes. This would relegate the Player and Enemy classes to simply dumb-data classes, and they wouldn't necessarily have any game logic in them at all.
void Update(GameTime gameTime)
{
var ctlInput = GetcontrollerInput();
updateManager.Update(gameTime, ctlInput);
}
The advantages of this approach, in my eyes, are:
- Keeps code away from Game.Update()
- Doesn't require Player and Enemy classes to know about each other at all
Disadvantages as I see it:
- Adds a new class to my game
- The new class, will have a lot of code as time goes on and more types of objects enter the game world
Actual Question
Of the pro/cons I've come up with for my two approaches,
- do you think I've missed any, what are they?
- which makes you lean for or against an approach, why?
Which approach would you take, and also why would you go that route? Would you elect for a third option? What would that option look like?