Skip to content

Commit

Permalink
Output text to single object for easy maintenance
Browse files Browse the repository at this point in the history
  • Loading branch information
DLzer committed Dec 21, 2021
1 parent bfe86d2 commit 3ccd1a2
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions assets/js/Domain/GameEngine/GameEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ GameEngine = {
* Start the game engine
*/
init: () => {
console.log('**GameEngine: Initializing...')
// GameEngine.startCommandListener()
GameEngine.initializeCLI();
GameEngine.cli.startCommandListener();
console.log('**GameEngine: Determining player state');
GameEngine.initalizePlayer();
console.log('**GameEngine: Last game state:', GameEngine.player);
GameEngine.lookAction();
},

Expand All @@ -31,33 +27,31 @@ GameEngine = {
GameEngine.player = new Player();
GameEngine.player = GameEngine.player.loadPlayerState();

if ( GameEngine.player.gameIsSaved ) { GameEngine.cli.output("<strong>Game loaded from a previous save.</strong>"); }
if ( GameEngine.player.gameIsSaved ) { GameEngine.cli.output(GameEngine.outputList.saveLoaded); }
},

/**
* Save a current gameState object
*/
saveGame: () => {
GameEngine.player.savePlayerState();
GameEngine.cli.output("<strong>Your game state has been saved.</strong>");
console.log("**GameEngine: Game state saved");
GameEngine.cli.output(GameEngine.outputList.gameSaved);
},

/**
* Reset a gameState object
*/
resetGame: () => {
GameEngine.player.resetPlayerState();
GameEngine.cli.output("<strong>Your game state has been reset.</strong>");
console.log("**GameEngine: Game state reset");
GameEngine.cli.output(GameEngine.outputList.gameReset);
},

/********* CORE COMMANDS *********/

// Outputs a help dialog to the player
printHelp: () => {
GameEngine.cli.output("Here is a list of acceptable commands:");
var acceptedCommands = ['> go [direction]', '> north', '> east', '> south', '> west', '> up', '> down', '> look', '> open', '> enter', '> exit','> climb', '> brief [ short descriptions ]', '> verbose [ long descriptions ]', '> help', '> take', '> bag', '> save [ Save current game]', '> reset [ Reset game including save ]'];
GameEngine.cli.output(GameEngine.outputList.acceptableCommands);
var acceptedCommands = GameEngine.outputList.acceptableCommandList;
for(i = 0; i < acceptedCommands.length; i++) {
GameEngine.cli.output(acceptedCommands[i]);
}
Expand All @@ -67,9 +61,9 @@ GameEngine = {
let inventory = GameEngine.player.getPlayerInventory();

if(inventory === undefined || inventory.length == 0) {
GameEngine.cli.output("There is nothing in your bag!");
GameEngine.cli.output(GameEngine.outputList.emptyBag);
} else {
GameEngine.cli.output("Your bag contains:");
GameEngine.cli.output(GameEngine.outputList.bagContains);
for(j=0;j<inventory.length;j++) {
GameEngine.cli.output(GameEngine.player.inventory[j]);
}
Expand All @@ -78,12 +72,12 @@ GameEngine = {
// Sets the output of items and rooms to verbose mode
setVerboseOutput: () => {
GameEngine.player.setVerboseMode(true);
GameEngine.cli.output("ZORK is now in its \"verbose\" mode, which always gives long descriptions of locations (even if you've been there before).");
GameEngine.cli.output(GameEngine.outputList.verboseMode);
},
// Sets the output of items and rooms to brief mode
setBriefOutput: () => {
GameEngine.player.setVerboseMode(false);
GameEngine.cli.output("ZORK is now in its normal \"brief\" printing mode, which gives long descriptions of places never before visited, and short descriptions otherwise.");
GameEngine.cli.output(GameEngine.outputList.briefMode);
},

getCurrentRoom: () => {
Expand Down Expand Up @@ -161,19 +155,17 @@ GameEngine = {
let lDirection = direction.toLowerCase();

if ( lDirection == "back" ) {
console.log("**GameEngine: Moving "+lDirection);
GameEngine.player.setCurrentLocation(GameEngine.player.getPreviousLocation());
GameEngine.player.setPreviousLocation(roomList[currentRoom].varName);
currentRoom = GameEngine.getCurrentRoom();
} else {

if (roomList[currentRoom][lDirection] === undefined)
{
GameEngine.cli.output("You can't go that way.");
GameEngine.cli.output(GameEngine.outputList.invalidDirection);
return;
}

console.log("**GameEngine: Moving "+lDirection);
GameEngine.player.setPreviousLocation(roomList[currentRoom].varName);
GameEngine.player.setCurrentLocation(roomList[currentRoom][lDirection].varName);
currentRoom = GameEngine.getCurrentRoom();
Expand Down Expand Up @@ -207,7 +199,7 @@ GameEngine = {

if (roomList[currentRoom]["open"] === undefined || !roomList[currentRoom]["open"])
{
GameEngine.cli.output("You can't open that.");
GameEngine.cli.output(GameEngine.outputList.notOpenable);
}
else
{
Expand Down Expand Up @@ -268,7 +260,7 @@ GameEngine = {

if (!itemObject.actionArray.includes("read"))
{
GameEngine.cli.output("This is not a readable item.");
GameEngine.cli.output(GameEngine.outputList.notReadable);
return;
}

Expand Down Expand Up @@ -297,7 +289,7 @@ GameEngine = {
useAction: (item) => {

if ( !item ) {
GameEngine.cli.output("Use what?");
GameEngine.cli.output(GameEngine.outputList.notUseable);
}

let lItem = item.toLowerCase();
Expand All @@ -307,7 +299,7 @@ GameEngine = {
}

if (itemObjects[lItem].inUse) {
GameEngine.cli.output("The item is already in use. Putting item away.");
GameEngine.cli.output(GameEngine.outputList.alreadyInUse);
itemObjects[lItem].inUse = false;
GameEngine.lookAction();
} else {
Expand All @@ -326,6 +318,23 @@ GameEngine = {

},

outputList: {
saveLoaded: "<strong>Game loaded from a previous save.</strong>",
gameSaved: "<strong>Your game state has been saved.</strong>",
gameReset: "<strong>Your game state has been reset.</strong>",
emptyBag: "There is nothing in your bag!",
bagContains: "Your bag contains:",
acceptableCommands: "Here is a list of acceptable commands:",
acceptableCommandList: ['> go [direction]', '> north', '> east', '> south', '> west', '> up', '> down', '> look', '> open', '> enter', '> exit','> climb', '> brief [ short descriptions ]', '> verbose [ long descriptions ]', '> help', '> take', '> bag', '> save [ Save current game]', '> reset [ Reset game including save ]'],
verboseMode: "ZORK is now in its \"verbose\" mode, which always gives long descriptions of locations (even if you've been there before).",
briefMode: "ZORK is now in its normal \"brief\" printing mode, which gives long descriptions of places never before visited, and short descriptions otherwise.",
invalidDirection: "You can't go that way.",
notOpenable: "You can't open that.",
notUseable: "Use what?",
alreadyInUse: "The item is already in use. Putting item away.",
notReadable: "You can't read that."
},

}

$(window).on('load', function() {
Expand Down

0 comments on commit 3ccd1a2

Please sign in to comment.