Friday, July 5, 2019

What is the best design pattern to apply effects and abilities?


What would be the best design pattern to apply effects and abilities in a game. For example:


I have a unit that has base stats like attack power, defense etc. The unit can hold several items that will boost one or more of these effects. Also the unit can have several skills that can either boost stats, or be like a special attack. The trick behind the skills is, it should get stronger per level.


I have the following requirements:




  1. Data for the skills and items should be loaded through (for example) xml or serialized classes.

  2. The skills should contain a level system. Each level they increase in strength (but not with a fixed percentage each level).

  3. An item or skill can have multiple effects (time based and immediate effects).

  4. The skills should have something like an Execute method with a target unit (for example special attack on unit Y) This effect has a graphical representation (like a rocket firing) that should occur and an effect (damage, armor decrease etc) on the target. This effect could be applied over time or instantaneously.


What is the most usefull design pattern to achieve this?


I can think of the following:


I have several classes: Unit, Ability, Effect.


The Unit class has an array of applied Effects. On add to and on remove from this array the Apply and Remove methods on the Effect class are called, with the Unit object as parameter. The Effect class then handles the appropriate Boost or Decrease on the Units properties. This will work, however, I don't see this working with time based effects.



Are there better solutions?




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...