vendredi 24 février 2017

Storing different kind of items in an inventory (Role Playing Game)

I'm making a small RPG in Java which obviously support players. A Player has a Inventory in which it can keep all kind of items. For now I want to store Equipment and Potions in the Inventory.

With a single Equipment class, all player gear can be made (helmet, torso, legs, boots, gloves). The Equipment class has an attribute called type. The attribute type says what kind of gear it represents (e.g. helmet, torso).

For a potion on the other hand, there are multiple subclasses. Each potion has it's own behavior.

Both equipment and potions, can be used by the player (potion by drinking it, equipment by wearing it). So I want to share them under an interface Usable, by doing so, equipment and potions can also be stored in a single datastructure in the Inventory class.

I want to use a factory pattern to produce the usables. However, there is a catch, because as stated, different kinds of Equipment can be made with the type attribute. So in the createUsable(type:Enum), type will be either Equipment or Potion, but for Equipment I also need to know which kind of equipment (helmet, torso, legs). What is a neat way to do this?

enter image description here (Current design in progress, for simplicity it is just a part of the total design)

Aucun commentaire:

Enregistrer un commentaire