lundi 8 mai 2017

How to handle multiple Lists and a player inventory

public interface Character {
    void attack(Weapon toAttackwith);
    void reload(Reloadable reloadObj);

    void useHealthPack();

    void pickup(HealthPack healthPack);
    void pickup(ReloadableWeapon rlWeapon);
    void pickup(CQWeapon closeQweapon);
}

public interface Weapon {
    void attack();
}

public interface Reloadable {
     void replenish (final int amount);
}

List<ReloadableWeapon> reloadWeaponsList = new ArrayList<ReloadableWeapon>();
ReloadableWeapon chargeGun = new ReloadableWeapon("Charge Gun",10);
reloadWeaponsList.add(chargeGun);

public final class ReloadableWeapon implements GameItem, Reloadable, Weapon {}

public final class CQWeapons implements GameItem, Weapon {}

I have a problem where I have multiple weapons, however, not all are reloadable(ex. Swords) which means I have to store them in a specific List<ReloadableWeapon>. If I were to store all of them in a List<Weapon> list, I wouldn't be able to call replenish if necessary.

This means for every type of Weapon type that implements a specific interface I have to create a List for it. For example, close quarters weapons(Swords). I would also have to update my Character interface with specific pickup/use methods for those items.

Is there a better approach? Maybe a specific design pattern? Would the visitor pattern be valid in this case?

Aucun commentaire:

Enregistrer un commentaire