diff --git a/src/engine/Game.ts b/src/engine/Game.ts index 1d0383d..b436ddc 100644 --- a/src/engine/Game.ts +++ b/src/engine/Game.ts @@ -27,7 +27,6 @@ export default class Game { private onChangeListeners : ChangeListener [] = [] constructor() { - console.log('adding directions') let state = this.getState() state.directions.set('north', {type: ObjectType.Direction, name: 'north', printableName: 'north', aliases: ['n']}) state.directions.set('east', {type: ObjectType.Direction, name: 'east', printableName: 'east', aliases: ['e']}) @@ -157,6 +156,29 @@ export default class Game { return null } + findObjectsInRoom(name : string | undefined) : Item [] { + let items : Item [] = [] + console.log(items) + + for(const item of this.getState().items.values()) + if(item.location === name) + items.push(item) + + console.log(items) + return items; + } + + findDoorsInRoom(name : string | undefined) : Door [] { + const room = this.findObjectByName(name, ObjectType.Room) as Room + + if(!room) + return [] + + return Array.from(room.neighbors.values()) + .map(name => this.findObjectByName(name, ObjectType.Door)) + .filter(door => door !== null) as Door [] + } + isVisible(object : GameObject) : boolean { const state = this.getState() const currentRoom = this.getCurrentRoom() diff --git a/src/engine/RulesEngine.ts b/src/engine/RulesEngine.ts index 3983ebd..1b01c75 100644 --- a/src/engine/RulesEngine.ts +++ b/src/engine/RulesEngine.ts @@ -23,7 +23,7 @@ export default class RulesEngine extends EventEmitter{ }) this.on('locationChange', () => { - printArea(game.getCurrentRoom(), game.say.bind(game)) + printArea(game) }) } diff --git a/src/engine/definitions/look.ts b/src/engine/definitions/look.ts index 2dd5a19..b1d02d7 100644 --- a/src/engine/definitions/look.ts +++ b/src/engine/definitions/look.ts @@ -11,6 +11,6 @@ export default function(parser : Parser, rules : RulesEngine, game : Game) { .as('l') rules.onCommand('look', (command : ValidCommandDetails) => { - printArea(game.getCurrentRoom(), game.say.bind(game)) + printArea(game) }) -} \ No newline at end of file +} diff --git a/src/engine/definitions/unlockDoor.ts b/src/engine/definitions/unlockDoor.ts index c7b30e7..53400d9 100644 --- a/src/engine/definitions/unlockDoor.ts +++ b/src/engine/definitions/unlockDoor.ts @@ -3,6 +3,7 @@ import RulesEngine from "../RulesEngine"; import Game from "../Game"; import { Door, Item } from "../types/GameState"; import { Draft } from "immer"; +import { TokenType, ParsedTokenLiteral } from "../types/ParsedCommand"; export default function(parser : Parser, rules : RulesEngine, game : Game) { parser.understand('unlockDoor') @@ -15,7 +16,7 @@ export default function(parser : Parser, rules : RulesEngine, game : Game) { rules.onCommand('unlockDoor', command => { if(!command.object) throw new Error(`Please specify what you would like to unlock the ${command.subject!.name} with`) - + const door = command.subject as Draft const key = command.object as Item @@ -27,5 +28,9 @@ export default function(parser : Parser, rules : RulesEngine, game : Game) { door.locked = false game.say(`With a sharp **click** the ${door.name} unlocks.`) + + const firstWord = command.parsed.getTokens()[0] as ParsedTokenLiteral + if(firstWord.type === TokenType.Literal && firstWord.word === 'open') + parser.runCommand(`open ${command.subject!.name}`) }) -} \ No newline at end of file +} diff --git a/src/engine/types/ParsedCommand.ts b/src/engine/types/ParsedCommand.ts index 8c27f85..711d123 100644 --- a/src/engine/types/ParsedCommand.ts +++ b/src/engine/types/ParsedCommand.ts @@ -43,7 +43,7 @@ export enum ParsingErrorSeverity { NoSuchObject = 1 } -export type ValidCommandDetails = {isValid: true, verb: Verb, subject: GameObject | null, object: GameObject | null} +export type ValidCommandDetails = {isValid: true, parsed: ParsedCommand, verb: Verb, subject: GameObject | null, object: GameObject | null} export type InvalidCommandDetails = {isValid: false, command: ParsedCommand, reason: string, severity: ParsingErrorSeverity} export default class ParsedCommand { @@ -54,6 +54,10 @@ export default class ParsedCommand { this.verb = verb } + getTokens() { + return [...this.tokens] + } + getNumTokens() : number { return this.tokens.length } @@ -99,6 +103,7 @@ export default class ParsedCommand { return { isValid: true, + parsed: this, verb: this.verb, subject, object diff --git a/src/index.tsx b/src/index.tsx index 57145e2..b7e6b54 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -68,6 +68,14 @@ game.addItem({ location: 'office' }) +game.addItem({ + type: ObjectType.Item, + printableName: 'teddy bear', + name: 'teddy', + aliases: ['teddy bear'], + location: 'entry' +}) + game.getState().player.location = 'entry' game.saveDraft() diff --git a/src/utils/printArea.js b/src/utils/printArea.js deleted file mode 100644 index 5424347..0000000 --- a/src/utils/printArea.js +++ /dev/null @@ -1,6 +0,0 @@ -import capitalize from "./capitalize" - -export default function printArea(location, say) { - say(`**${capitalize(location.printableName)}**`) - say(location.description) -} diff --git a/src/utils/printArea.ts b/src/utils/printArea.ts new file mode 100644 index 0000000..8f54aee --- /dev/null +++ b/src/utils/printArea.ts @@ -0,0 +1,38 @@ +import capitalize from "./capitalize" +import Game from "../engine/Game" + +export default function printArea(game : Game) { + const location = game.getCurrentRoom()! + + game.say(`**${capitalize(location.printableName)}**`) + game.say(location.description!) + + const items = game.findObjectsInRoom(location.name) + const doors = game.findDoorsInRoom(location.name) + const things = [...items, ...doors] + + if(things.length === 0) + return + + if(things.length === 1) + game.say(`You see the ${things[0].printableName} here.`) + else if (things.length === 2) + game.say(`You see the ${things[0].printableName} and the ${things[1].printableName} here.`) + else{ + let youCanSee = 'You see ' + + for(const i in things) { + const thing = things[i] + + if(i === '0') + youCanSee += 'the ' + thing.printableName + else if(i === '' + (things.length - 1)) + youCanSee += ', and the ' + thing.printableName + ' here.' + else + youCanSee += ', the ' + thing.printableName + } + + game.say(youCanSee) + } + +}