|
|
|
@ -8,16 +8,19 @@ const Grid = function() { |
|
|
|
|
this.shadows = []; |
|
|
|
|
this.walls = []; |
|
|
|
|
this.objective = {}; |
|
|
|
|
this.timers = {}; |
|
|
|
|
|
|
|
|
|
this.squaresPerSide = 20; |
|
|
|
|
this.squareSideLength = 0; |
|
|
|
|
|
|
|
|
|
document.addEventListener('L-newround', this.msgNewRound.bind(this)); |
|
|
|
|
document.addEventListener('L-stack', this.msgStack.bind(this)); |
|
|
|
|
document.addEventListener('L-shadows', this.msgShadows.bind(this)); |
|
|
|
|
|
|
|
|
|
document.addEventListener('G-robots', this.msgRobots.bind(this)); |
|
|
|
|
document.addEventListener('G-walls', this.msgWalls.bind(this)); |
|
|
|
|
document.addEventListener('G-objective', this.msgObjective.bind(this)); |
|
|
|
|
document.addEventListener('G-win', this.msgWin.bind(this)); |
|
|
|
|
|
|
|
|
|
window.addEventListener('resize', this.debounce(this.onResize.bind(this), 500)); |
|
|
|
|
|
|
|
|
@ -336,6 +339,37 @@ Grid.prototype.checkObjective = function({ id, i, j }) { |
|
|
|
|
document.dispatchEvent(evtSolve); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Grid.prototype.replayStack = function(stack) { |
|
|
|
|
// All to initial positions
|
|
|
|
|
for (let i = 0; i < this.robots.length; i++) { |
|
|
|
|
this.replayMove(stack[i]); |
|
|
|
|
document.getElementById(`arrows-${stack[i].id}`).style.display = 'none'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function replayRemaining(remainingStack) { |
|
|
|
|
if (remainingStack.length > 0) { |
|
|
|
|
this.replayMove(remainingStack[0]); |
|
|
|
|
this.timers.replay = setTimeout(replayRemaining.bind(this, remainingStack.slice(1)), 750); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
const evtSolve = new Event('L-replay-complete'); |
|
|
|
|
document.dispatchEvent(evtSolve); |
|
|
|
|
|
|
|
|
|
this.timers.replay = setTimeout(this.replayStack.bind(this, stack), 750); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.timers.replay = setTimeout(replayRemaining.bind(this, stack.slice(this.robots.length)), 750) |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Grid.prototype.replayMove = function({ id, i, j }) { |
|
|
|
|
const robot = document.getElementById(`robot-${id}`); |
|
|
|
|
const s = this.squareSideLength; |
|
|
|
|
|
|
|
|
|
robot.style.left = `${i * s}px`; |
|
|
|
|
robot.style.top = `${j * s}px`; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
//===== DOM event handlers
|
|
|
|
|
|
|
|
|
|
Grid.prototype.onArrowClick = function(evt) { |
|
|
|
@ -398,6 +432,11 @@ Grid.prototype.debounce = function(fn, ms) { |
|
|
|
|
|
|
|
|
|
//===== Message handlers
|
|
|
|
|
|
|
|
|
|
Grid.prototype.msgNewRound = function() { |
|
|
|
|
this.robots.forEach(({ id }) => document.getElementById(`arrows-${id}`).style.display = 'block'); |
|
|
|
|
clearTimeout(this.timers.replay); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Grid.prototype.msgRobots = function(evt) { |
|
|
|
|
// Do not assign position or redraw here: movements are fully managed using the stack.
|
|
|
|
|
this.colors = {}; |
|
|
|
@ -442,3 +481,7 @@ Grid.prototype.msgObjective = function(evt) { |
|
|
|
|
this.objective = evt.detail.body; |
|
|
|
|
this.drawObjective(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Grid.prototype.msgWin = function(evt) { |
|
|
|
|
this.replayStack(evt.detail.body.stack) |
|
|
|
|
}; |
|
|
|
|