2
\$\begingroup\$

I'm currently wondering how to properly structure game code.

I have come across MVC (Model View Controller) method/approach, which seams to be a decent way to get a good extendable structure.

However, I'm wondering how one should put in animations and visual transitions into this model?

The simples example I can think of is Tetris.

In many newer Tetris clones, if a line is cleared, before it's removed from the screen, it sometimes flashes in different colors for visual effect. Where does this fit in the model?

The way I understand is that MVC has a controller that should update the board state, and if one line is full, it should be removed, yet the model would do this in a single frame? The view should not interact with the model, so what would be a good way to make these visual effects appear without loosing the benefits of decoupled logic and rendering?

Sorry if this is an ill posed question.

Thanks in advance : )

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Lately I've been wasting my spare time on an MVC/MVVM Monogame framework. I am writing a Roguelike as I go along.

Animations aren't easy. As you noticed, merely updating the model is not enough, since the animation lasts longer than the simple "remove a line" instruction in your code.

Moreover, the flashing animation wouldn't even belong in the model, as the animation state and duration aren't model values per se (model is our tetris block, for instance), but artifacts of a specific event (such as line removal).

You could have a viewmodel wrapping your model. A viewmodel's contents do not have to mirror its underlying model properties 1:1. A viewmodel is another abstraction layer. Something that includes view-specific properties that would otherwise pollute your model.

Now, such a system without observable properties (think WPF's Dependency Properties or Knockout.js observables) is somewhat of a pain to maintain and error prone due to its specular nature.

(By the way, this is the MVVM approach, model, view, view-model, if you want to look it up).

It's not easy. Development complexity increases in exchange for a potential long-term benefit you might not reap because you end up scrapping everything before completion.

Please let me know if this helps.

\$\endgroup\$
4
  • \$\begingroup\$ It sure is nice to hear that I haven't just misunderstod every aspect of it. I have before done something a long the lines of wrapping a view around the model, yet I were really attracted by the pure MVC due to it's very clear distinctions between functions. Could another approach, at least to the flashing animation to split 'full line detection' and removal into two seperate 'ticks', so that a full line at first simply 'stages' the line for removal later? \$\endgroup\$ Sep 13, 2015 at 17:02
  • \$\begingroup\$ @user1291510 I am using the term "view model" in the sense of "a data model used by the presentation layer of the program", which is usually a wrapper for the actual data model. I'll give you a very brief example: imagine a rating system, from 1 to 5. each entry has a rating. ratings towards 5 get greener, towards 1 redder. each entry's rating belongs to the data model. the fact that it becomes either green or red depends purely on how you present the data. \$\endgroup\$
    – raine
    Sep 13, 2015 at 19:07
  • \$\begingroup\$ @user1291510 so, you can see that the "color" property belongs in the viewmodel, it depends on the rating (which exists in the model) but it's something strictly related to the view. \$\endgroup\$
    – raine
    Sep 13, 2015 at 19:09
  • \$\begingroup\$ @user1291510 I honestly believe you can use a viewmodel in this case, I wouldn't worry about the "purity" of the approach. in my book, a viewmodel is still an "elegant" separation of concerns. \$\endgroup\$
    – raine
    Sep 13, 2015 at 19:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .