Tracks last seen item positions for help in remembering

main
Ashelyn Dawn 4 years ago
parent 13ff0dcf44
commit 3f83517484

@ -176,7 +176,8 @@ export default class Game {
let item : Item = {
type: ObjectType.Item,
name, aliases: [], printableName: name, description,
location
location,
seen: false
}
let state = this.getState()
@ -211,6 +212,8 @@ export default class Game {
findObjectByName(name : string | undefined | null, type : ObjectType) : GameObject | null {
if(!name) return null;
let lowerCaseName = name.toLocaleLowerCase()
let collection
switch(type) {
case ObjectType.Door:
@ -232,18 +235,18 @@ export default class Game {
const objects = [...collection!.values()]
const exactMatch = objects.find((object) => name === object.name)
const exactMatch = objects.find((object) => lowerCaseName === object.name.toLocaleLowerCase())
if(exactMatch)
return exactMatch
const aliasMatch = objects.find(({aliases}) => aliases.includes(name))
const aliasMatch = objects.find(({aliases}) => aliases.map(a => a.toLocaleLowerCase()).includes(lowerCaseName))
if(aliasMatch)
return aliasMatch
return null
}
findObjectsInRoom(name : string | undefined) : Item [] {
findObjectsInRoom(name : string | undefined) : Draft<Item> [] {
let items : Item [] = []
for(const item of this.getState().items.values())

@ -16,11 +16,8 @@ export default function(parser : Parser, rules : RulesEngine, game : Game) {
rules.onCommand('take', command => {
const item = command.subject as Draft<Item>
if(item.location !== game.getState().player.location)
throw new Error(`You cannot see the ${item.name}`)
item.location = 'inventory'
game.say('Taken.')
})
}
}

@ -52,5 +52,7 @@ export type Door = GameObject & {
export type Item = GameObject & {
readonly type : ObjectType.Item,
readonly location: ObjectID | 'inventory'
readonly location: ObjectID | 'inventory',
readonly seen : boolean,
readonly lastKnownLocation? : string
}

@ -1,4 +1,4 @@
import { ObjectType, GameObject } from "./GameState"
import { ObjectType, GameObject, Item } from "./GameState"
import NounPosition from "./NounPosition"
import Verb from "./Verb"
import Game from "../Game"
@ -76,7 +76,7 @@ export default class ParsedCommand {
for(const noun of nouns) {
let gameObject = game.findObjectByName(noun.name, noun.itemType)
if(!gameObject)
if(!gameObject || (gameObject.type === ObjectType.Item && !((gameObject as Item).seen)))
return {
isValid: false,
command: this,
@ -91,7 +91,11 @@ export default class ParsedCommand {
return {
isValid: false,
command: this,
reason: `You cannot see the ${noun.name}`,
reason: `You cannot see the ${noun.name}${(() => {
if(gameObject.type === ObjectType.Item && (gameObject as Item).lastKnownLocation)
return ` (last seen in the ${(gameObject as Item).lastKnownLocation})`
return ''
})()}`,
severity: ParsingErrorSeverity.NotVisible
}

@ -2,15 +2,28 @@ import {game, rules, renderer} from './engine/'
import './rooms.tsx'
// Initial player location
game.getState().player.location = 'cabin'
game.getCurrentRoom()!.visited = true
rules.onAfterCommand((command, game) => {
game.getCurrentRoom()!.visited = true
})
// Mark room visited when ending a turn there
const markRoomVisited = () => {game.getCurrentRoom()!.visited = true}
rules.onGameStart(markRoomVisited)
rules.onAfterCommand(markRoomVisited)
// Mark items as seen when ending a turn in their room
const setItemsSeen = () => game.findObjectsInRoom(game.getCurrentRoom()!.name).forEach(item => {item.seen = true})
rules.onGameStart(setItemsSeen)
rules.onAfterCommand(setItemsSeen)
// Track items' last seen location
const setItemLastLoc = () => game.findObjectsInRoom(game.getCurrentRoom()!.name).forEach(item => {item.lastKnownLocation = game.getCurrentRoom()!.name})
rules.onGameStart(setItemLastLoc)
rules.onAfterCommand(setItemLastLoc)
game.addItem('Block', 'A boring wooden block', 'commons')
game.saveDraft()
renderer.start(document.getElementById('root'))

Loading…
Cancel
Save