|
|
|
@ -5,13 +5,27 @@ const Board = function({ parent, squares }) { |
|
|
|
|
this.walls = {}; |
|
|
|
|
this.robots = {}; |
|
|
|
|
this.listeners = {}; |
|
|
|
|
this.initialRobotState = []; |
|
|
|
|
|
|
|
|
|
this.drawSquares(); |
|
|
|
|
|
|
|
|
|
document.addEventListener('board-reset', this.onReset.bind(this)); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Board.prototype.drawRobots = function(robots) { |
|
|
|
|
this.initialRobotState = robots; |
|
|
|
|
this.robots = {}; |
|
|
|
|
|
|
|
|
|
while (this.parent.getElementsByClassName('content-robot').length) { |
|
|
|
|
const child = this.parent.getElementsByClassName('content-robot')[0]; |
|
|
|
|
child.parentNode.removeChild(child); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
while (this.parent.getElementsByClassName('content-arrows').length) { |
|
|
|
|
const child = this.parent.getElementsByClassName('content-arrows')[0]; |
|
|
|
|
child.parentNode.removeChild(child); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
robots.forEach(({ color, i, j }) => { |
|
|
|
|
const { x, y } = this.squares.ijToXy({ i, j }); |
|
|
|
|
const s = this.squares.sideLength; |
|
|
|
@ -163,6 +177,9 @@ Board.prototype.moveRobot = function({ id, i, j }) { |
|
|
|
|
arrows.style.top = (y - s) + 'px'; |
|
|
|
|
|
|
|
|
|
this.updateArrowVisibilities(); |
|
|
|
|
|
|
|
|
|
var event = new Event('move-increment'); |
|
|
|
|
document.dispatchEvent(event); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Board.prototype.updateArrowVisibilities = function() { |
|
|
|
@ -207,6 +224,10 @@ Board.prototype.onArrowClick = function(evt) { |
|
|
|
|
this.moveRobot({ id, i: i2, j: j2 }) |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Board.prototype.onReset = function() { |
|
|
|
|
this.drawRobots(this.initialRobotState); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Board.prototype.findNextObstacle = function({ direction, i, j }) { |
|
|
|
|
switch (direction) { |
|
|
|
|
case 'right': |
|
|
|
|