I don't really get it, you are using polymorphism and then you don't?
So far I understand it as the following, you have a base class Ability
and derived classes that actually implement the ability. So something like:
abstract class Ability
{
abstract public void Execute();
}
class CastMagicMissile : Ability
{
override public void Execute()
{
// some logic to case magic missile
}
}
Why do you want to pass the id of the ability to Execute
? The ability already knows what type it is. The only thing that remains to actually do is find a mapping from ID to ability,
but this can either be solved by a dictionary or a switch statement. I can think of two situations where this may be needed, construction and invocation via the network.
With construction, I would use a factory function, like so:
#define MAGIC_MISSILE_ABILITY 1337
abstract class Ability
{
static public Ability Create(byte id)
{
switch (id)
{
case MAGIC_MISSILE_ABILITY:
return new CastMagicMissile();
default:
// throw some appropriate error
}
}
//...
}
You may want to have a more flexible factory facility, but you get the idea.
With the network invocation issue, I would put an id on each ability, with something like a GetType
method and the loop that in the invocation. Something along the lines of:
class Unit
{
private List<Ability> abilities;
public void ExecuteAbility(byte id)
{
foreach (Ability ability in abilities)
{
if (id == ability.GetType())
{
ability.Execute();
return;
}
}
// throw invalid ability error or ignore
}
}
Note: If you share the ability instances among the units, you may need to pass the reference to the Execute
method, but that is about it.