ymir-js
Version:
This toolkit is created to make it easier for you to develop games like chess, checkers, go, match 3 puzzle and more. It is still under development.
179 lines (178 loc) • 7.29 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var getAvailableColumns_1 = require("../../utils/getAvailableColumns");
var parseCoord_1 = require("../../utils/parseCoord");
var item_1 = require("./item");
var Board = /** @class */ (function () {
function Board(config) {
var _this = this;
this.board = {};
this.updateBoard = function (board) {
_this.board = board;
return _this;
};
this.updateBoardWithMatrix = function (matrix) {
var newBoard = {};
matrix.forEach(function (row, rowIndex) {
row.forEach(function (item, colIndex) {
newBoard["".concat(rowIndex, "|").concat(colIndex)] = { item: new item_1.default(item) };
});
});
_this.updateBoard(newBoard);
};
this.getBoardMatrix = function () {
var matrix = [];
Object.entries(_this.board).forEach(function (_a) {
var coord = _a[0], data = _a[1];
var _b = (0, parseCoord_1.default)(coord), rowId = _b[0], colId = _b[1];
var item = __assign({ coord: coord }, data);
if (matrix[rowId]) {
matrix[rowId][colId] = item;
}
else {
matrix[rowId] = [item];
}
});
return matrix;
};
this.getItem = function (coord) {
var isExistCoord = _this.isExistCoord(coord);
if (!isExistCoord)
return null;
return _this.board[coord].item;
};
this.setItem = function (coord, item) {
var isExistCoord = _this.isExistCoord(coord);
if (!isExistCoord)
return;
_this.board[coord].item = item;
};
this.removeItem = function (coord) {
var isExistCoord = _this.isExistCoord(coord);
if (!isExistCoord)
return;
_this.board[coord].item = null;
};
this.moveItem = function (fromCoord, toCoord) {
var isExistFromCoord = _this.isExistCoord(fromCoord);
var isExistToCoord = _this.isExistCoord(toCoord);
if (!isExistFromCoord || !isExistToCoord)
return;
var item = _this.board[fromCoord].item;
_this.board[fromCoord].item = null;
_this.board[toCoord].item = item;
};
this.switchItem = function (fromCoord, toCoord) {
var isExistFromCoord = _this.isExistCoord(fromCoord);
var isExistToCoord = _this.isExistCoord(toCoord);
if (!isExistFromCoord || !isExistToCoord)
return;
var fromItem = _this.board[fromCoord].item;
var toItem = _this.board[toCoord].item;
_this.board[fromCoord].item = toItem;
_this.board[toCoord].item = fromItem;
};
this.selectItem = function (coord) {
var isExistCoord = _this.isExistCoord(coord);
if (!isExistCoord)
return;
var item = _this.board[coord].item;
if (item) {
item.selected = true;
}
};
this.deselectItem = function (coord) {
var isExistCoord = _this.isExistCoord(coord);
if (!isExistCoord)
return;
var item = _this.board[coord].item;
if (item) {
item.selected = false;
}
};
this.deselectAllItems = function () {
Object.keys(_this.board).forEach(function (coord) {
var item = _this.board[coord].item;
if (item) {
item.selected = false;
}
});
};
this.isEmpty = function (coord) {
var isExistCoord = _this.isExistCoord(coord);
if (!isExistCoord)
return;
return !_this.board[coord].item;
};
this.isExistCoord = function (coord) { return !!_this.board[coord]; };
this.getDistanceBetweenTwoCoords = function (fromCoord, toCoord) {
var isExistFromCoord = _this.isExistCoord(fromCoord);
var isExistToCoord = _this.isExistCoord(toCoord);
if (!isExistFromCoord || !isExistToCoord)
return;
var _a = (0, parseCoord_1.default)(fromCoord), fromRowId = _a[0], fromColId = _a[1];
var _b = (0, parseCoord_1.default)(toCoord), toRowId = _b[0], toColId = _b[1];
return { y: toRowId - fromRowId, x: toColId - fromColId };
};
this.getDirection = function (fromCoord, toCoord) {
var isExistFromCoord = _this.isExistCoord(fromCoord);
var isExistToCoord = _this.isExistCoord(toCoord);
if (!isExistFromCoord || !isExistToCoord)
return;
var _a = (0, parseCoord_1.default)(fromCoord), fromRowId = _a[0], fromColId = _a[1];
var _b = (0, parseCoord_1.default)(toCoord), toRowId = _b[0], toColId = _b[1];
if (fromColId > toColId && fromRowId < toRowId)
return 'bottomLeft';
if (fromColId < toColId && fromRowId < toRowId)
return 'bottomRight';
if (fromColId > toColId && fromRowId > toRowId)
return 'topLeft';
if (fromColId < toColId && fromRowId > toRowId)
return 'topRight';
if (fromColId < toColId && fromRowId === toRowId)
return 'right';
if (fromColId > toColId && fromRowId === toRowId)
return 'left';
if (fromColId === toColId && fromRowId > toRowId)
return 'top';
if (fromColId === toColId && fromRowId < toRowId)
return 'bottom';
return null;
};
this.getAvailableColumns = function (coord, movement, columnsObj) {
var avaiblableColumns = [];
var columns = (0, getAvailableColumns_1.default)(coord, movement);
if (columnsObj)
return columns;
return avaiblableColumns
.concat.apply(avaiblableColumns, Object.values(columns)).filter(_this.isExistCoord);
};
var board = Board.createBoard(config);
this.config = config;
this.board = board;
return this;
}
Board.createBoard = function (config) {
var x = config.x, y = config.y;
var board = {};
for (var rowIndex = 0; rowIndex < x; rowIndex += 1) {
for (var colIndex = 0; colIndex < y; colIndex += 1) {
board["".concat(rowIndex, "|").concat(colIndex)] = { item: null };
}
}
return board;
};
return Board;
}());
exports.default = Board;