@gamepark/rules-api
Version:
API to implement the rules of a board game
70 lines • 2.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PositiveSequenceStrategy = void 0;
/**
* This strategy help to maintain a consecutive sequence of numbers starting with 0 for items at the same location,
* for example a deck or a hand of cards.
*/
var PositiveSequenceStrategy = /** @class */ (function () {
function PositiveSequenceStrategy(axis) {
if (axis === void 0) { axis = 'x'; }
this.axis = axis;
}
PositiveSequenceStrategy.prototype.addItem = function (material, item) {
var x = item.location[this.axis];
if (x === undefined) {
item.location[this.axis] = material.length;
}
else {
for (var _i = 0, _a = material.getItems(); _i < _a.length; _i++) {
var item_1 = _a[_i];
var itemX = item_1.location[this.axis];
if (itemX !== undefined && itemX >= x) {
item_1.location[this.axis]++;
}
}
}
};
PositiveSequenceStrategy.prototype.moveItem = function (material, item, index) {
if (item.location[this.axis] === undefined) {
item.location[this.axis] = material.length - 1;
}
var x = material.getItem(index).location[this.axis];
if (x === undefined)
return;
var newX = item.location[this.axis];
if (x < newX) {
for (var _i = 0, _a = material.getItems(); _i < _a.length; _i++) {
var item_2 = _a[_i];
var itemX = item_2.location[this.axis];
if (itemX !== undefined && itemX > x && itemX <= newX) {
item_2.location[this.axis]--;
}
}
}
else if (newX < x) {
for (var _b = 0, _c = material.getItems(); _b < _c.length; _b++) {
var item_3 = _c[_b];
var itemX = item_3.location[this.axis];
if (itemX !== undefined && itemX >= newX && itemX < x) {
item_3.location[this.axis]++;
}
}
}
};
PositiveSequenceStrategy.prototype.removeItem = function (material, item) {
var x = item.location[this.axis];
if (x === undefined)
return;
for (var _i = 0, _a = material.getItems(); _i < _a.length; _i++) {
var item_4 = _a[_i];
var itemX = item_4.location[this.axis];
if (itemX !== undefined && itemX > x) {
item_4.location[this.axis]--;
}
}
};
return PositiveSequenceStrategy;
}());
exports.PositiveSequenceStrategy = PositiveSequenceStrategy;
//# sourceMappingURL=PositiveSequenceStrategy.js.map