0
0
Fork 0
mirror of https://gitlab.com/Oreolek/salet-module.git synced 2024-06-26 03:50:49 +03:00

Made "salet" a global object

This commit is contained in:
Alexander Yakovlev 2016-09-11 17:47:39 +07:00
parent 757c12e2dd
commit e03c9372cd
5 changed files with 56 additions and 59 deletions

View file

@ -1,6 +1,6 @@
{
"name": "salet",
"version": "1.3.4",
"version": "1.4",
"description": "A general client-side framework for cybertext interactive fiction games.",
"keywords": ["ifiction", "interactive fiction", "games", "coffee-script", "text", "menu"],
"homepage": "http://salet.oreolek.ru",

View file

@ -37,7 +37,7 @@ class SaletRoom
Unlike @after this gets called after the section is closed.
It's a styling difference.
###
@exit = (system, to) =>
@exit = (to) =>
return true
###
@ -48,91 +48,90 @@ class SaletRoom
The upstream Undum version does not allow you to redefine @enter function easily but allows custom @exit one.
It was renamed as @entering to achieve API consistency.
###
@enter = (system, from) =>
@enter = (from) =>
return true
###
Salet's Undum version calls Situation.entering every time a situation is entered, and
passes it three arguments; The character object, the system object,
and a string referencing the previous situation, or null if there is
passes it a string referencing the previous situation, or null if there is
none (ie, for the starting situation).
My version of `enter` splits the location description from the effects.
Also if f == this.name (we're in the same location) the `before` and `after` callbacks are ignored.
###
@entering = (system, f) =>
@entering = (f) =>
if (
f != @name and
f? and
system.rooms[f].canExit? and
system.rooms[f].canExit == false
salet.rooms[f]? and
salet.rooms[f].canExit? and
salet.rooms[f].canExit == false
)
return system.goTo(f, f)
return salet.goTo(f)
if @clear and f?
system.view.clearContent()
salet.view.clearContent()
else
system.view.removeTransient()
salet.view.removeTransient()
if f != @name and f?
if f != @name and salet.rooms[f]?
@visited++
if system.rooms[f].exit?
system.rooms[f].exit system, @name
if salet.rooms[f].exit?
salet.rooms[f].exit @name
history.pushState(@name, @title)
if @enter
@enter system, f
@enter f
if not @extendSection
classes = if @classes then ' ' + @classes.join(' ') else ''
room = document.getElementById('current-room')
if room?
room.removeAttribute('id')
system.view.append "<section id='current-room' data-room='#{@name}' class='room-#{@name}#{classes}'></section>"
salet.view.append "<section id='current-room' data-room='#{@name}' class='room-#{@name}#{classes}'></section>"
if f != @name and @before?
system.view.write markdown(@before.fcall(this, system, f))
salet.view.write markdown(@before.fcall(this, f))
system.view.write @look system, f
salet.view.write @look f
if f != @name and @after?
system.view.write markdown(@after.fcall(this, system, f))
salet.view.write markdown(@after.fcall(this, f))
if @beforeChoices?
@beforeChoices.fcall(this, system, f)
@beforeChoices.fcall(this, f)
if @choices
system.view.writeChoices(system, system.getSituationIdChoices(@choices, @maxChoices))
salet.view.writeChoices(salet.getSituationIdChoices(@choices, @maxChoices))
if @afterChoices?
@afterChoices.fcall(this, system, f)
@afterChoices.fcall(this, f)
if system.autosave and @canSave
system.saveGame()
if salet.autosave and @canSave
salet.saveGame()
###
An internal function to get the room's description and the descriptions of
every unit in this room.
###
@look = (system, f) =>
system.view.updateWays(system, @ways, @name)
@look = (f) =>
salet.view.updateWays(@ways, @name)
retval = ""
if @pic
retval += '<div class="pic">'+system.view.pictureTag(@pic.fcall(this, system, f))+'</div>'
retval += '<div class="pic">'+salet.view.pictureTag(@pic.fcall(this, f))+'</div>'
# Print the room description
if @dsc and @dsc != ""
dsc = @dsc.fcall(this, system, f).toString()
dsc = @dsc.fcall(this, f).toString()
retval += markdown(dsc)
unitDescriptions = []
for thing in @units
if thing.name and typeof(thing.look) == "function" and thing.look(system, f)
if thing.name and typeof(thing.look) == "function" and thing.look(f)
unitDescriptions.push ({
order: thing.order,
content: thing.look(system, f)
content: thing.look(f)
})
unitDescriptions.sort((a, b) ->
@ -160,21 +159,21 @@ class SaletRoom
Unit action. A function or a string which comes when you click on a link in unit description.
You could interpret this as an EXAMINE verb or USE one, it's your call.
###
@act = (system, action) =>
@act = (action) =>
if (link = action.match(/^_(act|cycle|inv)_(.+)$/)) #unit action
if link[1] == "inv"
return system.view.write system.character.inv(link[2])
return salet.view.write salet.character.inv(link[2])
for thing in @units
if thing.name == link[2]
if link[1] == "act"
# If it's takeable, the player can take this unit.
# If not, we check the "act" function.
if thing.takeable
system.character.take(thing)
salet.character.take(thing)
@drop link[2]
return system.view.write(thing.take.fcall(thing, system).toString())
return salet.view.write(thing.take.fcall(thing).toString())
else if thing.act?
return system.view.write thing.act.fcall(thing, system)
return salet.view.write thing.act.fcall(thing)
# the loop is done but no return came - match not found
console.error("Could not find #{link[2]} in current room.")
@ -185,16 +184,16 @@ class SaletRoom
responses = {
writer: (ref) ->
content = that.writers[ref].fcall(that, system, action)
content = that.writers[ref].fcall(that, action)
output = markdown(content)
system.view.write(output)
salet.view.write(output)
replacer: (ref) ->
content = that.writers[ref].fcall(that, system, action)
system.view.replace(content, '#'+ref)
content = that.writers[ref].fcall(that, action)
salet.view.replace(content, '#'+ref)
inserter: (ref) ->
content = that.writers[ref].fcall(that, system, action)
content = that.writers[ref].fcall(that, action)
output = markdown(content)
system.view.write(output, '#'+ref)
salet.view.write(output, '#'+ref)
}
if (actionClass)
@ -205,7 +204,7 @@ class SaletRoom
throw new Error("Tried to call undefined writer: #{action}");
responses[responder](ref);
else if (@actions.hasOwnProperty(action))
@actions[action].call(this, system, action);
@actions[action].call(this, action);
else
throw new Error("Tried to call undefined action: #{action}");
@ -222,7 +221,7 @@ class SaletRoom
node.distance = current_room.distance + 1
candidates.push(node)
@register = (salet) =>
@register = () =>
if not @name?
console.error("Situation has no name")
return this
@ -230,7 +229,7 @@ class SaletRoom
return this
@writers = {
cyclewriter: (salet) =>
cyclewriter: () =>
responses = @cycle
if typeof responses == "function"
responses = responses()
@ -248,7 +247,7 @@ class SaletRoom
this[index] = value
return this
window.room = (name, salet, spec) ->
window.room = (name, spec) ->
spec ?= {}
spec.name = name
return new SaletRoom(spec).register(salet)
return new SaletRoom(spec).register()

View file

@ -281,7 +281,7 @@ class Salet
consumed = @beforeAction(room, action)
if consumed != true
room.act(this, action)
room.act(action)
if @afterAction
@afterAction(room, action)
@ -446,7 +446,5 @@ class Salet
return this
salet = new Salet()
salet.view.init(salet)
module.exports = salet
window.salet = new Salet()
salet.view.init()

View file

@ -32,14 +32,14 @@ class SaletUnit
@dsc = (system) => "You see a {{#{@name}}} here." # unit description
@inv = (system) => "It's a #{@name}." # inventory description
@location = ""
@put = (salet, location) =>
@put = (location) =>
@level = 0 # this is scenery
if salet.rooms[location]?
@location = location
salet.rooms[location].take(this)
else
console.log("Could not find location #{location} for a unit #{@name}")
@delete = (salet, location = false) =>
@delete = (location = false) =>
if location == false
location = @location
salet.rooms[location].drop(this)

View file

@ -24,7 +24,7 @@ addClass = (element, className) ->
element.className += ' ' + className
class SaletView
init: (salet) =>
init: () =>
jQuery("#page").on("click", "a", (event) ->
event.preventDefault()
a = $(this)
@ -150,7 +150,7 @@ class SaletView
manually, ot else use the `getSituationIdChoices` method to
return an ordered list of valid viewable situation ids.
###
writeChoices: (salet, listOfIds) =>
writeChoices: (listOfIds) =>
if (not listOfIds? or listOfIds.length == 0)
return
@ -162,12 +162,12 @@ class SaletView
if (room == currentRoom)
continue
optionText = room.optionText.fcall(salet, currentRoom)
optionText = room.optionText.fcall(currentRoom)
if (!optionText)
optionText = "choice".l({number:i+1})
$option = jQuery("<li>")
$a = jQuery("<span>")
if (room.canChoose.fcall(this, salet, currentRoom))
if (room.canChoose.fcall(this, currentRoom))
$a = jQuery("<a>").attr({href: roomId})
$a.html(optionText)
$option.html($a)
@ -256,7 +256,7 @@ class SaletView
block = document.querySelector(selector)
if block
block.style.display = "none"
updateWays: (salet, ways, name) ->
updateWays: (ways, name) ->
if document.getElementById("ways") == null
return
content = ""