Monday, December 26, 2011

Commands or just a big bunch of entangled classes

I have been thinking about how I want to handle the gaming aspects of the game. How to handle player moves, what happens when the player clicks om screen. Can he pickup that item or is that nailed to the ground.

In the past I have just had a bunch of functions in different classes that I could call. On the map I could check if a tile was blocked and then move the player. That meant that everything basically knew something about everything else. That meant that one change could spill over in multiple classes that didn't have anything to do with the original class.
This time I will try to make a command based system. A global commands list is available to all components. They then generate commands depending on what they want done and a commands processor then executes them. It all sounds weary nice, but at the moment, all I have is a vague idea about what it should look like, and no idea how to actually implement it.

My thoughts is that the processor should call a class that have an execute function, passing along any parameters with the command. The function then performs the action.

Lets say the player wants to move one tile to the left. Player presses "A" button on keyboard.
  1. A command with name = move and a string containing direction "west" is added to parameters and the player is added.
  2. Command is placed in the commands list.
  3. Command is extracted by the processor.
  4. Processor determines what class should be called "move" and calls execute with the given parameters.
  5. Move class execute function checks map, moves player
That way I can write a bunch of different classes that perform specific actions without having to know about everything else and if "when" I change something, I only have to rewrite those commands that use it.

No comments:

Post a Comment