Files
prosperon/examples/chess/movement.cm
2026-01-20 12:04:30 -06:00

37 lines
1017 B
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function MovementSystem(grid, rules) {
var newmover = meme(MovementSystem_prototype)
newmover.grid = grid;
newmover.rules = rules || {}; // expects { canMove: fn }
newmover.turn = 'white';
return newmover
}
var MovementSystem_prototype = {
tryMove: function (piece, to) {
if (piece.colour != this.turn) return false;
// normalise to into our hybrid coord
var dest = [to.x ?? t[0],
to.y ?? to[1]];
if (!this.grid.inBounds(dest)) return false;
if (!this.rules.canMove(piece, piece.coord, dest, this.grid)) return false;
var victims = this.grid.at(dest);
if (length(victims) && victims[0].colour == piece.colour) return false;
if (length(victims)) victims[0].captured = true;
this.grid.remove(piece, piece.coord);
this.grid.add (piece, dest);
// grid.add() re-creates coord; re-add .x/.y fields:
piece.coord.x = dest.x;
piece.coord.y = dest.y;
this.turn = (this.turn == 'white') ? 'black' : 'white';
return true;
}
}
return MovementSystem