Let the fighting commence!

time to read 2 min | 290 words

Another interesting problem that I run into in Jamie Farser’s blog, he is building a game and run into a problem designing the object model for it. The problem can be sum up pretty easily in two pictures:

[1.png]

And here is what Jamie has to say about it:

Each interface defines several methods or properties (for example, Attack or AttackRanged). However this feels a bit wrong to me - I'd ideally like just a single Attack method, which would then do something to determine whether the unit was capable of ranged attack

There is another issue here, the object model as it is structured now is fixed. But in most games, units gain additional capabilities as they mature. For example, a spearman may start out as a simple hand to hand unit but be upgraded to a javelin thrower.

This calls for a state pattern, because the state that a unit may be is going to changed over time. More over, I don’t like the idea that we have things like Attack or AttackRanged, it seems to me that the game engine knows way too much about the way that units are operating. I would much rather have something like ExecuteTurn, instead.

That would move the game engine into a simple command executer, and the code inside a a unit to a simple forward to the appropriate state. It seems to be simpler. And upgrading can be done by simply switching the state that you are currently in. It is a remarkably stable design. Something very similar to that is sitting in the heart of most of what Rhino Mocks is doing.