BattleManagerTBS.checkScopeWalkable = function (cells, range, center) {
for (var i = 0; i < cells.length; i++) {
cells[i]._walkable = false;
}
var grid = this.getWalkableGridForEasyStar();
this._easystar.setGrid(grid);
this._easystar.setAcceptableTiles([0]);
this._easystar.enableSync();
var scope = LeUtilities.sortByClosestDistance(center, cells);
scope.reverse();
var reachables = [];
for (i = 0; i < scope.length; i++) {
var cell = scope[i];
cell._walkable = this.isCellReachable(cell, range, center, reachables);
reachables = LeUtilities.uniqArray(reachables);
}
};
BattleManagerTBS.isCellReachable = function (cell, range, center, reachables) {
if (cell.getEntity()) {
if (!cell.getEntity().entitiesCanLayOnMe())
return false;
} else if (cell.isObstacle())
return false;
for (var j = 0; j < reachables.length; j++) {
if (cell.isSame(reachables[j])) {
return true;
}
}
var path = [];
var pathResult = null;
var sx = center.x;
var sy = center.y;
var dx = cell.x;
var dy = cell.y;
this._easystar.findPath(sx, sy, dx, dy, function (result) {
pathResult = result;
});
this._easystar.calculate();
if (!pathResult)
return false;
pathResult.shift();
if (pathResult.length > range)
return false;
if (reachables) reachables.push(cell);
return true;
};
BattleManagerTBS.getPathFromAToB = function (sx, sy, dx, dy, gridType, cellsToIgnore) {
var path = [];
var pathResult = null;
var grid;
if (gridType === "walkable")
grid = this.getWalkableGridForEasyStar(cellsToIgnore);
else if (gridType === "free")
grid = this.getFreeGridForEasyStar();
else
grid = this.getGridForEasyStar();
this._easystar.setGrid(grid);
this._easystar.setAcceptableTiles([0]);
this._easystar.enableSync();
this._easystar.findPath(sx, sy, dx, dy, function (result) {
pathResult = result;
});
this._easystar.calculate();
if (!pathResult) return [];
pathResult.shift();
for (var i = 0; i < pathResult.length; i++) {
var cellArr = pathResult[i];
var cell = this.getCellAt(cellArr.x, cellArr.y);
path.push(cell);
}
return path;
};
BattleManagerTBS.getGridForEasyStar = function () {
var grid = [];
for (var y = 0; y < $gameMap.height(); y++) {
var arr = [];
for (var x = 0; x < $gameMap.width(); x++) {
var cell = this.getCellAt(x, y);
if (!cell || cell.isObstacle())
arr.push(1);
else
arr.push(0);
}
grid.push(arr);
}
return grid;
};
BattleManagerTBS.getFreeGridForEasyStar = function () {
var grid = [];
for (var y = 0; y < $gameMap.height(); y++) {
var arr = [];
for (var x = 0; x < $gameMap.width(); x++) {
var cell = this.getCellAt(x, y);
if (!cell)
arr.push(1);
else
arr.push(0);
}
grid.push(arr);
}
return grid;
};
BattleManagerTBS.getWalkableGridForEasyStar = function (cellsToIgnore) {
cellsToIgnore = cellsToIgnore || [];
var grid = [];
for (var y = 0; y < $gameMap.height(); y++) {
var arr = [];
for (var x = 0; x < $gameMap.width(); x++) {
var cell = this.getCellAt(x, y);
if (this.isCellInScope(cell, cellsToIgnore)) {
arr.push(0);
} else if (cell) {
var entity = cell.getEntity();
if (entity) {
arr.push(entity.isPassable() ? 0 : 1);
} else {
arr.push(cell.isObstacle() ? 1 : 0);
}
} else
arr.push(1);
}
grid.push(arr);
}
return grid;
};