0
\$\begingroup\$

I am working on a turn based game that utilizes the model, view, controller design pattern to separate logic from input from rendering. I am still a little new to the pattern, but from my understanding I have laid out the following MVC system.

The model(s) comprise a set of pawns with game flow and interaction logic. Pawns/models know how to interact with each other and the model world, but only when signaled to do so via function calls. (i.e. pawn.move(startLocation, endLocation) or pawn.attack(enemyPawn))

The view(s) make up everything that is viewable by the player. This includes menus, visual representations of the pawns/models, and any user interface components like cursors and buttons. The view(s) query the model for data to draw visual representations of pawns. The view(s) also can dispatch user interface events to any observers interested in these events.

The controller(s) handle all transition and input logic. When an interesting user interface or input events occur, the controller decides what the model should do and signals the model to do it. The controller also handles swapping out of views when necessary.

I know there are many ways to implement MVC and I am open to suggestions if something seems off with this design, but the real problem I am having is how to handle animations in the view. Say the user commands a pawn to move 5 spaces North and 1 space West. I want an animation to show this over several seconds. How would I handle something like that?

\$\endgroup\$
0
\$\begingroup\$

You need a class called Renderable. Each pawn (and other game entity) holds a reference to its own instance. Renderable is part of the View, so the Pawn is never allowed to read properties from it. It can only set its values, or call void methods.

Inside Renderable, there should be methods like these:

public void SetAnimationStateFlag(AnimModifier state)
{

}

public void ClearAnimationStateFlag(AnimModifier state)
{
}

When the pawn moves, it calls SetAnimationStateFlag on its renderable with a flag AnimModifer.Moving. The flags should be stored in a bit field member in the Renderable.

public enum AnimModifier
{

    Dying,      
    Attacking,
    Moving
}

During Draw(), it is then up to the Renderable to select an animation that matches the flags that have been set on it.

\$\endgroup\$
  • \$\begingroup\$ This kind of makes sense, but I do have one question about it. Since I have already updated the model with my move data, how do I keep the pawn view in it's starting location until the animation takes place? Like say I'm playing Fire Emblem and a tell a unit to move (Up, Up, Left, Up, Left). The unit in the model knows its start/end location and possibly its path, but the pawn view should can only draw in one location and should not update the model as it animates. So how would the pawn view know each draw cycle where to animate to? \$\endgroup\$ – MrJman006 Jun 18 '15 at 6:37
  • \$\begingroup\$ The only thing coming to my mind is to keep a second model in the view that is used for animations and when a pawn's location matches the location in the real model, the animation has completed, but I don't know if that is good way to do it. \$\endgroup\$ – MrJman006 Jun 18 '15 at 6:40
  • \$\begingroup\$ Yes, if things are not being displayed in realtime, you may need a buffering system of some sort between the pawn and its renderable. You should probably wait with setting the flags and the renderable position until the animation phase is supposed to begin. \$\endgroup\$ – Rye bread Jun 18 '15 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.