Thoughts on class structure for multiple enemy types

Hi guys,

I’d be interested to hear your thoughts on what’s the best practice (or at least good practice) of writing classes for multiple enemy types.

I’m writing a TPS Desert Strike clone and I have several enemy vehicle and soldier types. I’ve begun writing classes like EnemyHealth and EnemyMovement but I’m finding that each types has it’s own unique differences. For example, an enemy soldier needs animating when it moves, whereas a tank does not but can only drive in straight lines (for the sake of brevity). A Jeep can’t turn on its Y axis like a tank and a soldier doesn’t explode like when a fuel truck is destroyed.

So, how should I structure my classes? ie shoud I have class EnemyTankHealth inherit from class EnemyHealth. or should I just write completely separate code for each enemy type.

What are your thoughts?

Dave

I go for a component based pattern. For something like this I typically have two levels of inheritance.

On the top level I have abstract base classes for

  • Movement
  • Health
  • Weapon
  • Death
  • … You get the idea

These classes contain all of the information that is required to be shared between components. All of the dependencies are denoted at this level with RequiresComponent.

At the second inheritance level I split things up into individual components

  • TankMovement
  • SoldierMovement
  • HelicoptorMovement

These second level components can only access each other through the abstract base level.

If TankMovement is only ever going to pair with TankHealth and TankWeapon then this set up might be over done. But if you might pair HelicopterMovement or TankMovement with RocketWeapon or MachineGunWeapon then it works well.

2 Likes

Hi BoredMormon, Thanks for your thoughts on this. This is just the sort of advice I was hoping for.

Any chance you could give us an example of what this might look like in pseudo code? - excuse my ignorance but I’m still working on my knowledge of class structures :slight_smile: