mirror of
https://gitlab.com/Oreolek/salet-module.git
synced 2024-06-28 21:05:03 +03:00
Made "salet" a global object
This commit is contained in:
parent
757c12e2dd
commit
e03c9372cd
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "salet",
|
"name": "salet",
|
||||||
"version": "1.3.4",
|
"version": "1.4",
|
||||||
"description": "A general client-side framework for cybertext interactive fiction games.",
|
"description": "A general client-side framework for cybertext interactive fiction games.",
|
||||||
"keywords": ["ifiction", "interactive fiction", "games", "coffee-script", "text", "menu"],
|
"keywords": ["ifiction", "interactive fiction", "games", "coffee-script", "text", "menu"],
|
||||||
"homepage": "http://salet.oreolek.ru",
|
"homepage": "http://salet.oreolek.ru",
|
||||||
|
|
|
@ -37,7 +37,7 @@ class SaletRoom
|
||||||
Unlike @after this gets called after the section is closed.
|
Unlike @after this gets called after the section is closed.
|
||||||
It's a styling difference.
|
It's a styling difference.
|
||||||
###
|
###
|
||||||
@exit = (system, to) =>
|
@exit = (to) =>
|
||||||
return true
|
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.
|
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.
|
It was renamed as @entering to achieve API consistency.
|
||||||
###
|
###
|
||||||
@enter = (system, from) =>
|
@enter = (from) =>
|
||||||
return true
|
return true
|
||||||
|
|
||||||
###
|
###
|
||||||
Salet's Undum version calls Situation.entering every time a situation is entered, and
|
Salet's Undum version calls Situation.entering every time a situation is entered, and
|
||||||
passes it three arguments; The character object, the system object,
|
passes it a string referencing the previous situation, or null if there is
|
||||||
and a string referencing the previous situation, or null if there is
|
|
||||||
none (ie, for the starting situation).
|
none (ie, for the starting situation).
|
||||||
|
|
||||||
My version of `enter` splits the location description from the effects.
|
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.
|
Also if f == this.name (we're in the same location) the `before` and `after` callbacks are ignored.
|
||||||
###
|
###
|
||||||
@entering = (system, f) =>
|
@entering = (f) =>
|
||||||
if (
|
if (
|
||||||
f != @name and
|
f != @name and
|
||||||
f? and
|
salet.rooms[f]? and
|
||||||
system.rooms[f].canExit? and
|
salet.rooms[f].canExit? and
|
||||||
system.rooms[f].canExit == false
|
salet.rooms[f].canExit == false
|
||||||
)
|
)
|
||||||
return system.goTo(f, f)
|
return salet.goTo(f)
|
||||||
|
|
||||||
if @clear and f?
|
if @clear and f?
|
||||||
system.view.clearContent()
|
salet.view.clearContent()
|
||||||
else
|
else
|
||||||
system.view.removeTransient()
|
salet.view.removeTransient()
|
||||||
|
|
||||||
if f != @name and f?
|
if f != @name and salet.rooms[f]?
|
||||||
@visited++
|
@visited++
|
||||||
if system.rooms[f].exit?
|
if salet.rooms[f].exit?
|
||||||
system.rooms[f].exit system, @name
|
salet.rooms[f].exit @name
|
||||||
|
|
||||||
history.pushState(@name, @title)
|
history.pushState(@name, @title)
|
||||||
|
|
||||||
if @enter
|
if @enter
|
||||||
@enter system, f
|
@enter f
|
||||||
|
|
||||||
if not @extendSection
|
if not @extendSection
|
||||||
classes = if @classes then ' ' + @classes.join(' ') else ''
|
classes = if @classes then ' ' + @classes.join(' ') else ''
|
||||||
room = document.getElementById('current-room')
|
room = document.getElementById('current-room')
|
||||||
if room?
|
if room?
|
||||||
room.removeAttribute('id')
|
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?
|
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?
|
if f != @name and @after?
|
||||||
system.view.write markdown(@after.fcall(this, system, f))
|
salet.view.write markdown(@after.fcall(this, f))
|
||||||
|
|
||||||
if @beforeChoices?
|
if @beforeChoices?
|
||||||
@beforeChoices.fcall(this, system, f)
|
@beforeChoices.fcall(this, f)
|
||||||
|
|
||||||
if @choices
|
if @choices
|
||||||
system.view.writeChoices(system, system.getSituationIdChoices(@choices, @maxChoices))
|
salet.view.writeChoices(salet.getSituationIdChoices(@choices, @maxChoices))
|
||||||
|
|
||||||
if @afterChoices?
|
if @afterChoices?
|
||||||
@afterChoices.fcall(this, system, f)
|
@afterChoices.fcall(this, f)
|
||||||
|
|
||||||
if system.autosave and @canSave
|
if salet.autosave and @canSave
|
||||||
system.saveGame()
|
salet.saveGame()
|
||||||
|
|
||||||
###
|
###
|
||||||
An internal function to get the room's description and the descriptions of
|
An internal function to get the room's description and the descriptions of
|
||||||
every unit in this room.
|
every unit in this room.
|
||||||
###
|
###
|
||||||
@look = (system, f) =>
|
@look = (f) =>
|
||||||
system.view.updateWays(system, @ways, @name)
|
salet.view.updateWays(@ways, @name)
|
||||||
retval = ""
|
retval = ""
|
||||||
|
|
||||||
if @pic
|
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
|
# Print the room description
|
||||||
if @dsc and @dsc != ""
|
if @dsc and @dsc != ""
|
||||||
dsc = @dsc.fcall(this, system, f).toString()
|
dsc = @dsc.fcall(this, f).toString()
|
||||||
retval += markdown(dsc)
|
retval += markdown(dsc)
|
||||||
|
|
||||||
unitDescriptions = []
|
unitDescriptions = []
|
||||||
for thing in @units
|
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 ({
|
unitDescriptions.push ({
|
||||||
order: thing.order,
|
order: thing.order,
|
||||||
content: thing.look(system, f)
|
content: thing.look(f)
|
||||||
})
|
})
|
||||||
|
|
||||||
unitDescriptions.sort((a, b) ->
|
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.
|
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.
|
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 = action.match(/^_(act|cycle|inv)_(.+)$/)) #unit action
|
||||||
if link[1] == "inv"
|
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
|
for thing in @units
|
||||||
if thing.name == link[2]
|
if thing.name == link[2]
|
||||||
if link[1] == "act"
|
if link[1] == "act"
|
||||||
# If it's takeable, the player can take this unit.
|
# If it's takeable, the player can take this unit.
|
||||||
# If not, we check the "act" function.
|
# If not, we check the "act" function.
|
||||||
if thing.takeable
|
if thing.takeable
|
||||||
system.character.take(thing)
|
salet.character.take(thing)
|
||||||
@drop link[2]
|
@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?
|
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
|
# the loop is done but no return came - match not found
|
||||||
console.error("Could not find #{link[2]} in current room.")
|
console.error("Could not find #{link[2]} in current room.")
|
||||||
|
@ -185,16 +184,16 @@ class SaletRoom
|
||||||
|
|
||||||
responses = {
|
responses = {
|
||||||
writer: (ref) ->
|
writer: (ref) ->
|
||||||
content = that.writers[ref].fcall(that, system, action)
|
content = that.writers[ref].fcall(that, action)
|
||||||
output = markdown(content)
|
output = markdown(content)
|
||||||
system.view.write(output)
|
salet.view.write(output)
|
||||||
replacer: (ref) ->
|
replacer: (ref) ->
|
||||||
content = that.writers[ref].fcall(that, system, action)
|
content = that.writers[ref].fcall(that, action)
|
||||||
system.view.replace(content, '#'+ref)
|
salet.view.replace(content, '#'+ref)
|
||||||
inserter: (ref) ->
|
inserter: (ref) ->
|
||||||
content = that.writers[ref].fcall(that, system, action)
|
content = that.writers[ref].fcall(that, action)
|
||||||
output = markdown(content)
|
output = markdown(content)
|
||||||
system.view.write(output, '#'+ref)
|
salet.view.write(output, '#'+ref)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (actionClass)
|
if (actionClass)
|
||||||
|
@ -205,7 +204,7 @@ class SaletRoom
|
||||||
throw new Error("Tried to call undefined writer: #{action}");
|
throw new Error("Tried to call undefined writer: #{action}");
|
||||||
responses[responder](ref);
|
responses[responder](ref);
|
||||||
else if (@actions.hasOwnProperty(action))
|
else if (@actions.hasOwnProperty(action))
|
||||||
@actions[action].call(this, system, action);
|
@actions[action].call(this, action);
|
||||||
else
|
else
|
||||||
throw new Error("Tried to call undefined action: #{action}");
|
throw new Error("Tried to call undefined action: #{action}");
|
||||||
|
|
||||||
|
@ -222,7 +221,7 @@ class SaletRoom
|
||||||
node.distance = current_room.distance + 1
|
node.distance = current_room.distance + 1
|
||||||
candidates.push(node)
|
candidates.push(node)
|
||||||
|
|
||||||
@register = (salet) =>
|
@register = () =>
|
||||||
if not @name?
|
if not @name?
|
||||||
console.error("Situation has no name")
|
console.error("Situation has no name")
|
||||||
return this
|
return this
|
||||||
|
@ -230,7 +229,7 @@ class SaletRoom
|
||||||
return this
|
return this
|
||||||
|
|
||||||
@writers = {
|
@writers = {
|
||||||
cyclewriter: (salet) =>
|
cyclewriter: () =>
|
||||||
responses = @cycle
|
responses = @cycle
|
||||||
if typeof responses == "function"
|
if typeof responses == "function"
|
||||||
responses = responses()
|
responses = responses()
|
||||||
|
@ -248,7 +247,7 @@ class SaletRoom
|
||||||
this[index] = value
|
this[index] = value
|
||||||
return this
|
return this
|
||||||
|
|
||||||
window.room = (name, salet, spec) ->
|
window.room = (name, spec) ->
|
||||||
spec ?= {}
|
spec ?= {}
|
||||||
spec.name = name
|
spec.name = name
|
||||||
return new SaletRoom(spec).register(salet)
|
return new SaletRoom(spec).register()
|
||||||
|
|
|
@ -281,7 +281,7 @@ class Salet
|
||||||
consumed = @beforeAction(room, action)
|
consumed = @beforeAction(room, action)
|
||||||
|
|
||||||
if consumed != true
|
if consumed != true
|
||||||
room.act(this, action)
|
room.act(action)
|
||||||
|
|
||||||
if @afterAction
|
if @afterAction
|
||||||
@afterAction(room, action)
|
@afterAction(room, action)
|
||||||
|
@ -446,7 +446,5 @@ class Salet
|
||||||
|
|
||||||
return this
|
return this
|
||||||
|
|
||||||
salet = new Salet()
|
window.salet = new Salet()
|
||||||
salet.view.init(salet)
|
salet.view.init()
|
||||||
|
|
||||||
module.exports = salet
|
|
||||||
|
|
|
@ -32,14 +32,14 @@ class SaletUnit
|
||||||
@dsc = (system) => "You see a {{#{@name}}} here." # unit description
|
@dsc = (system) => "You see a {{#{@name}}} here." # unit description
|
||||||
@inv = (system) => "It's a #{@name}." # inventory description
|
@inv = (system) => "It's a #{@name}." # inventory description
|
||||||
@location = ""
|
@location = ""
|
||||||
@put = (salet, location) =>
|
@put = (location) =>
|
||||||
@level = 0 # this is scenery
|
@level = 0 # this is scenery
|
||||||
if salet.rooms[location]?
|
if salet.rooms[location]?
|
||||||
@location = location
|
@location = location
|
||||||
salet.rooms[location].take(this)
|
salet.rooms[location].take(this)
|
||||||
else
|
else
|
||||||
console.log("Could not find location #{location} for a unit #{@name}")
|
console.log("Could not find location #{location} for a unit #{@name}")
|
||||||
@delete = (salet, location = false) =>
|
@delete = (location = false) =>
|
||||||
if location == false
|
if location == false
|
||||||
location = @location
|
location = @location
|
||||||
salet.rooms[location].drop(this)
|
salet.rooms[location].drop(this)
|
||||||
|
|
|
@ -24,7 +24,7 @@ addClass = (element, className) ->
|
||||||
element.className += ' ' + className
|
element.className += ' ' + className
|
||||||
|
|
||||||
class SaletView
|
class SaletView
|
||||||
init: (salet) =>
|
init: () =>
|
||||||
jQuery("#page").on("click", "a", (event) ->
|
jQuery("#page").on("click", "a", (event) ->
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
a = $(this)
|
a = $(this)
|
||||||
|
@ -150,7 +150,7 @@ class SaletView
|
||||||
manually, ot else use the `getSituationIdChoices` method to
|
manually, ot else use the `getSituationIdChoices` method to
|
||||||
return an ordered list of valid viewable situation ids.
|
return an ordered list of valid viewable situation ids.
|
||||||
###
|
###
|
||||||
writeChoices: (salet, listOfIds) =>
|
writeChoices: (listOfIds) =>
|
||||||
if (not listOfIds? or listOfIds.length == 0)
|
if (not listOfIds? or listOfIds.length == 0)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -162,12 +162,12 @@ class SaletView
|
||||||
if (room == currentRoom)
|
if (room == currentRoom)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
optionText = room.optionText.fcall(salet, currentRoom)
|
optionText = room.optionText.fcall(currentRoom)
|
||||||
if (!optionText)
|
if (!optionText)
|
||||||
optionText = "choice".l({number:i+1})
|
optionText = "choice".l({number:i+1})
|
||||||
$option = jQuery("<li>")
|
$option = jQuery("<li>")
|
||||||
$a = jQuery("<span>")
|
$a = jQuery("<span>")
|
||||||
if (room.canChoose.fcall(this, salet, currentRoom))
|
if (room.canChoose.fcall(this, currentRoom))
|
||||||
$a = jQuery("<a>").attr({href: roomId})
|
$a = jQuery("<a>").attr({href: roomId})
|
||||||
$a.html(optionText)
|
$a.html(optionText)
|
||||||
$option.html($a)
|
$option.html($a)
|
||||||
|
@ -256,7 +256,7 @@ class SaletView
|
||||||
block = document.querySelector(selector)
|
block = document.querySelector(selector)
|
||||||
if block
|
if block
|
||||||
block.style.display = "none"
|
block.style.display = "none"
|
||||||
updateWays: (salet, ways, name) ->
|
updateWays: (ways, name) ->
|
||||||
if document.getElementById("ways") == null
|
if document.getElementById("ways") == null
|
||||||
return
|
return
|
||||||
content = ""
|
content = ""
|
||||||
|
|
Loading…
Reference in a new issue