Enter.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package eu.oschl.gui.actions;
  2. import eu.oschl.gui.Output;
  3. import eu.oschl.textadventure.Game;
  4. import eu.oschl.textadventure.exceptions.InvalidGameState;
  5. import eu.oschl.textadventure.map.Passage;
  6. import javafx.scene.paint.Color;
  7. public class Enter implements Action {
  8. private final Game game;
  9. public Enter(Game game) {
  10. this.game = game;
  11. }
  12. @Override
  13. public String getId() {
  14. return "enter";
  15. }
  16. @Override
  17. public String getName() {
  18. return "Enter";
  19. }
  20. @Override
  21. public String getDescription() {
  22. return "walk through a passage";
  23. }
  24. @Override
  25. public void execute(String[] args) {
  26. String input = String.join(" ", args);
  27. var passages = this.game.getCurrentRoom().getPassages();
  28. Passage passage = null;
  29. for (Passage p : passages) {
  30. if (p.getName().equalsIgnoreCase(input)) {
  31. passage = p;
  32. break;
  33. }
  34. }
  35. var printEnterText = !passage.getOtherRoom(game.getCurrentRoom()).wasEntered();
  36. var result = passage.passThrough(false);
  37. if (!result) {
  38. if (game.getCurrentRoom().getEnemy().isPresent()) {
  39. Output.print(
  40. game.getCurrentRoom().getEnemy().get().getName() + " blocks the way. It is only possible to go back.",
  41. Color.RED
  42. );
  43. } else if (passage.getBlockage().isPresent()) {
  44. Output.print(passage.getBlockage().get().getDescription(), Color.RED);
  45. } else {
  46. Output.print("The passage is blocked.", Color.RED);
  47. }
  48. return;
  49. }
  50. printSuccessfulPassage(printEnterText);
  51. }
  52. /**
  53. * Prints a message indicating that the player has successfully passed through a passage.
  54. *
  55. * @param printEnterText whether to print the enter text of the current room
  56. */
  57. private void printSuccessfulPassage(boolean printEnterText) {
  58. if (game.getLastPassage().isEmpty()) {
  59. throw new InvalidGameState("Last passages stack is empty even though a passage was passed");
  60. }
  61. Output.print("Passed through the ");
  62. Output.print(game.getLastPassage().get().getName(), Color.YELLOW);
  63. Output.print(" and entered ");
  64. Output.print(game.getCurrentRoom().getName(), Color.LIGHTBLUE);
  65. Output.print(".");
  66. if (game.getCurrentRoom().isBlockedByEnemy()) {
  67. Output.printLine();
  68. Output.print("...", Color.WHITE);
  69. Output.printLine();
  70. Output.print("There is somebody in here.", Color.WHITE);
  71. }
  72. if (printEnterText && game.getCurrentRoom().getEnterText().isPresent()) {
  73. Output.printLine();
  74. Output.printLine();
  75. Output.print(game.getCurrentRoom().getEnterText().get());
  76. Output.printLine();
  77. }
  78. }
  79. }