Location summary prints item list

main
Ashelyn Dawn 4 years ago
parent 88c596b60b
commit 01dcae094a

@ -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()

@ -23,7 +23,7 @@ export default class RulesEngine extends EventEmitter{
})
this.on('locationChange', () => {
printArea(game.getCurrentRoom(), game.say.bind(game))
printArea(game)
})
}

@ -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)
})
}
}

@ -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<Door>
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}`)
})
}
}

@ -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

@ -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()

@ -1,6 +0,0 @@
import capitalize from "./capitalize"
export default function printArea(location, say) {
say(`**${capitalize(location.printableName)}**`)
say(location.description)
}

@ -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)
}
}
Loading…
Cancel
Save