@@ -37,10 +37,37 @@ let components = [ | |||||
return 'js/components/' + c; | return 'js/components/' + c; | ||||
}); | }); | ||||
define(components, function () { | |||||
define([ | |||||
...components, | |||||
'../system/events' | |||||
], function () { | |||||
const events = arguments[arguments.length - 1]; | |||||
const hookEvent = function (e, cb) { | |||||
if (!this.eventList[e]) | |||||
this.eventList[e] = []; | |||||
this.eventList[e].push(cb); | |||||
events.on(e, cb); | |||||
}; | |||||
const unhookEvents = function () { | |||||
Object.entries(this.eventList).forEach(([eventName, callbacks]) => { | |||||
callbacks.forEach(c => events.off(eventName, c)); | |||||
}); | |||||
}; | |||||
let templates = {}; | let templates = {}; | ||||
[].forEach.call(arguments, function (t) { | |||||
[].forEach.call(arguments, function (t, i) { | |||||
//Don't do this for the events module | |||||
if (i === arguments.length - 1) | |||||
return; | |||||
t.eventList = {}; | |||||
t.hookEvent = hookEvent.bind(t); | |||||
t.unhookEvents = unhookEvents.bind(t); | |||||
templates[t.type] = t; | templates[t.type] = t; | ||||
}); | }); | ||||
@@ -13,7 +13,7 @@ define([ | |||||
init: function () { | init: function () { | ||||
this.obj.on('onKeyDown', this.onKeyDown.bind(this)); | this.obj.on('onKeyDown', this.onKeyDown.bind(this)); | ||||
events.on('onRezone', this.onRezone.bind(this)); | |||||
this.hookEvent('onRezone', this.onRezone.bind(this)); | |||||
}, | }, | ||||
extend: function (msg) { | extend: function (msg) { | ||||
@@ -72,6 +72,10 @@ define([ | |||||
method: 'gather' | method: 'gather' | ||||
} | } | ||||
}); | }); | ||||
}, | |||||
destroy: function () { | |||||
this.unhookEvents(); | |||||
} | } | ||||
}; | }; | ||||
}); | }); |
@@ -16,7 +16,7 @@ define([ | |||||
moveCdMax: 8, | moveCdMax: 8, | ||||
init: function () { | init: function () { | ||||
events.on('onCanvasKeyDown', this.onCanvasKeyDown.bind(this)); | |||||
this.hookEvent('onCanvasKeyDown', this.onCanvasKeyDown.bind(this)); | |||||
}, | }, | ||||
update: function () { | update: function () { | ||||
@@ -74,6 +74,10 @@ define([ | |||||
this.moveCd = this.moveCdMax; | this.moveCd = this.moveCdMax; | ||||
this.obj.pather.add(newX, newY); | this.obj.pather.add(newX, newY); | ||||
}, | |||||
destroy: function () { | |||||
this.unhookEvents(); | |||||
} | } | ||||
}; | }; | ||||
}); | }); |
@@ -29,8 +29,8 @@ define([ | |||||
sprite: null, | sprite: null, | ||||
init: function () { | init: function () { | ||||
events.on('onUiHover', this.onUiHover.bind(this, true)); | |||||
events.on('onUiLeave', this.onUiHover.bind(this, false)); | |||||
this.hookEvent('onUiHover', this.onUiHover.bind(this, true)); | |||||
this.hookEvent('onUiLeave', this.onUiHover.bind(this, false)); | |||||
this.sprite = renderer.buildObject({ | this.sprite = renderer.buildObject({ | ||||
layerName: 'effects', | layerName: 'effects', | ||||
@@ -76,6 +76,8 @@ define([ | |||||
renderer.destroyObject({ | renderer.destroyObject({ | ||||
sprite: this.sprite | sprite: this.sprite | ||||
}); | }); | ||||
this.unhookEvents(); | |||||
} | } | ||||
}; | }; | ||||
}); | }); |
@@ -116,6 +116,10 @@ define([ | |||||
return; | return; | ||||
} | } | ||||
} | } | ||||
}, | |||||
destroy: function () { | |||||
this.unhookEvents(); | |||||
} | } | ||||
}; | }; | ||||
}); | }); |
@@ -30,7 +30,7 @@ define([ | |||||
obj.addComponent('serverActions'); | obj.addComponent('serverActions'); | ||||
obj.addComponent('pather'); | obj.addComponent('pather'); | ||||
events.on('onRespawn', this.onRespawn.bind(this)); | |||||
this.hookEvent('onRespawn', this.onRespawn.bind(this)); | |||||
events.emit('onGetPortrait', obj.portrait); | events.emit('onGetPortrait', obj.portrait); | ||||
}, | }, | ||||
@@ -70,6 +70,10 @@ define([ | |||||
onRespawn: function (position) { | onRespawn: function (position) { | ||||
this.positionCamera(position.x, position.y, true); | this.positionCamera(position.x, position.y, true); | ||||
}, | |||||
destroy: function () { | |||||
this.unhookEvents(); | |||||
} | } | ||||
}; | }; | ||||
}); | }); |
@@ -11,7 +11,7 @@ define([ | |||||
actions: [], | actions: [], | ||||
init: function (blueprint) { | init: function (blueprint) { | ||||
events.on('onKeyUp', this.onKeyUp.bind(this)); | |||||
this.hookEvent('onKeyUp', this.onKeyUp.bind(this)); | |||||
}, | }, | ||||
onKeyUp: function (key) { | onKeyUp: function (key) { | ||||
@@ -55,6 +55,10 @@ define([ | |||||
} | } | ||||
events.emit('onGetServerActions', this.actions); | events.emit('onGetServerActions', this.actions); | ||||
}, | |||||
destroy: function () { | |||||
this.unhookEvents(); | |||||
} | } | ||||
}; | }; | ||||
}); | }); |