Model View Controller

I learnt about the MVC pattern when I was presented to the book Game Coding Complete. Although the implementation of this pattern on a game development related environment can take some pre-planning, using it allows a much cleaner implementation of your objects with particular relevance for those that have a visual implementation, such as a level or a game token.

I’m saying object, not as an instance of a class, but rather as an abstract game object from a design point of view.

Let’s break the pattern down into manageable parts… after all, that’s exactly what the pattern does.

Model

The model is the data representation of your object. Outside game development, model refers to the data model, usually supported by a database engine. From a game development perspective, model is what your object is, like a level, a platform, the player character, an enemy and so on. If you coded a class that represents something, you have already created a model already.

The main difference with application development is that the data models you use are not persistent and are discarded when they’re not needed.

The idea you should retain game development wise is that your “typical” class design, considering variables, constants and so on is your model, even if this is not 100% accurate from the pattern point of view.

View

The view is the visual representation of your object, which would be any display object, from a movie clip to a bitmap. It is nothing more than this.

Controller

The controller glues everything together! It is the logic behind the actions of your object. Imagine a player character that moves with WASD for the actions Jump, Crouch, Move Left and Move Right. If the player presses W the controller will receive the event and order the view to run the jump animation. Now imagine that some enemy shoots the player character while it is jumping. The controller receives the information that the player was shot, informs the model to subtract damage to player’s life and orders the view to run the hurt animation.

Encapsulating all together

The easiest way to implement OOP on your AS3 game is to simply extend some movie clip to a class and write all code there. With MVC, you need to encapsulate a bit. There are many ways to do this, but this is how I do it.

My controller and view are only one class: the main object class. The view is separated which allows me to have a cleaner interface when I’m working and the possibility of extending or not the view class. This offers a wide range of possibilities since the view class can hold a large ammount of functionality, such as movement patterns, pathfinding and so on or it can simply be the display object.

Controller and view work together regarding data and logic. As I see it they’re not even separarated as a proper implementation of MVC. This often means that a simple state machine is at work, like in the example below.

package
{
   // This is my controller/model class
   public class PlayerController
   {
      public function PlayerController()
      {
         // Creates the view
         playerView = new PlayerView();
 
         // Code to add it to the level movie clip goes here
      }
 
      public function Update()
      {
         // This method is called from the gameloop
 
         // Depending on the state, chooses actions
         switch(state)
         {
            case IDLE:
               playerView.UpdateAnimation(IDLE);
               break;
            case WALK:
               playerView.UpdateAnimation(WALK);
               break;
            case HURT:
               playerView.UpdateAnimation(HURT);
               break;
            case NONE:
               SetState(IDLE);
               break;
         }
      }
 
      public function SetState(newState:int)
      {
         // No point in doing something that
         if (newState == state) return;
 
         // Only does something different if the player is hurt
         if (newState == HURT) life--;
 
         // And assigns the new state
         state = newState;
      }
 
      // Vars ---------------------------------------------------------------
 
      private var life:int = 100;      // Hit points
      private var state:int = NONE;   // Current state
 
      private const NONE:int = 0;
      private const IDLE:int = 1;
      private const WALK:int = 2;
      private const HURT:int = 3;
 
      private var playerView:PlayerView;   // The view (displayObject)
   }
}

A word of caution

MVC is very powerful, easy and intuitive, but use it when your game asks for it. If you have an object that only needs a couple of methods, by all means, simply extend the movie class or whatever you are extending. Use it to break the complex objects into manageable parts.

Posted: March 18th, 2009
at 12:00am by Vlad

Tagged with ,


Categories: The code of VGS

Comments: No comments