Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
Why wouldn't it work with time based effects? Couldn't you write custom logic in you effect class that does stuff over time? –  Tetrad Mar 23 '13 at 13:03
    
Yes this is all quite possible, though there must be some cleaner way to do all this. –  Thomas Mar 23 '13 at 13:07
2  
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. –  Tetrad Mar 23 '13 at 13:10
    
1  
The apply and remove sound like visitors to a unit/skill hierarchy. Maybe consider the visitor pattern. –  Ray Tayek Mar 23 '13 at 20:08
show 1 more comment

1 Answer 1

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.