mirror of
https://github.com/exane/not-gwent-online
synced 2024-10-31 10:36:53 +00:00
80 lines
1.4 KiB
JavaScript
80 lines
1.4 KiB
JavaScript
var Battle = (function(){
|
|
var Battle = function(){
|
|
if(!(this instanceof Battle)){
|
|
return (new Battle());
|
|
}
|
|
/**
|
|
* constructor here
|
|
*/
|
|
|
|
this.events = {};
|
|
|
|
|
|
};
|
|
var r = Battle.prototype;
|
|
/**
|
|
* methods && properties here
|
|
* r.property = null;
|
|
* r.getProperty = function() {...}
|
|
*/
|
|
|
|
r.events = null;
|
|
|
|
r.runEvent = function(eventid, ctx, args){
|
|
ctx = ctx || this;
|
|
args = args || [];
|
|
var event = "on" + eventid;
|
|
|
|
if(!this.events[event]){
|
|
return;
|
|
}
|
|
this.events[event].forEach(function(e){
|
|
var obj = e;
|
|
obj.cb = obj.cb.bind(ctx)
|
|
obj.cb.apply(ctx, obj.onArgs.concat(args));
|
|
});
|
|
}
|
|
|
|
r.on = function(eventid, cb, ctx, args){
|
|
ctx = ctx || null;
|
|
args = args || [];
|
|
var event = "on" + eventid;
|
|
|
|
var obj = {};
|
|
if(!ctx) {
|
|
obj.cb = cb;
|
|
} else {
|
|
obj.cb = cb.bind(ctx);
|
|
}
|
|
obj.onArgs = args;
|
|
|
|
if(!(event in this.events)){
|
|
this.events[event] = [];
|
|
}
|
|
|
|
if(typeof cb !== "function"){
|
|
throw new Error("cb not a function");
|
|
}
|
|
|
|
if(args){
|
|
this.events[event].push(obj);
|
|
}
|
|
|
|
else {
|
|
this.events[event].push(obj);
|
|
}
|
|
}
|
|
|
|
r.off = function(eventid){
|
|
var event = "on" + eventid;
|
|
this.events[event].forEach(function(e){
|
|
e = null;
|
|
});
|
|
delete this.events[event];
|
|
}
|
|
|
|
|
|
return Battle;
|
|
})();
|
|
|
|
module.exports = Battle; |