2 Replies - 1489 Views - Last Post: 06 March 2013 - 07:06 PM Rate Topic: -----

#1 haxic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 28-May 11

Implementing the MVC pattern?

Posted 06 March 2013 - 03:30 PM

Goodevening gentlemen.

I have decided to learn how to use MVC pattern by implementing it in a rather simple "risk"-like game. I bet the MVC pattern isn't optimal for games, but as said, I'd like to learn how to use it. I have read 15 different articles about how to implement the MVC pattern, but they are all very basic and doesn't really explain how to deal with, for example, different game modes, menus, layers etc etc... Which is what I was hoping to learn how to handle by creating this thread!

Before I decided to use the MVC pattern I split everything into hypothetical "components". I mainly used two Screen objects MenuScreen and GameScreen depending on wheather a game is active or not.
The MenuScreen was mainly used to create a nice background with particles moving accross the screen using Background object, but it also handled all the necessary Menu objects such as "main menu", "options menu", "new game menu" etc etc.
Once a game got created the MenuScreen got replaced by the GameScreen. The GameScreen contained the actual game such as the Player objects, the Board(a grid of nodes) object etc etc..
I was also planning on using LoadingScreen and ScoreScreen between/before/after creating/exiting a game.
When I used that I had both logic and graphics calculated in every individual object. I didn't have any direct problem using this structure other than it got a bit complex since logic, input and graphics were all merged into every object.

On to my understanding of the MVC pattern, well, I understand the basics;

Controller -> Model
Controller -> View
View -> Model

I havn't yet coded anything important on my new MVC approach, simply because I want to do some research and understand the pattern a bit before I get to work. I have only implemented the basics stuff; the window, logic/graphic loop and the main MVC classes. I have also implemented a BackgroundAnimation object which runs smoothly, but it is irrelevant atm, because my problem is about dealing with the component transition... Anyways, currently in my MVC impementation:

The GameEngine:
The core of my game is a "GameEngine" class which contains the main Model, View and Controller objects, the window-frame and the logics and graphics loop threads. Furthermore, the logics and graphics loop are passed to the main Controller.

The Controller:
Uses the logical loop and inputs (user-key/mouse and AI inputs) to modify the Model. The Controller also passes the graphical loop on to the View.

The View:
Renders the graphics of the game. It takes the data from the Model and renders the graphics based on those.

The Model:
Just contains data.


The problem I have now with the MVC pattern is that I do not understand how to have these Screen-transitions to switch between screens, which contained completely different types of functions/graphics. Like I said, I used to think "component", but if I try to think component within the MVC pattern then is becomes kind of messy... This is how I understand it:

public class Controller {
	Model model;
	View view;
	
	ModeController modecont;

	public Controller(Model model, View view) {
		this.model = model;
		this.view = view;
		initiateMode();
	}

	private void initiateMode() {
		Mode mode = new Mode (); // this is the new mode model
		model.mode  = mode;
		view.modeview = new ModeViewer(mode); // the modeviewer in the View gets replaced
		modecont = new ModeController(mode, view.modeview); // the modecontroller in the View gets replaced
	}

	public void logic() {
		modecont.logic();
	}

}


This would, in almost the same way, switch the Screen component as I did in my old structure, but there are some major problems... This would work for one Mode, but if I use different Modes that do completely different things, then I would create a ModeController and a ModeView for every different Mode model I want to use... This also means that everytime I want to switch Mode I need a method/function for every different Mode, just like the method "initiateMode();". I havn't implemented it yet so I don't know if it works. It it did it would split up into MVC, but require alot more coding for every individual Mode object I want to implement... But, is this the right way to do it? The purpose is that when I'm in the MenuMode I don't need to have a Game object running, it would be a waste system usage.... I find it better to replace the MenuMode with a GameMode when I'm actually playing the Game. I would love a visual explaination such as an example class diagram or something similar I would be most grateful. But ofc, an explaination in words or a good link is just fine :P - but don't give me any links to simple MVC articles, because I have most likely already read them :/

Thank you for your time.

Is This A Good Question/Topic? 0
  • +

Replies To: Implementing the MVC pattern?

#2 haxic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 28-May 11

Re: Implementing the MVC pattern?

Posted 06 March 2013 - 03:35 PM

I forgot to mention that I use Java.

I have also used this tutorial: http://obviam.net/in...building-games/
Was This Post Helpful? 0
  • +
  • -

#3 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Implementing the MVC pattern?

Posted 06 March 2013 - 07:06 PM

Not 100% the MVC pattern is ideal for what you're trying to achieve. A menu system is normally a bit more complicated, and I would have thought something more like the Observer pattern might have been more useful. I'm not a UI programmer per se, so I could be wrong.

Anyway, sticking with MVC:

Model - This should contain the 'data' for the overall menu system, such as the current state of the system and values for all possible states that it can represent. The Model sets internal states according to the Controller.

View - This will be an object that references a number of menu templates that represent the current state of the menu system. The View constructs it's internal state according to the Model.

Controller - As you would expect, this will accept user input and set menu system states that are stored in the Model.

Can you see how I have partitioned things here?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1