y.event's Emitter now uses an interface similar to node.js
authordsc <david.schoonover@gmail.com>
Mon, 31 Jan 2011 09:30:29 +0000 (01:30 -0800)
committerdsc <david.schoonover@gmail.com>
Mon, 31 Jan 2011 09:30:29 +0000 (01:30 -0800)
28 files changed:
src/Y/modules/y.config.cjs
src/Y/modules/y.event.cjs
src/Y/modules/y.scaffold.cjs
src/evt.cjs
src/ezl/loop/eventloop.cjs
src/ezl/mixins/speciated.cjs
src/ezl/util/data/datafile.cjs
src/ezl/util/data/loader.cjs
src/tanks/config.cjs
src/tanks/effects/buff.cjs
src/tanks/game.cjs
src/tanks/item/container.cjs
src/tanks/map/level.cjs
src/tanks/map/pathing/traversal.cjs
src/tanks/map/rock.cjs
src/tanks/map/wall.cjs
src/tanks/mixins/inventoried.cjs
src/tanks/mixins/quantified.cjs
src/tanks/thing/bullet.cjs
src/tanks/thing/item.cjs
src/tanks/thing/tank.cjs
src/tanks/thing/thing.cjs
src/tanks/ui/configui.cjs
src/tanks/ui/inventory/backpack.cjs
src/tanks/ui/inventory/containerui.cjs
src/tanks/ui/inventory/equipslot.cjs
src/tanks/ui/main.cjs
src/tanks/ui/pathmapui.cjs

index 55ebb00..719aa6f 100644 (file)
@@ -32,7 +32,7 @@ Y.YObject.subclass('Config', function(Config){
                 value = (value === undefined ? def : value);
                 
                 meta.obj[meta.key] = value;
-                this._fireMutation('set', key, old, value, meta.obj);
+                this._emitMutation('set', key, old, value, meta.obj);
             }
             return this;
         },
