codecombat/app/lib/world/world_script_note.coffee
2014-01-03 10:32:13 -08:00

44 lines
1.5 KiB
CoffeeScript

{clone} = require './world_utils'
{scriptMatchesEventPrereqs} = require './script_event_prereqs'
Thang = require './thang'
Vector = require './vector'
module.exports = class WorldScriptNote
@className: "WorldScriptNote"
constructor: (script, @event, world) ->
return unless script?
@invalid = true
return unless scriptMatchesEventPrereqs(script, event)
# Could add the scriptPrereqsSatisfied or seen/repeats stuff if needed
@invalid = false
@channel = script.channel
@event ?= {}
@event.replacedNoteChain = script.noteChain
serialize: ->
o = {channel: @channel, event: {}}
for key, value of @event
if value instanceof Thang
value = {isThang: true, id: value.id}
else if _.isArray value
for subval, i in value
if subval instanceof Thang
value[i] = {isThang: true, id: subval.id}
o.event[key] = value
o
@deserialize: (o, world, classMap) ->
scriptNote = new WorldScriptNote
scriptNote.channel = o.channel
scriptNote.event = {}
for key, value of o.event
if value? and typeof value is 'object' and value.isThang
value = world.getThangByID value.id
else if _.isArray value
for subval, i in value
if subval? and typeof subval is 'object' and subval.isThang
value[i] = world.getThangByID subval.id
else if value? and typeof value is 'object' and value.CN
value = classMap[value.CN].deserialize value, world, classMap
scriptNote.event[key] = value
scriptNote