# In a Tower defense game, how to do buffs/debuffs

The question is at the very bottom. If you understand Buffs/Debuffs in tower defense games then you should skip the bulk of this question and go to the bottom (seperated with the long line)

I plan on making an IPhone TD game. The fact that its an iPhone game isn't relevant but I am coding in Objective-c with Cocos2D.

I am relatively inexperienced in the field of game design so I'm looking for some advice from someone experienced in this field.

In tower defense, there are two things that are relevant to my question: towers/enemies (both have their own classes/children). They each have stats like hp, damage, speed, etc.

I want to add buffs/defuffs, for instance: Towers A,B and C each have 15 base damage. Tower D would be a buff tower with no damage, a tower with an AOE(area of effect) aura that gives 10% damage to all towers in range. Tower E might slow enemies in its AOE, a debuff. Stuff like that.

The same could go for enemies. Enemy A is a boss that has a slow aura that affects towers and slows their base attack speed or something along those lines.

So the question is, what would be the most effective way to implement this? If it was just towers then I would just mess around with the tower classes, but since tower classes and enemy classes are both affected, should I make a buff class?

TD games can consume quite a bit of memory with large amounts of creeps and towers, and buffs I feel like would also consume quit a bit... So I'm trying to be as effective as possible.

• You are seriously underestimating the amount of memory you have avaiable. The amount of units in TD's are nothing. Say you have 1000 units on the field at the same time and each unit is 64 byte (which is quite generous) then you have a total of 62 kilobyte! Even when you target 64 MB that's less than 0.1% of the complete memory. – API-Beast Oct 31 '12 at 20:11
• I would also not worry about pre-mature optimization. First write what you want, and if, and only if, you are unable to reach your target memory/CPU constraints, would I look into profiling. Most developers assume whatever they are doing is too hard/too much memory when in reality that isn't the case. Prove yourself wrong instead of worrying about each for-loop and each extra class you add. – kurtzbot Oct 31 '12 at 23:39
• @Mr.Beast You are seriously underestimating the possibility of bad coding. – Gabriel Nov 4 '12 at 17:37

I would represent Buff/Debuff effects with a single class like this:

public class Buff
{
public BuffType BuffType;
public float Value;  //The bonus percentage, for example: -12.5 or 25
}

enum BuffType
{
Speed, FireDamage, ColdDamage, Range //etc.
}


Each and every Tower and Creature instance has a property public List<Buff> CurrentBuffs; . If a buff tower affects a creature / other tower, it adds (or updates) an item in the CurrentBuffs list.

In each update, you calculate which buff towers affect which creatures / towers. This way, you fill the CurrentBuffs list of every such entity. Then you use the list to calculate the accumulated bonuses, which will affect the actual mechanics of the entity.

You might not need to do this in every update: For towers affected by buff towers, you only calculate this when a tower is placed / destroyed / upgraded.

You can calculate the list of Towers than a Tower D affect in the moment you build it, or when other towers are built in its area of effect.

In memory you will have something like:

t1( TowerA ) {...}
t2( TowerB ) {...}
t3( TowerA ) {...}
t4( TowerB ) {...}
t5( TowerD ) {... affects: [t2,t4] ...}


And apply the buff/debuff only when it changes /or at the beggining of the frame update.