Add score and turns
This commit is contained in:
+76
-9
@@ -26,6 +26,9 @@ class Inventory
|
|||||||
@items[item.name] = item
|
@items[item.name] = item
|
||||||
@length += 1
|
@length += 1
|
||||||
|
|
||||||
|
all: ->
|
||||||
|
return (item for name, item of @items)
|
||||||
|
|
||||||
describe: (options={})->
|
describe: (options={})->
|
||||||
options.simple ?= false
|
options.simple ?= false
|
||||||
result = []
|
result = []
|
||||||
@@ -55,8 +58,16 @@ class Inventory
|
|||||||
|
|
||||||
class Item
|
class Item
|
||||||
|
|
||||||
constructor: (@name)->
|
constructor: (@name, options={})->
|
||||||
|
options.fixed ?= false
|
||||||
|
|
||||||
@description = "non-descript item"
|
@description = "non-descript item"
|
||||||
|
@fixed = options.fixed
|
||||||
|
|
||||||
|
# Public Methods ###############################################################################
|
||||||
|
|
||||||
|
attemptTake: ->
|
||||||
|
return true
|
||||||
|
|
||||||
describe: ->
|
describe: ->
|
||||||
return @description
|
return @description
|
||||||
@@ -104,7 +115,6 @@ class Location
|
|||||||
|
|
||||||
return undefined
|
return undefined
|
||||||
|
|
||||||
|
|
||||||
removeItem: (item)->
|
removeItem: (item)->
|
||||||
@items.remove(item)
|
@items.remove(item)
|
||||||
return this
|
return this
|
||||||
@@ -258,12 +268,39 @@ class Player
|
|||||||
|
|
||||||
constructor: (@story)->
|
constructor: (@story)->
|
||||||
@inventory = new Inventory()
|
@inventory = new Inventory()
|
||||||
|
@onChange = (->)
|
||||||
@verbs = {}
|
@verbs = {}
|
||||||
|
|
||||||
|
@_score = 0
|
||||||
|
|
||||||
|
# Properties ###################################################################################
|
||||||
|
|
||||||
|
Object.defineProperties @prototype,
|
||||||
|
score:
|
||||||
|
get: ->
|
||||||
|
return @_score
|
||||||
|
set: (value)->
|
||||||
|
@_score = value
|
||||||
|
@onChange(this)
|
||||||
|
|
||||||
|
# Public Methods ###############################################################################
|
||||||
|
|
||||||
addVerb: (verb, onVerb)->
|
addVerb: (verb, onVerb)->
|
||||||
@verbs[verb] = onVerb
|
@verbs[verb] = onVerb
|
||||||
@story.parser.addVerb(verb)
|
@story.parser.addVerb(verb)
|
||||||
|
|
||||||
|
drop: (items...)->
|
||||||
|
if items.length is 0
|
||||||
|
items = @inventory.all()
|
||||||
|
|
||||||
|
for item in items
|
||||||
|
if @inventory.has(item)
|
||||||
|
@inventory.remove(item)
|
||||||
|
@story.currentLocation.inventory.add(item)
|
||||||
|
@story.log.writeln("Dropped #{item.name}.")
|
||||||
|
else
|
||||||
|
@story.log.writeln("You're not holding a #{item.name}.")
|
||||||
|
|
||||||
enact: (sentence)->
|
enact: (sentence)->
|
||||||
onVerb = @verbs[sentence.verb]
|
onVerb = @verbs[sentence.verb]
|
||||||
if onVerb
|
if onVerb
|
||||||
@@ -283,16 +320,25 @@ class Player
|
|||||||
take: (item)->
|
take: (item)->
|
||||||
localItems = @story.currentLocation.inventory
|
localItems = @story.currentLocation.inventory
|
||||||
if not item
|
if not item
|
||||||
localItems.eachItem (item)=> @take(item)
|
tookSomething = false
|
||||||
|
localItems.eachItem (item)=>
|
||||||
|
if not item.fixed
|
||||||
|
@take(item)
|
||||||
|
tookSomething = true
|
||||||
|
|
||||||
|
if not tookSomething then @story.log.writeln("There's nothing here you can take.")
|
||||||
else if localItems.has(item)
|
else if localItems.has(item)
|
||||||
localItems.remove(item)
|
console.log(item)
|
||||||
@inventory.add(item)
|
if not item.fixed
|
||||||
@story.log.writeln("Taken.")
|
localItems.remove(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("You can't take #{item} because there isn't one here.")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
########################################################################################################################
|
########################################################################################################################
|
||||||
|
|
||||||
class Sentence
|
class Sentence
|
||||||
@@ -339,15 +385,28 @@ class Story
|
|||||||
@items = []
|
@items = []
|
||||||
@locations = []
|
@locations = []
|
||||||
@log = new GameLog()
|
@log = new GameLog()
|
||||||
|
@onChange = (->)
|
||||||
@parser = new Parser(this)
|
@parser = new Parser(this)
|
||||||
@player = new Player(this)
|
@player = new Player(this)
|
||||||
|
|
||||||
|
@_turns = 0
|
||||||
|
|
||||||
@_configure()
|
@_configure()
|
||||||
|
|
||||||
|
# Properties ###################################################################################
|
||||||
|
|
||||||
|
Object.defineProperties @prototype,
|
||||||
|
"turns":
|
||||||
|
get: ->
|
||||||
|
return @_turns
|
||||||
|
set: (value)->
|
||||||
|
@_turns = value
|
||||||
|
@onChange(this)
|
||||||
|
|
||||||
# Configuration Methods ###########3############################################################
|
# Configuration Methods ###########3############################################################
|
||||||
|
|
||||||
addItem: (name)->
|
addItem: (name, options={})->
|
||||||
item = new Item(name)
|
item = new Item(name, options)
|
||||||
@items.push(item)
|
@items.push(item)
|
||||||
return item
|
return item
|
||||||
|
|
||||||
@@ -375,6 +434,7 @@ class Story
|
|||||||
interpret: (userInput)->
|
interpret: (userInput)->
|
||||||
try
|
try
|
||||||
@log.echoInput(userInput)
|
@log.echoInput(userInput)
|
||||||
|
@turns += 1
|
||||||
sentence = @parser.interpret(userInput)
|
sentence = @parser.interpret(userInput)
|
||||||
@player.enact(sentence)
|
@player.enact(sentence)
|
||||||
catch e
|
catch e
|
||||||
@@ -415,6 +475,13 @@ class Story
|
|||||||
"w": "west",
|
"w": "west",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@player.addVerb "drop", (sentence)=>
|
||||||
|
if sentence.has(item: 0)
|
||||||
|
@player.drop()
|
||||||
|
else
|
||||||
|
for itemToken in sentence.tokens.item
|
||||||
|
@player.drop(itemToken.referant)
|
||||||
|
|
||||||
@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)
|
||||||
|
|||||||
@@ -2,6 +2,10 @@
|
|||||||
<link rel="stylesheet" type="text/css" href="styles.css"/>
|
<link rel="stylesheet" type="text/css" href="styles.css"/>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
<div class="status">
|
||||||
|
<span class="turns"> </span>
|
||||||
|
<span class="score"> </span>
|
||||||
|
</div>
|
||||||
<div class="log">
|
<div class="log">
|
||||||
<p class="log-text"></p>
|
<p class="log-text"></p>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
+15
-1
@@ -6,18 +6,32 @@ RETURN_KEY = 13
|
|||||||
# Global Data ##########################################################################################################
|
# Global Data ##########################################################################################################
|
||||||
|
|
||||||
$(document).ready ->
|
$(document).ready ->
|
||||||
|
# configure player input box
|
||||||
$input = $("input.entry")
|
$input = $("input.entry")
|
||||||
$input.on "keydown", ->
|
$input.on "keydown", ->
|
||||||
if event.which isnt RETURN_KEY then return
|
if event.which isnt RETURN_KEY then return
|
||||||
STORY.interpret($input.val())
|
STORY.interpret($input.val())
|
||||||
$input.val("")
|
$input.val("")
|
||||||
|
|
||||||
|
# configure the story log display
|
||||||
$log = $(".log")
|
$log = $(".log")
|
||||||
$logText = $("p.log-text")
|
$logText = $("p.log-text")
|
||||||
|
|
||||||
STORY.log.onChange = (log)->
|
STORY.log.onChange = (log)->
|
||||||
$logText.html(log.content)
|
$logText.html(log.content)
|
||||||
$log.scrollTop($log[0].scrollHeight)
|
$log.scrollTop($log[0].scrollHeight)
|
||||||
|
|
||||||
|
# configure the score display
|
||||||
|
$score = $(".score")
|
||||||
|
STORY.player.onChange = (player)->
|
||||||
|
$score.text("score: #{player.score}")
|
||||||
|
STORY.player.onChange(STORY.player)
|
||||||
|
|
||||||
|
# configure the turns display
|
||||||
|
$turns = $(".turns")
|
||||||
|
STORY.onChange = (story)->
|
||||||
|
$turns.text("turns: #{story.turns}")
|
||||||
|
STORY.onChange(STORY)
|
||||||
|
|
||||||
|
# start the story
|
||||||
STORY.begin()
|
STORY.begin()
|
||||||
$input.focus()
|
$input.focus()
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@ window.STORY = s = new Story("A Walk Through My House")
|
|||||||
|
|
||||||
# Items ################################################################################################################
|
# Items ################################################################################################################
|
||||||
|
|
||||||
boxOfTile = s.addItem("box of tiles")
|
boxOfTile = s.addItem("box of tiles", fixed: true)
|
||||||
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."
|
||||||
|
|||||||
+27
-13
@@ -11,16 +11,7 @@ body {
|
|||||||
justify-content: center;
|
justify-content: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
div.log {
|
.entry {
|
||||||
background: black;
|
|
||||||
color: green;
|
|
||||||
height: 600px;
|
|
||||||
overflow-y: scroll;
|
|
||||||
padding: 0.25em 1em;
|
|
||||||
width: 800px;
|
|
||||||
}
|
|
||||||
|
|
||||||
input.entry {
|
|
||||||
background: black;
|
background: black;
|
||||||
border: none;
|
border: none;
|
||||||
border-top: 1px solid gray;
|
border-top: 1px solid gray;
|
||||||
@@ -33,10 +24,33 @@ input.entry {
|
|||||||
width: 800px;
|
width: 800px;
|
||||||
}
|
}
|
||||||
|
|
||||||
p.log-text {
|
.log {
|
||||||
|
background: black;
|
||||||
|
color: green;
|
||||||
|
height: 600px;
|
||||||
|
overflow-y: auto;
|
||||||
|
padding: 0.25em 1em;
|
||||||
|
width: 800px;
|
||||||
}
|
}
|
||||||
|
|
||||||
p.placeholder {
|
.status {
|
||||||
color: gray;
|
background: black;
|
||||||
|
border: none;
|
||||||
|
border-bottom: 1px solid gray;
|
||||||
|
color: green;
|
||||||
|
display: flex;
|
||||||
|
font-family: monospace;
|
||||||
|
font-size: 16px;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0.5em 1em;
|
||||||
|
width: 800px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.score {
|
||||||
|
flex: 1 1 50%;
|
||||||
|
text-align: right;
|
||||||
|
}
|
||||||
|
|
||||||
|
.turns {
|
||||||
|
flex: 1 1 50%;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user