From 2c8380a776cd352d10e8911db0694010bb9b56ce Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Thu, 3 Mar 2011 07:41:55 -0800 Subject: [PATCH] add conditionals, variable setting to nev primitives --- iffy.coffee | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/iffy.coffee b/iffy.coffee index ae9ee8a..70c2b15 100644 --- a/iffy.coffee +++ b/iffy.coffee @@ -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 # the resultant effect on the state of the model of the world class Nev + @RgstelemReserved = ["cond", "set", "donext"] constructor: (@dNev) -> ID: () -> if not @id? @@ -305,9 +306,23 @@ class Nev return true wst = wst.WstPrev() 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")? 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) -> stSectionNext = $(@dNev).attr("nextsection") if stSectionNext? @@ -317,7 +332,13 @@ class Nev StHtmlDisplay: (gst, wst) -> jdivTmp = $("
") for dHTML in $(@dNev).contents() - jdivTmp.append(document.importNode(dHTML, true)) + fReserved = false + for stElemReserved in Nev.RgstelemReserved + if ($.nodeName(dHTML, stElemReserved)) + fReserved = true + break + if not fReserved + jdivTmp.append(document.importNode(dHTML, true)) stHtml = jdivTmp[0].innerHTML "
#{gst.StHtmlUi(wst)}
#{gst.FilterStHtml(stHtml, wst)}
" @@ -443,7 +464,7 @@ class Word rgverb = [] for dVerb in @jWord.find("verb") 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") dgActivate = ((dVerbT) => () => @gst.story.actorPlayer.RespondTo(wst.nev, new Nev(dVerbT)))(dVerb) rgverb.push(new Verb(stDisplay, dgActivate))