Jump to content
  • Advertisement
Sign in to follow this  
adt7

MVC: Struggling to remove graphics from the model

This topic is 2041 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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"?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!