|
|
|
@ -2,20 +2,10 @@ const Board = function({ parent, squares }) { |
|
|
|
|
this.parent = parent; |
|
|
|
|
this.squares = squares; |
|
|
|
|
|
|
|
|
|
this.blockers = {}; |
|
|
|
|
this.walls = {}; |
|
|
|
|
this.listeners = {}; |
|
|
|
|
|
|
|
|
|
this.drawSquares(); |
|
|
|
|
this.resetBlockers(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Board.prototype.resetBlockers = function() { |
|
|
|
|
this.blockers = { |
|
|
|
|
negativeI: null, |
|
|
|
|
negativeJ: null, |
|
|
|
|
positiveI: null, |
|
|
|
|
positiveJ: null |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Board.prototype.drawRobots = function(robots) { |
|
|
|
@ -46,6 +36,8 @@ Board.prototype.drawRobots = function(robots) { |
|
|
|
|
shadow.style.left = x + 'px'; |
|
|
|
|
shadow.style.top = y + 'px'; |
|
|
|
|
shadow.dataset.parentRobot = id; |
|
|
|
|
shadow.dataset.currentI = i; |
|
|
|
|
shadow.dataset.currentJ = j; |
|
|
|
|
|
|
|
|
|
this.parent.appendChild(robot); |
|
|
|
|
this.parent.appendChild(shadow); |
|
|
|
@ -73,7 +65,11 @@ Board.prototype.drawSquares = function() { |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Board.prototype.drawWalls = function(edges) { |
|
|
|
|
this.walls = {}; |
|
|
|
|
|
|
|
|
|
edges.forEach(edge => { |
|
|
|
|
this.walls[edge] = true; |
|
|
|
|
|
|
|
|
|
const id = `wall-${edge}`; |
|
|
|
|
|
|
|
|
|
if (document.getElementById(id)) { |
|
|
|
@ -82,8 +78,6 @@ Board.prototype.drawWalls = function(edges) { |
|
|
|
|
|
|
|
|
|
const [i1, j1, i2, j2] = edge.split('-'); |
|
|
|
|
|
|
|
|
|
console.log(edge) |
|
|
|
|
|
|
|
|
|
const wall = document.createElement('div'); |
|
|
|
|
wall.id = id; |
|
|
|
|
wall.title = edge; |
|
|
|
@ -107,37 +101,61 @@ Board.prototype.drawWalls = function(edges) { |
|
|
|
|
}) |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// i1 and j1 are the original position.
|
|
|
|
|
// i2 and j2 are the requested position.
|
|
|
|
|
// Check all the edges crossed between the two to find if it's a valid move.
|
|
|
|
|
Board.prototype.getBlockers = function({ i1, j1, i2, j2 }) { |
|
|
|
|
// const upperEdge = `${i1}-${j1}-${i2}-${j2}`;
|
|
|
|
|
// const lowerEdge = `${i1}-${j1}-${i2}-${j2}`;
|
|
|
|
|
|
|
|
|
|
if (i1 === i2 && j1 < j2) { |
|
|
|
|
if (i1 !== i2 && j1 !== j2) { |
|
|
|
|
return "uh oh"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (i1 === i2 && j1 == j2) { |
|
|
|
|
return; |
|
|
|
|
} else if (i1 === i2 && j1 > j2) { |
|
|
|
|
// Moving up
|
|
|
|
|
// Moving down.
|
|
|
|
|
for (let j = j2; j < j1; j++) { |
|
|
|
|
const edge = `${i1}-${j + 1}-${i1 + 1}-${j + 1}`; |
|
|
|
|
|
|
|
|
|
if (this.walls[edge]) { |
|
|
|
|
return document.querySelector(`[data-wall='${edge}']`); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} else if (j1 === j2 && i1 <= i2) { |
|
|
|
|
this.blockers.positiveI && console.log(this.blockers.positiveI.i, i1) |
|
|
|
|
} else if (i1 === i2 && j1 < j2) { |
|
|
|
|
// Moving down.
|
|
|
|
|
for (let j = j1; j < j2; j++) { |
|
|
|
|
const edge = `${i1}-${j + 1}-${i1 + 1}-${j + 1}`; |
|
|
|
|
|
|
|
|
|
if (this.blockers.positiveI && this.blockers.positiveI.i <= i1) { |
|
|
|
|
return this.blockers.positiveI.blockage; |
|
|
|
|
if (this.walls[edge]) { |
|
|
|
|
return document.querySelector(`[data-wall='${edge}']`); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else if (j1 === j2 && i1 < i2) { |
|
|
|
|
// Moving right.
|
|
|
|
|
for (let i = i1; i < i2; i++) { |
|
|
|
|
const edge = `${i + 1}-${j1}-${i + 1}-${j1 + 1}`; |
|
|
|
|
|
|
|
|
|
const rightEdge = `${i1 + 1}-${j1}-${i1 + 1}-${j1 + 1}`; |
|
|
|
|
|
|
|
|
|
const wall = document.querySelector(`[data-wall='${rightEdge}']`); |
|
|
|
|
if (this.walls[edge]) { |
|
|
|
|
return document.querySelector(`[data-wall='${edge}']`); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else if (j1 === j2 && i1 > i2) { |
|
|
|
|
// Moving left.
|
|
|
|
|
for (let i = i2; i < i1; i++) { |
|
|
|
|
const edge = `${i + 1}-${j1}-${i + 1}-${j1 + 1}`; |
|
|
|
|
|
|
|
|
|
if (wall) { |
|
|
|
|
this.blockers.positiveI = { i: i1 + 1, blockage: wall }; |
|
|
|
|
return wall; |
|
|
|
|
if (this.walls[edge]) { |
|
|
|
|
return document.querySelector(`[data-wall='${edge}']`); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
// const leftEdge = `${i1}-${j1}-${i2}-${j2}`;
|
|
|
|
|
// console.log("Left edge: ", leftEdge)
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return null; |
|
|
|
|
// return "uh oh";
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Board.prototype.onRobotDragStart = function(evt) { |
|
|
|
@ -157,16 +175,15 @@ Board.prototype.onRobotDragStart = function(evt) { |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Board.prototype.onRobotDrag = function(dragTarget, evt) { |
|
|
|
|
const { i: i1, j: j1 } = this.squares.xyToIj({ x: evt.x - evt.movementX, y: evt.y - evt.movementY }); |
|
|
|
|
const i1 = dragTarget.dataset.currentI * 1; |
|
|
|
|
const j1 = dragTarget.dataset.currentJ * 1; |
|
|
|
|
// const { i: i1, j: j1 } = this.squares.xyToIj({ x: evt.x - evt.movementX, y: evt.y - evt.movementY });
|
|
|
|
|
const { i: i2, j: j2 } = this.squares.xyToIj({ x: evt.x, y: evt.y }); |
|
|
|
|
|
|
|
|
|
const blockage = this.getBlockers({ i1, j1, i2, j2 }); |
|
|
|
|
// console.warn(blockers)
|
|
|
|
|
|
|
|
|
|
if (blockage) { |
|
|
|
|
console.log(blockage) |
|
|
|
|
console.log("NOPE") |
|
|
|
|
// this.onRobotDragStop(); works but i don't like it
|
|
|
|
|
// console.log(blockage)
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -177,8 +194,6 @@ Board.prototype.onRobotDrag = function(dragTarget, evt) { |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Board.prototype.onRobotDragStop = function(_) { |
|
|
|
|
this.resetBlockers(); |
|
|
|
|
|
|
|
|
|
document.body.removeEventListener('mouseup', this.listeners.onRobotDragStop); |
|
|
|
|
document.body.removeEventListener('mousemove', this.listeners.onRobotDrag); |
|
|
|
|
}; |