Skip to content

Commit

Permalink
Start reworking menus to process PlayerCommands and work independentl…
Browse files Browse the repository at this point in the history
…y of the GUI.
  • Loading branch information
EvanQuan committed Aug 1, 2018
1 parent 735302e commit d501fbd
Show file tree
Hide file tree
Showing 13 changed files with 255 additions and 201 deletions.
66 changes: 48 additions & 18 deletions src/game/menu/MainMenu.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package game.menu;

import game.system.input.PlayerCommand;
import util.ArrayUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.WeakHashMap;

public class MainMenu extends Menu {
private static MainMenu instance;

/**
* ASCII text: http://patorjk.com/software/taag/ Title Font: Doom Character
* Width: Default Character Height: Default Subtitle Font: Small Character
Expand Down Expand Up @@ -37,8 +40,39 @@ private MainMenu() {
Arrays.asList(startGameVerbs, howToPlayVerbs, aboutThisGameVerbs));
}

@Override
protected void initializeCommands() {
addCommand(new String[] {"1", "1.", "start game", "start", "s"}, () -> startGame());
addCommand(new String[] {"2", "2.", "how to play", "how", "h"}, () -> startHowToPlay());
addCommand(new String[] {"3", "3.", "about this game", "about", "about game", "a"}, () -> startAboutThisGame());

}

/**
* To start the game, go the load menu to choose a game to start.
*/
private void startGame() {
changeTo(LoadMenu.getInstance());
}

/**
* Print information about how to play this game and re-prompt options.
*/
public void startHowToPlay() {
outputHowToPlay();
outputOptions();
}

/**
* Print information about this game and re-prompt options.
*/
public void startAboutThisGame() {
outputAboutThisGame();
outputOptions();
}

public void outputAboutThisGame() {
// printTitleln("Current version: " + CheeseQuest.getVersion());
// printTitleln("Current version: " + Main.getVersion());
// printItem("Cheese Quest 2");
// append(" started development in ");
// printItem("August of 2017");
Expand Down Expand Up @@ -177,7 +211,7 @@ public void outputTitleScreen() {
// + "| | _ __ _ _ |___/ _ |\n"
// + "| | | |/ / __ _ ___| |_ | |__ __ _ __ __ __ _ | | |\n"
// + "| | | ' < / _` |(_-<| ' \\ | / // _` |\\ V // _` || | |\n"
// + "| | |_|\\_\\\\__,_|/__/|_||_||_\\_\\\\__,_| \\_/ \\__,_||_| |\n"
// + "| | |_|\\_s\\\__,_|/__/|_||_||_\\_\\\\__,_| \\_/ \\__,_||_| |\n"
// + "| | /\n"
// + "| |--------------------------------------------------------------------------'\n"
// + "\\ |\n" + " \\ /");
Expand All @@ -195,25 +229,21 @@ public void preProcessInput() {
// } else {
// processInput();
// }

}

/**
* Traverse main menu
*
* @param playerCommand to processInput
*/
public void processInput() {
// if (inputEquals(startGameVerbs)) {
// changeToLoadMenu();
// // changeToTestMenu();
// } else if (inputEquals(howToPlayVerbs)) {
// outputHowToPlay();
// outputOptions();
// } else if (inputEquals(aboutThisGameVerbs)) {
// outputAboutThisGame();
// outputOptions();
// } else {
// outputInvalid();
// outputOptions();
// }
}
@Override
public void processInput(PlayerCommand playerCommand) {
// Only care about matching the total string for simplicity.
String command = playerCommand.getString().toLowerCase();
if (commands.containsKey(command)) {
commands.get(command).run();
} else {

}
}
}
102 changes: 55 additions & 47 deletions src/game/menu/Menu.java
Original file line number Diff line number Diff line change
@@ -1,84 +1,92 @@
package game.menu;

import game.system.input.PlayerCommand;
import game.system.output.IPrintBuffer;

import java.util.ArrayList;
import java.util.HashMap;

/**
* Receives input string Outputs corresponding output string NOTE Unchecked
* ArrayList methods imply types are String, Double, or Integer only
* Receives a {@link PlayerCommand}. Directly outputs to its static {@link IPrintBuffer}.
*/
public abstract class Menu {

public static final String EMPTY = "";
/**
* Output
*/
public static IPrintBuffer out;

/**
* Individual inputs correspond to runnable actions for the menu to do. Each runnable should call a menu method.
*/
protected HashMap<String, Runnable> commands;

protected ArrayList<ArrayList> validVerbs;
private String originalInputString;
private String inputString;
private String[] originalInputWords;
private String[] inputWords;
// private String remainingString;
// private String[] remainingWords;
private String verb;
private MenuManager menuManager; // Cannot have menuManeger, or infinite recursion occurs??

// Input options
protected String[] yes;
protected String[] no;
protected String[] returnToPreviousMenu;

public Menu() {
menuManager = MenuManager.getInstance();
commands = new HashMap<>();
initializeCommands();
// validVerbs = new ArrayList<ArrayList<String>>();
yes = new String[] { "yes", "y", "yeah", "yee", "yup" };
no = new String[] { "no", "n", "nay", "nope" };
returnToPreviousMenu = new String[] { "return", "r", "return to previous menu", "return menu",
"return to menu" };
}

public void changeToAskSaveMenu() {
menuManager.setMenu(AskToSaveMenu.getInstance());
}

public void changeToCreateGameMenu() {
menuManager.setMenu(CreateGameMenu.getInstance());
}

public void changeToGameMenu() {
menuManager.setMenu(GameMenu.getInstance());
}

public void changeToGameOverMenu() {
menuManager.setMenu(GameOverMenu.getInstance());
}

public void changeToLoadMenu() {
menuManager.setMenu(LoadMenu.getInstance());
/**
* Set the {@link IPrintBuffer} for all menus to output to.
*
* @param printBuffer
*/
public static void setOut(IPrintBuffer printBuffer) {
out = printBuffer;
}

// Change menu
/**
* Change from current menu to next menu
* Set the {@link MenuManager}s current menu. This will immediately change the menu after the current menu's
* input is done processing.
*
* @param menu
*/
public void changeToMainMenu() {
menuManager.setMenu(MainMenu.getInstance());
public void changeTo(Menu menu) {
MenuManager.setCurrentMenu(menu);
}

/**
* Change the {@link MenuManager}s current menu to the previous menu.
*/
public void changeToPreviousMenu() {
menuManager.setMenu(menuManager.getPreviousMenu());
MenuManager.setCurrentMenu(MenuManager.getPreviousMenu());
}

public void changeToRestartMenu() {
menuManager.setMenu(RestartMenu.getInstance());
}
/**
* Process a {@link PlayerCommand} as input. This will set some corresponding output to this menu's currently set
* {@link game.system.output.IPrintBuffer}.
*
* @param playerCommand to processInput
*/
public abstract void processInput(PlayerCommand playerCommand);

public void changeToSaveMenu() {
menuManager.setMenu(SaveMenu.getInstance());
}
/**
* Create all valid commands for this menu. Use addCommand().
*/
protected abstract void initializeCommands();

public void changeToTestMenu() {
try {
menuManager.setMenu(TestMenu.getInstance());
} catch (Exception e) {
System.out.println("Uh Oh...");
e.printStackTrace();
/**
* Every command is composed of a list of possible input options that correspond with Menu method for the Menu to
* execute. As a result, be careful not to have commands share options, or an option will be overridden
*
* @param options all strings that corresponding to method. All are converted to lower case.
* @param method with no parameters and Menu method in body
*/
public void addCommand(String[] options, Runnable method) {
for (String option : options) {
commands.put(option.toLowerCase(), method);
}
}

Expand Down
Loading

0 comments on commit d501fbd

Please sign in to comment.