diff --git a/game/story.coffee b/game/story.coffee index 3d54a10..dbacade 100644 --- a/game/story.coffee +++ b/game/story.coffee @@ -21,7 +21,7 @@ room "plaza", salet, return "Town plaza" cycle: ["quirky", "distinct", "kooky", "crazy", "quaint"] ways: ["shop"] - before: (character, system, from) -> + before: (system, from) -> if from == 'world' """ You climb up the well and come out to a central plaza of a #{cyclelink("quaint")} little town. @@ -32,11 +32,11 @@ room "plaza", salet, objects: policeman: obj "policeman", dsc: "There is a policeman nearby. You could ask him {{for directions.}}" - act: (character) -> - if character.sandbox.has_mark? + act: (salet) -> + if salet.character.has_mark? return "You already talked to him, no need to bug the man twice." - character.sandbox.has_mark ?= true - get_room("lair").destination() + salet.character.has_mark ?= true + salet.getRoom("lair").destination() """ “Here, let me mark it on your map.” """ @@ -65,8 +65,8 @@ room "lair", salet, bugg: obj "bugg", dsc: "You see a particularly beautiful slimy {{bugg.}}" takeable: false - act: () => - here().drop(@name) + act: (salet) => + salet.here().drop(@name) return "You eat the bugg mass. Delicious and raw. Perhaps it's a good lair to live in." dialogue "Yes", salet, "merchant", "merchant", """ @@ -85,8 +85,8 @@ room "shop-inside", salet, merchant: obj "merchant", dsc: "A {{merchant}} eyes you warily." takeable: false - act: (character, system) => - undum.processClick("merchdialogue") + act: (system) => + salet.processClick("merchdialogue") return "" ### diff --git a/lib/cycle.coffee b/lib/cycle.coffee index 6d3f94f..72da16e 100644 --- a/lib/cycle.coffee +++ b/lib/cycle.coffee @@ -8,12 +8,12 @@ cyclelink = (content) -> cycle = (responses, name, character) -> if typeof responses == "function" responses = responses() - character.sandbox.cycle_index ?= [] # initialize with empty array - character.sandbox.cycle_index[name] ?= 0 # initialize with 0 - response = responses[character.sandbox.cycle_index[name]] - character.sandbox.cycle_index[name]++ - if character.sandbox.cycle_index[name] == responses.length - character.sandbox.cycle_index[name] = 0 + character.cycle_index ?= [] # initialize with empty array + character.cycle_index[name] ?= 0 # initialize with 0 + response = responses[character.cycle_index[name]] + character.cycle_index[name]++ + if character.cycle_index[name] == responses.length + character.cycle_index[name] = 0 return cyclelink(response) module.exports = cycle diff --git a/lib/obj.coffee b/lib/obj.coffee index 4c93150..c0c5bae 100644 --- a/lib/obj.coffee +++ b/lib/obj.coffee @@ -23,17 +23,17 @@ class SaletObj for key, value of spec this[key] = value level: 0 - look: (character, system, f) => + look: (system, f) => if @dsc - text = markdown(@dsc.fcall(this, character, system, f)) + text = markdown(@dsc.fcall(this, system, f).toString()) text = "" + text + "" # replace braces {{}} with link to _act_ return parsedsc(text, @name) takeable: false - take: (character, system) => "You take the #{@name}." # taking to inventory - act: (character, system) => "You don't find anything extraordinary about the #{@name}." # object action - dsc: (character, system) => "You see a {{#{@name}}} here." # object description - inv: (character, system) => "It's a {{#{@name}.}}" # inventory description + take: (system) => "You take the #{@name}." # taking to inventory + act: (system) => "You don't find anything extraordinary about the #{@name}." # object action + dsc: (system) => "You see a {{#{@name}}} here." # object description + inv: (system) => "It's a {{#{@name}.}}" # inventory description location: "" put: (location) => @level = 0 # this is scenery diff --git a/lib/room.coffee b/lib/room.coffee index a84fac6..8155ecf 100644 --- a/lib/room.coffee +++ b/lib/room.coffee @@ -5,20 +5,19 @@ obj = require('./obj.coffee') markdown = require('./markdown.coffee') cycle = require('./cycle.coffee') -# Assertion assert = (msg, assertion) -> console.assert assertion, msg +Function.prototype.fcall = Function.prototype.call; +Boolean.prototype.fcall = () -> + return this +String.prototype.fcall = () -> + return this + way_to = (content, ref) -> return "#{content}" Array::remove = (e) -> @[t..t] = [] if (t = @indexOf(e)) > -1 -addClass = (element, className) -> - if (element.classList) - element.classList.add(className) - else - element.className += ' ' + className - class SaletRoom constructor: (spec) -> for index, value of spec @@ -126,7 +125,8 @@ class SaletRoom # Print the room description if @dsc - retval += markdown(@dsc.fcall(this, system, f)) + dsc = @dsc.fcall(this, system, f).toString() + retval += markdown(dsc) for name, thing of @objects retval += thing.look() @@ -157,13 +157,13 @@ class SaletRoom # If it's takeable, the player can take this object. # If not, we check the "act" function. if thing.takeable - character.sandbox.inventory.push thing + system.character.inventory.push thing @drop name - cls(system) - @entering.fcall(this, character, system, @name) - return print(thing.take.fcall(thing, character, system)) + system.view.clearContent() + @entering.fcall(this, system, @name) + return system.view.write(thing.take.fcall(thing, system).toString()) if thing.act - return print(thing.act.fcall(thing, character, system)) + return system.view.write(thing.act.fcall(thing, system).toString()) elseif link[1] == "cycle" # TODO object cyclewriter # the loop is done but no return came - match not found @@ -177,15 +177,15 @@ class SaletRoom writer: (ref) -> content = that.writers[ref].fcall(that, system, action) output = markdown(content) - system.writeInto(output, '#current-room') + system.view.write(output) replacer: (ref) -> content = that.writers[ref].fcall(that, system, action) output = ""+content+"" #

