Renamed objects to units.

Hopefully *this* will clear up the terminology a little bit.
This commit is contained in:
Alexander Yakovlev 2016-06-03 09:40:48 +07:00
parent 9163c61211
commit f406c0e692
5 changed files with 46 additions and 52 deletions

View file

@ -1,5 +1,5 @@
room = require("../../lib/room.coffee")
obj = require('../../lib/obj.coffee')
unit = require('../../lib/unit.coffee')
dialogue = require('../../lib/util/dialogue.coffee')
phrase = require('../../lib/util/phrase.coffee')
oneOf = require('../../lib/oneOf.coffee')

View file

@ -8,8 +8,8 @@ room "world", salet,
You're in a large room carved inside a giant milky rock mountain.
The floor and walls are littered with signs and signatures of the previous visitors.
"""
objects: [
obj "well",
units: [
unit "well",
dsc: "A steep narrow {{well}} proceeds upward."
act: "There is only one passage out. See the „Other rooms“ block popped up? Click it."
]
@ -30,8 +30,8 @@ room "plaza", salet,
"""
else
"You quickly find the central plaza."
objects: [
obj "policeman",
units: [
unit "policeman",
dsc: "There is a policeman nearby. You could ask him {{for directions.}}"
act: (salet) ->
if salet.character.has_mark?
@ -41,7 +41,7 @@ room "plaza", salet,
"""
Here, let me mark it on your map.
"""
obj "people",
unit "people",
dsc: "There are {{people shouting}} nearby."
act: 'Just some weirdos shouting "Viva la Cthulhu!". Typical.'
]
@ -64,8 +64,8 @@ room "lair", salet,
The Lair of Yog-Sothoth is a very *n'gai* cave, full of *buggs-shoggogs* and *n'ghaa ng'aa*.
"""
ways: ["shop"]
objects: [
obj "bugg",
units: [
unit "bugg",
dsc: "You see a particularly beautiful slimy {{bugg.}}"
takeable: false
act: (salet) =>
@ -103,8 +103,8 @@ room "shop-inside", salet,
dsc: """
The insides are painted pastel white, honouring The Great Milk Spill of 1985.
"""
objects: [
obj "merchant",
units: [
unit "merchant",
dsc: "A {{merchant}} eyes you warily."
takeable: false
act: (salet) =>
@ -112,13 +112,7 @@ room "shop-inside", salet,
return ""
]
###
I want to be able to do this but I can't because I'm lost in all the `this` and @objects and `new`.
The chain of calls is very weird and this puts an object IN EVERY ROOM for God's sake.
I need someone smarter than me to fix this.
###
lamp = obj "lamp",
lamp = unit "lamp",
takeable: true
lamp.put(salet, "shop-inside")

View file

@ -10,7 +10,7 @@ class Character
@drop = (thing) =>
for i in @inventory
if i.name == thing
index = @objects.indexOf(thing)
index = @inventory.indexOf(thing)
@inventory.splice(index, 1)
@has = (thing) =>

View file

@ -1,4 +1,4 @@
obj = require('./obj.coffee')
unit = require('./unit.coffee')
markdown = require('./markdown.coffee')
assert = (msg, assertion) -> console.assert assertion, msg
@ -18,7 +18,7 @@ class SaletRoom
constructor: (spec) ->
@visited = 0
@title = "Room"
@objects = {}
@units = {}
@canView = true
@canChoose = true
@priority = 1
@ -114,7 +114,7 @@ class SaletRoom
###
An internal function to get the room's description and the descriptions of
every object in this room.
every unit in this room.
###
@look = (system, f) =>
system.view.updateWays(system, @ways, @name)
@ -128,47 +128,47 @@ class SaletRoom
dsc = @dsc.fcall(this, system, f).toString()
retval += markdown(dsc)
objDescriptions = []
for thing in @objects
unitDescriptions = []
for thing in @units
if thing.name and typeof(thing.look) == "function" and thing.look(system, f)
objDescriptions.push ({
unitDescriptions.push ({
order: thing.order,
content: thing.look(system, f)
})
objDescriptions.sort((a, b) ->
unitDescriptions.sort((a, b) ->
return a.order - b.order
)
for description in objDescriptions
for description in unitDescriptions
retval += description.content
return retval
###
Puts an object in this room.
Places a unit in this room.
###
@take = (thing) =>
@objects.push(thing)
@units.push(thing)
@drop = (name) =>
for thing in @objects
for thing in @units
if thing.name == name
@objects.splice(@objects.indexOf(thing), 1)
return @objects
@units.splice(@units.indexOf(thing), 1)
return @units
###
Object action. A function or a string which comes when you click on the object link.
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) =>
if (link = action.match(/^_(act|cycle|inv)_(.+)$/)) #object action
if (link = action.match(/^_(act|cycle|inv)_(.+)$/)) #unit action
if link[1] == "inv"
return system.view.write system.character.inv(link[2])
for thing in @objects
for thing in @units
if thing.name == link[2]
if link[1] == "act"
# If it's takeable, the player can take this object.
# If it's takeable, the player can take this unit.
# If not, we check the "act" function.
if thing.takeable
system.character.take(thing)
@ -180,7 +180,7 @@ class SaletRoom
# the loop is done but no return came - match not found
console.error("Could not find #{link[2]} in current room.")
# we're done with objects, now check the regular actions
# we're done with units, now check the regular actions
actionClass = action.match(/^_(\w+)_(.+)$/)
that = this

View file

@ -1,24 +1,24 @@
markdown = require('./markdown.coffee')
require('./salet.coffee')
objlink = (content, ref) ->
#require('./salet.coffee')
unitlink = (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
window.unitname = name
text = text.replace /\{\{(.+)\}\}/g, (str, p1) ->
name = window.objname
window.objname = undefined
return objlink(p1, name)
name = window.unitname
window.unitname = undefined
return unitlink(p1, name)
return text
# An object class.
# An object cannot be in several locations at once, you must clone the variable.
class SaletObj
# A unit class.
# A unit cannot be in several locations at once, you must clone the variable.
class SaletUnit
constructor: (spec) ->
unless spec.name?
console.error("Trying to create an object with no name")
console.error("Trying to create a unit with no name")
return null
@order = 0 # you can use this to sort the descriptions
@ -30,8 +30,8 @@ class SaletObj
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
@act = (system) => "You don't find anything extraordinary about the #{@name}." # unit action
@dsc = (system) => "You see a {{#{@name}}} here." # unit description
@inv = (system) => "It's a #{@name}." # inventory description
@location = ""
@put = (salet, location) =>
@ -40,7 +40,7 @@ class SaletObj
@location = location
salet.rooms[location].take(this)
else
console.log("Could not find location #{location} for an object #{@name}")
console.log("Could not find location #{location} for a unit #{@name}")
@delete = (salet, location = false) =>
if location == false
location = @location
@ -49,8 +49,8 @@ class SaletObj
for key, value of spec
this[key] = value
obj = (name, spec) ->
unit = (name, spec) ->
spec ?= {}
spec.name = name
return new SaletObj(spec)
module.exports = obj
return new SaletUnit(spec)
module.exports = unit