# Creating Gun objects with upgrades? [duplicate]

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.

## marked as duplicate by Sean Middleditch, msell, Josh♦, MichaelHouse♦, AnkoMay 31 '13 at 22:48

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.

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 calculateGunStats()
{
for (int i = 0; i < gunStatModifiers.Count; i++)
{
switch (gunStatModifiers[i].getStat())
{
case Damage:
// add stat to base damage

//etc etc
}
}
}
}