I’m having a hard time wrapping my head around how best to set this up. I’ll explain what I have so far.
• NPC class that has basic stats like health.
• EnemySpirit, EnemyRock, EnemyFireCaster, etc classes for each type of enemy. Inherits NPC class. Needs to process the damage received from a player spell.
• Player class that is the player. Casts spells with the intension of directing them toward the Enemy* class component. Handles player input, for now.
Player can access public void TakeDamage() from EnemySpirit with something like target.GetComponent().TakeDamage(n). That’s fine if every enemy happens to be an EnemySpirit, but the moment I broaden my game to include EnemyRock enemies and EnemyFireCaster enemies, that code breaks because they are not of the EnemySpirit class obviously.
Here are some solutions I have considered:
• Have NPC keep a variable that indicates what class the enemy is. It would be set whenever the enemy is created. That way the code could be written like this: target.GetComponent().ForwardDamageFromNPCtoEnemy(n). NPC would simply have a long list of all the enemy classes and could essentially choose the right version (GetComponent from a switch(). This list could be put on the Player, NPC, in a static singleton… not sure where the best place might be, but NPC for now.
• Have NPC handle the damage. I don’t see how this could be done because the entire purpose of the Enemy class is to handle the buffs and debuffs the enemy has, which affect how much damage they take. The NPC class would end up having to manage every possible enemy class I create which defeats the purpose of enemy classes to begin with.
• Interfaces? I am less experienced with interfaces. Would this be a possible solution?
• Generics? I am inexperienced with these and don’t know if this is a possible solution or not.
• Have Player somehow figure out what class the GameObject (which is “target” in my code) like
var mysteryClass = target.FigureOutWhatEnemyThisIsAndReturnClass();
target.GetComponent().TakeDamage(n)
but I don’t even know how you’d write such code correctly.
Maybe I’m just going about this all wrong. I’m open to any and all advice. I’m early in the coding stages of doing this, so it’s not difficult to go in any direction.