|
|
@ -9,23 +9,22 @@ const Content = function({ parent, squares }) { |
|
|
|
this.listeners = {}; |
|
|
|
this.listeners = {}; |
|
|
|
this.initialRobotState = []; |
|
|
|
this.initialRobotState = []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.playerId = null; |
|
|
|
|
|
|
|
this.isSolving = false; |
|
|
|
|
|
|
|
this.isSolver = false; |
|
|
|
|
|
|
|
|
|
|
|
this.drawSquares(); |
|
|
|
this.drawSquares(); |
|
|
|
|
|
|
|
|
|
|
|
document.addEventListener('L-reset', this.onReset.bind(this)); |
|
|
|
document.addEventListener('L-reset', this.msgReset.bind(this)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
document.addEventListener('G-connected', this.msgConnected.bind(this)); |
|
|
|
document.addEventListener('G-walls', this.msgWalls.bind(this)); |
|
|
|
document.addEventListener('G-walls', this.msgWalls.bind(this)); |
|
|
|
document.addEventListener('G-robots', this.msgRobots.bind(this)); |
|
|
|
document.addEventListener('G-robots', this.msgRobots.bind(this)); |
|
|
|
|
|
|
|
document.addEventListener('G-solve', this.msgSolve.bind(this)); |
|
|
|
|
|
|
|
document.addEventListener('G-move', this.msgMove.bind(this)); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
//===== Event handlers
|
|
|
|
//===== UI
|
|
|
|
|
|
|
|
|
|
|
|
Content.prototype.msgRobots = function(evt) { |
|
|
|
|
|
|
|
this.initialRobotState = evt.detail.body; |
|
|
|
|
|
|
|
this.drawRobots()
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//===== Event handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Content.prototype.drawRobots = function() { |
|
|
|
Content.prototype.drawRobots = function() { |
|
|
|
this.robots = {}; |
|
|
|
this.robots = {}; |
|
|
@ -127,9 +126,7 @@ Content.prototype.drawSquares = function() { |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Content.prototype.msgWalls = function(msg) { |
|
|
|
Content.prototype.drawWalls = function(edges) { |
|
|
|
const edges = msg.detail.body; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.walls = {}; |
|
|
|
this.walls = {}; |
|
|
|
|
|
|
|
|
|
|
|
this.parent.querySelectorAll('.content-wall-x').forEach(el => el.parentNode.removeChild(el)); |
|
|
|
this.parent.querySelectorAll('.content-wall-x').forEach(el => el.parentNode.removeChild(el)); |
|
|
@ -189,10 +186,7 @@ Content.prototype.moveRobot = function({ id, i, j }) { |
|
|
|
arrows.style.top = (y - s) + 'px'; |
|
|
|
arrows.style.top = (y - s) + 'px'; |
|
|
|
|
|
|
|
|
|
|
|
this.updateArrowVisibilities(); |
|
|
|
this.updateArrowVisibilities(); |
|
|
|
|
|
|
|
}; |
|
|
|
var event = new Event('L-move'); |
|
|
|
|
|
|
|
document.dispatchEvent(event); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Content.prototype.updateArrowVisibilities = function() { |
|
|
|
Content.prototype.updateArrowVisibilities = function() { |
|
|
|
const keys = Object.keys(this.robots); |
|
|
|
const keys = Object.keys(this.robots); |
|
|
@ -219,29 +213,6 @@ Content.prototype.updateArrowVisibilities = function() { |
|
|
|
arrows.querySelector("[data-direction='up']").style.display = (this.robots[ijU] || this.walls[edgeU] || j === 0) ? 'none' : 'block'; |
|
|
|
arrows.querySelector("[data-direction='up']").style.display = (this.robots[ijU] || this.walls[edgeU] || j === 0) ? 'none' : 'block'; |
|
|
|
arrows.querySelector("[data-direction='down']").style.display = (this.robots[ijD] || this.walls[edgeD] || j === (this.squares.perSide - 1)) ? 'none' : 'block'; |
|
|
|
arrows.querySelector("[data-direction='down']").style.display = (this.robots[ijD] || this.walls[edgeD] || j === (this.squares.perSide - 1)) ? 'none' : 'block'; |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Content.prototype.onArrowClick = function(evt) { |
|
|
|
|
|
|
|
const direction = evt.currentTarget.dataset.direction; |
|
|
|
|
|
|
|
const id = evt.currentTarget.dataset.parent; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const robot = document.getElementById(`robot-${id}`); |
|
|
|
|
|
|
|
const i = robot.dataset.i * 1; |
|
|
|
|
|
|
|
const j = robot.dataset.j * 1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
delete this.robots[`${i}-${j}`]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { i: i2, j: j2 } = this.findNextObstacle({ direction, i, j }); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.moveRobot({ id, i: i2, j: j2 }) |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Content.prototype.onReceiveGuess = function() { |
|
|
|
|
|
|
|
console.log("Board onReceiveGuess()") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Content.prototype.onReset = function() { |
|
|
|
|
|
|
|
this.drawRobots(); |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Content.prototype.findNextObstacle = function({ direction, i, j }) { |
|
|
|
Content.prototype.findNextObstacle = function({ direction, i, j }) { |
|
|
@ -291,4 +262,66 @@ Content.prototype.findNextObstacle = function({ direction, i, j }) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
throw Error("Could not find next obstacle, no direction found. ", direction, i, j); |
|
|
|
throw Error("Could not find next obstacle, no direction found. ", direction, i, j); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//===== Click handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Content.prototype.onArrowClick = function(evt) { |
|
|
|
|
|
|
|
const direction = evt.currentTarget.dataset.direction; |
|
|
|
|
|
|
|
const id = evt.currentTarget.dataset.parent; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const robot = document.getElementById(`robot-${id}`); |
|
|
|
|
|
|
|
const i = robot.dataset.i * 1; |
|
|
|
|
|
|
|
const j = robot.dataset.j * 1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
delete this.robots[`${i}-${j}`]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { i: i2, j: j2 } = this.findNextObstacle({ direction, i, j }); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.moveRobot({ id, i: i2, j: j2 }); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const detail = { id, i: i2, j: j2, emitIf: this.isSolving }; |
|
|
|
|
|
|
|
const evtMove = new CustomEvent('L-move', { detail }); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
document.dispatchEvent(evtMove); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//===== Message handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Content.prototype.msgRobots = function(evt) { |
|
|
|
|
|
|
|
this.initialRobotState = evt.detail.body; |
|
|
|
|
|
|
|
this.drawRobots()
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Content.prototype.msgWalls = function(evt) { |
|
|
|
|
|
|
|
this.drawWalls(evt.detail.body); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Content.prototype.msgMove = function(evt) { |
|
|
|
|
|
|
|
const { id, i, j } = evt.detail.body; |
|
|
|
|
|
|
|
this.moveRobot({ id, i, j }); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Content.prototype.msgSolve = function(evt) { |
|
|
|
|
|
|
|
this.isSolving = true; |
|
|
|
|
|
|
|
this.isSolver = (this.playerId === evt.detail.id); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (this.isSolver) { |
|
|
|
|
|
|
|
console.error("The current player is attempting a solve."); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
console.error("A different player is attempting a solve."); |
|
|
|
|
|
|
|
//...modal.
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Content.prototype.msgJoin = function(evt) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Content.prototype.msgConnected = function(evt) { |
|
|
|
|
|
|
|
this.playerId = evt.detail.body; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Content.prototype.msgReset = function() { |
|
|
|
|
|
|
|
this.drawRobots(); |
|
|
|
|
|
|
|
}; |
|
|
|