voyageur-review/lib/obj.coffee

53 lines
1.6 KiB
CoffeeScript

markdown = require('./markdown.coffee')
require('./salet.coffee')
objlink = (content, ref) ->
return "<a href='./_act_#{ref}' class='once'>#{content}</a>"
Array::remove = (e) -> @[t..t] = [] if (t = @indexOf(e)) > -1
parsedsc = (text, name) ->
window.objname = name
text = text.replace /\{\{(.+)\}\}/g, (str, p1) ->
name = window.objname
window.objname = undefined
return objlink(p1, name)
return text
# An object class.
# An object cannot be in several locations at once, you must clone the variable.
class SaletObj
constructor: (spec) ->
unless spec.name?
console.error("Trying to create an object with no name")
return null
for key, value of spec
this[key] = value
level: 0
look: (system, f) =>
if @dsc
text = markdown(@dsc.fcall(this, system, f).toString())
text = "<span class='look lvl#{@level}'>" + text + "</span>"
# replace braces {{}} with link to _act_
return parsedsc(text, @name)
takeable: false
take: (system) => "You take the #{@name}." # taking to inventory
act: (system) => "You don't find anything extraordinary about the #{@name}." # object action
dsc: (system) => "You see a {{#{@name}}} here." # object description
inv: (system) => "It's a {{#{@name}.}}" # inventory description
location: ""
put: (location) =>
@level = 0 # this is scenery
if salet.rooms[location]?
salet.rooms[location].take(this)
@location = location
delete: (location = false) =>
if location == false
location = @location
salet.rooms[location].drop(this)
obj = (name, spec) ->
spec ?= {}
spec.name = name
return new SaletObj(spec)
module.exports = obj