Take verb

main
Ashelyn Dawn 4 years ago
parent 17825c319f
commit 75048c3003

@ -73,14 +73,29 @@ export default class GameState {
if(itemType === 'direction' && !this.state.directions.includes(itemName)) if(itemType === 'direction' && !this.state.directions.includes(itemName))
return false; return false;
if(itemType === 'door' && !Object.values(this.state.locations).filter(loc => loc.type === 'door').map(door => door.name).includes(itemName)) if(itemType === 'door') {
return false; const door = Object.values(this.state.locations).filter(loc => loc.type === 'door').find(door => itemName === door.name)
if(door)
if(itemType === 'room' && !Object.values(this.state.locations).filter(loc => loc.type === 'room').map(door => door.name).includes(itemName)) expression.id = door.id
return false; else
return false;
if(itemType === 'item' && !Object.values(this.state.items).map(item => item.name).includes(itemName)) }
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; return true;

@ -52,10 +52,13 @@ export default class Parser {
const [command] = validParsings const [command] = validParsings
const {verb, tokens} = command const {verb, tokens} = command
const subject = tokens.filter(({type, position}) => (type === 'expression' && position === 'subject'))[0]?.value const subject = tokens.filter(({type, position}) => (type === 'expression' && position === 'subject'))[0]
const object = tokens.filter(({type, position}) => (type === 'expression' && position === 'object' ))[0]?.value const object = tokens.filter(({type, position}) => (type === 'expression' && position === 'object' ))[0]
const action = {type: 'player', verb, subject, object}; const action = {type: 'player', verb, subject, object};
action.getSubject = () => subject.id
action.getObject = () => object.id
console.log(action) console.log(action)
this._processAction(action) this._processAction(action)
} }

@ -65,7 +65,7 @@ RuleEngine.SUCCESS = 'success'
RuleEngine.FAILURE = 'failure' RuleEngine.FAILURE = 'failure'
// TODO: Apply rules conditionally depending on state, location, verb, etc // 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 testLocation(state, action, rule) { return true }
function testVerb(state, action, rule) { return true } function testVerb(state, action, rule) { return true }
function testSubject(state, action, rule) { return true } function testSubject(state, action, rule) { return true }

@ -1,10 +1,11 @@
import printArea from '../../utils/printArea' import printArea from '../../utils/printArea'
// import {current} from 'immer'
let pastArea let pastArea
export default [{ export default [{
type: 'internal',
hooks: { hooks: {
// After the player's location changes, print the new area's description
after: (state, action, say) => { after: (state, action, say) => {
const {location} = state.player const {location} = state.player
if(location !== pastArea) { 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.')
}
}
}
}] }]

@ -40,7 +40,8 @@ export default [{
state.items['brass key'] = { state.items['brass key'] = {
id: 'brass key', id: 'brass key',
name: 'brass key', name: 'brass key',
description: 'A heavy brass key' description: 'A heavy brass key',
location: 'safe'
} }
} }
} }

Loading…
Cancel
Save