Skip to content

Commit

Permalink
Merge pull request #107 from BestDownLoader365/Add_reset_command
Browse files Browse the repository at this point in the history
Add reset command
  • Loading branch information
BestDownLoader365 committed Apr 12, 2024
2 parents fc409ad + f072371 commit 1c04025
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 41 deletions.
15 changes: 8 additions & 7 deletions src/main/java/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/command/CommandType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
99 changes: 98 additions & 1 deletion src/main/java/command/ResetCommand.java
Original file line number Diff line number Diff line change
@@ -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<Item> 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);
}
}
16 changes: 8 additions & 8 deletions src/main/java/command/fight/FightingCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/command/fight/RunningCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/command/mapmove/ExitShop.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
6 changes: 3 additions & 3 deletions src/main/java/command/mapmove/InteractingCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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, "@") ||
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/command/mapmove/MovingDownwardCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public MovingDownwardCommand(String userInput) {
@Override
public void execute() {
for (int i = 0; i < commandModifier; i++) {
currentMap.movePlayerDownOne();
currentMapForCommand.movePlayerDownOne();
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/command/mapmove/MovingForwardCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public MovingForwardCommand(String userInput) {
@Override
public void execute() {
for (int i = 0; i < commandModifier; i++) {
currentMap.movePlayerUpOne();
currentMapForCommand.movePlayerUpOne();
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/command/mapmove/MovingLeftCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public MovingLeftCommand(String userInput) {
@Override
public void execute() {
for (int i = 0; i < commandModifier; i++) {
currentMap.movePlayerLeftOne();
currentMapForCommand.movePlayerLeftOne();
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/command/mapmove/MovingRightCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public MovingRightCommand(String userInput) {
@Override
public void execute() {
for (int i = 0; i < commandModifier; i++) {
currentMap.movePlayerRightOne();
currentMapForCommand.movePlayerRightOne();
}
}
}
18 changes: 11 additions & 7 deletions src/main/java/main/CalculaChroniclesOfTheAlgorithmicKingdom.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -78,13 +78,15 @@ 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);
mapIndex.put(FIRST_MAP_IDENTITY, storedMaps.size() - 1);
currentMap = mapIndex.get(FIRST_MAP_IDENTITY);


assert playerStatus != null;
ui.printPlayerStatus(playerStatus);
ui.printMap(storedMaps.get(currentMap));
ui.printTextBox(textBox);
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/parser/Parser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package parser;

import command.CommandType;
import command.*;
import command.fight.FightingCommand;
import command.fight.RunningCommand;
import command.inventory.OpenInventoryCommand;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

0 comments on commit 1c04025

Please sign in to comment.