MVC: Struggling to remove graphics from the model

Started by
0 comments, last by Ashaman73 10 years, 11 months ago
For my latest (small) project I thought I'd try out a slightly different way of doing things, namely I've decided to try using MVC for a game.

I've got a reasonable handle on it in terms of observers and events in the model and communicating these to the view (and controller), however I'm having a bit of difficultly in figuring out how to completely eliminate data about the graphical representation from the model.

Take this (massively simple) example of a fairly basic RPG. Since my model is data-driven, I have a single "Unit" class which contains information about the units (no need for Troll, Goblin, etc subclasses).

class Unit
{
    string name; //Friendly name, e.g. Dave
    int hp;
    int damage;
int range;
 
    Unit(string name, int hp, int damage);
}

Then in my model I keep a list of these Units as well as a method to retrieve them so the view can access them.

class Model // LevelState or similar would be a better name for this
{
    Unit[] units;
 
    Unit[] GetUnits();
}

The problem occurs when I come to drawing the units, as far as my model is concerned it doesn't care about if the unit is a goblin, an archer or a giant salamader, all it cares about is the unit's stats which define it's behavior.

However, the view very much cares about if the unit is a goblin, an archer or a giant salamader as it will have to draw different sprites for each of them.

What is the best way to solve this? I could add an enumerated "type" field to the Unit class, but this feels wrong as it's polluting the model with data that it doesn't care about, but I really can't see any other way of doing this.

Am I just overthinking and getting too concerned with what is "right" and "clean"?
Advertisement

What is the best way to solve this? I could add an enumerated "type" field to the Unit class, but this feels wrong as it's polluting the model with data that it doesn't care about, but I really can't see any other way of doing this.

This is the way I would do it. The model should contain all important information, not only information you need for some calculations, but although data which are used to communicate important infos to the player, like a unit type (ah.. a goblin which seems to be quite weak).

Even a model name would be ok, all other model related data, like textures, mesh, animation description etc. are then included in view/controller.

This topic is closed to new replies.

Advertisement