TakeItem.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package eu.oschl.gui.actions;
  2. import eu.oschl.gui.Output;
  3. import eu.oschl.textadventure.Game;
  4. import eu.oschl.textadventure.objects.PickableObject;
  5. import eu.oschl.textadventure.objects.Weapon;
  6. import javafx.scene.paint.Color;
  7. /**
  8. * Action for taking an item from the current room.
  9. *
  10. * @author Ondřej Schlaichert
  11. */
  12. public class TakeItem implements Action {
  13. private final Game game;
  14. public TakeItem(Game game) {
  15. this.game = game;
  16. }
  17. @Override
  18. public String getId() {
  19. return "takeitem";
  20. }
  21. @Override
  22. public String getName() {
  23. return "Take";
  24. }
  25. @Override
  26. public String getDescription() {
  27. return "take an item";
  28. }
  29. @Override
  30. public void execute(String[] args) {
  31. if (game.getCurrentRoom().isBlockedByEnemy()) {
  32. Output.print("The way is blocked. It's impossible to pick up items.", Color.RED);
  33. return;
  34. }
  35. for (var object : this.game.getCurrentRoom().getObjects()) {
  36. if (object.getName().equalsIgnoreCase(String.join(" ", args))) {
  37. if (object instanceof PickableObject item) {
  38. var result = item.pickUp();
  39. if (item instanceof Weapon) {
  40. if (result) {
  41. Output.print("Weapon ", Color.MAGENTA);
  42. Output.print(item.getName(), Color.MAGENTA);
  43. Output.print(" obtained.", Color.MAGENTA);
  44. } else {
  45. Output.print("This weapon is weaker than the current one.", Color.RED);
  46. }
  47. } else {
  48. if (result) {
  49. Output.print("Item ", Color.MAGENTA);
  50. Output.print(item.getName(), Color.MAGENTA);
  51. Output.print(" added to inventory.", Color.MAGENTA);
  52. } else {
  53. Output.print("Carrying too many items.", Color.MAGENTA);
  54. }
  55. }
  56. } else {
  57. Output.print("Can't pick up ", Color.RED);
  58. Output.print(object.getName(), Color.MAGENTA);
  59. Output.print(".", Color.RED);
  60. }
  61. return;
  62. }
  63. }
  64. Output.print("That item is not here.", Color.RED);
  65. }
  66. }