@@ -45,7 +45,7 @@ Y.YObject.subclass('Config', function(Config){
                 
                 if ( meta.obj ) {
                     delete meta.obj[meta.key];
-                    this._fireMutation('remove', key, old, undefined, meta.obj);
+                    this._emitMutation('remove', key, old, undefined, meta.obj);
                 }
             }
             return this;
@@ -54,7 +54,7 @@ Y.YObject.subclass('Config', function(Config){
         /**
          * @private
          */
-        _fireMutation : function _fireMutation(evt, key, oldval, newval, obj){
+        _emitMutation : function _emitMutation(evt, key, oldval, newval, obj){
             if (newval !== oldval){
                 var data = {
                     'key'    : key,
@@ -64,8 +64,8 @@ Y.YObject.subclass('Config', function(Config){
                     'obj'    : obj,
                     'root'   : this
                 };
-                this.fire(evt+':'+key, this, data);
-                this.fire(evt, this, data);
+                this.emit(evt+':'+key, this, data);
+                this.emit(evt, this, data);
             }
             return this;
         },
@@ -180,7 +180,7 @@ Y.YObject.subclass('Config', function(Config){
                     key = 'set:'+key;
                 return key;
             }, this);
-            this.addEventListener(events, function(evt){
+            this.on(events, function(evt){
                 obj[evt.data.key.split('.').pop()] = evt.data.newval;
             });
             return this;
index 2e15d46..0135273 100644 (file)
@@ -1,14 +1,23 @@
-var Y = require('Y').Y;
-Y['event'] = exports;
+var Y = require('Y').Y
+,
 
 /**
  * A simple event.
  * TODO: Detect jQuery event objects.
  * TODO: If DOM event, wrap with consistent API (use jQuery if present).
  */
-var Event =
+Event =
 exports['Event'] =
 Y.YObject.subclass('Event', {
+    _stopped : false, // whether propagation is stopped
+    
+    type : 'event', // event type
+    target : null,  // target of the emitter
+    trigger : null, // object which caused this event
+    data : null,    // event data
+    
+    
+    
     init : function init( type, target, trigger, data ){
         data = data || {};
         for (var k in data) this[k] = data[k];
@@ -19,6 +28,21 @@ Y.YObject.subclass('Event', {
         this.trigger = trigger || target;
     },
     
+    /**
+     * @see http://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/events.html#Events-Event-stopPropagation
+     */
+    stopPropagation : function stopPropagation(){
+        this._stopped = true;
+        return this;
+    },
+    
+    /**
+     * @see http://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/events.html#Events-Event-isPropagationStopped
+     */
+    isPropagationStopped : function isPropagationStopped(){
+        return this._stopped;
+    },
+    
     toString: function(){ return "Event("+this.type+")"; }
 })
 
@@ -26,33 +50,93 @@ Y.YObject.subclass('Event', {
  * A simple multicaster.
  */
 , methods = {
-    getQueue : function getQueue(evt){
-        var Qs = this.queues;
-        if ( !Qs[evt] )
-            Qs[evt] = Y([]);
-        return Qs[evt];
+    
+    listeners : function listeners(evt){
+        return this.queues[evt] ? this.queues[evt].clone() : new Y.YArray();
     },
     
-    addEventListener : function addEventListener(evts, fn){
+    willTrigger : function willTrigger(evt){
+        var queue = this.queues[evt];
+        return !!( (queue && queue.length) || (this.parent && this.parent.willTrigger(evt)) );
+    },
+    
+    hasListener : function hasListener(evt){
+        var queue = this.queues[evt];
+        return !!(queue && queue.length);
+    },
+    
+    addListener : function addListener(evts, fn){
         fn = fn.toFunction();
         if ( !Y.isArray(evts) )
             evts = evts.split(/\s+/);
-        evts.forEach(function addEvent(evt){
-            this.getQueue(evt).push(fn);
-        }, this);
+        
+        var queues = this.queues
+        ,   i=0, L=evts.length, evt=evts[i];
+        for (; i<L; evt=evts[++i])
+            (queues[evt] || (queues[evt] = new Y.YArray()))
+                .push(fn);
+        
         return this.target;
     },
     
-    removeEventListener : function removeEventListener(evt, fn){
-        this.getQueue(evt).remove(fn.toFunction());
+    removeListener : function removeListener(evts, fn){
+        fn = fn.toFunction();
+        if ( !Y.isArray(evts) )
+            evts = evts.split(/\s+/);
+        
+        var queues = this.queues
+        ,   i=0, L=evts.length, evt=evts[i];
+        for (; i<L; evt=evts[++i])
+            if ( queues[evt] ) queues[evt].remove(fn);
+        
+        return this.target;
     },
     
-    fire : function fire(evtname, trigger, data, async){
-        var evt = new Event(evtname, this.target, trigger, data);
-        if (async)
-            setTimeout(this.dispatchEvent.bind(this, evt), 10);
-        else
-            this.dispatchEvent(evt);
+    removeAllListeners : function removeAllListeners(evts){
+        if ( !Y.isArray(evts) )
+            evts = evts.split(/\s+/);
+        
+        for ( var i=0, L=evts.length, evt=evts[i]; i<L; evt=evts[++i] )
+            delete this.queues[evt];
+        
+        return this;
+    },
+    
+    once : function once(evts, fn){
+        fn = fn.toFunction();
+        var self = this;
+        return self.addListener(evts,
+            function onceHandler(){
+                self.removeListener(evts, arguments.callee);
+                fn.apply(this, arguments);
+            });
+    },
+    
+    emit : function emit(evtname, trigger, data){
+        var q = this.queues[evtname]
+        ,   L = q ? q.length : 0
+        ;
+        if ( !L )
+            return this;
+        
+        q = q._o.slice(0);
+        var A = arguments
+        ,   target = this.target
+        ,   evt = new Event(evtname, target, trigger, data)
+        ,   method = (A.length > 3 ? 'apply' : 'call')
+        ,   args   = (A.length > 3 ? [evt].concat(Y(A,3)) : evt)
+        ;
+        for (var i=0, fn=q[i]; i<L; fn=q[++i])
+            fn[method](target, args);
+        
+        if (this.parent && !evt._stopped)
+            this.parent.emit.apply(this.parent, A);
+        
+        return evt;
+    },
+    
+    emitAsync : function emitAsync(evtname, trigger, data){
+        setTimeout(this.emit.bind.apply(this.emit, [this].concat(Y(arguments))), 10);
         return evt;
     },
     
@@ -62,8 +146,11 @@ Y.YObject.subclass('Event', {
      * XXX: does not handle degenerate or recursive event dispatch
      */
     dispatchEvent : function dispatchEvent(evt){
-        this.getQueue(evt.type).invoke('call', evt.target, evt);
-        if (this.parent) this.parent.fire(evt.type, evt.trigger, evt.data);
+        var queue = this.queues[evt.type];
+        if (queue && queue.length)
+            queue.invoke('call', evt.target, evt);
+        if (this.parent)
+            this.parent.emit(evt.type, evt.trigger, evt.data);
         return evt;
     },
     
@@ -71,12 +158,14 @@ Y.YObject.subclass('Event', {
      * Decorates object with bound methods to act as a delegate of this hub.
      */
     decorate : function decorate(delegate){
-        if (!delegate) return;
+        if (!delegate)
+            return delegate;
         for (var k in methods)
             delegate[k] = methods[k].bind(this);
         return delegate;
     }
-},
+}
+,
 
 Emitter =
 exports['Emitter'] =
@@ -94,8 +183,11 @@ Y.YObject.subclass('Emitter',
         }
     }, methods) )
 ;
+methods.on = methods.addListener;
 Emitter.methods = methods;
 
 // Global Event Hub
 Emitter.global = new Emitter()
 Emitter.global.decorate(exports);
+
+Y['event'] = exports;
index ea0f200..8b1a1fa 100644 (file)
@@ -104,7 +104,7 @@ Y.subclass('Field', {
         if (val !== this.val) {
             this.old = this.val;
             this.val = val;
-            this.fire('change', this, {
+            this.emit('change', this, {
                 'key'    : this.id,
                 'oldval' : this.old,
                 'newval' : this.val,
@@ -157,11 +157,11 @@ function create(config, el){
             var field = new Field(chain, def, value);
             fields[chain] = field;
             group.append(field.el);
-            config.addEventListener('set:'+chain, function onConfigSet(evt){
+            config.on('set:'+chain, function onConfigSet(evt){
                 // console.log('Config at '+evt.data.key+' changed!', field, evt.newval, evt);
                 field.update(evt.data.newval);
             });
-            field.addEventListener('change', function onFieldChange(evt){
+            field.on('change', function onFieldChange(evt){
                 // console.log('Field '+evt.data.key+' changed!', field, evt.newval, evt);
                 config.set(evt.data.key, evt.data.newval);
             });
index 611ed6a..bb45e16 100644 (file)
@@ -82,7 +82,7 @@ function ConstructorTemplate() {
         // Perform actions that should only happen once in Constructor
         instance.__emitter__ = new Emitter(instance, cls);
         
-        cls.fire('create', instance, {
+        cls.emit('create', instance, {
             'instance' : instance,
             'cls'      : cls,
             'args'     : Y(args)
@@ -92,7 +92,7 @@ function ConstructorTemplate() {
         if ( isFunction(initialise) )
             initialise.apply(instance, args);
         
-        cls.fire('created', instance, {
+        cls.emit('created', instance, {
             'instance' : instance,
             'cls'      : cls,
             'args'     : Y(args)
@@ -125,7 +125,7 @@ function createInitialise(cls){
             if (result) instance = result; // XXX: I think this needs to go away
         }
         
-        instance.fire('init', instance, {
+        instance.emit('init', instance, {
             'instance' : instance,
             'cls'      : cls,
             'args'     : Y(arguments)
@@ -274,12 +274,12 @@ function Class(className, Parent, members){
     // Notify mixins to let them finish up any customization
     if (mixins.length)
         mixins.forEach(function(mxn){
-            if ( mxn && isFunction(mxn.fire) )
-                mxn.fire('mixin', NewClass, { 'mixin':mxn, 'cls':NewClass });
+            if ( mxn && isFunction(mxn.emit) )
+                mxn.emit('mixin', NewClass, { 'mixin':mxn, 'cls':NewClass });
         });
     
     // Notify parent of the subclass
-    ParentEmitter.fire('subclass',
+    ParentEmitter.emit('subclass',
         NewClass, {
             'className' : className,
             'parent'    : Parent,
@@ -417,18 +417,18 @@ function mixin(cls, _mxn){
         bases.unshift(mxn);
         
         // Register onCreate to fire whenever a new instance is initialized
-        if ( isFunction(cls.addEventListener) && hasOwn.call(mproto,'onCreate') && isFunction(onCreate) )
-            cls.addEventListener('create', onCreate);
+        if ( isFunction(cls.on) && hasOwn.call(mproto,'onCreate') && isFunction(onCreate) )
+            cls.on('create', onCreate);
         
         // Fire the mixin event on this Mixin only if we're done constructing the class
-        if ( isFunction(mxn.fire) && mixin.caller !== Class )
-            mxn.fire('mixin', cls, { 'mixin':mxn, 'cls':cls });
+        if ( isFunction(mxn.emit) && mixin.caller !== Class )
+            mxn.emit('mixin', cls, { 'mixin':mxn, 'cls':cls });
     });
     return cls;
 }
 
 
-Mixin.addEventListener('subclass',
+Mixin.on('subclass',
     function onMixinSubclass(evt){
         var d       = evt.data
         ,   mxn     = d.child
@@ -437,7 +437,7 @@ Mixin.addEventListener('subclass',
         ;
         
         if ( isFunction(onMixin) )
-            mxn.addEventListener('mixin', onMixin);
+            mxn.on('mixin', onMixin);
         
         // console.log('Mixin.subclass()', mxn, '<', d.parent, 'onMixin:', onMixin);
     });
index 7ba9fc9..02f1406 100644 (file)
@@ -67,7 +67,7 @@ Emitter.subclass('EventLoop', {
         
         this.running = true;
         this.now = new Date().getTime() - (this.elapsedAtStop || this.targetTime);
-        this.fire('start');
+        this.emit('start');
         
         if (this.elapsedAtStop > 0)
             this.sleepMinus(this.elapsedAtStop);
@@ -83,7 +83,7 @@ Emitter.subclass('EventLoop', {
         this.elapsedAtStop = Math.min(this.targetTime, new Date().getTime() - this.now);
         this.timer = null;
         this.running = false;
-        this.fire('stop');
+        this.emit('stop');
     },
     
     sleepMinus : function sleepMinus(tFrame){
@@ -105,7 +105,7 @@ Emitter.subclass('EventLoop', {
         clearTimeout(this.timer);
         this.timer = null;
         
-        this.fire('tick', this, {
+        this.emit('tick', this, {
             'now'     : this.clock,
             'elapsed' : this.perceived,
             'ticks'   : ++this.ticks
index 13357f3..dad1630 100644 (file)
@@ -93,7 +93,7 @@ Mixin.subclass('Speciated', {
             ;
             Species.__species_props__ = props;
             
-            cls.fire('speciate', Species, { 'id':id, 'species':Species, 'cls':cls });
+            cls.emit('speciate', Species, { 'id':id, 'species':Species, 'cls':cls });
             return Species;
         },
         
index 093afb4..b074682 100644 (file)
@@ -21,14 +21,14 @@ new evt.Class('DataFile', {
     },
     
     load : function load(){
-        this.fire('load');
+        this.emit('load');
         jQuery.getJSON(this.path, this.process);
         return this;
     },
     
     process : function process(data){
         this.data = data;
-        this.fire('process', data);
+        this.emit('process', data);
         // console.group('DataFile: processing '+this.path);
         
         var types = Y(data.types)
@@ -57,7 +57,7 @@ new evt.Class('DataFile', {
         }, this);
         
         // console.groupEnd();
-        this.fire('complete');
+        this.emit('complete');
         return this;
     },
     
@@ -65,7 +65,7 @@ new evt.Class('DataFile', {
      * @private
      */
     die : function die(reason){
-        this.fire('error', this, { 'reason':reason });
+        this.emit('error', this, { 'reason':reason });
         throw new Error(this+' Error: '+reason);
     },
     
index b2f564a..9e00b8d 100644 (file)
@@ -60,7 +60,7 @@ new evt.Class('Loader', {
     start : function start(){
         if (!this.running) {
             this.running = true;
-            this.fire('start');
+            this.emit('start');
             this.nextJob();
         }
         return this;
@@ -73,7 +73,7 @@ new evt.Class('Loader', {
      */
     stop : function stop(){
         if (this.running) {
-            this.fire('stop');
+            this.emit('stop');
             this.running = false;
         }
         return this;
@@ -90,7 +90,7 @@ new evt.Class('Loader', {
                 && self.queue.length === 0 
                 && self.inFlight.length === 0 ) {
             self.running = false;
-            self.fire('complete');
+            self.emit('complete');
             return;
         }
         
@@ -101,9 +101,9 @@ new evt.Class('Loader', {
         
         var job = self.queue.shift();
         // console.log(self+'.nextJob() --> '+job);
-        job.addEventListener('complete', function onJobComplete(evt){
+        job.on('complete', function onJobComplete(evt){
             // console.log(self+'.onJobComplete('+job+')');
-            job.removeEventListener('complete', arguments.callee);
+            job.removeListener('complete', arguments.callee);
             self.inFlight.remove(job);
             self.nextJob();
         });
index a30c26f..433d37b 100644 (file)
@@ -21,7 +21,7 @@ exports['config'] = new Config(defaults)
 ;
 
 // Updates the midpoint square when square-size changes
-config.addEventListener('set:pathing.pathSquare', function(evt){
+config.on('set:pathing.pathSquare', function(evt){
     var sq = evt.data.newval;
     config.set('pathing.pathSquareMid', new Vec(sq/2, sq/2));
 });
index cd8b6e4..65be09b 100644 (file)
@@ -45,8 +45,8 @@ new evt.Class('Buff', {
         this.applyStatMods();
         
         var data = { 'buff':this, 'unit':this.target, 'owner':this.owner };
-        this.fire('buff.conjure', this.target, data);
-        this.target.fire('buff.conjure', this, data);
+        this.emit('buff.conjure', this.target, data);
+        this.target.emit('buff.conjure', this, data);
     },
     
     tick : function tick(elapsed, now){
@@ -63,8 +63,8 @@ new evt.Class('Buff', {
         // var evt = 'buff.die.'+reason // We'll add this when we add polymorphic dispatchers
         var evt = 'buff.die'
         ,   data = { 'buff':this, 'unit':this.target, 'owner':this.owner };
-        this.fire(evt, this.target, data);
-        this.target.fire(evt, this, data);
+        this.emit(evt, this.target, data);
+        this.target.emit(evt, this, data);
         return this;
     },
     
@@ -95,7 +95,7 @@ new evt.Class('Buff', {
     
 });
 
-Buff.addEventListener('speciate',
+Buff.on('speciate',
     function onSpeciate(evt){
         var NewBuff = evt.data.species;
         if (NewBuff.fn.timeout === -1)
index cb81a02..5ca3037 100644 (file)
@@ -72,9 +72,9 @@ Y.subclass('Game', {
         this.viewport.append(this.map.ui);
         
         // automatically keep track of units
-        Thing.addEventListener('created', this.addUnit);
-        Thing.addEventListener('destroy', this.killUnit);
-        this.addEventListener('tick', this.tick);
+        Thing.on('created', this.addUnit);
+        Thing.on('destroy', this.killUnit);
+        this.on('tick', this.tick);
         
         this.level.setup();
         if (this.player) {
@@ -87,14 +87,14 @@ Y.subclass('Game', {
             this.isReplay = true;
             this.loadLog(replayFile);
         } else
-            this.fire('ready', this);
+            this.emit('ready', this);
     },
     
     destroy : function destroy(){
         this.stop();
         this.player.destroy();
-        Thing.removeEventListener('created', this.addUnit);
-        Thing.removeEventListener('destroy', this.killUnit);
+        Thing.removeListener('created', this.addUnit);
+        Thing.removeListener('destroy', this.killUnit);
         this.root.remove();
         this.resetGlobals();
     },
@@ -142,7 +142,7 @@ Y.subclass('Game', {
         
         if ( this.gameover )
             setTimeout(function(){
-                self.fire(self.gameover, self.player);
+                self.emit(self.gameover, self.player);
                 self.stop();
             }, this.gameoverDelay);
     },
@@ -291,7 +291,7 @@ Y.subclass('Game', {
                 return console.error('No replay data found!');
             self.player.setReplay(replayData);
             console.log('Replay loaded!');
-            self.fire('ready', self);
+            self.emit('ready', self);
         });
         console.log('Loading replay log from //'+(window.location.host)+url+' ...');
         return this;
index b728b43..436d5e1 100644 (file)
@@ -105,12 +105,12 @@ Mixin.subclass('Container', {
                 evtName = 'item.move'
         
         idx = item.ui.bpIdx;
-        this._fireItemChange(evtName, item, { 'idx':idx, 'oldIdx':oldIdx });
+        this._emitItemChange(evtName, item, { 'idx':idx, 'oldIdx':oldIdx });
         
         if ( displacedItem && displacedItem !== item ) {
             displacedItem.ui.bpIdx = null;
             this._putItem(displacedItem, oldIdx);
-            this._fireItemChange('item.move', displacedItem, { 'idx':displacedItem.ui.bpIdx, 'oldIdx':idx });
+            this._emitItemChange('item.move', displacedItem, { 'idx':displacedItem.ui.bpIdx, 'oldIdx':idx });
         }
         
         return this;
@@ -130,7 +130,7 @@ Mixin.subclass('Container', {
         if ( !this._removeItem(item) )
             return null;
         
-        this._fireItemChange('item.lose'+(drop ? '.drop' : ''), item, { 'idx':idx });
+        this._emitItemChange('item.lose'+(drop ? '.drop' : ''), item, { 'idx':idx });
         return item;
     },
     
@@ -158,7 +158,7 @@ Mixin.subclass('Container', {
         eqs[slot] = item;
         item.slot = slot;
         
-        return this._fireItemChange('item.equip', item, { 'slot':slot });
+        return this._emitItemChange('item.equip', item, { 'slot':slot });
     },
     
     unequipSlot : function unequipSlot(slot, idx){
@@ -173,7 +173,7 @@ Mixin.subclass('Container', {
             
             eqs[slot] = null;
             item.slot = null;
-            this._fireItemChange('item.unequip', item, { 'slot':slot, 'idx':idx });
+            this._emitItemChange('item.unequip', item, { 'slot':slot, 'idx':idx });
         }
         return this;
     },
@@ -262,10 +262,10 @@ Mixin.subclass('Container', {
     },
     
     
-    _fireItemChange : function _fireItemChange(evt, item, data){
+    _emitItemChange : function _emitItemChange(evt, item, data){
         data = Y.extend({ 'unit':this, 'item':item }, data || {});
-        this.fire(evt, item, data);
-        item.fire(evt, this, data);
+        this.emit(evt, item, data);
+        item.emit(evt, this, data);
         return this;
     }
     
index ad898b1..b0b8425 100644 (file)
@@ -71,7 +71,7 @@ new evt.Class('Level', {
 proxy({ target:Level, donor:Rect, context:'shape', chain:true,
     names:'append appendTo remove invoke clear'.split(' ') });
 
-Level.addEventListener('speciate',
+Level.on('speciate',
     function onSpeciate(evt){
         var NewLevel = evt.data.species
         ,   proto = NewLevel.fn
index 9afb802..837ed3e 100644 (file)
@@ -124,8 +124,8 @@ Y.subclass('Traversal', {
     
     collide : function collide(blocker){
         var thing = this.thing;
-        thing.fire('collide', blocker, { 'traversal':this, 'unit':blocker });
-        blocker.fire('collide', thing, { 'traversal':this, 'unit':thing });
+        thing.emit('collide', blocker, { 'traversal':this, 'unit':blocker });
+        blocker.emit('collide', thing, { 'traversal':this, 'unit':thing });
     },
     
     rewind : function rewind(blocker){
index 4cac56d..07af4ab 100644 (file)
@@ -73,7 +73,7 @@ Wall.subclass('Rock', {
 });
 
 // Divide on death (if possible)
-Rock.addEventListener('destroy', function(evt){
+Rock.on('destroy', function(evt){
     var d = evt.data
     ,   killer = d.killer
     ,   side = d.side
index 5c5e37a..d55914c 100644 (file)
@@ -105,7 +105,7 @@ Thing.subclass('Wall', {
 });
 
 Wall.register('wall', Wall);
-Wall.addEventListener('subclass',
+Wall.on('subclass',
     function(evt){
         var subcls = evt.data.child;
         Wall.register(Y(subcls.className).camelToSnake(), subcls);
index 6af5fa4..ae2ef2f 100644 (file)
@@ -77,7 +77,7 @@ Mixin.subclass('Inventoried', {
         if ( !this._putItem(item, idx) )
             return this;
         
-        return this._fireItemChange('item.acquire', item, { 'idx':idx });
+        return this._emitItemChange('item.acquire', item, { 'idx':idx });
     },
     
     moveItem : function moveItem(item, idx){
@@ -94,12 +94,12 @@ Mixin.subclass('Inventoried', {
             return this;
         
         idx = item.ui.bpIdx;
-        this._fireItemChange('item.move', item, { 'idx':idx, 'oldIdx':oldIdx });
+        this._emitItemChange('item.move', item, { 'idx':idx, 'oldIdx':oldIdx });
         
         if ( displacedItem && displacedItem !== item ) {
             displacedItem.ui.bpIdx = null;
             this._putItem(displacedItem, oldIdx);
-            this._fireItemChange('item.move', displacedItem, { 'idx':displacedItem.ui.bpIdx, 'oldIdx':idx });
+            this._emitItemChange('item.move', displacedItem, { 'idx':displacedItem.ui.bpIdx, 'oldIdx':idx });
         }
         
         return true;
@@ -113,7 +113,7 @@ Mixin.subclass('Inventoried', {
         if ( this._removeItem(item) )
             return this;
         
-        this._fireItemChange('item.lose', item, { 'idx':idx });
+        this._emitItemChange('item.lose', item, { 'idx':idx });
         return item;
     },
     
@@ -125,7 +125,7 @@ Mixin.subclass('Inventoried', {
         if ( this._removeItem(item) )
             return this;
         
-        this._fireItemChange('item.lose.drop', item, { 'idx':idx });
+        this._emitItemChange('item.lose.drop', item, { 'idx':idx });
         return item;
     },
     
@@ -153,7 +153,7 @@ Mixin.subclass('Inventoried', {
         eqs[slot] = item;
         item.slot = slot;
         
-        return this._fireItemChange('item.equip', item, { 'slot':slot });
+        return this._emitItemChange('item.equip', item, { 'slot':slot });
     },
     
     unequipSlot : function unequipSlot(slot, idx){
@@ -168,7 +168,7 @@ Mixin.subclass('Inventoried', {
             
             eqs[slot] = null;
             item.slot = null;
-            this._fireItemChange('item.unequip', item, { 'slot':slot, 'idx':idx });
+            this._emitItemChange('item.unequip', item, { 'slot':slot, 'idx':idx });
         }
         return this;
     },
@@ -260,10 +260,10 @@ Mixin.subclass('Inventoried', {
     },
     
     
-    _fireItemChange : function _fireItemChange(evt, item, data){
+    _emitItemChange : function _emitItemChange(evt, item, data){
         data = Y.extend({ 'unit':this, 'item':item }, data || {});
-        this.fire(evt, item, data);
-        item.fire(evt, this, data);
+        this.emit(evt, item, data);
+        item.emit(evt, this, data);
         return this;
     },
     
index 95a3e4b..453f165 100644 (file)
@@ -19,8 +19,8 @@ Mixin.subclass('Quantified', {
     onCreate : function initQuantified(evt){
         var self = evt.data.instance;
         self.buffs = Y([]);
-        self.addEventListener('buff.conjure', self.onBuffAcquired.bind(self));
-        self.addEventListener('buff.die',     self.onBuffLost.bind(self));
+        self.on('buff.conjure', self.onBuffAcquired.bind(self));
+        self.on('buff.die',     self.onBuffLost.bind(self));
     },
     
     stat : function stat(k){
index fe85c15..2c23beb 100644 (file)
@@ -63,7 +63,7 @@ Thing.subclass('Bullet', {
         this.position(x1,y1);
         this.trajectory = new Trajectory(this, x1,y1, x2,y2, this.movePerMs);
         
-        this.addEventListener('collide', this.onCollide.bind(this));
+        this.on('collide', this.onCollide.bind(this));
     },
     
     createStats : function createStats(){
index 946c2df..c70295c 100644 (file)
@@ -68,10 +68,10 @@ Thing.subclass('Item', {
         
         this.activateGauge = new CooldownGauge(this.cooldowns.activate, REF_SIZE+1,REF_SIZE+1);
         
-        this.addEventListener('collide', this.onCollide);
-        this.addEventListener('item.acquire', this.onAcquired);
-        this.addEventListener('item.lose', this.onLost);
-        this.addEventListener('item.equip', this.onEquip);
+        this.on('collide', this.onCollide);
+        this.on('item.acquire', this.onAcquired);
+        this.on('item.lose', this.onLost);
+        this.on('item.equip', this.onEquip);
     },
     
     /**
@@ -167,7 +167,7 @@ Thing.subclass('Item', {
     }
 });
 
-Item.addEventListener('speciate',
+Item.on('speciate',
     function onSpeciate(evt){
         var NewItem = evt.data.species
         ,   proto = NewItem.fn;
index 8dec937..3720205 100644 (file)
@@ -177,7 +177,7 @@ Thing.subclass('Tank', function(Tank){
         this.nShots++;
         
         var p = new Projectile(this, tx,ty, x,y);
-        p.addEventListener('destroy', this.onBulletDeath);
+        p.on('destroy', this.onBulletDeath);
         return p;
     };
     function filterShoot(v){
index 1e4f577..fbe7e42 100644 (file)
@@ -116,7 +116,7 @@ new evt.Class('Thing', {
     destroy : function destroy(killer, side){
         if (this.dead) return this;
         this.dead = true;
-        return this.remove().fire('destroy', this, { 'unit':this, 'killer':killer, 'side':side });
+        return this.remove().emit('destroy', this, { 'unit':this, 'killer':killer, 'side':side });
     },
     
     remove : function remove(){
index 21af19d..496e312 100644 (file)
@@ -54,7 +54,7 @@ function initConfigUi(){
 };
 
 // Persist config via cookies
-config.addEventListener('set', function(evt){
+config.on('set', function(evt){
     // console.log('config.set', evt);
     var d = evt.data;
     if (d.newval !== d.defval)
index 946678b..9068b70 100644 (file)
@@ -27,9 +27,9 @@ Layer.subclass('Backpack', {
             return new BackpackSlot(this, idx).appendTo(this);
         }, this);
         
-        unit.addEventListener('item.acquire', this.onItemUpdated);
-        unit.addEventListener('item.lose',    this.onItemUpdated);
-        unit.addEventListener('item.move',    this.onItemUpdated);
+        unit.on('item.acquire', this.onItemUpdated);
+        unit.on('item.lose',    this.onItemUpdated);
+        unit.on('item.move',    this.onItemUpdated);
     },
     
     refresh : function refresh(){
index faf1ce2..fe20c3a 100644 (file)
@@ -39,9 +39,9 @@ HtmlLayer.subclass('ItemContainerUI', {
         this.slots = new Y(0, this.max).map(this._makeSlot, this);
         
         if (this.defaultContainer) {
-            unit.addEventListener('item.acquire', this.onItemUpdated);
-            unit.addEventListener('item.lose',    this.onItemUpdated);
-            unit.addEventListener('item.move',    this.onItemUpdated);
+            unit.on('item.acquire', this.onItemUpdated);
+            unit.on('item.lose',    this.onItemUpdated);
+            unit.on('item.move',    this.onItemUpdated);
         }
     },
     
index 50e05ab..bbce0a8 100644 (file)
@@ -31,9 +31,9 @@ HtmlLayer.subclass('EquipSlot', {
             return new BackpackSlot(this, idx).appendTo(this);
         }, this);
         
-        unit.addEventListener('item.acquire', this.onItemUpdated);
-        unit.addEventListener('item.lose',    this.onItemUpdated);
-        unit.addEventListener('item.move',    this.onItemUpdated);
+        unit.on('item.acquire', this.onItemUpdated);
+        unit.on('item.lose',    this.onItemUpdated);
+        unit.on('item.move',    this.onItemUpdated);
     },
     
     refresh : function refresh(){
index 06c4b19..f306a6b 100644 (file)
@@ -35,10 +35,10 @@ function main(){
     
     overlay = $('#overlay');
     updateOverlay( config.get('ui.overlayOnPause') );
-    config.addEventListener('set:ui.overlayOnPause',     function(evt){ updateOverlay(evt.newval); });
-    config.addEventListener('set:ui.debug.showFpsGraph', function(evt){ updateUI('#info', evt.newval); });
+    config.on('set:ui.overlayOnPause',     function(evt){ updateOverlay(evt.newval); });
+    config.on('set:ui.debug.showFpsGraph', function(evt){ updateUI('#info', evt.newval); });
     
-    // Player.addEventListener('create', function(evt){ P = evt.data.instance; });
+    // Player.on('create', function(evt){ P = evt.data.instance; });
     
     /// Debug ///
     if (qkv.debug || config.get('debug.showFpsGraph'))
@@ -76,7 +76,7 @@ function main(){
     
     // Load all data files
     cfg.dataLoader()
-        .addEventListener('complete', function(evt){
+        .on('complete', function(evt){
             $('#loading').hide();
             $('#welcome').show();
             setupGame();
@@ -98,11 +98,11 @@ function setupGame(){
     P = game.player;
     
     if (replayFile) {
-        game.addEventListener('win',  gameover('You Won the Replay!', 'Watch Again', ''));
-        game.addEventListener('lose', gameover('You Lost the Replay :(', 'Watch Again', ''));
+        game.on('win',  gameover('You Won the Replay!', 'Watch Again', ''));
+        game.on('lose', gameover('You Lost the Replay :(', 'Watch Again', ''));
     } else {
-        game.addEventListener('win',  gameover('You Win!', 'Play Again', ''));
-        game.addEventListener('lose', gameover('You Lose :(', 'Try Again', ''));
+        game.on('win',  gameover('You Win!', 'Play Again', ''));
+        game.on('lose', gameover('You Lose :(', 'Try Again', ''));
     }
     ctx = game.level.ctx;
 }
index 435c5d7..bb5f017 100644 (file)
@@ -119,7 +119,7 @@ Rect.subclass('PathMapUI', {
     
     monitorAgent : function monitorAgent(agent){
         if ( agent && !this.hasPath(agent.__id__) )
-            agent.addEventListener('destroy', this.cleanUpAgent);
+            agent.on('destroy', this.cleanUpAgent);
     },
     
     cleanUpAgent : function cleanUpAgent(evt){ this.destroyPath(evt.target.__id__); },