[Kde-games-devel] A design issue when using C++

Ian Wadham ianw2 at optusnet.com.au
Sat Mar 14 02:28:41 CET 2009


Hi everybody,

The rewrite of KGoldrunner I mentioned a few weeks ago is proceeding
well and now plays levels that use Traditional rules.  But I am not happy
with the design of the KGrLevelPlayer class and am looking for some advice.

That class handles all aspects of setting up and playing a level (on the model
side), and signals the view (KGrCanvas) to display tiles and animate sprites.
It is the only class that can "see" all the runners, the dug bricks, the
playing area and the game controls (keyboard, mouse or touchpad).  In
old KGoldrunner, every runner could see all the other runners and all the tile 
layout, which led to some very complicated code.

The problem is that each runner (the hero and each enemy) sometimes needs
to know what is going on outside the square he is in.  For example, the hero
needs to know where to head next, based on where the pointer is or what
key has been pressed and an enemy needs to know if he can enter a square
which might be occupied by another enemy.

To solve this problem, I have added a number of "helper" methods to the
KGrLevelPlayer class, while I have been hacking away at the KGrHero and
KGrEnemy code, but the methods are all <public>.

That seems not at all elegant.  It would make no sense, for example, for
the KGrGame class that creates a KGrLevelPlayer object to call these
"helper" methods.  They are only meaningful in the context of a KGrHero
or KGrEnemy method.  Of course, I can easily avoid such meaningless
calls in practice (and have done that) and so the code all works OK.

I thought of having KGrHero and KGrEnemy double-inherit the level
player and making the helper methods <protected>, but I am keen to
keep overheads low (one level has 25 enemies, all active 50 times
a second) and there is not much family "relationship" between the
KGrLevelPlayer and runner objects, no more than between a soccer
player and a soccer match, say.

Any ideas anyone?  See code and API doco in kgrlevelplayer.h at:
http://websvn.kde.org/branches/work/kgoldrunner/src

Cheers, Ian W.


More information about the kde-games-devel mailing list