0

'm developing a 2D game and I want separate the game engine from the graphics. I decided to use the model-view pattern in the following way: the game engine owns game's entities (EnemyModel, BulletModel, ExplosionModel) which implement interfaces (Enemy, Bullet, Explosion).

The View receives events when entities are created, getting the pointer to the interface: in this way the View can only use the interface methods (i.e. ask for informations to perform the drawing) and cannot change the object state. The View has its onw classes (EnemyView, BulletView, ExplosionView) which own pointers to the interfaces. (There is also an event-base pattern involved so that the Model can notify the View about entity changes, since a pure query approach is impraticable but I wont' discuss it here).

*Model classes use a compile-time component approach: they use the boost::fusion library to store different state componets, like PositionComponent, HealthComponent and so on.

At present moment the View isn't aware of the component based design but only of the model-view part: to get the position of an enemy it calls the Enemy::get_xy() method. The EnemyModel, which implements the interface, forwards this call to the PositionComponent and returns the result.

Since the bullet has position too, I have to add the get_xy method to Bullet too. BulletModel uses then the same implementation as the EnemyModel class (i.e. it forwards the call).

This approch then leads to have a lot of duplicate code: interfaces have a lot of similar methods and *Model classes are full of forward-methods.

So I have basically two options:

1) Expose the compoment based design so that each component has an interface as well: the View can use this interface to directly query the component. It keeps the View and the Model separated, only at a component level instead of a entity level.

2) Abandon the model-view part and go for pure component based design: the View is just a component (the RenderableComponent part) which has basically full access to the game engine.

Based on your experience which approach would be best?

3 Answers 3

1

I'll give my two cents worth. From the problem you're describing, it seems to me that you need an abstract class that will do the operations that are common amongst all of your classes (like the get_xy, which should apply to bullet, enemy, explosion, etc.). This class is a game entity that does the basic grunt work. Inheriting classes can override it if they want.

This abstract class should be the core of all your interfaces (luckily you're in C++ where there is no physical difference between a class, and abstract class and an interface). Thus the Views will know about the specific interfaces, and still have the generic entity methods.

A rule of thumb I have for design - if more than one class has the same data members or methods, it should probably be a single class from which they inherit.

Anyway, exposing the internal structure of your Model classes is not a good idea. Say you'll want to replace boost with something else? You'd have to re-write the entire program, not just the relevant parts.

1

MVC isn't easy for games as when the game becomes larger (including menu, enemies, levels, GUI...) and transitions, it'll break.

Component or entity-system are pretty good for games.

As a simpler case for you, you may consider using a HMVC. You'll still have issues with transitions, but at least your code will be grouped together in a more clean manner. You probably want your tank's code (rendering and logic) to get close together.

6
  • HMVC is really just some bullshit made up by somebody who wanted to use inheritance to share lots of code and not have to rewrite or copy-and-paste stuff. Otherwise I agree on your remarks about entity-based systems being well-suited for games. Games need to handle interrupts such as the features offered in UnRealScript that allow the programmer to say "do this every 2 real-world seconds" sort of interrupts. For a formal treatment of interrupts, read Graham Hutton's What is the meaning of all these interruptions? (for Haskell).
    – user429921
    Nov 10, 2010 at 20:12
  • If HMVC only reduces duplicated code, it's already good. It's good, in my point of view, only for components (or something that behaves like a component you could drop in a UI without further work). A strict HMVC (passing only via controllers) is pretty much like having multiple MVC projects.
    – Wernight
    Nov 12, 2010 at 16:15
  • I think this is simply a matter of technical misinterpretation on your behalf. The original HMVC description does not use passing only via controllers. The major difference between HMVC and PAC is that in PAC you pass only via controllers, and views and models cannot talk directly to one another. I also think it is conceptually wrong to say HMVC "is pretty much like having multiple MVC projects", since reuse of domain knowledge cannot be had for free. Real-world systems are not composable for free, and can have deadlocks, livelocks, service interrupts interfering with real-time specs, etc.
    – user429921
    Nov 12, 2010 at 18:06
  • To be clear, what I am objecting to is system control hierarchies that only exist to reuse code. This is especially true if you hijack a code reuse mechanism like OO inheritance, because then your generalization/specialization relationships are tightly coupled to your reuse patterns and not your problem domain. Since GUIs are extremely fungible systems and do not have stable definitions for what they represent, other than meta-things, you can't use inheritance in a reliable way. Trying to will lead to Open-Closed Principle violation and breaking class invariants. Use logic programming instead
    – user429921
    Nov 12, 2010 at 18:10
  • I agree. HMVC isn't probably best for such anyway (PAC/strict HMVC or lose HMVC). PS: You may be interested in immediate mode GUI (sure it's GameDev).
    – Wernight
    Nov 18, 2010 at 23:15
1

There have been presentation architectures designed especially for agent-based systems, such as Presentation-Abstraction-Control. The hard part in designing such a system is that you ultimately end up hardwiring sequences of collaborations between the agents.

You can do this, but don't use OO inheritance to model the message passing hierarchy. You will regret it. If you think about it, you are really not interested in using the OO inheritance relationship, since the interfaces defined are really just a "Record of functions" that the object can respond to. In that case, you are better off formally modeling your communication protocol.

If you have questions, please ask -- this is not an obvious solution and easy to get wrong.

3
  • Can you provide me some good links about the PAC? Also, I can undersstand the tip about avoiding inheritance. As a matter of fact I'm not using that in the View part.
    – Emiliano
    Nov 12, 2010 at 10:12
  • Wikipedia has the first search hit for "Presentation-Abstraction-Control" [1], and it has an extensive References section. See the first paper listed, by Joëlle Coutaz, and at [2] you can get a free copy of the paper. [1] en.wikipedia.org/wiki/Presentation-abstraction-control [2] iihm.imag.fr/publs/1987/Interact87.PAC.pdf
    – user429921
    Nov 12, 2010 at 17:59
  • I forgot to mention that you might want to have a look at Croquet and Alan Kay's Croquet Consortium. Unfortunately the demo is no longer available online as I recall. But the ideas are pertinent. (Alan invented the laptop and coined the term "object-oriented programming", and is a Turing Award winner.)
    – user429921
    Nov 12, 2010 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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