36 lines
988 B
Plaintext
36 lines
988 B
Plaintext
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 = [!is_null(to.x) ? to.x : to[0],
|
|
!is_null(to.y) ? 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(victims[0], dest);
|
|
}
|
|
|
|
this.grid.remove(piece, piece.coord);
|
|
this.grid.add (piece, dest);
|
|
|
|
this.turn = (this.turn == 'white') ? 'black' : 'white';
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return MovementSystem
|