I'm trying to write a simple game and I really want to finally create something that would be "programmatically correct". I stuck with a problem like this: I have class
public abstract class ProjectileLogic<T> where T : ProjectileInfo{
protected T _projectileInfo;
public abstract void OnInitialize();
public abstract void OnHitTerrain(Vector2 hitDirection); //hitting the floor should give Vector2.down, etc.
public abstract void OnHitEnemy();
public abstract void Destroy();
}
I'm using generics, because inheriting classes need different objects inheriting from ProjectileInfo class. ProjectileInfo contains data like damage, initial speed etc.
The problem appears, when spawner creates the projectile and calls it's "OnInitialize".
var projectile = spawner.CreateProjectile(ProjectileType.Pellet);
projectile.GetComponent<ProjectileLogic<...what exactly?...>>().OnInitialize();
GetComponent returns the component attatched do game object "projectile" of a given type.
I thought, I could create interface IProjectileLogic containing all methods of ProjectileLogic and then:
public class ProjectileLogic<T> : IProjectileLogic where T : ProjectileInfo{
protected T _projectileInfo;
}
and then spawner:
var projectile = spawner.CreateProjectile(ProjectileType.Pellet);
projectile.GetComponentByInterface<IProjectileLogic>().OnInitialize();
But I really don't know if it wouldn't be misuse of interfaces and bad practice used only because "it helps in this single case".