2
\$\begingroup\$

I have a number of stats and effects in game that are subject to change, they have some kind of base value and the current value. For stats, I implemented this in a dynamic way, but for less important things, like buffs that is too much work. For buffs I have 2 properties - current and base value. For example, the shield buff action takes the base value and copies it into the current value at the start of each turn.

What is a good way to implement having the current value and base value for effects?

Right now I have a base property defined along with the modified value.

+(BuffBase*)buffWithEffectType:(kEffectType)effectType intensity:(int)intensity
{
    BuffBase* buff = [[BuffBase alloc] init];
    buff.intensity = intensity;
    buff.baseIntensity = intensity;

    return buff;
}

Should I refactor such code early on and replace it with some kind of tuple, like EffectValue(current,Base) where x is current and y is base?

+(BuffBase*)buffWithEffectType:(kEffectType)effectType intensity:(int)intensity
{
    BuffBase* buff = [[BuffBase alloc] init];
    buff.intensityTuple = EffectValue(intensity, intensity);
    return buff;
}

Or should I start with a dictionary/map right from the start? This seems to be the easiest to expand later on, although I have to remember string keys:

+(BuffBase*)buffWithEffectType:(kEffectType)effectType intensity:(int)intensity
{
    BuffBase* buff = [[BuffBase alloc] init];
    NSMutableDictionary* dictionary = [[NSMutableDictionary alloc] init];
    [dictionary setObject:@(intensity) forKey:@"base"];
    [dictionary setObject:@(intensity) forKey:@"current"];

    //optional parameters for potential future expansion
    [dictionary setObject:@(1) forKey:@"decay"];
    [dictionary setObject:@(4) forKey:@"maxHitsBlocked"];
    buff.valueDictionary = dictionary;

    return buff;
}
\$\endgroup\$
  • \$\begingroup\$ This appears to fundamentally be a special-case of a flexible buff/debuff system, which is a topic already covered here. If for some reason you feel that isn't the case, please edit your question to be more specific. \$\endgroup\$ – Josh Jan 4 '14 at 17:27