views:

338

answers:

3

As a hobby project I am currently trying to create a small browser-based game - and I thought it would be the "right thing" to code it using the MVC pattern, separating the game's "engine" from presentation. It would be a simple "rpg" game, where the player's character wander the world fighting monsters and gathering items.

My problem is that I got stuck with the engine's structure. In the current design most of the engine is dominated by the "player's character" object - there are such things as location, monster or item, but it is a character that does most of the actions: travels, attacks, buys items etc. I know there are probably other ways to design such engine, but somehow I can't figure it out, so I'm asking for some tips or advices here.

Oh, and the server backend will be php+mysql, if that in any way matters.

+5  A: 

"but it is a character that does most of the actions:"

Yes, but...

First, separate the human user ("player") from their character. The character is not the player.

You have a large number of objects. Locations, passive things, active things (monsters), and the character. They all interact with each other.

You have to isolate responsibility completely. The character doesn't dominate, the character merely participates.

You'll find it more pleasant if you define each possible human-player action as a Command. Each subclass of Command can make a change to the state of the model. Things move, monsters move, the character moves (character, not player.)

Picking something up or laying it down are each a subclass of Command that moves a thing from character to location or location to character. The player issues the command; the command updates the location, item and character.

The Character is passive. Things are mostly passive. Locations are mostly passive. The Command objects have algorithms to update the state and association among all the various things in your model.

If you want to carry this a step further, each Command can be a Memento with an undo capability as well as a do capability. Then the history of the game is a history of these Mementos.

S.Lott
I do separated character from the player, mostly because I wanted to allow multiple characters - but I have not thought about going this far, so thanks for the advice.
Krzysztof Sikorski
A: 

I am a bit confused about your question.

MVC is a separate pattern from you game engine design - you will still need an MVC pattern regardless of what the underlying engine structure looks like - users will interact with Views, which submit back to the Control. In your case the "M" may be a quite complex model of the current game state.

I would start by thinking about this from a purely data-driven perspective. Everything that happens ends up being an operation on the Character model.

Toby Hede
A: 

The character is part of the model, but that character is abstract - it's just an object or data structure on the server. One technique I use to keep myself honest w.r.t. MVC is have a way to drive the model without the graphical View(s) and Controller(s). For a web based app, you might drive it via textual GET requests, or via XMLRPC, etc.

ja