4
\$\begingroup\$

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?

\$\endgroup\$
6
  • \$\begingroup\$ Why wouldn't it work with time based effects? Couldn't you write custom logic in you effect class that does stuff over time? \$\endgroup\$
    – Tetrad
    Commented Mar 23, 2013 at 13:03
  • 1
    \$\begingroup\$ Yes this is all quite possible, though there must be some cleaner way to do all this. \$\endgroup\$
    – Thomas
    Commented Mar 23, 2013 at 13:07
  • 2
    \$\begingroup\$ Sometimes because of the breadth of things that a game can do you sometimes have to write messy code. However, in my experience, once you get all the functionality implemented, it gets easier to simplify as it's just a refactoring job. \$\endgroup\$
    – Tetrad
    Commented Mar 23, 2013 at 13:10
  • \$\begingroup\$ Possible duplicate: gamedev.stackexchange.com/questions/49343/… resp. gamedev.stackexchange.com/questions/29982/… \$\endgroup\$
    – Exilyth
    Commented Mar 23, 2013 at 14:29
  • 1
    \$\begingroup\$ The apply and remove sound like visitors to a unit/skill hierarchy. Maybe consider the visitor pattern. \$\endgroup\$
    – Ray Tayek
    Commented Mar 23, 2013 at 20:08

1 Answer 1

4
\$\begingroup\$

So this sort of system can be really long winded to describe, I'll try to summarize briefly how we've usually done it in the past with some variation depending on needs but the starting point of my design has usually been something like the following.

Unit/Entity

Unit has many behaviors, behaviors are "stepped" per time step.

Behavior/Modifier

Behavior belongs to a target, behaviors are functors with variations such as, apply effect behavior, applies an effect every N seconds for T seconds, expires after LT seconds.

Ability/Action

Ability applies an effect to a target.

Effect

Effects are functors, with variations like modify unit, create unit, apply behavior, apply effect set (supports conditionals).

Example

So a basic flow for lets say a damage over time ability would be something like this, Unit uses ability A, ability A ensures the target is valid, if valid applies an apply behavior effect to target of ability A.

The behavior applied expires after 10 seconds, and every two seconds it applies an effect to modify subtracting 10 health.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .