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.

This question already has an answer here:

I have a series of guns in my game. I use the Gun class/object like this:

(Just an example)

@interface Gun : NSObject
{
    NSString *name; // Six-shooter
    NSNumber *cost;

    NSNumber *clipPrice; // ie: 700
    NSNumber *clipCapacity; // 6
    NSNumber *ammoCapacity; // 6


    NSNumber *damage;   // 0-10

    NSNumber *accuracy; // 0-10
    NSNumber *fireRate; // 0-10
    NSNumber *range;    // 0-10
    // Not sure if I have all the stats, but this is fine for now        
}

Lets say I want to have 3 upgrades per gun. My problem is I am not sure how to do this.

Examples:

  • increase fire-rate
  • increase range
  • increase accuracy
  • silencer
  • double ammo capacity (ie: Drum)
  • double clip capacity (ie: Taped magazine)

Thus my question is, I'd like to implement an upgrade system to guns but I am not sure how to do it.

Would there be an Upgrade object which is a child to the Gun class, or would it be seperate class altogether.

Thanks for your time.

share|improve this question

marked as duplicate by Sean Middleditch, msell, Josh Petrie, Byte56, Anko May 31 '13 at 22:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers 2

up vote 5 down vote accepted

Not knowing much about your particular game, telling you what numbers to increase with an upgrade (or by how much) is a bit useless. You'll have to decide on that yourself, I'm afraid.

As to how to handle upgrades, The simplest way I can think of to do this is to turn every NSNumber into an array, and add a new attribute "UpgradeLevel". Then, whenever you need to get an attribute from your Gun, you go to, for instance:

id damageValue = [damage objectAtIndex:UpgradeLevel];

To handle things that simply add new features, you can make an array of bools that will then return true or false depending on the upgrade.

share|improve this answer
    
This seems reasonable and very simple. I have accepted your answer. Thanks –  zardon Jul 9 '12 at 5:48
    
+1 simple and effective –  ashes999 May 29 '13 at 3:26

Personally I would use a StatModifier class and stick percentage (or just straight values) modifiers onto weapons, then you would multiply or add these modifiers to your weapons base stats.

You would need some functions to perform the calculations to get the "final stat value"

For example

public class StatModifier
{
    private int modValue    // value or percentage
    enum StatType { AmmoCapacity, Damage, FireRate, Range, Accuracy }

    StatType statToAffect;

    public StatModifer(int value, StatType stat)
    {
        modValue = value;
        statToEffect = stat;
    }

    public int getModValue()
    {
        return modValue;
    }

    public int setModValue(int mValue)
    {
        modValue = mValue;
    }

    public StatType getStat()
    {
        return statToEffect;
    }
}

then in gun class

@interface Gun : NSObject
{   
    private List<StatModifier> gunStatModifiers;

    public void AddGunModifier(int value)
    {
        gunStatModifiers.Add(new StatModifier(value));
    }

    public void calculateGunStats()
    {
        for (int i = 0; i < gunStatModifiers.Count; i++)
        {
            switch (gunStatModifiers[i].getStat())
            {
                case Damage:
                    // add stat to base damage

                //etc etc
            }
        }
    }
}
share|improve this answer

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