From 5f7f142028ea904392a55019f13f5bdf71c22658 Mon Sep 17 00:00:00 2001 From: Alexander Yakovlev Date: Sun, 24 Jan 2016 13:33:39 +0700 Subject: [PATCH] Did a crude MVC structure. --- game/begin.coffee | 2 +- lib/interface.coffee | 15 --- lib/obj.coffee | 4 +- lib/room.coffee | 41 ++----- lib/salet.coffee | 287 +++++++++++-------------------------------- lib/view.coffee | 184 +++++++++++++++++++++++++++ 6 files changed, 273 insertions(+), 260 deletions(-) delete mode 100644 lib/interface.coffee create mode 100644 lib/view.coffee diff --git a/game/begin.coffee b/game/begin.coffee index f39dda7..e3cd81d 100644 --- a/game/begin.coffee +++ b/game/begin.coffee @@ -4,7 +4,7 @@ obj = require('../../lib/obj.coffee') dialogue = require('../../lib/dialogue.coffee') oneOf = require('../../lib/oneOf.coffee') require('../../lib/interface.coffee') -undum = require('../../lib/undum.js') +salet = require('../../lib/salet.coffee') undum.game.id = "your-game-id-here" undum.game.version = "1.0" diff --git a/lib/interface.coffee b/lib/interface.coffee deleted file mode 100644 index 9cef3a0..0000000 --- a/lib/interface.coffee +++ /dev/null @@ -1,15 +0,0 @@ -### -Salet interface configuration. -### -$(document).ready(() -> - $("#ways").on("click", "a", (event) -> - event.preventDefault() - undum.processClick($(this).attr("href")) - ) - $("#inventory").on("click", "a", (event) -> - event.preventDefault() - ) - $("#load").on("click", "a", (event) -> - window.location.reload() - ) -) diff --git a/lib/obj.coffee b/lib/obj.coffee index f8ba818..49b42ae 100644 --- a/lib/obj.coffee +++ b/lib/obj.coffee @@ -1,5 +1,5 @@ markdown = require('./markdown.coffee') -undum = require('./undum.js') +salet = require('./salet.coffee') objlink = (content, ref) -> return "#{content}" @@ -25,7 +25,7 @@ class SaletObj level: 0 look: (character, system, f) => if @dsc - text = markdown(@dsc.fcall(this, character, system, f)) + text = markdown(@dsc.fcall(this, character, system, f)) text = "" + text + "" # replace braces {{}} with link to _act_ return parsedsc(text, @name) diff --git a/lib/room.coffee b/lib/room.coffee index 927612b..33b994b 100644 --- a/lib/room.coffee +++ b/lib/room.coffee @@ -6,19 +6,6 @@ cycle = require('./cycle.coffee') Random = require('./random.js') languages = require('./localize.coffee') -# Feature detection -hasLocalStorage = () -> - hasStorage = false - try { - hasStorage = ('localStorage' in window) && - window.localStorage !== null && - window.localStorage !== undefined; - } - catch (err) { - hasStorage = false - } - return hasStorage - # Assertion assert = console.assert @@ -40,16 +27,14 @@ cls = (system) -> update_ways = (ways, name) -> content = "" distances = [] - if ways - document.querySelector(".ways h2").style.display = "block" - for way in ways - if undum.game.situations[way]? - title = undum.game.situations[way].title.fcall(this, name) - content += way_to(title, way) - distances.push({ - key: way - distance: undum.game.situations[way].distance - }) + if ways then for way in ways + if undum.game.situations[way]? + title = undum.game.situations[way].title.fcall(this, name) + content += way_to(title, way) + distances.push({ + key: way + distance: undum.game.situations[way].distance + }) else document.querySelector(".ways h2").style.display = "none" document.getElementById("ways").innerHTML = content @@ -85,7 +70,7 @@ class SaletRoom extends undum.Situation visited: 0 title: "Room" objects: {} - + # room illustration image, VN-style. Can be a GIF or WEBM. Can be a function. pic: false @@ -103,7 +88,7 @@ class SaletRoom extends undum.Situation distance: Infinity # distance to the destination clear: true # clear the screen on entering the room? - entering: (character, system, from) => + entering: (character, system, from) => ### I call SaletRoom.exit every time the player exits to another room. @@ -148,13 +133,13 @@ class SaletRoom extends undum.Situation current_situation = "" if not @extendSection classes = if @classes then ' ' + @classes.join(' ') else '' - situation = document.getElementById('current-situation') + situation = document.getElementById('current-room') if situation? situation.removeAttribute('id') # Javascript DOM manipulation functions like jQuery's append() or document.createElement # don't work like a typical printLn - they create *DOM nodes*. # You can't leave an unclosed tag just like that. So we have to buffer the output. - current_situation = "
" + current_situation = "
" if f != @name and @before? current_situation += markdown(@before.fcall(this, character, system, f)) @@ -236,7 +221,7 @@ class SaletRoom extends undum.Situation writer: (ref) -> content = that.writers[ref].fcall(that, character, system, action) output = markdown(content) - system.writeInto(output, '#current-situation') + system.writeInto(output, '#current-room') replacer: (ref) -> content = that.writers[ref].fcall(that, character, system, action) output = ""+content+"" #

