|
|
|
@ -9,29 +9,31 @@ const Content = function({ parent, squares }) { |
|
|
|
|
this.listeners = {}; |
|
|
|
|
this.initialRobotState = []; |
|
|
|
|
|
|
|
|
|
// this.drawSquares();
|
|
|
|
|
// document.addEventListener('L-conn-open', this.msgStartGame.bind(this));
|
|
|
|
|
// document.addEventListener('G-walls', this.msgStart.bind(this));
|
|
|
|
|
// document.addEventListener('G-robots', this.msgStart.bind(this));
|
|
|
|
|
// document.addEventListener('board-reset', this.onReset.bind(this));
|
|
|
|
|
// from connection: board.drawRobots(data.body);
|
|
|
|
|
// from connection: board.drawWalls(data.body);
|
|
|
|
|
// from connection: board.onReceiveGuess(data.body);
|
|
|
|
|
this.drawSquares(); |
|
|
|
|
|
|
|
|
|
document.addEventListener('L-reset', this.onReset.bind(this)); |
|
|
|
|
|
|
|
|
|
document.addEventListener('G-walls', this.msgWalls.bind(this)); |
|
|
|
|
document.addEventListener('G-robots', this.msgRobots.bind(this)); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
//===== Event handlers
|
|
|
|
|
|
|
|
|
|
Content.prototype.msgRobots = function(evt) { |
|
|
|
|
this.initialRobotState = evt.detail.body; |
|
|
|
|
this.drawRobots()
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//===== Event handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Content.prototype.drawRobots = function(robots) { |
|
|
|
|
this.initialRobotState = robots; |
|
|
|
|
Content.prototype.drawRobots = function() { |
|
|
|
|
this.robots = {}; |
|
|
|
|
|
|
|
|
|
this.parent.querySelectorAll('.content-robot').forEach(el => el.parentNode.removeChild(el)); |
|
|
|
|
this.parent.querySelectorAll('.content-arrows').forEach(el => el.parentNode.removeChild(el)); |
|
|
|
|
|
|
|
|
|
robots.forEach(({ color, i, j }) => { |
|
|
|
|
this.initialRobotState.forEach(({ color, i, j }) => { |
|
|
|
|
const { x, y } = this.squares.ijToXy({ i, j }); |
|
|
|
|
const s = this.squares.sideLength; |
|
|
|
|
const id = color.replace('#', '').toUpperCase(); |
|
|
|
@ -61,19 +63,19 @@ Content.prototype.drawRobots = function(robots) { |
|
|
|
|
arrows.style.top = (y - s) + 'px'; |
|
|
|
|
|
|
|
|
|
const up = this.drawArrow({ |
|
|
|
|
direction: 'up', label: '▲', i, j, left: s, top: 0, parentId: id |
|
|
|
|
direction: 'up', label: '▲', i, j, left: (s * 1.25), top: (s * 0.5), parentId: id |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
const down = this.drawArrow({ |
|
|
|
|
direction: 'down', label: '▼', i, j, left: s, top: (2 * s), parentId: id |
|
|
|
|
direction: 'down', label: '▼', i, j, left: (s * 1.25), top: (s * 2), parentId: id |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
const left = this.drawArrow({ |
|
|
|
|
direction: 'left', label: '◀', i, j, left: 0, top: s, parentId: id |
|
|
|
|
direction: 'left', label: '◀', i, j, left: (s * 0.5), top: (s * 1.25), parentId: id |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
const right = this.drawArrow({ |
|
|
|
|
direction: 'right', label: '▶', i, j, left: (2 * s), top: s, parentId: id |
|
|
|
|
direction: 'right', label: '▶', i, j, left: (s * 2), top: (s * 1.25), parentId: id |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
arrows.appendChild(up); |
|
|
|
@ -89,7 +91,7 @@ Content.prototype.drawRobots = function(robots) { |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Content.prototype.drawArrow = function({ direction, label, i, j, left, top, parentId }) { |
|
|
|
|
const s = this.squares.sideLength; |
|
|
|
|
const s = this.squares.sideLength / 2; |
|
|
|
|
|
|
|
|
|
const arrow = document.createElement('div'); |
|
|
|
|
arrow.className = 'content-arrow'; |
|
|
|
@ -125,7 +127,9 @@ Content.prototype.drawSquares = function() { |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Content.prototype.drawWalls = function(edges) { |
|
|
|
|
Content.prototype.msgWalls = function(msg) { |
|
|
|
|
const edges = msg.detail.body; |
|
|
|
|
|
|
|
|
|
this.walls = {}; |
|
|
|
|
|
|
|
|
|
this.parent.querySelectorAll('.content-wall-x').forEach(el => el.parentNode.removeChild(el)); |
|
|
|
@ -186,7 +190,7 @@ Content.prototype.moveRobot = function({ id, i, j }) { |
|
|
|
|
|
|
|
|
|
this.updateArrowVisibilities(); |
|
|
|
|
|
|
|
|
|
var event = new Event('move-increment'); |
|
|
|
|
var event = new Event('L-move'); |
|
|
|
|
document.dispatchEvent(event); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -237,7 +241,7 @@ Content.prototype.onReceiveGuess = function() { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Content.prototype.onReset = function() { |
|
|
|
|
this.drawRobots(this.initialRobotState); |
|
|
|
|
this.drawRobots(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Content.prototype.findNextObstacle = function({ direction, i, j }) { |
|
|
|
|