Wednesday 21 January 2015

Model-View-Controller Design with Unity

This article will discuss the overall design architecture that will be used to create Essence of Glory in Unity. Since Essence of Glory is a tabletop game, we can compare it to other such games and think about how they would be designed in Unity.


Let's say you were going to implement a game like Jenga in Unity. How would you do it? Well, objects in Unity are modeled with GameObjects. These follow the Composite design pattern; every GameObject is a collection of GameObjects, and/or MonoBehaviors. If you are familiar with Entity-Component systems, you can think of GameObjects as Entitites and MonoBehaviors as Components.

Coming back to Jenga, you would probably have each block as a GameObject and attach the right MonoBehaviours to it to give it meshes, textures, shaders, physics collision and so on. Furthermore, you'd want to know when a block hit the ground. So you could add script, in the form of a MonoBehaviour, to each block that would trigger when it collides with the ground.

But Essence of Glory is not a game like Jenga. A better comparison might be chess.


What happens when, in a game of chess, a piece falls over? Unlike in Jenga, this occurrence has no bearing on the rules. You would simply pick up the piece and replace it.

But hold on, I just posted a picture of a game of chess, but that isn't what chess looks like.

This is a game of chess:
And so is this:
 Or even this:
"1. e4 c5 2. Nf3 d6 3. Bb5+ Bd7 4. Bxd7+ Qxd7 5. c4 Nc6 6. Nc3 Nf6 7. 0-0 g6 8. d4 cxd4 9. Nxd4 Bg7 10. Nde2 Qe6!? 11. Nd5 Qxe4 12. Nc7+ Kd7 13. Nxa8 Qxc4 14. Nb6+ axb6 15. Nc3 Ra8 16. a4 Ne4 17. Nxe4 Qxe4 18. Qb3 f5 19. Bg5 Qb4 20. Qf7 Be5 21. h3 Rxa4 22. Rxa4 Qxa4 23. Qxh7 Bxb2 24. Qxg6 Qe4 25. Qf7 Bd4 26. Qb3 f4 27. Qf7 Be5 28. h4 b5 29. h5 Qc4 30. Qf5+ Qe6 31. Qxe6+ Kxe6  32. g3 fxg3 33. fxg3 b4  34. Bf4 Bd4+ 35. Kh1! b3 36. g4 Kd5 37. g5 e6 38. h6 Ne7 39. Rd1 e5 40. Be3 Kc4 41. Bxd4 exd4 42. Kg2 b2 43. Kf3 Kc3 44. h7 Ng6 45. Ke4 Kc2 46. Rh1 d3  47. Kf5 b1=Q 48. Rxb1 Kxb1 49. Kxg6 d2 50. h8=Q d1=Q 51. Qh7 b5?! 52. Kf6+ Kb2 53. Qh2+ Ka1 54. Qf4 b4? 55. Qxb4 Qf3+ 56. Kg7 d5 57. Qd4+ Kb1 58. g6 Qe4 59. Qg1+ Kb2 60. Qf2+ Kc1 61. Kf6 d4 62. g7 1–0"
In fact, some people play chess blind-folded, or via mail.

What does this tell us? That the way a game of chess is visually represented is distinct from what constitutes the actual game. The game of chess is an abstract rules construct which is merely represented in some form or another to players. This is in contrast to the earlier example of Jenga, in which the pieces that players manipulate are also significant to the rules.

Furthermore, this indicates that chess (and Essence of Glory) is a good fit for the Model-View-Controller (MVC) software architecture pattern. In MVC, the software is divided into three areas of concern:

  1. The Model - simulates the state of the system. For a game like ours, this would involve tracking the position of pieces, resolving the outcome of moves, rolling dice, and determining if a player has won. This can be implemented with plain-old-C# classes and need not involve Unity at all.
  2. The View - represents the state of the system to the user. Everything that the user sees is part of the View, this includes any buttons or other interface elements. As such, the View will mostly be made up of Unity GameObjects. It is updated based on information received from either the Controller or directly from the Model.
  3. The Controller - updates the Model based on user input. May also be responsible for updating the View based on changes in the Model. Often implements the Observer pattern with UI elements.
This provides separation of concerns. Each part can be updated relatively independently of the others. It doesn't matter if a ship being moved is being represented by temporary place-holder art or a finalized and polished high-def model, the state contained in the Model and the command issued by the Controller will be the same.

This also helps for input. Only the Controller is concerned about where input comes from. Whether a command is received from mouse, keyboard, touch, over the network or generated by the A.I., the change in the Model will be the same and so will its effect on the View.

Model-View-Controller does not seem to be common in Unity, this article was the only real source I found after a quick search. It's an informative read, though it references the no-longer-used NGUI library. The example project linked in the above article is a very good example of how MVC can work.

No comments:

Post a Comment