From 07b994cc7fdf0763f5561bbe562dde64d856eaa3 Mon Sep 17 00:00:00 2001 From: Alexander Yakovlev Date: Mon, 8 Feb 2016 19:12:12 +0700 Subject: [PATCH] Examination and syntax rewrites --- html/index.html | 16 ---------------- lib/obj.coffee | 7 ++++--- lib/room.coffee | 37 +++++++++++++++++++++++++++---------- lib/salet.coffee | 35 ++++++++++++++++------------------- lib/view.coffee | 34 ++++++++++++++++++++++++++++++---- 5 files changed, 77 insertions(+), 52 deletions(-) diff --git a/html/index.html b/html/index.html index 76e613b..e697423 100644 --- a/html/index.html +++ b/html/index.html @@ -89,22 +89,6 @@ -
-
- - -
- -
-

-
-
-
- -
-
- -
diff --git a/lib/obj.coffee b/lib/obj.coffee index c0c5bae..5443135 100644 --- a/lib/obj.coffee +++ b/lib/obj.coffee @@ -22,11 +22,12 @@ class SaletObj return null for key, value of spec this[key] = value - level: 0 + level: 0 # if > 0 it's hidden + order: 0 # you can use this to sort the descriptions look: (system, f) => - if @dsc + if @dsc and @dsc != "" text = markdown(@dsc.fcall(this, system, f).toString()) - text = "" + text + "" + text = system.view.wrapLevel(text, @level) # replace braces {{}} with link to _act_ return parsedsc(text, @name) takeable: false diff --git a/lib/room.coffee b/lib/room.coffee index 587c00e..43e75d6 100644 --- a/lib/room.coffee +++ b/lib/room.coffee @@ -126,12 +126,25 @@ class SaletRoom retval += '
'+system.view.pictureTag(@pic.fcall(this, system, f))+'
' # Print the room description - if @dsc + if @dsc and @dsc != "" dsc = @dsc.fcall(this, system, f).toString() retval += markdown(dsc) - for name, thing of @objects - retval += thing.look() + objDescriptions = [] + for thing in @objects + console.log thing + if thing.name and typeof(thing.look) == "function" and thing.level == 0 and thing.look(system, f) + objDescriptions.push ({ + order: thing.order, + content: thing.look(system, f) + }) + + objDescriptions.sort((a, b) -> + return a.order - b.order + ) + + for description in objDescriptions + retval += description.content return retval @@ -153,8 +166,8 @@ class SaletRoom ### act: (system, action) => if (link = action.match(/^_(act|cycle)_(.+)$/)) #object action - for name, thing of @objects - if name == link[2] + for thing in @objects + if thing.name == link[2] if link[1] == "act" # If it's takeable, the player can take this object. # If not, we check the "act" function. @@ -165,11 +178,15 @@ class SaletRoom @entering.fcall(this, system, @name) return system.view.write(thing.take.fcall(thing, system).toString()) if thing.act - return system.view.write(thing.act.fcall(thing, system).toString()) - elseif link[1] == "cycle" - # TODO object cyclewriter + system.view.changeLevel(thing.level) + return system.view.write( + system.view.wrapLevel( + thing.act.fcall(thing, system).toString(), + thing.level + ) + ) # the loop is done but no return came - match not found - console.error("Could not find #{link[1]} in current room.") + console.error("Could not find #{link[2]} in current room.") # we're done with objects, now check the regular actions actionClass = action.match(/^_(\w+)_(.+)$/) @@ -222,7 +239,7 @@ class SaletRoom return this writers: - cyclewriter: (salet) -> + cyclewriter: (salet) => responses = @cycle if typeof responses == "function" responses = responses() diff --git a/lib/salet.coffee b/lib/salet.coffee index 64e4cb2..d3fe645 100644 --- a/lib/salet.coffee +++ b/lib/salet.coffee @@ -138,7 +138,7 @@ class Salet Before this function returns its result, it sorts the situations in increasing order of their displayOrder values. ### - getSituationIdChoices: (listOfOrOneIdsOrTags, maxChoices) -> + getSituationIdChoices: (listOfOrOneIdsOrTags, maxChoices) => datum = null i = 0 @@ -228,7 +228,7 @@ class Salet return null # Gets the unique id used to identify saved games. - getSaveId: (slot = "") -> + getSaveId: (slot = "") => return 'salet_'+@game_id+'_'+@game_version#+'_'+slot # This gets called when a link needs to be followed, regardless @@ -259,13 +259,16 @@ class Salet # We're able to save, if we weren't already. @view.enableSaving() + goTo: (roomId) => + return @processLink(roomId) + ### This gets called to actually do the work of processing a code. When one doLink is called (or a link is clicked), this may set call code that further calls doLink, and so on. This method processes each one, and processLink manages this. ### - processOneLink: (code) -> + processOneLink: (code) => match = code.match(@linkRe) assert(match, "link_not_valid".l({link:code})) @@ -292,14 +295,14 @@ class Salet @afterAction(this, room, action) # This gets called when the user clicks a link to carry out an action. - processClick: (code) -> + processClick: (code) => now = (new Date()).getTime() * 0.001 @time = now - @startTime @progress.sequence.push({link:code, when:@time}) @processLink(code) # Transitions between situations. - doTransitionTo: (newRoomId) -> + doTransitionTo: (newRoomId) => oldRoomId = @current oldRoom = @getCurrentRoom() newRoom = @rooms[newRoomId] @@ -348,7 +351,7 @@ class Salet return result # Saves the character and the walking history to local storage. - saveGame: () -> + saveGame: () => # Store when we're saving the game, to avoid exploits where a # player loads their file to gain extra time. now = (new Date()).getTime() * 0.001 @@ -366,7 +369,7 @@ class Salet @view.enableLoading() # Loads the game from the given data - loadGame: (saveFile) -> + loadGame: (saveFile) => @progress = saveFile.progress @character = saveFile.character @@ -395,7 +398,9 @@ class Salet view: new SaletView - beginGame: () -> + beginGame: () => + @view.fixClicks() + # Handle storage. saveFile = false if (@view.hasLocalStorage()) @@ -425,21 +430,13 @@ class Salet # Do the first state. @doTransitionTo(@start) - # Any point that an option list appears, its options are its first links. - $("body").on('click', "ul.options li, #menu li", (event) -> - # Make option clicks pass through to their first link. - link = $("a", this) - if (link.length > 0) - $(link.get(0)).click() - ) - - getRoom: (name) -> + getRoom: (name) => return @rooms[name] # Just an alias for getCurrentRoom - here: () -> @getCurrentRoom() + here: () => @getCurrentRoom() - isVisited: (name) -> + isVisited: (name) => place = @getRoom(name) if place return Boolean place.visited diff --git a/lib/view.coffee b/lib/view.coffee index 2fdbc7b..952995d 100644 --- a/lib/view.coffee +++ b/lib/view.coffee @@ -23,7 +23,7 @@ addClass = (element, className) -> element.className += ' ' + className class SaletView - init: (salet) -> + init: (salet) => $("#content, #ways").on("click", "a", (event) -> event.preventDefault() a = $(this) @@ -95,7 +95,7 @@ class SaletView return content.toString() # Write content to current room - write: (content, elementSelector = "#current-room") -> + write: (content, elementSelector = "#current-room") => if content == "" return content = @prepareContent(content) @@ -109,7 +109,7 @@ class SaletView # Replaces the text in the given block with the given text. # !! Does not call markdown on the provided text. !! - replace: (content, elementSelector) -> + replace: (content, elementSelector) => if content == "" return content = @prepareContent(content) @@ -143,7 +143,7 @@ class SaletView manually, ot else use the `getSituationIdChoices` method to return an ordered list of valid viewable situation ids. ### - writeChoices: (salet, listOfIds) -> + writeChoices: (salet, listOfIds) => if (not listOfIds? or listOfIds.length == 0) return @@ -192,6 +192,23 @@ class SaletView else contentToHide.remove() + # Remove every section marked as a different level. + # For a link level 0, we hide every link of level 1 and above. + # It's for the player to focus. + changeLevel: (level) => + maxLevel = 6 + if level < maxLevel + i = level + 1 + hideArray = [] + while i <= maxLevel + hideArray.push("#content .lvl"+i) + i++ + directive = hideArray.join(", ") + $(directive).fadeOut("slow") + + wrapLevel: (text, level) => + return ""+text+'' + # At last, we scroll the view so that .new objects are in view. endOutputTransaction: () => if !@interactive @@ -232,6 +249,15 @@ class SaletView hasLocalStorage: () -> return window.localStorage? + # Any point that an option list appears, its options are its first links. + fixClicks: () -> + $("body").on('click', "ul.options li", (event) -> + # Make option clicks pass through to their first link. + link = $("a", this) + if (link.length > 0) + $(link.get(0)).click() + ) + updateWays: (salet, ways, name) -> content = "" distances = []