From 75048c3003841a774566495553c858e0fd1809a0 Mon Sep 17 00:00:00 2001 From: Ashelyn Dawn Date: Wed, 22 Jul 2020 19:44:37 -0600 Subject: [PATCH] Take verb --- src/GameState/index.js | 31 +++++++++++++++++++++++-------- src/Parser/index.js | 7 +++++-- src/RuleEngine/index.js | 2 +- src/RuleEngine/rules/core.js | 19 ++++++++++++++++++- src/RuleEngine/rules/game.js | 3 ++- 5 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/GameState/index.js b/src/GameState/index.js index b8d7755..ed46111 100644 --- a/src/GameState/index.js +++ b/src/GameState/index.js @@ -73,14 +73,29 @@ export default class GameState { if(itemType === 'direction' && !this.state.directions.includes(itemName)) return false; - if(itemType === 'door' && !Object.values(this.state.locations).filter(loc => loc.type === 'door').map(door => door.name).includes(itemName)) - return false; - - if(itemType === 'room' && !Object.values(this.state.locations).filter(loc => loc.type === 'room').map(door => door.name).includes(itemName)) - return false; - - if(itemType === 'item' && !Object.values(this.state.items).map(item => item.name).includes(itemName)) - return false; + if(itemType === 'door') { + const door = Object.values(this.state.locations).filter(loc => loc.type === 'door').find(door => itemName === door.name) + if(door) + expression.id = door.id + else + return false; + } + + if(itemType === 'room') { + const room = Object.values(this.state.locations).filter(loc => loc.type === 'room').find(room => itemName === room.name) + if(room) + expression.id = room.id + else + return false; + } + + if(itemType === 'item') { + const item = Object.values(this.state.items).find(item => itemName === item.name) + if(item) + expression.id = item.id + else + return false; + } } return true; diff --git a/src/Parser/index.js b/src/Parser/index.js index 983954a..d08fe84 100644 --- a/src/Parser/index.js +++ b/src/Parser/index.js @@ -52,10 +52,13 @@ export default class Parser { const [command] = validParsings const {verb, tokens} = command - const subject = tokens.filter(({type, position}) => (type === 'expression' && position === 'subject'))[0]?.value - const object = tokens.filter(({type, position}) => (type === 'expression' && position === 'object' ))[0]?.value + const subject = tokens.filter(({type, position}) => (type === 'expression' && position === 'subject'))[0] + const object = tokens.filter(({type, position}) => (type === 'expression' && position === 'object' ))[0] const action = {type: 'player', verb, subject, object}; + action.getSubject = () => subject.id + action.getObject = () => object.id + console.log(action) this._processAction(action) } diff --git a/src/RuleEngine/index.js b/src/RuleEngine/index.js index fc37593..6848cda 100644 --- a/src/RuleEngine/index.js +++ b/src/RuleEngine/index.js @@ -65,7 +65,7 @@ RuleEngine.SUCCESS = 'success' RuleEngine.FAILURE = 'failure' // TODO: Apply rules conditionally depending on state, location, verb, etc -function testType(state, action, rule) { return true } +function testType(state, action, rule) { return action.type === rule.type } function testLocation(state, action, rule) { return true } function testVerb(state, action, rule) { return true } function testSubject(state, action, rule) { return true } diff --git a/src/RuleEngine/rules/core.js b/src/RuleEngine/rules/core.js index 4819497..f1147e0 100644 --- a/src/RuleEngine/rules/core.js +++ b/src/RuleEngine/rules/core.js @@ -1,10 +1,11 @@ import printArea from '../../utils/printArea' +// import {current} from 'immer' let pastArea export default [{ - type: 'internal', hooks: { + // After the player's location changes, print the new area's description after: (state, action, say) => { const {location} = state.player if(location !== pastArea) { @@ -13,4 +14,20 @@ export default [{ } } } +},{ + type: 'player', + verb: 'take', + hooks: { + carryOut: (state, action, say) => { + const item = state.items[action.getSubject()] + const player = state.player + + if(item.location !== player.location) { + say(`You cannot see the ${action.subject.value}`) + } else { + item.location = 'inventory' + say('Taken.') + } + } + } }] diff --git a/src/RuleEngine/rules/game.js b/src/RuleEngine/rules/game.js index 3a6a9d7..ccb7044 100644 --- a/src/RuleEngine/rules/game.js +++ b/src/RuleEngine/rules/game.js @@ -40,7 +40,8 @@ export default [{ state.items['brass key'] = { id: 'brass key', name: 'brass key', - description: 'A heavy brass key' + description: 'A heavy brass key', + location: 'safe' } } }