I had a question about how to handle this type of class hierachy. The hierarchy is related to a game, but I think it's general enough to go here over the gamdev. I'd imagine this sort of thing comes up occasionally so I'm curious about the possible solutions
I have a base class called BaseCharacter that defines a character in my game. It contains all the basic stats, status effect holders, events etc. to have a baseline character. Most of my game logic operates on entities of BaseCharacter, casting where necessary.
From there I inherit into a Humanoid class which adds the necessary structures and methods to add equipment (weapons, armor, etc.) to the character, then finally I have a PlayableCharacter class that inheirts from humanoid and adds things like current EXP, configurable ability structures, etc.
So the class structure is simply:
BaseCharacter--->Humanoid--->PlayableCharacter
This structure works great for defining player side characters, though I may decide to refine the structure a little more. I have to be able to add "enemy" classes to the hierarchy. In order to satisfy the requirements for an enemy, I need to add some data structures/methods related to EXP/points for defeating the enemy, loot tables, etc.
The problem is, where do I add these structures? Adding them to BaseCharacter seems like a bad idea as I'm basically saying every character has the required methods/data structures for an enemy entity. I considered starting a separate stream of classes that would end up looking like this:
BaseCharacter--->Humanoid--->PlayableCharacter
\---->Enemy--->EnemyHumanoid
where Enemy inherits from BaseCharacter. EnemyHumanoid would add the same set of methods/data structures as the inheritance from BaseCharacter->Humanoid. THis works, but means I can't cast EnemyHumanoid into Humanoid despite the fact that EnemyHumanoid satisfies the requirements of Humanoid. This also adds redundancy as I'd have these two inheritances add the exact same set of objects/methods.
then in considered using a seperate class called EnemyInterface, and end up with an inheritance hierarchy like this:
BaseCharacter----->Humanoid----->PlayableCharacter
| |
V V
Enemy EnemyHumanoid
Enemy and EnemyHumanoid all implement EnemyInterface. However, I now lose the Enemy<-->EnemyHumanoid relationship. Should I use multiple inehritance here? (have EnemyHumanoid inheirt from Enemy and Humanoid?) is that considered bad form? Is there a more logical way to do this? The example here is rather simple, but if i do end up splitting my class hierarchy more, it'll only get more complicated.
Any help would be appreciated