I'm making a text adventure game, but I've run into trouble when it comes to structuring my objects.
In my game, every item (objects, weapons) implements the GameObject interface.
public interface GameObject {
void viewDetails();
}
My ultimate goal is to store them in an ArrayList. If the player wanted to view them i can just loop through the list and pass it to a method that displays the data. Like so:
List<HealthPack> healthList = new ArrayList<HealthPack>();
healthList.add(new HealthPack(3));
healthList.add(new HealthPack(5));
display(healthList);
public static void display(List<GameObject> objDetails){
//loop though the list and display.
}
Now, if I wanted methods that are more interactive I have to get a little more detail on the specific type. For example, not every game object is a health pack, therefore the GameObject interface should not contain a method called void replenishhealth(), however I can write a class that does this.
public abstract class HealthPack implements GameObject {
//the sub-class could be a ration, potion, or pills
public abstract void replenishHealth(){}
}
My problem is, I would have to store it in a List collection specifically for that type, and I would have to do this for all types of GameObjects. For example Weapons:
List<HealthPack> healthList = new ArrayList<HealthPack>();
List <Weapons> weaponsList = new ArrayList<Weapons>();
Questions:
1. Maybe I'm looking at it all wrong, but is there a more efficient way to restructure my code to not use so many ArrayLists? I don't mind if this is what I have to do, but there probably is a better way of creating, storing and using them.
Note: Ideally, I would like to only keep two arrayLists, one for GameObjects and the other for Weapons. The Weapon class is an abstract class that initializes the name & damage a weapon could do. The Weapon class also contains the abstract methods attack/replenish for the subclass weapons to implement.
2. Suppose I had a character interface that looked like this:
interface Character{
void pickUp(Weapon weapon);
void pickUp(HealthPack healthItem);
void use(Weapon weapon);
void use(HealthPack healthItem);
}
Everytime I have a new GameObject the player can interact with, I would have to update my Character interface with pickup/use methods. The problem with this is, a GameObject sometimes can be used in different ways. For example, if I had the GameObject Paper:
public class Paper implements GameObject {
public void writeText(String text);
public void foldPaper();
@Override
public void viewDetails();
}
I would then have to update the Character interface to accommodate all the different uses of the Paper object. Is there a better way of doing this? so that I don't have to keep updating my character interface every time I have a new GameObject?