8

I'm making a 2D game in java using the MVC pattern and after reading and searching my ass off I still haven't found a satisfying answear to how I'm supposed to handle the graphical representation of objects.

Should I divide every object, for example Player into PlayerModel (stored in Model) and PlayerView (stored in View)?

That seems kinda messy becuase then I would have to keep track of which grapical-representation-object, i.e. "ScaryMonsterEnemyView" is connected to which logical-representation-object, "ScaryMonsterEnemyModel". Is this really how I'm supposed to do it according to MVC? If so, where should this connection be stored? In the view?

I know this might be a silly problem to get stuck on, but I want to get as much as possible right from the start. Thanks for helping out :)

tobes
  • 131
  • 2
  • 1
    Maybe you will find [this article](http://www.gamasutra.com/view/feature/130693/the_guerrilla_guide_to_game_code.php) helpful .. if you have not read it already. – tereško Jul 14 '12 at 15:04
  • @tereško So basically ScaryMonsterEnemyView would hold ScaryMonsterEnemyModel? That could make sense I guess.. – tobes Jul 14 '12 at 15:23
  • in MVC the view does not hold model. The view only receives data from model layer. It's either sent from model layer (classical MVC) or request by view (Model2 MVC). – tereško Jul 14 '12 at 17:25
  • @tereško What is a model layer? Perhaps my terminology was wrong, let me rephrase. From the article you linked: "An EntityRepresentation can look at but not change the state of the Entity." So what I meant in my previous comment is that the representation(stored in view(?)) would need a variable for the entity(stored in model(?)) in order to "look at"/request data from the entity – tobes Jul 14 '12 at 17:52
  • 3
    Well , I have no simple explanation for what "model layer" is, [the one i have](http://stackoverflow.com/a/5864000/727208) is for php & web. Maybe you could read [PoEAA](http://www.amazon.com/Enterprise-Application-Architecture-Addison-Wesley-Signature/dp/0321127420) book. The instance of `EntityRepresentation` acquires the `Entity` instance via the constructor (either directly or as part of abstraction). I guess you can looks at it as "storing", but the `EntityRepresentation` actually holds only handler or reference (depends on language) to that object. – tereško Jul 15 '12 at 00:09

2 Answers2

4

If I am not mistaken, you are basically asking how to split up a game into the Model-View-Controller paradigm.

Simply put, the Model is the state of your game. In O-O terms, you can think of the Model as all of the objects in your game.

The Controller is the set of rules that are applied to your game's state (in this case, all of your game objects) in every update cycle. This can be implemented as a method called update() in all of your objects, or it can be a function called in your game loop that systematically goes through all the objects that need updated and, well, updates them. You can also think of the Controller as the game loop itself. It calls everything to update, and then draws it on the screen and repeats, unless some conditions are met, then it tells the program to do something else. In this way, you almost have two nested MVC structures. One controlling the flow of the program through menus and such, and one dedicated to the game itself.

The View is just the graphical representation of your game. This can be as simple as text on a screen, but in your case it is 2D graphics. To implement this, you could have each object also contain their graphical state, either directly, or by encapsulation. The View would do little more than query all of the objects for their graphical state, and then shunt it to the screen. Again, this can be implemented on a per-object basis, such as a method called draw(), or another systematic function to be called directly from the game loop. A common practice is to create an object called 'Sptite' or something similar to hold the graphical information, and have every game object that is drawn have a personal instance. Also note that the View need not be an object unto itself. A mere function called in a game loop will suffice, although it is sometimes necessary to store information that directly effects the View's operation (like window size), in which case the View can be an object. The same goes for the Controller.

Also keep in mind that these divisions can be sectioned up even further to make life simpler. For example: The Controller can be divided up into AI processing, movements updating, and collision checking. The View could be separated into the game object display and the HUD, and the Model can be all the objects + all the state independent of the game objects (like the games settings for resolution, window size, key config and such).

I know this might be a little overkill, and it probably has extra information, but hopefully it answers your question and gives you ideas on where to start.

Zistack
  • 516
  • 3
  • 10
1

The Model and View are two collections/categories/domains of objects.

The controller provides a set of interfaces to perform some functions on the model objects. And if required, the view can talk directly to model objects regarding their state information.

Traditionally, The View domain corresponds to GUIs, with concepts such as Buttons, Forms, Windows etc. In a desktop environment.

Your View domain (Or one of them) will correspond to a 3D environment that you will build. (Trees, Houses, Person etc). This includes collision details, and your physics. Both of which require the geometry relating to your 3D environment.

It may be very rare in a game for any of these objects to have to collaborate with an object in another domain. But an example, consider a telephone. The object will exist in your 3D environment, but will also collaborate with another domain which describes "half-calls", "channels", "switches" etc. These objects do not belong in your View domain but in a separate Model domain.

A more relevant example maybe some sort of scoring system, such as RPG stats, which would belong in a Model domain.

Michael Brown
  • 498
  • 4
  • 13