Skip to main content
Post Undeleted by Ryan
deleted 77 characters in body
Source Link
Ryan
  • 965
  • 1
  • 7
  • 12

One alternative that avoids lengthy switch statements all over the program would be to create adefine inside your Skill interface and a Skill factory which constructs skills according to specifications provided elsewhere (suchadditional components that define its behavior. These components would be implementing an interface as a JSON filewell.)

With thisFor example, creating a skill may be done simply by somethingall skills might contain an attack behavior, which could look like:

IAttackBehavior attackBehavior;

Creating an actual instance to put inside the (forgive my C++ syntax but it shouldattackBehavior can be simple enough to understand)done via a factory method, for example:

ISkill skillattackBehavior = SkillFactoryattackBehaviorFactory::createSkillcreateAttack("Fireball" /* relevant parameters here */ );

where createSkill() isThe parameters can be supplied by a staticJSON file as well. You would probably see some switch statements inside this factory method of the SkillFactory class, but they would be contained to one place.

Now that its behaviorOnce your attackBehavior is definedimplemented, using a skill may be done likeyou could call it with:

skillmySkill.attack(enemytarget); // calls mySkill::attackBehavior(target) 

Of course, this really depends on the interface you decide to use for skills. If you wanted to, you could even break a skill into separate components and use a factory method to provide these as well!

Some ofI'm not sure if this idea's benefits, along with its elegance, are that you can now delegate skills quite easily around the program, and additionally may change them at run-time.

This is the gist of the ideaanswers your question perfectly, but hopefully it provides some inspiration forso let me know if you to work withhave any questions.

One alternative that avoids lengthy switch statements would be to create a Skill interface and a Skill factory which constructs skills according to specifications provided elsewhere (such as a JSON file.)

With this, creating a skill may be done simply by something like (forgive my C++ syntax but it should be simple enough to understand)

ISkill skill = SkillFactory::createSkill("Fireball");

where createSkill() is a static method of the SkillFactory class.

Now that its behavior is defined, using a skill may be done like:

skill.attack(enemy);

Of course, this really depends on the interface you decide to use for skills. If you wanted to, you could even break a skill into separate components and use a factory method to provide these as well!

Some of this idea's benefits, along with its elegance, are that you can now delegate skills quite easily around the program, and additionally may change them at run-time.

This is the gist of the idea, but hopefully it provides some inspiration for you to work with.

One alternative that avoids lengthy switch statements all over the program would be to define inside your Skill interface additional components that define its behavior. These components would be implementing an interface as well.

For example, all skills might contain an attack behavior, which could look like:

IAttackBehavior attackBehavior;

Creating an actual instance to put inside the attackBehavior can be done via a factory method, for example:

attackBehavior = attackBehaviorFactory::createAttack( /* relevant parameters here */ );

The parameters can be supplied by a JSON file as well. You would probably see some switch statements inside this factory method, but they would be contained to one place.

Once your attackBehavior is implemented, you could call it with:

mySkill.attack(target); // calls mySkill::attackBehavior(target) 

I'm not sure if this answers your question perfectly, so let me know if you have any questions.

Post Deleted by Ryan
Source Link
Ryan
  • 965
  • 1
  • 7
  • 12

One alternative that avoids lengthy switch statements would be to create a Skill interface and a Skill factory which constructs skills according to specifications provided elsewhere (such as a JSON file.)

With this, creating a skill may be done simply by something like (forgive my C++ syntax but it should be simple enough to understand)

ISkill skill = SkillFactory::createSkill("Fireball");

where createSkill() is a static method of the SkillFactory class.

Now that its behavior is defined, using a skill may be done like:

skill.attack(enemy);

Of course, this really depends on the interface you decide to use for skills. If you wanted to, you could even break a skill into separate components and use a factory method to provide these as well!

Some of this idea's benefits, along with its elegance, are that you can now delegate skills quite easily around the program, and additionally may change them at run-time.

This is the gist of the idea, but hopefully it provides some inspiration for you to work with.