UNPKG

@real_one_chess_king/game-logic

Version:
60 lines 2.59 kB
import { Direction } from "./movement-rule"; import { directionToVector, StraightMovementRule, } from "./straight-movement.rule"; import { buildMoveAffect, markAsUserSelected } from "../../affect/affect.utils"; export class PositionSpecificMovementRule extends StraightMovementRule { activatePositions; constructor({ id, name, moveToEmpty, moveToKill, collision, distance, directions, speed, activatePositions, }) { super(id, name, moveToEmpty, moveToKill, collision, distance, directions, speed); this.activatePositions = activatePositions; if (activatePositions.x && !directions.has(Direction.Left) && !directions.has(Direction.Right) && !directions.has(Direction.UpRight) && !directions.has(Direction.UpLeft) && !directions.has(Direction.DownRight) && !directions.has(Direction.DownLeft)) { throw new Error("PositionSpecificMovementRule: x position provided but no horizontal directions"); } if (activatePositions.y && !directions.has(Direction.Up) && !directions.has(Direction.Down) && !directions.has(Direction.UpRight) && !directions.has(Direction.UpLeft) && !directions.has(Direction.DownRight) && !directions.has(Direction.DownLeft)) { throw new Error("PositionSpecificMovementRule: y position provided but no vertical directions"); } } possibleDirrections = [ Direction.Up, Direction.Down, Direction.Left, Direction.Right, Direction.DownLeft, Direction.UpLeft, Direction.UpRight, Direction.DownRight, ]; calculateNewCoord = (x, y, diff, dirrection, // eslint-disable-next-line @typescript-eslint/no-unused-vars _) => { if (this.activatePositions.y?.has(y) || this.activatePositions.x?.has(x)) { return [ markAsUserSelected(buildMoveAffect([x, y], directionToVector(dirrection, x, y, diff))), ]; } return []; }; getMeta() { const xPositions = this.activatePositions.x && Array.from(this.activatePositions.x); const yPositions = this.activatePositions.y && Array.from(this.activatePositions.y); return { activatePositions: { ...(xPositions ? { x: xPositions } : {}), ...(yPositions ? { y: yPositions } : {}), }, ...super.getMeta(), }; } } //# sourceMappingURL=position-specific-movement.rule.js.map