2015-06-14 14:01:25 +00:00
|
|
|
var Field = (function(){
|
|
|
|
var Field = function(){
|
|
|
|
if(!(this instanceof Field)){
|
|
|
|
return (new Field());
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* constructor here
|
|
|
|
*/
|
|
|
|
|
|
|
|
this._cards = [];
|
|
|
|
};
|
|
|
|
var r = Field.prototype;
|
|
|
|
/**
|
|
|
|
* methods && properties here
|
|
|
|
* r.property = null;
|
|
|
|
* r.getProperty = function() {...}
|
|
|
|
*/
|
|
|
|
|
|
|
|
r._cards = null;
|
|
|
|
r._score = 0;
|
|
|
|
|
2015-06-14 18:50:53 +00:00
|
|
|
r.add = function(card){
|
2015-06-14 14:01:25 +00:00
|
|
|
this._cards.push(card);
|
|
|
|
this.updateScore();
|
|
|
|
}
|
|
|
|
|
2015-06-14 18:50:53 +00:00
|
|
|
r.get = function(){
|
2015-06-14 14:01:25 +00:00
|
|
|
return this._cards;
|
|
|
|
}
|
|
|
|
|
2015-06-14 18:50:53 +00:00
|
|
|
r.getScore = function(){
|
|
|
|
this.updateScore();
|
2015-06-14 14:01:25 +00:00
|
|
|
return this._score;
|
|
|
|
}
|
|
|
|
|
2015-06-14 18:50:53 +00:00
|
|
|
r.updateScore = function(){
|
2015-06-14 14:01:25 +00:00
|
|
|
this._score = 0;
|
2015-06-14 18:50:53 +00:00
|
|
|
for(var i = 0; i < this._cards.length; i++) {
|
2015-06-14 14:01:25 +00:00
|
|
|
var card = this._cards[i];
|
|
|
|
this._score += card.getPower();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-14 18:50:53 +00:00
|
|
|
r.getPosition = function(card){
|
|
|
|
for(var i = 0; i < this._cards.length; i++) {
|
|
|
|
if(this._cards[i].getID() === card.getID()) return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-19 19:53:48 +00:00
|
|
|
r.isOnField = function(card){
|
|
|
|
return this.getPosition(card) >= 0;
|
|
|
|
}
|
|
|
|
|
2015-06-14 18:50:53 +00:00
|
|
|
r.replaceWith = function(oldCard, newCard){
|
|
|
|
var index = this.getPosition(oldCard);
|
|
|
|
this._cards[index] = newCard;
|
2015-06-19 19:53:48 +00:00
|
|
|
oldCard.reset();
|
2015-06-14 18:50:53 +00:00
|
|
|
return oldCard;
|
|
|
|
}
|
|
|
|
|
|
|
|
r.getCard = function(id){
|
|
|
|
for(var i = 0; i < this._cards.length; i++) {
|
|
|
|
var card = this._cards[i];
|
|
|
|
if(card.getID() == id) return card;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-19 19:53:48 +00:00
|
|
|
r.removeAll = function(){
|
2015-06-17 19:18:14 +00:00
|
|
|
var tmp = this._cards.slice();
|
2015-06-19 19:53:48 +00:00
|
|
|
tmp.forEach(function(card){
|
|
|
|
card.reset();
|
2015-06-19 12:14:37 +00:00
|
|
|
})
|
2015-06-17 19:18:14 +00:00
|
|
|
this._cards = [];
|
|
|
|
return tmp;
|
|
|
|
}
|
2015-06-14 14:01:25 +00:00
|
|
|
|
|
|
|
return Field;
|
|
|
|
})();
|
|
|
|
|
|
|
|
module.exports = Field;
|