Wednesday, 19 August 2009

First commit of game project

Did a few solid hours of TDD last night to get the bare bones of the game in place. So far there's adding/removing towers, and the logic for monsters entering/leaving tiles. That includes the Goal tile, and there's a GameOver event fired when the Goal runs out of points.

The way I imagined monster movement working last night was: every time they move a certain distance they fire a MonsterMoved event. The World is listening for these events. When it detects that a monster has moved over a tile boundary, it fires a MonsterChangedTile event. All the tiles are listening for these, and it allows them to keep a list of monsters they contain (especially important for the Goal tile).

This initially seemed wasteful to me - it's a lot of events - but then I thought "computers are fast as fuck", and ignored myself. I'd like to use the philosophy of naive implementation, only optimising where it's painfully obvious or actually costing us CPU.

(Thinking about it now, World should know which tile the monster has moved into, and message it directly - ah well, the events haven't actually been implemented yet so it's an easy change.)

Doing the tests first was pretty helpful - your code starts off being useable - it has to be so you can test it. It does seem quite slow going at first, but you have a certain level of confidence in your code afterwards that you don't get otherwise. Definitely recommend you guys give it a shot. Give me a shout if you need a hand with the mocking framework or the minutiae of NUnit.

5 comments:

  1. Gary- the event stuff sounds like a prime candidate for the Observer Pattern. I will look through the code when I get back from work.

    ReplyDelete
  2. There might be a subtle difference I'm not getting, but isn't C#'s built in events just a language-level implementation of the observer pattern?

    A guru at my work once said that design patterns show you where there are gaps in the language.

    ReplyDelete
  3. Oh, meant to add - the events aren't actually in, just what would be the handlers for them.

    ReplyDelete
  4. Perhaps, I don't know about the event system. It probably is. To do it manually, Tile should extend an Observable interface, World should maintain a list of "subscribed" Observables, Tile will subscribe to receive the event, and when Monster lets World know it has moved, World will push the event to all the subscribed Tiles.

    I wouldn't be surprised if this was built in though. We should mkae sure it is not constantly 'asking' for updates but rather 'told' via subscription- much more optimal :)

    ReplyDelete
  5. Yeah, exactly this is built into the language. The syntax is like:

    SomeEvent += OnSomeEvent(eventArguments)

    Now whenever "SomeEvent" is fired, OnSomeEvent() will be called.

    ReplyDelete