add conditionals, variable setting to nev primitives

This commit is contained in:
Jeremy Penner 2011-03-03 07:41:55 -08:00
parent dffc93d5e6
commit 2c8380a776

View file

@ -291,6 +291,7 @@ bind = (fn, me) -> ((args...) -> fn.apply(me, args))# argh new coffeescript won'
# narrative event happens, the preconditions to make sure the event should be allowed to occur, and # narrative event happens, the preconditions to make sure the event should be allowed to occur, and
# the resultant effect on the state of the model of the world # the resultant effect on the state of the model of the world
class Nev class Nev
@RgstelemReserved = ["cond", "set", "donext"]
constructor: (@dNev) -> constructor: (@dNev) ->
ID: () -> ID: () ->
if not @id? if not @id?
@ -305,9 +306,23 @@ class Nev
return true return true
wst = wst.WstPrev() wst = wst.WstPrev()
return false return false
FCanRun: (gst, wst) -> not @FHasRun(wst) FConds: (wst) ->
for dCond in $("cond", @dNev)
try
if (!(new Function("wst", "with(wst) {return #{dCond.textContent};}")(wst)))
return false
catch error
return false
true
FCanRun: (gst, wst) -> not @FHasRun(wst) and @FConds(wst)
FEndsSection: () -> $(@dNev).attr("nextsection")? FEndsSection: () -> $(@dNev).attr("nextsection")?
RunAction: (gst, wst) -> RunAction: (gst, wst) ->
for dSet in $("set", @dNev)
try
new Function("wst", "wst.#{$(dSet).attr('var')} = #{dSet.textContent};")(wst)
catch e
alert("error setting #{$(dSet).attr('var')} to #{dSet.textContent}")
StHtmlNextSection: (gst, wst) -> StHtmlNextSection: (gst, wst) ->
stSectionNext = $(@dNev).attr("nextsection") stSectionNext = $(@dNev).attr("nextsection")
if stSectionNext? if stSectionNext?
@ -317,6 +332,12 @@ class Nev
StHtmlDisplay: (gst, wst) -> StHtmlDisplay: (gst, wst) ->
jdivTmp = $("<div/>") jdivTmp = $("<div/>")
for dHTML in $(@dNev).contents() for dHTML in $(@dNev).contents()
fReserved = false
for stElemReserved in Nev.RgstelemReserved
if ($.nodeName(dHTML, stElemReserved))
fReserved = true
break
if not fReserved
jdivTmp.append(document.importNode(dHTML, true)) jdivTmp.append(document.importNode(dHTML, true))
stHtml = jdivTmp[0].innerHTML stHtml = jdivTmp[0].innerHTML
"<div class='iffy-nev-ui'>#{gst.StHtmlUi(wst)}</div><div class='iffy-nev-text'>#{gst.FilterStHtml(stHtml, wst)}</div>" "<div class='iffy-nev-ui'>#{gst.StHtmlUi(wst)}</div><div class='iffy-nev-text'>#{gst.FilterStHtml(stHtml, wst)}</div>"
@ -443,7 +464,7 @@ class Word
rgverb = [] rgverb = []
for dVerb in @jWord.find("verb") for dVerb in @jWord.find("verb")
nev = new Nev(dVerb) nev = new Nev(dVerb)
if not @gst.FWasRun(nev) and not @gst.story.actorPlayer.FWillAttempt(wst, nev) if not @gst.FWasRun(nev) and not @gst.story.actorPlayer.FWillAttempt(wst, nev) and nev.FConds(wst)
stDisplay = $(dVerb).attr("display") or $(dVerb).attr("name") stDisplay = $(dVerb).attr("display") or $(dVerb).attr("name")
dgActivate = ((dVerbT) => () => @gst.story.actorPlayer.RespondTo(wst.nev, new Nev(dVerbT)))(dVerb) dgActivate = ((dVerbT) => () => @gst.story.actorPlayer.RespondTo(wst.nev, new Nev(dVerbT)))(dVerb)
rgverb.push(new Verb(stDisplay, dgActivate)) rgverb.push(new Verb(stDisplay, dgActivate))