codecombat/app/lib/world/component.coffee

24 lines
907 B
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
componentKeywords = ['attach', 'constructor', 'validateArguments', 'toString', 'isComponent'] # Array is faster than object
module.exports = class Component
2014-06-30 22:16:26 -04:00
@className: 'Component'
2014-01-03 13:32:13 -05:00
isComponent: true
constructor: (config) ->
for key, value of config
@[key] = value # Hmm, might want to _.cloneDeep here? What if the config has nested object values and the Thang modifies them, then we re-use the config for, say, missile spawning? Well, for now we'll clone in the missile.
attach: (thang) ->
# Optimize; this is much of the World constructor time
2014-06-30 22:16:26 -04:00
for key, value of @ when key not in componentKeywords and key[0] isnt '_'
2014-01-03 13:32:13 -05:00
oldValue = thang[key]
if typeof oldValue is 'function'
thang.appendMethod key, value
else
thang[key] = value
validateArguments:
additionalProperties: false
toString: ->
"<Component: #{@constructor.className}"