mirror of
https://github.com/exane/not-gwent-online
synced 2024-12-23 21:03:14 +00:00
add own event system
This commit is contained in:
parent
14511b35fc
commit
f0cbb9de5d
@ -1,5 +1,5 @@
|
||||
var Battleside = require("./Battleside");
|
||||
var PubSub = require("pubsub-js");
|
||||
//var PubSub = require("pubsub-js");
|
||||
var Card = require("./Card");
|
||||
|
||||
/*var io = global.io;*/
|
||||
@ -12,6 +12,7 @@ var Battle = (function(){
|
||||
/**
|
||||
* constructor here
|
||||
*/
|
||||
this.events = {};
|
||||
this._id = id;
|
||||
this._user1 = p1;
|
||||
this._user2 = p2;
|
||||
@ -39,7 +40,8 @@ var Battle = (function(){
|
||||
r.events = null;
|
||||
|
||||
r.init = function(){
|
||||
PubSub.subscribe("update", this.update.bind(this));
|
||||
/*PubSub.subscribe("update", this.update.bind(this));*/
|
||||
this.on("Update", this.update);
|
||||
|
||||
|
||||
this.channel = this.socket.subscribe(this._id);
|
||||
@ -53,7 +55,6 @@ var Battle = (function(){
|
||||
this.start();
|
||||
}
|
||||
|
||||
|
||||
r.start = function(){
|
||||
this.p1.setLeadercard();
|
||||
this.p2.setLeadercard();
|
||||
@ -68,7 +69,8 @@ var Battle = (function(){
|
||||
this.update();
|
||||
|
||||
|
||||
PubSub.subscribe("nextTurn", this.switchTurn.bind(this));
|
||||
/*PubSub.subscribe("nextTurn", this.switchTurn.bind(this));*/
|
||||
this.on("NextTurn", this.switchTurn);
|
||||
|
||||
this.switchTurn();
|
||||
}
|
||||
@ -97,8 +99,11 @@ var Battle = (function(){
|
||||
return this.switchTurn(1);
|
||||
}
|
||||
|
||||
PubSub.publish("onEachTurn");
|
||||
PubSub.publish("turn/" + side.getID());
|
||||
/*PubSub.publish("onEachTurn");*/
|
||||
this.runEvent("EachTurn");
|
||||
/*
|
||||
PubSub.publish("turn/" + side.getID());*/
|
||||
this.runEvent("Turn" + side.getID());
|
||||
console.log("current Turn: ", side.getName());
|
||||
|
||||
}
|
||||
@ -135,37 +140,45 @@ var Battle = (function(){
|
||||
});
|
||||
}
|
||||
|
||||
r.runEvent = function(eventid, target){
|
||||
r.runEvent = function(eventid, target, args){
|
||||
target = target || this;
|
||||
this.events["on" + eventid].forEach(function(event) {
|
||||
event.call(target);
|
||||
});
|
||||
}
|
||||
args = args || [];
|
||||
var event = "on" + eventid;
|
||||
|
||||
r.on = function(eventid, cb){
|
||||
if(!this.events["on" + eventid]) {
|
||||
this.events["on" + eventid] = [];
|
||||
if(!this.events["on" + eventid]){
|
||||
return;
|
||||
}
|
||||
this.events["on" + eventid].push(cb);
|
||||
}
|
||||
|
||||
r.off = function(eventid) {
|
||||
this.events["on" + eventid].forEach(function(event) {
|
||||
event = null;
|
||||
this.events[event].forEach(function(e){
|
||||
e.apply(target, args);
|
||||
});
|
||||
delete this.events["on" + eventid];
|
||||
}
|
||||
|
||||
/*r._setUpChannel = function() {
|
||||
var self = this;
|
||||
this._abilityChannel.watch(function(d) {
|
||||
var event = d.event, data = d.data;
|
||||
r.on = function(eventid, cb, ctx, args){
|
||||
ctx = ctx || this;
|
||||
args = args || null;
|
||||
var event = "on" + eventid;
|
||||
if(!(event in this.events)){
|
||||
this.events[event] = [];
|
||||
}
|
||||
/*if(!this.events[event]) {
|
||||
this.events[event] = [];
|
||||
}*/
|
||||
if(args){
|
||||
this.events[event].push(cb.bind(ctx, args));
|
||||
}
|
||||
else {
|
||||
this.events[event].push(cb.bind(ctx));
|
||||
}
|
||||
}
|
||||
|
||||
r.off = function(eventid){
|
||||
var event = "on" + eventid;
|
||||
this.events[event].forEach(function(e){
|
||||
e = null;
|
||||
});
|
||||
delete this.events[event];
|
||||
}
|
||||
|
||||
if(event === "update") {
|
||||
data();
|
||||
}
|
||||
})
|
||||
}*/
|
||||
|
||||
return Battle;
|
||||
})();
|
||||
|
@ -4,7 +4,7 @@ var Deck = require("./Deck");
|
||||
var Hand = require("./Hand");
|
||||
var Card = require("./Card");
|
||||
var Field = require("./Field");
|
||||
var PubSub = require("pubsub-js");
|
||||
//var PubSub = require("pubsub-js");
|
||||
|
||||
|
||||
var Battleside;
|
||||
@ -31,6 +31,10 @@ Battleside = (function(){
|
||||
this.hand = Hand();
|
||||
this.deck = Deck(DeckData["test"]);
|
||||
|
||||
this.runEvent = this.battle.runEvent.bind(this.battle);
|
||||
this.on = this.battle.on.bind(this.battle);
|
||||
this.off = this.battle.off.bind(this.battle);
|
||||
|
||||
|
||||
this.receive("play:cardFromHand", function(data){
|
||||
if(self._isWaiting) return;
|
||||
@ -43,16 +47,19 @@ Battleside = (function(){
|
||||
this.receive("decoy:replaceWith", function(data){
|
||||
if(self._isWaiting) return;
|
||||
var card = self.findCardOnFieldByID(data.cardID);
|
||||
if(card === -1) throw "decoy:replace | unknown card";
|
||||
PubSub.publish("decoy:replaceWith", card);
|
||||
if(card === -1) throw new Error("decoy:replace | unknown card");
|
||||
/*PubSub.publish("decoy:replaceWith", card);*/
|
||||
self.runEvent("Decoy:replaceWith", self, [card]);
|
||||
})
|
||||
this.receive("set:passing", function() {
|
||||
self.setPassing(true);
|
||||
self.update();
|
||||
PubSub.publish("nextTurn");
|
||||
self.update();/*
|
||||
PubSub.publish("nextTurn");*/
|
||||
self.runEvent("NextTurn");
|
||||
})
|
||||
|
||||
PubSub.subscribe("turn/" + this.getID(), this.onTurnStart.bind(this));
|
||||
/*PubSub.subscribe("turn/" + this.getID(), this.onTurnStart.bind(this));*/
|
||||
this.on("Turn" + this.getID(), this.onTurnStart, this);
|
||||
};
|
||||
var r = Battleside.prototype;
|
||||
/**
|
||||
@ -174,7 +181,8 @@ Battleside = (function(){
|
||||
}
|
||||
|
||||
r.update = function(){
|
||||
PubSub.publish("update");
|
||||
//PubSub.publish("update");
|
||||
this.runEvent("Update");
|
||||
}
|
||||
|
||||
r.onTurnStart = function(){
|
||||
@ -195,7 +203,8 @@ Battleside = (function(){
|
||||
|
||||
this.update();
|
||||
|
||||
PubSub.publish("nextTurn");
|
||||
//PubSub.publish("nextTurn");
|
||||
this.runEvent("NextTurn");
|
||||
}
|
||||
|
||||
r.placeCard = function(card){
|
||||
@ -207,7 +216,8 @@ Battleside = (function(){
|
||||
var field = obj.targetSide.field[card.getType()];
|
||||
field.add(card);
|
||||
|
||||
PubSub.publish("onEachCardPlace");
|
||||
//PubSub.publish("onEachCardPlace");
|
||||
this.runEvent("OnEachCardPlace");
|
||||
|
||||
this.checkAbilityOnAfterPlace(card);
|
||||
|
||||
@ -226,13 +236,15 @@ Battleside = (function(){
|
||||
if(ability.replaceWith){
|
||||
obj._canclePlacement = true;
|
||||
|
||||
var decoy = PubSub.subscribe("decoy:replaceWith", function(event, replaceCard){
|
||||
//var decoy = PubSub.subscribe("decoy:replaceWith", function(event, replaceCard){
|
||||
this.on("Decoy:replaceWith", function(replaceCard) {
|
||||
if(replaceCard.getType() == Card.TYPE.LEADER ||
|
||||
replaceCard.getType() == Card.TYPE.WEATHER ||
|
||||
replaceCard.getType() == Card.TYPE.SPECIAL){
|
||||
return;
|
||||
}
|
||||
PubSub.unsubscribe(decoy);
|
||||
/*PubSub.unsubscribe(decoy);*/
|
||||
self.off("Decoy:replaceWith");
|
||||
var field = self.field[replaceCard.getType()];
|
||||
|
||||
field.replaceWith(replaceCard, card);
|
||||
@ -241,14 +253,17 @@ Battleside = (function(){
|
||||
|
||||
self.update();
|
||||
|
||||
PubSub.publish("nextTurn");
|
||||
/*PubSub.publish("nextTurn");*/
|
||||
self.runEvent("NextTurn");
|
||||
})
|
||||
}
|
||||
if(ability.onEachTurn){
|
||||
PubSub.subscribe("onEachTurn", ability.onEachTurn.bind(this, card));
|
||||
//PubSub.subscribe("onEachTurn", ability.onEachTurn.bind(this, card));
|
||||
this.on("EachTurn", ability.onEachTurn, this, [card])
|
||||
}
|
||||
if(ability.onEachCardPlace){
|
||||
PubSub.subscribe("onEachCardPlace", ability.onEachCardPlace.bind(this, card));
|
||||
//PubSub.subscribe("onEachCardPlace", ability.onEachCardPlace.bind(this, card));
|
||||
this.on("EachCardPlace", ability.onEachCardPlace, this, [card]);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user