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.

I'm making a small text-based adventure in Objective-C. I have a Player class with four properties which represent the player's attributes (strength, agility, stamina, and intelligence). I also have a Weapon class. My goal is to put a weapon in the game that will increase the player's stats as long as it is equipped. How can I achieve this in an object-oriented fashion?

I could add a method to the Weapon class that will increase the player's attributes, and call the method only when a flag is set to true (which I would set to true when initiating this particular weapon, and leave false for all other weapons I create), but that seems a bit messy and not particularly object-oriented. I recently learned about the decorator design pattern. Would I be able to accomplish my goal using decorators?

share|improve this question
2  
I wouldn't alter the stats of the character directly. You would create a method for calculating the stats, and it takes into account currently equipped items, spell effects, etc. The calculation method would probably live in the character class and the items and effects applied to the character would be "child" objects that the character class knows about. –  Byte56 Jul 19 '13 at 23:23
4  
1  
I think Sean is right, this is probably a duplicate of the linked (or its duplicate). –  Byte56 Jul 20 '13 at 3:35
    
This is something that made me appreciate C#'s properties a lot more. –  Garan Sep 18 '13 at 8:06
add comment

2 Answers

Using decorators is not a good idea in this case, because you also want to un-equip items. When you wrap decorators in decorators in decorators and then want to remove a specific one inside that stack, it's a quite messy operation (especially because an object shouldn't be aware that an object it uses is just a decorator wrapping something else).

What you could do instead is have an abstract base class StatModifier with a modifier function for each stat a player can have. These modifiers receive a value (base value) and return a value of the same type (modified value). The default implementation in the base class would just return the value unchanged.

The Player class would then have a vector of StatModifiers which represents the currently active modifiers (equipment, buffs etc.). When, for example, the method Player.getStrength() is called, the implementation would call the modifyStrength method of each StatModifier to "filter" the result value.

I don't know objective-c, so this example should be considered pseudocode:

List<StatModifier> modifiers;

int getStrength() {
    int strength = this.baseStrength;
    foreach (modifier in this.modifiers) {
        strength = modifier.modifyStrength(val);
    }
    return strength;
} 
share|improve this answer
add comment

I would make a base class for weapons/armor in which all weapons/armor inherit and have the base class include the stats so you don't need to access a specific weapon/armor. Then when you need the stats just add up the weapon/armor stats and the players.

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.