to be honest i would rather just focus on classes now. so are there any good examples of how classes should look? |
I'd need to shop around before I could point you at something that illustrates the principles clearly. It might be worth posting a thread just about this? (Asking for a pointer to a well-designed, open source, OO game which isn't that complicated.)
But I did (just) find this:
OGRE – Open Source 3D Graphics Engine
http://www.ogre3d.org/
You could download the source and have a look at it:
http://www.ogre3d.org/download/source
It's rather involved, but you could look at some of the samples and follow though to the class they use. Plenty of documentation is provided, too.
The same search also found this:
An Introduction to Object-Oriented Programming for C++ Game Developers
http://www.peachpit.com/articles/article.aspx?p=482334
Note that the approach I sketched out in my earlier mail is a pretty common way of teaching "object think". Classes are not really about how they look, but how they behave.
It looks ok as a starting point, but it's not really detailed enough to be good or bad.
As the classes are extended, what will be good (or bad) will depend on what they need to do. And there are, of course, usually a number of equally valid solutions. You just need the select one approach and use it consistently.
The Character / Enemy / Bat / Skeleton hierachy does, however, raise these questions:
1. when you say Character, you mean a good character? Are enemies bad characters? Do characters represent a player. Etc.
2. do characters also have health and name, as well as enemies? If so, name and health should be moved to a common base class
3. do enemies ever fall out and fight each other? in that case, the attack method should be altered to take a suitable base class. e.g. Fighter (or Combatant?) so enemies can fight other enemies as well as characters
4. Do enemies have all their own way of attacking, or do they share things in common? If the former, Attack should be pure virtual
5. You also need (e.g.) AcceptHit (this is just for illustrative purposes; need to work out how weapons, etc.)
1 2 3 4 5 6 7 8
|
void Skeleton::Attack(Fighter& other)
{
while(isFighting() && other.isFighting())
{
AcceptHit(other);
other.AcceptHit(*this);
}
}
|
5. etc
Andy
PS Found OGRE by googling ""well designed " oo "open source" project c++ game"...
What is the best designed open source game engine to take example? [closed]
http://gamedev.stackexchange.com/questions/5277/what-is-the-best-designed-open-source-game-engine-to-take-example
The Ogre3D rendering library is pretty close to engine code and from all accounts I've heard is very well designed and very OO.
http://www.ogre3d.org/ |
PPS Note that member variables should really be initialized using the constructor initializer list:
1 2 3
|
Enemy::Enemy(int HLTH, string NME) : health(HLTH), name(NME)
{
}
|
Not a bit deal when the variables are built-in types like int, double. But when they're classes, your using the default contructors plus operator= if you use the constructor body approach; but only the initializing constructor if you use the init list. (And it's best to be consistent...)