I'm making an adventure game with java and slick2d because of my school's object oriented software engineering project, unfortunately.
I'm applying MVC architecture and I'm creating Items by reading from a Tiled map file in an ItemFactory class. They seem to be suitable for this kind of game.
There are multiple items in the game which affects the game and especially character. (character is the same as player in this context)
There is a character model and it has an inner class Inventory which basically consist of an ArrayList of items.
Character controllers have a function that is called takeItem that takes a container argument (i.e. shelf) which has also an ArrayList of items, then removes what inside and puts to the inventory.
And it has a function which is called useItem, takes an int index and uses the item in the appropriate index.
Until that, everything is fine.
Now the main problem is, these items have specific uses. For example, health potion increases character's health, invisibility potion makes it invisible etc.
To be sustain a good design, how and where can I interpret and attach functions to these items?
So far I think something like this: 1- I did not add any function to item class. It only has name, description and Enumerated ItemType and int power 2- I'm doing every work in the character controller's useItem() method, i.e.
if (itemBeingUsed.getType() == Common.ItemType.POTION)
`characterModel.increaseHealth(itemBeingUsed.getPower())`
As you can see, this seems odd because character controller acts like an item interpreter.
3- Another approach may be creating an ItemInterpreter class that takes a character controller. Although this seems more elegant, it will make me do bunch of needless functions in character controller like "increaseCharacterHealth" function that calls character model's increaseHelth() function an so on.
Another possible problem is that I'm using Character Controller's functions to use items, but the items may also affect the game, i.e. slowing time, but Character Controller is below the class hierarchy of game and does not access to Game/GameController functions. Slowing game time etc. seems not the case at least right now, so it looks like I can use Items in CharacterController class but I would like to know your opinion if it is a good design or what could be better.
Some codes to clear issues: (note: they are not complete) Note: I put enumarated types like ItemType, Direction etc. in a Common class which si common to all classes.
Use item function in CharacterController:
public void useItem(int index){
Item item = ((Character)obj).inventory.takeItem(index);
if (item != null) {
if (item.getType() == Common.ItemType.POTION) {
((Character)obj).increaseHealth(item.getPower()); // I have extended a Person class, now I have to typecast to character everytime, which is also nonsense
}
else
System.out.println("and other items..");
}
else
System.out.println("Cannot use");
}
I did not put all the code, but I can if I was not clear in explaining.