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

Fix for takeable objects

This commit is contained in:
Alexander Yakovlev 2016-01-16 23:43:20 +07:00
parent 0ed4f98098
commit 5488736b48
5 changed files with 20 additions and 18 deletions

View file

@ -45,6 +45,11 @@ writemd = (system, text) ->
get_room = (name) -> get_room = (name) ->
return undum.game.situations[name] return undum.game.situations[name]
# Function to return the current room.
# Works because our `enter()` function sets the `data-situation` attribute.
here = () ->
return undum.game.situations[document.getElementById("current-situation").getAttribute("data-situation")]
# The first room of the game. # The first room of the game.
# For accessibility reasons the text is provided in HTML, not here. # For accessibility reasons the text is provided in HTML, not here.
room "start", room "start",

View file

@ -2,6 +2,5 @@
# All code in this file comes last, so the game is almost ready by this point. # All code in this file comes last, so the game is almost ready by this point.
undum.game.init = (character, system) -> undum.game.init = (character, system) ->
bugg.put("lair")
window.onload = undum.begin window.onload = undum.begin

View file

@ -67,8 +67,8 @@ room "lair",
bugg = obj "bugg-shoggog", bugg = obj "bugg-shoggog",
dsc: "You see a particularly beautiful slimy {{bugg.}}" dsc: "You see a particularly beautiful slimy {{bugg.}}"
act: () -> act: () =>
this.delete() here().drop(@name)
return "You eat the bugg mass. Delicious and raw." return "You eat the bugg mass. Delicious and raw."
room "shop-inside", room "shop-inside",
@ -80,4 +80,4 @@ room "shop-inside",
lamp = obj "lamp", lamp = obj "lamp",
dsc: "You see a {{lamp.}}" dsc: "You see a {{lamp.}}"
take: "You carefully take the lamp." takeable: true

View file

@ -1,16 +1,16 @@
markdown = require('./markdown.coffee') markdown = require('./markdown.coffee')
undum = require('./undum.js') undum = require('./undum.js')
objlink = (content, ref) -> objlink = (content, ref) ->
return "<a href='./_act_#{ref}'>#{content}</a>" return "<a href='./_act_#{ref}' class='once'>#{content}</a>"
Array::remove = (e) -> @[t..t] = [] if (t = @indexOf(e)) > -1 Array::remove = (e) -> @[t..t] = [] if (t = @indexOf(e)) > -1
parsedsc = (text, name) -> parsedsc = (text, name) ->
window.objname = name window.objname = name
text = text.replace /([\s^])\{\{(.+)\}\}([\s$])/g, (str, p1, p2, p3) -> text = text.replace /\{\{(.+)\}\}/g, (str, p1) ->
name = window.objname name = window.objname
window.objname = undefined window.objname = undefined
return p1+objlink(p2, name)+p3 return objlink(p1, name)
return text return text
# An object class. # An object class.
@ -18,7 +18,7 @@ parsedsc = (text, name) ->
class SaletObj class SaletObj
constructor: (spec) -> constructor: (spec) ->
for key, value of spec for key, value of spec
this[key] ?= value this[key] = value
level: 0 level: 0
look: (character, system, f) => look: (character, system, f) =>
if @dsc if @dsc
@ -26,6 +26,7 @@ class SaletObj
text = "<span class='look lvl#{@level}'>" + text + "</span>" text = "<span class='look lvl#{@level}'>" + text + "</span>"
# replace braces {{}} with link to _act_ # replace braces {{}} with link to _act_
return parsedsc(text, @name) return parsedsc(text, @name)
takeable: false
take: (character, system) => "You take the #{@name}." # taking to inventory take: (character, system) => "You take the #{@name}." # taking to inventory
act: (character, system) => "You don't find anything extraordinary about the #{@name}." # object action act: (character, system) => "You don't find anything extraordinary about the #{@name}." # object action
dsc: (character, system) => "You see a {{#{@name}}} here." # object description dsc: (character, system) => "You see a {{#{@name}}} here." # object description

View file

@ -26,11 +26,6 @@ addClass = (element, className) ->
else else
element.className += ' ' + className element.className += ' ' + className
# Function to return the current room.
# Works because our `enter()` function sets the `data-situation` attribute.
here = () ->
return undum.game.situations[document.getElementById("current-situation").getAttribute("data-situation")]
cls = (system) -> cls = (system) ->
system.clearContent() system.clearContent()
system.clearContent("#intro") system.clearContent("#intro")
@ -154,7 +149,6 @@ class SaletRoom extends RaconteurSituation
if @content if @content
retval += markdown(@content.fcall(this, character, system, f)) retval += markdown(@content.fcall(this, character, system, f))
console.log @objects
for name, thing of @objects for name, thing of @objects
retval += thing.look() retval += thing.look()
@ -164,11 +158,15 @@ class SaletRoom extends RaconteurSituation
Puts an object in this room. Puts an object in this room.
### ###
take: (thing) => take: (thing) =>
console.log this
@objects[thing.name] = thing @objects[thing.name] = thing
# BUG: for some really weird reason if the call is made in init function or # BUG: for some really weird reason if the call is made in init function or
# during the initialization, this ALSO puts the thing in the start room. # during the initialization, this ALSO puts the thing in the start room.
undum.game.situations["start"].objects = {} undum.game.situations["start"].objects = {}
drop: (name) =>
delete @objects[name]
### ###
Object action. A function or a string which comes when you click on the object link. Object action. A function or a string which comes when you click on the object link.
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.
@ -177,12 +175,11 @@ class SaletRoom extends RaconteurSituation
if (link = action.match(/^_act_(.+)$/)) #object action if (link = action.match(/^_act_(.+)$/)) #object action
for name, thing of @objects for name, thing of @objects
if name == link[1] if name == link[1]
# We check the "take" function. If it exists, the player can take this object. # If it's takeable, the player can take this object.
# If not, we check the "act" function. # If not, we check the "act" function.
if thing.take if thing.takeable
character.sandbox.inventory.push thing character.sandbox.inventory.push thing
delete @objects[name] @drop name
console.log @objects
cls(system) cls(system)
@entering.fcall(this, character, system, @name) @entering.fcall(this, character, system, @name)
return print(thing.take.fcall(thing, character, system)) return print(thing.take.fcall(thing, character, system))