Allow items to veto being taken.

This commit is contained in:
Andrew Miner
2018-05-28 15:31:56 -06:00
parent 56daf2ef62
commit 2e7772b488
2 changed files with 24 additions and 13 deletions
+20 -12
View File
@@ -8,10 +8,13 @@ class GameLog
@content += "<br>&gt; " + text + "<br>" @content += "<br>&gt; " + text + "<br>"
@onChange(this) @onChange(this)
writeln: (text="")-> write: (text="")->
@content += text.replace(/\n/g, "<br>") + "<br>" @content += text.replace(/\n/g, "<br>")
@onChange(this) @onChange(this)
writeln: (text="")->
@write(text + "\n")
######################################################################################################################## ########################################################################################################################
@@ -63,11 +66,17 @@ class Item
@description = "non-descript item" @description = "non-descript item"
@fixed = options.fixed @fixed = options.fixed
@story = null
# Public Methods ############################################################################### # Public Methods ###############################################################################
attemptTake: -> take: ->
return true if @fixed
@story.log.writeln("You cannot take the #{@name}.")
return false
else
@story.log.writeln("Taken.")
return true
describe: -> describe: ->
return @description return @description
@@ -320,23 +329,21 @@ class Player
take: (item)-> take: (item)->
localItems = @story.currentLocation.inventory localItems = @story.currentLocation.inventory
if not item if not item
tookSomething = false foundTakeableItem = false
localItems.eachItem (item)=> localItems.eachItem (item)=>
if not item.fixed if not item.fixed
@story.log.write("#{item}: ")
foundTakeableItem = true
@take(item) @take(item)
tookSomething = true
if not tookSomething then @story.log.writeln("There's nothing here you can take.") if not foundTakeableItem then @story.log.writeln("There's nothing here you can take.")
else if localItems.has(item) else if localItems.has(item)
console.log(item) console.log(item)
if not item.fixed if item.take()
localItems.remove(item) localItems.remove(item)
@inventory.add(item) @inventory.add(item)
@story.log.writeln("Took the #{item.name}.")
else
@story.log.writeln("You can't take the #{item.name}.")
else else
throw new ParseError("You can't take #{item} because there isn't one here.") throw new ParseError("There isn't a #{item} here.")
######################################################################################################################## ########################################################################################################################
@@ -407,6 +414,7 @@ class Story
addItem: (name, options={})-> addItem: (name, options={})->
item = new Item(name, options) item = new Item(name, options)
item.story = this
@items.push(item) @items.push(item)
return item return item
+4 -1
View File
@@ -2,10 +2,13 @@ window.STORY = s = new Story("A Walk Through My House")
# Items ################################################################################################################ # Items ################################################################################################################
boxOfTile = s.addItem("box of tiles", fixed: true) 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."
boxOfTile.take = ->
s.log.writeln("Oof! It's too heavy.")
return false
hose = s.addItem("garden hose") hose = s.addItem("garden hose")
hose.description = "It's a pretty ordinary garden hose about 20' long." hose.description = "It's a pretty ordinary garden hose about 20' long."