Add inventory command
This commit is contained in:
+19
-9
@@ -26,14 +26,18 @@ class Inventory
|
|||||||
@items[item.name] = item
|
@items[item.name] = item
|
||||||
@length += 1
|
@length += 1
|
||||||
|
|
||||||
describe: ->
|
describe: (options={})->
|
||||||
|
options.simple ?= false
|
||||||
result = []
|
result = []
|
||||||
|
|
||||||
needsDelimiter = false
|
needsDelimiter = false
|
||||||
for name, item of @items
|
for name, item of @items
|
||||||
if needsDelimiter
|
if needsDelimiter
|
||||||
result.push("\n")
|
result.push("\n")
|
||||||
result.push("There is a #{item.name} here.")
|
if options.simple
|
||||||
|
result.push(item.name)
|
||||||
|
else
|
||||||
|
result.push("There is a #{item.name} here.")
|
||||||
needsDelimiter = true
|
needsDelimiter = true
|
||||||
|
|
||||||
return result.join("\n")
|
return result.join("\n")
|
||||||
@@ -176,7 +180,6 @@ class Parser
|
|||||||
candidates = {}
|
candidates = {}
|
||||||
|
|
||||||
considerItem = (item)->
|
considerItem = (item)->
|
||||||
console.log("considering: #{item}")
|
|
||||||
if item.name.indexOf(rawWord) isnt -1
|
if item.name.indexOf(rawWord) isnt -1
|
||||||
candidate = candidates[item.name] ?= {item: item; count: 0}
|
candidate = candidates[item.name] ?= {item: item; count: 0}
|
||||||
candidate.count += 1
|
candidate.count += 1
|
||||||
@@ -392,6 +395,7 @@ class Story
|
|||||||
"e": "east",
|
"e": "east",
|
||||||
"everything": "all",
|
"everything": "all",
|
||||||
"g": "go"
|
"g": "go"
|
||||||
|
"i": "inventory"
|
||||||
"n": "north",
|
"n": "north",
|
||||||
"ne": "northeast",
|
"ne": "northeast",
|
||||||
"nw": "northwest",
|
"nw": "northwest",
|
||||||
@@ -402,18 +406,24 @@ class Story
|
|||||||
"w": "west",
|
"w": "west",
|
||||||
})
|
})
|
||||||
|
|
||||||
player = @player
|
@player.addVerb "go", (sentence)=>
|
||||||
@player.addVerb "go", (sentence)->
|
|
||||||
if sentence.has(location: 1)
|
if sentence.has(location: 1)
|
||||||
player.move(sentence.location)
|
@player.move(sentence.location)
|
||||||
else
|
else
|
||||||
throw new ParseError("I'm not sure where you want to go...")
|
throw new ParseError("I'm not sure where you want to go...")
|
||||||
|
|
||||||
@player.addVerb "take", (sentence)->
|
@player.addVerb "inventory", =>
|
||||||
|
if @player.inventory.length is 0
|
||||||
|
@log.writeln("You're not carrying anything.")
|
||||||
|
else
|
||||||
|
@log.writeln("You have:")
|
||||||
|
@log.writeln(@player.inventory.describe(simple: true))
|
||||||
|
|
||||||
|
@player.addVerb "take", (sentence)=>
|
||||||
if sentence.has(item: 1)
|
if sentence.has(item: 1)
|
||||||
player.take(sentence.item)
|
@player.take(sentence.item)
|
||||||
else if sentence.has(item: 0)
|
else if sentence.has(item: 0)
|
||||||
player.take()
|
@player.take()
|
||||||
|
|
||||||
|
|
||||||
########################################################################################################################
|
########################################################################################################################
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@ window.STORY = s = new Story("A Walk Through My House")
|
|||||||
|
|
||||||
# Items ################################################################################################################
|
# Items ################################################################################################################
|
||||||
|
|
||||||
boxOfTile = s.addItem("box of tile")
|
boxOfTile = s.addItem("box of tiles")
|
||||||
boxOfTile.description =
|
boxOfTile.description =
|
||||||
"This appears to be a box of flooring tiles. The tiles appear to be a very light color of natural stone. It is
|
"This appears to be a box of flooring tiles. The tiles appear to be a very light color of natural stone. It is
|
||||||
quite heavy."
|
quite heavy."
|
||||||
|
|||||||
Reference in New Issue
Block a user