tags are usually bad for replacers diff --git a/lib/salet.coffee b/lib/salet.coffee index 9e67365..9cd4325 100644 --- a/lib/salet.coffee +++ b/lib/salet.coffee @@ -1,4 +1,5 @@ markdown = require('./markdown.coffee') +view = require('./view.coffee') ### fcall() (by analogy with fmap) is added to the prototypes of both String and @@ -7,14 +8,15 @@ when called on a String, it only returns the string itself, discarding any input ### Function.prototype.fcall = Function.prototype.call; -String.prototype.fcall = function() { return this; } +String.prototype.fcall = () -> + return this # Utility functions parseFn = (str) -> unless str? return str - + fstr = """ (function(character, system, situation) { #{str} @@ -22,17 +24,16 @@ parseFn = (str) -> """ return eval(fstr) -# Scrolls the top of the screen to the specified point -scrollTopTo = (value) -> - $('html,body').animate({scrollTop: value}, 500) - -# Scrolls the bottom of the screen to the specified point -scrollBottomTo = (value) -> - scrollTopTo(value - $(window).height()) - -# Scrolls all the way to the bottom of the screen -scrollToBottom = () -> - scrollTopTo($('html').height() - $(window).height()); +# Feature detection +hasLocalStorage = () -> + hasStorage = false + try + hasStorage = ('localStorage' in window) && + window.localStorage != null && + window.localStorage != undefined; + catch err + hasStorage = false + return hasStorage # Regular expression to catch every link action. # Salet's default is a general URL-safe expression. @@ -77,8 +78,6 @@ Salet = { # The unique id of the starting room. start: "start" - # --- Hooks --- - ### This function is called at the start of the game. It is normally overridden to provide initial character creation @@ -93,21 +92,21 @@ Salet = { ### This function is called before entering any new situation. It is called before the corresponding situation - has its `enter` method called. + has its `enter` method called. ### entering: (character, system, oldSituationId, newSituationId) -> ### Hook for when the situation has already been carried out and printed. - ### + ### afterEnter: (character, system, oldSituationId, newSituationId) -> ### This function is called before carrying out any action in any situation. It is called before the corresponding situation has its `act` method called. - + If the function returns true, then it is indicating that it has consumed the action, and the action will not be passed on to the situation. Note that this is the only one of @@ -129,88 +128,10 @@ Salet = { ### exit: (character, system, oldSituationId, newSituationId) -> - ### - Removes all content from the page, clearing the main content area. - - If an elementSelector is given, then only that selector will be - cleared. Note that all content from the cleared element is removed, - but the element itself remains, ready to be filled again using - System.write. - ### - clearContent: (elementSelector) -> - document.querySelector(elementSelector).innerHTML = "" - - # jQuery was confused by this point where's the context so I did it vanilla-way - write: (content, elementSelector) -> - if content == "" - return - if typeof content == "function" - content = content() - block = document.getElementById("current-situation") - if block - block.innerHTML = block.innerHTML + markdown(content) - else - console.error("No current situation found.") - - ### - Turns any links that target the given href into plain - text. This can be used to remove action options when an action - is no longer available. It is used automatically when you give - a link the 'once' class. - ### - clearLinks: (code) -> - $("a[href='" + code + "']").each((index, element) -> - a = $(element) - a.replaceWith($("").addClass("ex_link").html(a.html())) - ) - - ### - Given a list of situation ids, this outputs a standard option - block with the situation choices in the given order. - - The contents of each choice will be a link to the situation, - the text of the link will be given by the situation's - outputText property. Note that the canChoose function is - called, and if it returns false, then the text will appear, but - the link will not be clickable. - - Although canChoose is honored, canView and displayOrder are - not. If you need to honor these, you should either do so - manually, ot else use the `getSituationIdChoices` method to - return an ordered list of valid viewable situation ids. - ### - writeChoices: (listOfIds, elementSelector) -> - if (listOfIds.length == 0) - return - - currentSituation = getCurrentSituation(); - $options = $("