4
\$\begingroup\$

I am currently working on my first java game and I am having trouble figuring out how I should implement my classes that will have heavy variety. For example, I am considering having an abstract item class that every item in the game will inherit from, but I don't really know how specific I should get with the sub classes. Is an armor class enough? Should I break it down into where the armor will be equipped(head, chest, etc)? Should I go so far as to give every single item it's own class and use a factory?

I apologize in advance for the noobie question, I have been searching around for a couple of hours but haven't been able to find a solid best practices guide that really goes in depth with this kind of stuff.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ gamedev.stackexchange.com/questions/80376/… This is a very similar question. The answer lies mostly in the question "Do the two structures differ in functionality, or simple content?". For example, two items that differ in image and stats would make more sense as two entities of a single class. Two items that differed greatly in many ways, such as functionality, might deserve different classes. \$\endgroup\$
    – Attackfarm
    Commented Apr 19, 2015 at 6:10

2 Answers 2

1
\$\begingroup\$

Well honestly, in a personal project, whatever works for your needs is good enough. In a professional game, if you were working with a team of other programmers and designers you'd want it to be data driven so designers could tweak numbers and such. There's no clear cut answer though, it really is kind of a personal preference of the people involved, and to some extent doesn't matter so long as it fits the needs of your project. You could always start out with a best guess and change course if needed along the way, but chances are this isn't going to be a major issue in your game whatever you choose (:

\$\endgroup\$
0
\$\begingroup\$

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 :)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .