I'm working on a text based adventure game in Java, and I'm trying to figure out a way to check what weapon the player has and set their damage output appropriately.
Here is what I have set up so far:
Item object:
Item (String name, String type, String description, String location)
("Assault Rifle", "rifle", "A rifle that goes pew pew.", "armory")
Player inventory:
HashMap<String, Item> inventory = new HashMap<String, Item>()
Right now, I can use an if statement to grab the name of the object, and set the damage modifiers for it:
if (player.getInventory().containsKey("Assault Rifle")) {
player.setMaxAttackDmg(55);
player.setWeaponModifier(2);
}
What I want to be able to do is match on the item type, and not the name. I'm assuming I need a loop of some kind to iterate through the player's inventory, but I don't know how I match the HashMap value since it is an object of type Item. So it would have something like:
for (item in inventory) {
if (item.getType().equals("rifle")) {
player.setMaxAttackDmg(55);
player.setWeaponModifier(2);
}
Is there way to do that, or a better way for me to do this?
WeaponandItemclass and then have stuff likeattackDmgas property of such classes. Then the player can just have aList<Item>and aWeaponand get his attack power from there with simple getter methods.