The way I am doing it with my game is as follows:
I have a very generic abstract Item class that is the basis for all types of items. This is what it looks like:
public abstract class Item {
public enum Type {
weapon;
}
public abstract Type getItemType();
public abstract String getName();
public abstract String getDescription();
@Override
public String toString() {
return getName() + ": " + getDescription();
}
}
Note that I only have weapons now because I am still building my game, but the way I am doing it should make it very easy to add new types of items.
I then have a more specific class depending the type of item (of course I only have weapons now):
public class Weapon extends Item {
private Weapons type;
private int uses;
public Weapon(Weapons type) {
this.type = type;
}
public int getUses() {
return uses;
}
public int getMaxUses() {
return type.uses;
}
public boolean use() {
if(getMaxUses() < 0)
return false;
uses--;
return uses <= 0;
}
public int getBaseExp() {
return type.baseExp;
}
public int getDamage() {
return type.damage;
}
@Override
public Type getItemType() {
return Type.weapon;
}
@Override
public String getName() {
return Stats.ucFirst(type.toString()); //this isn't important, only makes the first letter upper case
}
@Override
public String getDescription() {
return type.description;
}
}
Now you may be confused as to what Weapons type
is. This is how I am enabling my game to have easily added items, classes, etc. This is what Weapons.java
looks like (it only contains one item for now):
public enum Weapons {
claws(-1, 9, 40); //-1 uses, i.e. infinite
int uses, damage, baseExp;
String description;
Weapons(int uses, int damage, int baseExp) {
this.uses = uses;
this.damage = damage;
this.baseExp = baseExp;
this.description = ReverseBlade.ioHandler.getDescription(this);
}
}
Because it is an enum I do not need to create new classes for each individual weapon, instead I just add 1 easy line to this code. And I can easily give a character any type of weapon with new Weapon(Weapons.claw)
for example.
Using a system like this you can easily keep all of your items in order while still being able to create new ones. Of course, if you want a certain weapon to be very special with completely unorthodox effects you will have to create a new class (just have it extend Weapon for example). Although you should be able to add an infinite amount of parameters and just make defaults for each one if certain items won't use them. Just experiment with it :)