diff --git a/src/main/java/command/Command.java b/src/main/java/command/Command.java index 365334d86f..c5604dbc7e 100644 --- a/src/main/java/command/Command.java +++ b/src/main/java/command/Command.java @@ -11,30 +11,31 @@ public abstract class Command { protected TextBox textBox; protected PlayerStatus playerStatus; protected String commandDescription; - protected BaseMap currentMap; + protected BaseMap currentMapForCommand; public Command() { commandDescription = "Impossible"; - currentMap = null; + currentMapForCommand = null; } public abstract void execute(); public void execute(Scanner in) { - } + public void execute(PlayerStatus playerStatus) { + } public String getCommandDescription() { return commandDescription; } - public BaseMap getCurrentMap() { - return currentMap; + public BaseMap getCurrentMapForCommand() { + return currentMapForCommand; } - public void setCurrentMap(BaseMap givenMap) { - currentMap = givenMap; + public void setCurrentMapForCommand(BaseMap givenMap) { + currentMapForCommand = givenMap; } public void setPlayerStatus(PlayerStatus playerStatus) { diff --git a/src/main/java/command/CommandType.java b/src/main/java/command/CommandType.java index c1472ccfd2..ce4256f64d 100644 --- a/src/main/java/command/CommandType.java +++ b/src/main/java/command/CommandType.java @@ -14,7 +14,8 @@ public enum CommandType { ERROR(""), INVENTORY("(?i)\\h*(i|inventory)\\h*"), USE_ITEM("(?i)\\h*(use)(\\h+\\d+|\\h+\\w+)?\\h*"), - CLOSE_INV("(?i)\\h*(close)\\h*"); + CLOSE_INV("(?i)\\h*(close)\\h*"), + RESET("(?i)\\h*(reset)\\h*"); final String regExpression; CommandType(String regExpression) { diff --git a/src/main/java/command/ResetCommand.java b/src/main/java/command/ResetCommand.java index e65d61f78b..3a951fd31d 100644 --- a/src/main/java/command/ResetCommand.java +++ b/src/main/java/command/ResetCommand.java @@ -1,4 +1,101 @@ package command; -public class ResetCommand { +import inventoryitems.Item; +import map.BaseMap; +import map.FirstMap; +import map.MapGenerator; +import textbox.PlayerStatus; + +import java.util.ArrayList; + +import static main.CalculaChroniclesOfTheAlgorithmicKingdom.PLAYER_INVENTORY; +import static map.BaseMap.mapIndex; +import static map.BaseMap.currentMap; +import static map.BaseMap.storedMaps; +import static map.MapGenerator.FIRST_MAP_IDENTITY; +import static map.MapGenerator.INVENTORY_IDENTITY; + +public class ResetCommand extends Command { + public ResetCommand() { + commandDescription = "RESET!"; + } + + @Override + public void execute() { + + } + + @Override + public void execute(PlayerStatus playerStatus) { + System.out.print("\n\n\n\t\t\t\t\tResetting the game\n\n\n"); + for (int i = 0; i < 201; i++) { + try { + Thread.sleep(10); + } catch (InterruptedException e) { + System.out.println("Thread.sleep function failed!\n" + e.getMessage()); + } + System.out.print("Cleaning all maps |" + "#".repeat(i / 6) + + " ".repeat(33 - i / 6) + + "|" + "%" + i / 2 + "\r"); + } + System.out.println(); + + storedMaps.clear(); + mapIndex.clear(); + for (int i = 0; i < 201; i++) { + try { + Thread.sleep(10); + } catch (InterruptedException e) { + System.out.println("Thread.sleep function failed!\n" + e.getMessage()); + } + System.out.print("Cleaning player inventory |" + "#".repeat(i / 6) + + " ".repeat(33 - i / 6) + + "|" + "%" + i / 2 + "\r"); + } + System.out.println(); + + ArrayList generalItem = PLAYER_INVENTORY.getGeneralItems(); + generalItem.clear(); + + for (int i = 0; i < 201; i++) { + try { + Thread.sleep(10); + } catch (InterruptedException e) { + System.out.println("Thread.sleep function failed!\n" + e.getMessage()); + } + System.out.print("Resetting player status |" + "#".repeat(i / 6) + + " ".repeat(33 - i / 6) + + "|" + "%" + i / 2 + "\r"); + } + System.out.println(); + + playerStatus.setPlayerHealth(100); + playerStatus.setPlayerMoney(0); + playerStatus.setPlayerExp(0); + playerStatus.setPlayerDamage(5); + + for (int i = 0; i < 201; i++) { + try { + Thread.sleep(10); + } catch (InterruptedException e) { + System.out.println("Thread.sleep function failed!\n" + e.getMessage()); + } + System.out.print("Generating new map |" + "#".repeat(i / 6) + + " ".repeat(33 - i / 6) + + "|" + "%" + i / 2 + "\r"); + } + System.out.println(); + + BaseMap map = new FirstMap(); + MapGenerator.getInstance().generateMap(map); + map.setTextBox(textBox); + + storedMaps.add(PLAYER_INVENTORY); + mapIndex.put(INVENTORY_IDENTITY, storedMaps.size() - 1); + PLAYER_INVENTORY.setPlayerStatus(playerStatus); + PLAYER_INVENTORY.setCurrentTextBox(textBox); + storedMaps.add(map); + mapIndex.put(FIRST_MAP_IDENTITY, storedMaps.size() - 1); + currentMap = mapIndex.get(FIRST_MAP_IDENTITY); + } } diff --git a/src/main/java/command/fight/FightingCommand.java b/src/main/java/command/fight/FightingCommand.java index 61b66218c8..5680aeb260 100644 --- a/src/main/java/command/fight/FightingCommand.java +++ b/src/main/java/command/fight/FightingCommand.java @@ -21,21 +21,21 @@ public void execute() { @Override public void execute(Scanner in) { - if (currentMap instanceof map.battleinterface.BattleInterface) { - currentMap.enableFight(in); + if (currentMapForCommand instanceof map.battleinterface.BattleInterface) { + currentMapForCommand.enableFight(in); BaseMap.currentMap = mapIndex.get(FIRST_MAP_IDENTITY); - if (currentMap.getEntityDeath()) { + if (currentMapForCommand.getEntityDeath()) { int xPos = storedMaps.get(BaseMap.currentMap).getInteractX(); int yPos = storedMaps.get(BaseMap.currentMap).getInteractY(); storedMaps.get(BaseMap.currentMap).clearSpot(xPos, yPos); - currentMap.handleLootingByPlayer(); - } else if (currentMap.getPlayerDeath()) { - currentMap.handleDeath(); + currentMapForCommand.handleLootingByPlayer(); + } else if (currentMapForCommand.getPlayerDeath()) { + currentMapForCommand.handleDeath(); } } - if (currentMap instanceof ShopMap) { - currentMap.enableFight(in); + if (currentMapForCommand instanceof ShopMap) { + currentMapForCommand.enableFight(in); BaseMap.currentMap = mapIndex.get(FIRST_MAP_IDENTITY); } } diff --git a/src/main/java/command/fight/RunningCommand.java b/src/main/java/command/fight/RunningCommand.java index d5ddff1181..90d1dafcb6 100644 --- a/src/main/java/command/fight/RunningCommand.java +++ b/src/main/java/command/fight/RunningCommand.java @@ -14,10 +14,10 @@ public RunningCommand() { } @Override public void execute(){ - if(currentMap instanceof BattleInterface) { + if(currentMapForCommand instanceof BattleInterface) { textBox.setNextNarration("You decide to run and successfully got away"); BaseMap.currentMap = mapIndex.get(FIRST_MAP_IDENTITY); - currentMap = storedMaps.get(BaseMap.currentMap); + currentMapForCommand = storedMaps.get(BaseMap.currentMap); } } } diff --git a/src/main/java/command/mapmove/ExitShop.java b/src/main/java/command/mapmove/ExitShop.java index b3cc559380..4e5d2f2586 100644 --- a/src/main/java/command/mapmove/ExitShop.java +++ b/src/main/java/command/mapmove/ExitShop.java @@ -14,10 +14,10 @@ public ExitShop() { } @Override public void execute(){ - if(currentMap instanceof ShopMap) { + if(currentMapForCommand instanceof ShopMap) { textBox.setNextNarration("You exited the shop!!"); BaseMap.currentMap = mapIndex.get(FIRST_MAP_IDENTITY); - currentMap = storedMaps.get(BaseMap.currentMap); + currentMapForCommand = storedMaps.get(BaseMap.currentMap); } } } diff --git a/src/main/java/command/mapmove/InteractingCommand.java b/src/main/java/command/mapmove/InteractingCommand.java index d81d6e6071..53b3b7e22c 100644 --- a/src/main/java/command/mapmove/InteractingCommand.java +++ b/src/main/java/command/mapmove/InteractingCommand.java @@ -31,7 +31,7 @@ public InteractingCommand() { @Override public void execute() { - String entityInteractedWith = currentMap.handleInteract(); + String entityInteractedWith = currentMapForCommand.handleInteract(); if (Objects.equals(entityInteractedWith, "no interaction")) { textBox.setNextNarration("Nothing to interact with here"); } else if (Objects.equals(entityInteractedWith, "@") || @@ -47,8 +47,8 @@ public void execute() { char entity = entityInteractedWith.charAt(0); BaseMap battleMap; InteractableEntity monster; - int xPos = currentMap.getInteractX(); - int yPos = currentMap.getInteractY(); + int xPos = currentMapForCommand.getInteractX(); + int yPos = currentMapForCommand.getInteractY(); switch (entity) { case CENTAUR: diff --git a/src/main/java/command/mapmove/MovingDownwardCommand.java b/src/main/java/command/mapmove/MovingDownwardCommand.java index aa2dc900b2..85359f37c3 100644 --- a/src/main/java/command/mapmove/MovingDownwardCommand.java +++ b/src/main/java/command/mapmove/MovingDownwardCommand.java @@ -8,7 +8,7 @@ public MovingDownwardCommand(String userInput) { @Override public void execute() { for (int i = 0; i < commandModifier; i++) { - currentMap.movePlayerDownOne(); + currentMapForCommand.movePlayerDownOne(); } } } diff --git a/src/main/java/command/mapmove/MovingForwardCommand.java b/src/main/java/command/mapmove/MovingForwardCommand.java index 5930639215..7abd04a6f0 100644 --- a/src/main/java/command/mapmove/MovingForwardCommand.java +++ b/src/main/java/command/mapmove/MovingForwardCommand.java @@ -8,7 +8,7 @@ public MovingForwardCommand(String userInput) { @Override public void execute() { for (int i = 0; i < commandModifier; i++) { - currentMap.movePlayerUpOne(); + currentMapForCommand.movePlayerUpOne(); } } } diff --git a/src/main/java/command/mapmove/MovingLeftCommand.java b/src/main/java/command/mapmove/MovingLeftCommand.java index dcef057be3..0ec1d39c26 100644 --- a/src/main/java/command/mapmove/MovingLeftCommand.java +++ b/src/main/java/command/mapmove/MovingLeftCommand.java @@ -9,7 +9,7 @@ public MovingLeftCommand(String userInput) { @Override public void execute() { for (int i = 0; i < commandModifier; i++) { - currentMap.movePlayerLeftOne(); + currentMapForCommand.movePlayerLeftOne(); } } } diff --git a/src/main/java/command/mapmove/MovingRightCommand.java b/src/main/java/command/mapmove/MovingRightCommand.java index 737f68a462..0f4986dc3d 100644 --- a/src/main/java/command/mapmove/MovingRightCommand.java +++ b/src/main/java/command/mapmove/MovingRightCommand.java @@ -10,7 +10,7 @@ public MovingRightCommand(String userInput) { @Override public void execute() { for (int i = 0; i < commandModifier; i++) { - currentMap.movePlayerRightOne(); + currentMapForCommand.movePlayerRightOne(); } } } diff --git a/src/main/java/main/CalculaChroniclesOfTheAlgorithmicKingdom.java b/src/main/java/main/CalculaChroniclesOfTheAlgorithmicKingdom.java index 7e9d3c49b1..65ddffa44e 100644 --- a/src/main/java/main/CalculaChroniclesOfTheAlgorithmicKingdom.java +++ b/src/main/java/main/CalculaChroniclesOfTheAlgorithmicKingdom.java @@ -29,7 +29,7 @@ public class CalculaChroniclesOfTheAlgorithmicKingdom { public static final int START_HEALTH = 100; - public static final int START_MONEY = 200; + public static final int START_MONEY = 0; public static final int START_EXP = 0; public static final int START_DAMAGE = 5; public static final PlayerInventory PLAYER_INVENTORY = new PlayerInventory(); @@ -78,6 +78,7 @@ public void startGame() { } catch (InterruptedException e) { System.out.println("Timer error !!\n" + e.getMessage()); } + assert map != null; map.setTextBox(textBox); // so that first map can use the text box HintHandler hints = new HintHandler(map, textBox); storedMaps.add(map); @@ -85,6 +86,7 @@ public void startGame() { currentMap = mapIndex.get(FIRST_MAP_IDENTITY); + assert playerStatus != null; ui.printPlayerStatus(playerStatus); ui.printMap(storedMaps.get(currentMap)); ui.printTextBox(textBox); @@ -97,16 +99,16 @@ public void startGame() { userCommand = parser.parseCommand(userCommandText); setUserCommand(userCommand, storedMaps.get(currentMap), playerStatus, textBox); - executeCommand(userCommand, in); + executeCommand(userCommand, in, playerStatus); printMessageUnderMap(userCommand, ui, playerStatus, textBox); saveAllGameFile(mapStorage, playerStatusStorage, playerStatus, userCommand, inventoryItemsStorage); } while (!userCommand.getCommandDescription().equals("TIRED")); } - private void saveAllGameFile(MapStorage mapStorage, PlayerStatusStorage playerStatusStorage, - PlayerStatus playerStatus, Command userCommand, - InventoryItemsStorage inventoryItemsStorage) { + private static void saveAllGameFile(MapStorage mapStorage, PlayerStatusStorage playerStatusStorage, + PlayerStatus playerStatus, Command userCommand, + InventoryItemsStorage inventoryItemsStorage) { try { mapStorage.saveMap(storedMaps.get(mapIndex.get(FIRST_MAP_IDENTITY))); } catch (IOException e) { @@ -143,16 +145,18 @@ private static void printMessageUnderMap(Command userCommand, Ui ui, PlayerStatu } } - private static void executeCommand(Command userCommand, Scanner in) { + private static void executeCommand(Command userCommand, Scanner in, PlayerStatus playerStatus) { if (userCommand.getCommandDescription().equals("FIGHT!")) { userCommand.execute(in); + } else if (userCommand.getCommandDescription().equals("RESET!")) { + userCommand.execute(playerStatus); } else { userCommand.execute(); } } private static void setUserCommand(Command userCommand, BaseMap map, PlayerStatus playerStatus, TextBox textBox) { - userCommand.setCurrentMap(map); + userCommand.setCurrentMapForCommand(map); userCommand.setPlayerStatus(playerStatus); userCommand.setTextBox(textBox); } diff --git a/src/main/java/parser/Parser.java b/src/main/java/parser/Parser.java index c5f5c564a7..0f3c7fc84d 100644 --- a/src/main/java/parser/Parser.java +++ b/src/main/java/parser/Parser.java @@ -1,6 +1,6 @@ package parser; -import command.CommandType; +import command.*; import command.fight.FightingCommand; import command.fight.RunningCommand; import command.inventory.OpenInventoryCommand; @@ -11,10 +11,6 @@ import command.mapmove.MovingForwardCommand; import command.mapmove.MovingLeftCommand; import command.mapmove.MovingRightCommand; -import command.ErrorCommand; -import command.HelpCommand; -import command.QuitCommand; -import command.Command; import command.mapmove.ExitShop; import java.util.regex.Matcher; @@ -102,6 +98,9 @@ public Command parseCommand(String userCommand) { command = (currentMap == mapIndex.get(INVENTORY_IDENTITY)) ? new CloseInventoryCommand() : new ErrorCommand(); break; + case RESET: + command = new ResetCommand(); + break; default: command = new ErrorCommand(); } diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java index c6072202d7..bf7090027c 100644 --- a/src/main/java/ui/Ui.java +++ b/src/main/java/ui/Ui.java @@ -228,6 +228,6 @@ public void insertOutOfBoundsMessage(TextBox box){ } public void insertObjectObstructionMessage(TextBox box){ - box.setNextNarration("Something appears to be blocking your way"); + System.out.println();box.setNextNarration("Something appears to be blocking your way"); } }