1
0
Fork 0
mirror of https://gitlab.com/Oreolek/salet.git synced 2024-07-04 07:45:03 +03:00

Localization supports functions, init fixes

This commit is contained in:
Alexander Yakovlev 2016-02-13 10:20:56 +07:00
parent 84d6538e61
commit ba9b59b53a
3 changed files with 25 additions and 20 deletions

View file

@ -49,13 +49,14 @@ String.prototype.l = (args) ->
# Find the localized form. # Find the localized form.
localized = localize(lang, this) localized = localize(lang, this)
if typeof(localized) == "function"
# Merge in any replacement content. localized = localized(args)
if args else # Merge in any replacement content.
for name in args if args
localized = localized.replace( for name in args
new RegExp("\\{"+name+"\\}"), args[name] localized = localized.replace(
) new RegExp("\\{"+name+"\\}"), args[name]
)
return localized return localized
module.exports = languages; module.exports = languages;

View file

@ -166,5 +166,7 @@ oneOf = (ary...) ->
Array.prototype.oneOf = () -> Array.prototype.oneOf = () ->
oneOf.apply(null, this) oneOf.apply(null, this)
String.prototype.oneOf = () ->
return this
module.exports = oneOf; module.exports = oneOf;

View file

@ -2,7 +2,7 @@ markdown = require('./markdown.coffee')
SaletView = require('./view.coffee') SaletView = require('./view.coffee')
Random = require('./random.js') Random = require('./random.js')
character = require('./character.coffee') character = require('./character.coffee')
languages = require('./localize.coffee') require('./localize.coffee')
### ###
fcall() (by analogy with fmap) is added to the prototypes of both String and fcall() (by analogy with fmap) is added to the prototypes of both String and
@ -27,12 +27,12 @@ There is only one instance of this class.
class Salet class Salet
constructor: (spec) -> constructor: (spec) ->
@character = character() @character = character()
# REDEFINE THIS IN YOUR GAME # REDEFINE THIS IN YOUR GAME
@game_id = null @game_id = null
@game_version = "1.0" @game_version = "1.0"
@autosave = true @autosave = true
@rnd = null @rnd = null
@time = 0 @time = 0
@ -230,7 +230,7 @@ class Salet
return return
@view.mark_all_links_old @view.mark_all_links_old
# We're processing, so make the stack available. # We're processing, so make the stack available.
@linkStack = [] @linkStack = []
@ -296,15 +296,16 @@ class Salet
oldRoomId = @current oldRoomId = @current
oldRoom = @getCurrentRoom() oldRoom = @getCurrentRoom()
newRoom = @rooms[newRoomId] newRoom = @rooms[newRoomId]
console.log @rooms, newRoomId
assert(newRoom, "unknown_situation".l({id:newRoomId})) assert(newRoom, "unknown_situation".l({id:newRoomId}))
# We might not have an old situation if this is the start of the game. # We might not have an old situation if this is the start of the game.
if (oldRoom and @exit) if (oldRoom and @exit)
@exit(oldRoomId, newRoomId) @exit(oldRoomId, newRoomId)
@current = newRoomId @current = newRoomId
# Remove links and transient sections. # Remove links and transient sections.
@view.removeTransient(@interactive) @view.removeTransient(@interactive)
@ -361,7 +362,9 @@ class Salet
# Loads the game from the given data # Loads the game from the given data
@loadGame = (saveFile) => @loadGame = (saveFile) =>
@progress = saveFile.progress @progress = saveFile.progress
@character = saveFile.character @character = new Character
for index, value of saveFile.character
@character[index] = value
@rnd = new Random(@progress.seed) @rnd = new Random(@progress.seed)
@ -370,9 +373,8 @@ class Salet
if saveFile.progress? and saveFile.progress.sequence.length > 1 if saveFile.progress? and saveFile.progress.sequence.length > 1
@view.clearContent() @view.clearContent()
# Now play through the actions so far: # Start the game
if (@init) @init()
@init()
# Run through all the player's history. # Run through all the player's history.
interactive = false interactive = false
@ -413,8 +415,7 @@ class Salet
# Start the game # Start the game
@startTime = new Date().getTime() * 0.001 @startTime = new Date().getTime() * 0.001
if (@init) @init()
@init(character)
# Do the first state. # Do the first state.
@doTransitionTo(@start) @doTransitionTo(@start)
@ -432,7 +433,8 @@ class Salet
for index, value of spec for index, value of spec
this[index] = value this[index] = value
return this
return this
salet = (spec) -> salet = (spec) ->
spec ?= {} spec ?= {}