How should I structure my unit class relationships?

I want to structure my classes in a way that makes sense and is easily adaptable when implementing new types of units etc.

I was thinking that I’d have a base Class: Unit. This class would define the abstract interface for all the Units including players and AI.

I would then have three classes that Implement/Inherit from Unit. They would be the different elemental types of the Unit.

So i’d have the following:

  • FireUnit : Unit
  • WaterUnit: Unit
  • EarthUnit: Unit

My problem is that I want players and AI units to act differently but still use the same Unit classes.

I can’t seem to get my head around how this would work.
Would Unit inherit from player? Or would player inherit from Unit?

Hopefully this makes sense, I’m quite new to OOP so I’m not even sure if i’m asking the right questions!

Thanks

Ditch deep inheritance (for the most part) and use components to go wide instead.

Each GameObject has the following components

  • Movement component
  • Weapon component
  • Armour component
  • Input component

Then each component type can have an inheritance structure

Movement component is the base for

  • Tracked
  • Walker
  • Flying

Weapon

  • Gun
  • Flamethrower
  • Water pistol

Input

  • Player controlled
  • AI (You can further derive for different AI behaviours)

Get the idea?

Your AI component simply requires a TypeOf(Movement) to be present. The AI interacts with the movement component, and never needs to know exactly what type of component it is dealing with.

The weapon component behaves the same if it is fired by a PlayerInput or an AgressiveAI2. Again, it doesn’t care.