tags are usually bad for replacers - system.replaceWith(output, '#'+ref) + system.view.replace(output, '#'+ref) inserter: (ref) -> content = that.writers[ref].fcall(that, system, action) output = markdown(content) - system.writeInto(output, '#'+ref) + system.view.write(output, '#'+ref) } if (actionClass) @@ -221,8 +221,8 @@ class SaletRoom return this writers: - cyclewriter: (character) -> - cycle(this.cycle, this.name, character) + cyclewriter: (salet) -> + cycle(this.cycle, this.name, salet.character) room = (name, salet, spec) -> spec ?= {} diff --git a/lib/salet.coffee b/lib/salet.coffee index 7fe80c7..c3d7784 100644 --- a/lib/salet.coffee +++ b/lib/salet.coffee @@ -18,19 +18,7 @@ String.prototype.fcall = () -> assert = (msg, assertion) -> console.assert assertion, msg class Character - -# Utility functions - -parseFn = (str) -> - unless str? - return str - - fstr = """ -(function(character, situation) { -#{str} -#}) -""" - return eval(fstr) + inventory: [] # Regular expression to catch every link action. # Salet's default is a general URL-safe expression. @@ -257,8 +245,8 @@ class Salet # Handle each link in turn. @processOneLink(code); while (@linkStack.length > 0) - code = linkStack.shift() - processOneLink(code) + code = @linkStack.shift() + @processOneLink(code) # We're done, so remove the stack to prevent future pushes. @linkStack = null; @@ -291,22 +279,15 @@ class Salet # Carry out the action if (action) - situation = @getCurrentRoom() - if (situation) - if (@beforeAction) - # Try the global act handler, and see if we need - # to notify the situation. - consumed = @beforeAction( - character, current, action - ) - if (consumed != true) - situation.act(character, action) - else - # We have no global act handler, always notify the situation. - situation.act(character, action) + room = @getCurrentRoom() + if (room and @beforeAction) + # Try the global act handler + consumed = @beforeAction(room, action) + if (consumed != true) + room.act(this, action) - if (@afterAction) - @afterAction(character, current, action) + if (@afterAction) + @afterAction(this, room, action) # This gets called when the user clicks a link to carry out an action. processClick: (code) -> diff --git a/lib/view.coffee b/lib/view.coffee index 24f9f6d..95fa947 100644 --- a/lib/view.coffee +++ b/lib/view.coffee @@ -16,6 +16,12 @@ assert = (msg, assertion) -> console.assert assertion, msg way_to = (content, ref) -> return "#{content}" +addClass = (element, className) -> + if (element.classList) + element.classList.add(className) + else + element.className += ' ' + className + class SaletView init: (salet) -> $("#content, #ways").on("click", "a", (event) -> @@ -70,15 +76,19 @@ class SaletView document.getElementById("intro").innerHTML = "" document.querySelector(elementSelector).innerHTML = "" - # Write content to current room - write: (content) -> - if content == "" - return + prepareContent: (content) -> if typeof content == "function" content = content() if content instanceof jQuery content = content[0].outerHTML - block = document.getElementById("current-room") + return content.toString() + + # Write content to current room + write: (content, elementSelector = "#current-room") -> + if content == "" + return + content = @prepareContent(content) + block = document.querySelector(elementSelector) if block block.innerHTML = block.innerHTML + markdown(content) else @@ -86,6 +96,14 @@ class SaletView block = document.getElementById("content") block.innerHTML = content + # Replaces the text in the given block with the given text. + # !! Does not call markdown on the provided text. !! + replace: (content, elementSelector) -> + if content == "" + return + content = @prepareContent(content) + block = document.querySelector(elementSelector) + block.innerHTML = content ### Turns any links that target the given href into plain text. This can be used to remove action options when an action