Apologies if the title is incorrect, I couldn't think of better wording. I have the following code
template <class E>
void ResolveEntityHit(E& entity, Projectile& projectile) {
static_assert(std::is_base_of<Entity, E>::value, "entity must be of type Entity");
entity.DealDamage(projectile.GetProjectileDamage());
if (entity.IsDead()) {
DestroyEntity(entity); //here is the problem
}
m_projectileManager->DestroyProjectile(projectile);
}
void DestroyEntity(Enemy & enemy)
{
m_enemyManager->DestroyEnemy(enemy);
}
void DestroyEntity(Bunker & bunker)
{
m_bunkerManager->DestroyBunker(bunker);
}
I'm trying to avoid using dynamic_cast as I have read that this isn't good practice. I'm trying to keep the ResolveEntityHit as basic as possible that can accept multiple types, but then I would like to branch off and do different things depending on which type the entity is.
For example, I have my entities separated into different classes, and each class is responsible for removing/adding entities, so I would need to call the function on the correct manager to remove the entity.
The code above doesn't compile and I get error C2664: 'void DestroyEntity(Bunker &)': cannot convert argument 1 from 'E' to 'Enemy &'
Hopefully it's clear what I'm trying to achieve, but I'm asking is there a better way to do this in terms of design/architecture and without the use of dynamic_cast? Possibly through using templates?
ResolveEntityHit()then template there would not work, You could as well declare it asResolveEntityHit(Entity& entity...)