Weapon.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package eu.oschl.textadventure.objects;
  2. /**
  3. * Represents a weapon in the game that can be picked up and used to attack enemies.
  4. *
  5. * @author Ondřej Schlaichert
  6. */
  7. public class Weapon extends PickableObject {
  8. public final String attackText;
  9. public final int damage;
  10. public Weapon(String name, String description, String attackText, int damage, String iconImagePath) {
  11. super(name, description, iconImagePath);
  12. this.attackText = attackText;
  13. this.damage = damage;
  14. }
  15. public Weapon(String name, String description, String attackText, int damage) {
  16. super(name, description);
  17. this.attackText = attackText;
  18. this.damage = damage;
  19. }
  20. public String getAttackText() {
  21. return attackText;
  22. }
  23. public int getDamage() {
  24. return damage;
  25. }
  26. /**
  27. * Attempts to pick up the weapon. If the current room is blocked by an enemy or if the player already has a more
  28. * powerful weapon, the weapon cannot be picked up.
  29. *
  30. * @return true if the weapon was successfully picked up, false otherwise
  31. */
  32. @Override
  33. public boolean pickUp() {
  34. if (game.getCurrentRoom().isBlockedByEnemy()) {
  35. return false;
  36. }
  37. if (game.getInventory().getWeapon().isPresent() && game.getInventory().getWeapon().get().getDamage() > damage) {
  38. return false;
  39. }
  40. game.getCurrentRoom().removeObject(this);
  41. game.getInventory().setWeapon(this);
  42. return true;
  43. }
  44. }