1
\$\begingroup\$

I'm making a 2d rpg with libgdx and java. I have an Inventory class with an Array of Items, and now I want to be able to equip items onto the player. Would it be more managable to do

  1. have every item have an "equipped" boolean flag
  2. have an "equipped" array in the player class
  3. have individual equipped fields in player class, e.g.

    private Item equippedWeapon;
    private Item equippedArmor;
    public void equipWeapon(Item weapon){ equippedWeapon = weapon; }

Or just another way completely? Thanks.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ "Would it be better design/more efficient to do" - that is highly subjective and can depend on the specifics of your game and needs; it's not something that can be answered "correctly" with any real authority. Just write whatever seems easiest and does what you need. "Most efficient" is especially non-useful as you're not exactly writing Frostbite/Crytek/Unreal and you're hardly going to be stressing the hardware with inventory management. \$\endgroup\$ Commented Nov 2, 2013 at 20:49
  • \$\begingroup\$ changed to "more managable"-I just want to make sure I have a solid system I can build the rest of the rpg around, that makes sense. \$\endgroup\$
    – Jimmt
    Commented Nov 2, 2013 at 21:20
  • \$\begingroup\$ @Jimmt Do you have any feedback for my answer? \$\endgroup\$ Commented Nov 2, 2013 at 21:26
  • \$\begingroup\$ It highly depends on what equipment actually does in your game system. Do they all just affect a single aspect of gameplay? Or are there gameplay aspects which are affected by each piece of equipment? \$\endgroup\$
    – Philipp
    Commented Nov 2, 2013 at 23:40

2 Answers 2

3
\$\begingroup\$

There is no correct answer. However, I believe design wise there are better ways to do this. Personally, I would use the 3rd method you described. It would allow you to easily switch out each kind of item. And, whenever calculating damage or some similar action you could have code such as:

if(equippedWeapon != null)
    Damage = BaseDamage + equippedWeapon.DamageModifier
else 
    Damage = BaseDamage

I'm assuming there is a better way to do what I just wrote but you understand what I mean. Just comment if you have any more questions or want some form of clarification.

\$\endgroup\$
1
  • \$\begingroup\$ I see what you mean, I will try this when I have time. Thanks! \$\endgroup\$
    – Jimmt
    Commented Nov 2, 2013 at 21:28
1
\$\begingroup\$

The best solution will obviously be whatever works for your system, but this is how I've done it in the past:

public class Equipment
{
    WornItem[] wornEquipment = new WornItem[EquipmentSlot.values().length];
    public void equip(WornItem wornItem)
    {
        wornEquipment[wornItem.equipmentSlot.ordinal()] = wornItem;
    }

    public WornItem get(EquipmentSlot equipmentSlot)
    {
        return wornEquipment[equipmentSlot.ordinal()];
    }
}
public class WornItem extends Item
{
    EquipmentSlot equipmentSlot;
    //Item's properties here
    //id
    //name
    //etc.
}

public enum EquipmentSlot 
{
    Weapon, Head, Torso, Legs
}

So if you want to get your weapon, you do:

Weapon weapon = (Weapon) equipment.get(EquipmentSlot.Weapon)

my inventory class is a list of Items

List<Item> items

and Weapon extends WornItem which extends Item

\$\endgroup\$
1
  • \$\begingroup\$ You should just use a Dictionary. \$\endgroup\$
    – Krythic
    Commented Apr 12, 2017 at 1:51

You must log in to answer this question.

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