I am writing a game in ActionScript where I've got multiple classes that should be "hitable" by shots.
The most generic class that all other in-game entities inherit from is CombatObject; The classes CombatShip, CombatAsteroid and various others inherit from it. The classes CombatAi and CombatPlayer both inherit from CombatShip.
Now I want to make CombatAi, CombatPlayer and CombatAsteroid hitable by shots, but I don't want them to inherit it (later there might be CombatShips that shouldn't be hitable).
My idea now was to have these three implement an Interface, IHitable, so that when they collide with a shot, I can ask if(hitObject is IHitable)
and if true, have the shot destroyed and do damage.
Now the question is, would it be best to have each of these classes
- Implement all the needed code to take damage (such as checking shields, calculating damage percentages, etc.) themselves
- Have them all own an instance of the class
DamageManager
and implement a functiongetDamageManager():DamageManager
, that returns a class that handles all damage related functionality - One disadvantage of (2.) would be that each CombatShip, CombatAsteroid, etc would have to own an Instance of DamageManager. Would it perhaps be even better to have DamageManager be a Singleton and just have a reference to the ship and the shot given to it and let it handle the rest?