When doing MVC-style architecture (not just in gaming!) it can be helpful to remember that there is often more than one model. Besides the (usually) obvious model of the Player
's state (his position, life, and so on), there may be models (dependant on language and engine) for Sprite
, or other 'display' classes.
For instance in Valve's Source engine, the 3-dimensional art (and animations) are encapsulated in their own files (representing the mesh, current animation, a few other things, but not the object's position), and have absolutely no relation to an entity's data (which contains position, orientation, and so on).
Because the two models usually don't (or aren't supposed to) have much to do with each other, they can (or possibly should) be wired together with seperate controllers: One for Player
model to Sprite
model updates (setting the currently playing animation, display position, etc), and one from the Sprite
(for what, and where relative to other display elements) model to the view.
Here's a quick (Java-based Psuedo-code) example with attributes:
public Beast {
private String name;
private List<Behaviour> behaviours;
}
public Player {
private List<Beast> paddock;
private List<Beast> leashed;
private List<Beast> hospital;
public BeastAnimation { // One per beast!
private Map<Behaviour, Animation> animations;
private Animation current;
}
With these entities, you'd want a controller that would set the BeastAnimation
's current
based on whatever Behaviour
was being executed - depending on game type, you usually need to start the animation even if the object is otherwise not (at least initially) visible, you don't want the entity doing nothing the first time he's seen (especially if the animation is long). Please note that this doesn't necessarily mean rendering the animations or (depending on display type) even applying transforms.
You're also going to want a controller that takes the 'current' BeastAnimation
when actually rendering out the animation. Please note that the various animations only their relative positions, and shouldn't really know where the camera is - this controller acts as the necessary translation bridge. It's good to note that a fair number of graphics engines essentially take care of this for you - you only have to hand it the necessary models/animations, and say where the camera is - they're a controller/view package in one. Otherwise, how (and what the necessary translations are) varies by case
Player
's state (position, lives, and so on), and one for theSprite
state (frame, image offset, etc). The two models don't really have much to do with each other; wire them together with a controller as normal, and wire a controller for theSprite
-model to view translation. – Clockwork-Muse Dec 06 '11 at 21:21