-
Notifications
You must be signed in to change notification settings - Fork 1
Add more output functionality, remove debug display code #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
ddab20f
8f22ae8
f5350d8
4e659b5
ca97684
b7ac260
82883f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /** | ||
| * For displaying descriptions to the player (text about game that replaces itself) | ||
| */ | ||
| class Descriptor { | ||
|
|
||
| /** | ||
| * @param descriptionArea A dom object in which to place description text | ||
| */ | ||
| constructor($descriptionArea) { | ||
| this.descriptionArea = $descriptionArea // A dom object in which to place text | ||
|
|
||
| /** | ||
| * @param description An info string to display to the user | ||
| */ | ||
| this.display = description => { | ||
| // Format the description for html | ||
| const htmlDescription = `<div class="description">${description}</div>` | ||
|
|
||
| // Display the description | ||
| this.descriptionArea.children().remove() | ||
| this.descriptionArea.append(htmlDescription) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,23 @@ | ||
| // Jacob's working on this... | ||
|
|
||
| /** | ||
| * Main game object that controls game flow. | ||
| */ | ||
| class Game { | ||
|
|
||
| /** | ||
| * @param setupData An object specifcally formatted to provide info to the game. | ||
| * See config.js for example structure. | ||
| */ | ||
| constructor(setupData) { | ||
| //setup the DOM with jquery | ||
| // Link the DOM command input area using jQuery | ||
| const $userInput = $("#command") | ||
| const $outputBox = $("#message") | ||
|
|
||
| //create a messenger with access to the dom output box | ||
| const messenger = new Messenger($outputBox) | ||
| // Link the DOM output areas using jQuery | ||
| const $messageArea = $("#message") | ||
| const messenger = new Messenger($messageArea) | ||
| const $nameArea = $("#name") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure what the ID "name" means in the context of the DOM element. Player name, room name, current item name?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good comment - I'll work on making the name more descriptive/clear. |
||
| const roomNamer = new Descriptor($nameArea) | ||
| const $descriptionArea = $("#description") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the "description" ID in context of the DOM? What type of descriptions will be viewed there? Isn't the message area what shows descriptions when a user enters a room? Usually the way text based adventures behave, what do you think?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good comment - I'll work on making the name more descriptive/clear. |
||
| const descriptor = new Descriptor($descriptionArea) | ||
|
|
||
| //create a new interpreter/parser with availible commands and synonyms | ||
| const parser = new Parser({ | ||
|
|
@@ -19,10 +29,15 @@ class Game { | |
| drop: ['drop', 'd', 'leave', 'throw', 'abandon'] | ||
| }) | ||
|
|
||
| //init the game state | ||
| // Initialize the game state | ||
| let {_player, _rooms, _doors, _currentRoom} = loadGame(setupData) | ||
|
|
||
| // Sets up the game data with gameData (or loads previous game) | ||
| /** | ||
| * Loads a game from a saved game state (or initializes from a default state). | ||
| * | ||
| * @param gameData Specifically formatted object that represents the game state. | ||
| * See config.js for example structure. | ||
| */ | ||
| function loadGame(gameData) { | ||
| const player = gameData.entities.player | ||
| const gameState = { | ||
|
|
@@ -68,16 +83,15 @@ class Game { | |
| return gameState | ||
| } | ||
|
|
||
| //debug function that shows info about room in UI | ||
| function displayRoomStats(room){ | ||
| $('#room').text(room.name()) | ||
| $('#description').text(room.description()) | ||
| $('#items').text(room.listOfItems()) | ||
| // Function to display initial info about the current room | ||
| function displayRoomInfo(room) { | ||
| roomNamer.display(room.name()) | ||
| descriptor.display(room.description()) | ||
| } | ||
|
|
||
| //run function starts the game accepting input | ||
| this.run = () => { | ||
| displayRoomStats(_currentRoom) | ||
| displayRoomInfo(_currentRoom) | ||
|
|
||
| $userInput.on('keydown', (event) => { | ||
| //user presses enter | ||
|
|
@@ -107,7 +121,7 @@ class Game { | |
|
|
||
| //clear the input field | ||
| $userInput.val('') | ||
| displayRoomStats(_currentRoom) | ||
| displayRoomInfo(_currentRoom) | ||
| } | ||
| else if(event.which === 38){//Up arror pushed | ||
| $userInput.val(parser.getLastCommandHistory()) | ||
|
|
@@ -125,7 +139,7 @@ class Game { | |
| checkWinningConditions() | ||
| } | ||
| else{ | ||
| messenger.addOutput(`You can't go ${direction}.`) | ||
| messenger.addMessage(`You can't go ${direction}.`) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -136,12 +150,12 @@ class Game { | |
| $('#items').text(_currentRoom.listOfItems()) | ||
| } | ||
| else{ | ||
| messenger.addOutput(`There's no ${itemName} here.`) | ||
| messenger.addMessage(`There's no ${itemName} here.`) | ||
| } | ||
| } | ||
|
|
||
| function inventory() { | ||
| messenger.addOutput(_player.inventory()) | ||
| messenger.addMessage(_player.inventory()) | ||
| } | ||
|
|
||
| function drop(itemName) { | ||
|
|
@@ -150,7 +164,7 @@ class Game { | |
| _currentRoom.addItem(item) | ||
| } | ||
| else{ | ||
| messenger.addOutput(`You don't have a ${itemName}.`) | ||
| messenger.addMessage(`You don't have a ${itemName}.`) | ||
| } | ||
|
|
||
| checkWinningConditions() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,44 @@ | ||
| // Jacob's working on this... | ||
|
|
||
| /** | ||
| * For displaying messages to the player | ||
| */ | ||
| class Messenger { | ||
| constructor($outputBox) { | ||
| this.outputBox = $outputBox | ||
| this.outputHistory = [] | ||
| } | ||
|
|
||
| addOutput(str) { | ||
| this.outputHistory.push(str) | ||
| this.render() | ||
| } | ||
| /** | ||
| * @param messageArea A dom object in which to place message text | ||
| */ | ||
| constructor($messageArea) { | ||
| const _messageArea = $messageArea // A dom object in which to place messages | ||
| let _messageHistory = [] // A list of all previous messages, newest first | ||
|
|
||
| /** | ||
| * @param message A string to display to the user | ||
| */ | ||
| this.addMessage = message => { | ||
| // Add the new message to the beginning of the history list (FILO) | ||
| _messageHistory.unshift(message) | ||
|
|
||
| clearHistory() { | ||
| this.outputHistory = [] | ||
| } | ||
| // And display all messages | ||
| this.render() | ||
| } | ||
|
|
||
| this.clearHistory = () => { _messageHistory = [] } | ||
|
|
||
| // Display the list of messages | ||
| this.render = () => { | ||
| // Trim off all but the last ten messages in the history (first ten in array) | ||
| const MAX_MESSAGES_TO_DISPLAY = 10 | ||
| let lastTenMessages = _messageHistory.slice(0, MAX_MESSAGES_TO_DISPLAY) | ||
|
|
||
| // Create an html message list (using BEM css class structure) | ||
| let htmlMessages = lastTenMessages.reduce( | ||
| (acc, message) => acc + `<div class="messages_message">${message}</div>`, | ||
| '' | ||
| ) | ||
| let htmlMessagesGroup = `<div class="messages">${htmlMessages}</div>` | ||
|
|
||
| render() { | ||
| let listItems = this.outputHistory.reduce((acc, curr) => acc + `<div>${curr}</div>`, '') | ||
| let messagesList = `<div class="outputList">${listItems}</div>` | ||
| this.outputBox.children().remove() | ||
| this.outputBox.append(messagesList) | ||
| // Replace current messages with the updated list | ||
| _messageArea.children().remove() | ||
| _messageArea.append(htmlMessagesGroup) | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a descriptor or a displayer? To me this class changes any arbitrary DOM element text passed to it. Later in the code it is used as a descriptor and a namer type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good comment - I'll work on making the name more descriptive/clear.