Monday, 3 August 2009

Interface overload

Whilst deciding on a unit architecture for everland, I hit a bit of a stumbling block when trying to decide upon an overall architecture for in game units, and how they change over time.

For example, I have the following units so far (on paper I have >30 units), but these are my initial testing units:



The names are fairly self explanatory, with the exception of Unit, which is:



Again, fairly self explanatory. The Nation is simply the user owned nation to which the unit belongs (and from the Nation we can traverse other related properties such as Race, User etc).

The units implement one of several interfaces,



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. I think I'll probably refactor the interfaces to use the Strategy Pattern, and have some overall service responsible for combat. The service would then examine the range between units, determine if they were capable of combat, and if necessary inject a strategy of the correct type (in terms of a battlefield, it might be equivalent to a commander telling his arches to draw swords as an enemy approached).

Part of the fun I'm having with everland is hitting walls like this and trying out different solutions - I'm quite sure some of my solutions would be frowned upon, and some are downright wrong, but refactoring is fun, and I am learning!

Do you contribute (or run) an open source project? If not, why not? It doesn't have to be earth shatteringly brilliant, even if you use it as a learning experience it is a good idea. Give it a try.

0 comments:

Post a Comment