So I've been trying to use OOP where I need polymorphic behavior. I know that a lot of people have problems with it but it has worked out so far for me. I've tried to avoid using inheritance too much and tried to use composition to make bigger classes out of smaller ones.
This is my situation. I have an attack class like
abstract Class Attack{
abstract void execute(vector3 Dir);
}
And melee and ranged attack classes like
class MeleeAttack{
override void execute(vector3 Dir){//do stuff}
}
class ProjectileAttack{
override void execute(vector3 Dir){//spawn stuff}
}
I need player specific attack behaviour so I have a player attack class that I use for the player.
class PlayerAttack{//does not inherit attack since I need more parameters in execute
Attack meleeOrRangedAttack;
void execute(vector3 Dir, random params....){
//do meleeOrRangedAttack while obeying player rules
dir = calculateDirTBasedOnInputOrSomething()
meleeOrRangedAttack.execute(dir)
}
}
And a different enemy Attack Class because enemies need to target a player.
class EnemyAttack{
Player target;
Attack meleeOrRangedAttack;
override void execute(target){
//do meleeOrRangedAttack while targetting player
dir = calculateDirToTarget()
meleeOrRangedAttack(dir)
}
}
This works well and I can reuse melee or ranged attacks both for players and enemies.
But say I need a homing Attack for enemies. If I make it a subclass of attack then I can't just override execute(). I need some way to pass in a target. Is there a better way to structure this code while keeping composition(not writing separate enemy homing attack, player homing attack classes)?