@real_one_chess_king/game-logic
Version:
R.O.C.K. chess game logic
107 lines • 4.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StraightMovementRule = void 0;
exports.directionToVector = directionToVector;
const movement_rule_1 = require("./movement-rule");
const affect_types_1 = require("../../affect/affect.types");
const affect_utils_1 = require("../../affect/affect.utils");
const mapDirectionToVector = {
[movement_rule_1.Direction.UpLeft]: (x, y, diff) => [
x - diff,
y - diff,
],
[movement_rule_1.Direction.UpRight]: (x, y, diff) => [
x + diff,
y - diff,
],
[movement_rule_1.Direction.DownLeft]: (x, y, diff) => [
x - diff,
y + diff,
],
[movement_rule_1.Direction.DownRight]: (x, y, diff) => [
x + diff,
y + diff,
],
[movement_rule_1.Direction.Up]: (x, y, diff) => [
x,
y - diff,
],
[movement_rule_1.Direction.Down]: (x, y, diff) => [
x,
y + diff,
],
[movement_rule_1.Direction.Right]: (x, y, diff) => [
x + diff,
y,
],
[movement_rule_1.Direction.Left]: (x, y, diff) => [
x - diff,
y,
],
};
function directionToVector(direction, x, y, diff) {
return mapDirectionToVector[direction](x, y, diff);
}
class StraightMovementRule extends movement_rule_1.MovementRule {
constructor(id, name, moveToEmpty, moveToKill, collision, // true - will move until first enemy, false - will jump like horse
distance, directions, speed) {
super(id, name, moveToEmpty, moveToKill, collision, distance, directions, speed);
}
isCoordInvalid(x, y, size) {
return x >= size || x < 0 || y >= size || y < 0;
}
availableMoves(fromX, fromY, getPiece, turns, size) {
const moves = [];
const availableDirections = new Set(this.possibleDirrections);
for (let diff = this.speed; diff <= this.distance; diff += this.speed) {
for (const dirrection of this.directions) {
if (availableDirections.has(dirrection)) {
const affects = this.calculateNewCoord(fromX, fromY, diff, dirrection, turns);
if (!affects.length) {
availableDirections.delete(dirrection);
continue;
}
// const [newX, newY] = availableMove;
const moveAffect = affects.find((a) => a.type === affect_types_1.AffectType.move);
if (!moveAffect) {
throw new Error("Move affect not found");
}
const [newX, newY] = moveAffect.to;
if (this.isCoordInvalid(newX, newY, size)) {
// todo remove magic
availableDirections.delete(dirrection);
}
else {
const fromPiece = getPiece(fromX, fromY);
if (!fromPiece) {
throw new Error("Not found piece at from location");
}
const toPiece = getPiece(newX, newY);
if (this.moveToEmpty && !toPiece) {
// can move to empty square
moves.push(affects);
}
else if (toPiece) {
const newLocationPieceColor = toPiece.color;
if (this.moveToKill &&
newLocationPieceColor !== fromPiece.color) {
const killAffect = (0, affect_utils_1.buildKillAffect)(moveAffect.to);
// affects.push(killAffect);
moves.push([killAffect, ...affects]);
}
if (this.collision) {
availableDirections.delete(dirrection);
}
}
}
}
if (availableDirections.size === 0) {
diff = this.distance + 1; // end search, nowhere to go
}
}
}
return moves;
}
}
exports.StraightMovementRule = StraightMovementRule;
//# sourceMappingURL=straight-movement.rule.js.map