summaryrefslogtreecommitdiff
path: root/src/util/tile-calculations.js
diff options
context:
space:
mode:
authorGeorgios Andreadis <g.andreadis@student.tudelft.nl>2017-08-25 23:03:05 +0200
committerGeorgios Andreadis <g.andreadis@student.tudelft.nl>2017-09-23 10:05:44 +0200
commit8302923a08728d36746af3560ebc35685c2b9da5 (patch)
tree028b0e0db8f7e8e60d9127a522ceea49ec395952 /src/util/tile-calculations.js
parent8f5e6d1e73f16e3cdd523f961d06e4b4eb5a8cef (diff)
Enable going from building to room and back
Diffstat (limited to 'src/util/tile-calculations.js')
-rw-r--r--src/util/tile-calculations.js75
1 files changed, 43 insertions, 32 deletions
diff --git a/src/util/tile-calculations.js b/src/util/tile-calculations.js
index f358442f..478c09f9 100644
--- a/src/util/tile-calculations.js
+++ b/src/util/tile-calculations.js
@@ -2,6 +2,8 @@ export function deriveWallLocations(room) {
const verticalWalls = {};
const horizontalWalls = {};
+ // Determine wall segments
+
room.tiles.forEach(tile => {
const x = tile.positionX, y = tile.positionY;
for (let dX = -1; dX <= 1; dX++) {
@@ -11,47 +13,50 @@ export function deriveWallLocations(room) {
}
let doInsert = true;
- room.tiles.forEach((otherTile) => {
+ room.tiles.forEach(otherTile => {
if (otherTile.positionX === x + dX && otherTile.positionY === y + dY) {
doInsert = false;
}
});
+ if (!doInsert) {
+ continue;
+ }
- if (doInsert) {
- if (dX === -1) {
- if (verticalWalls[x] === undefined) {
- verticalWalls[x] = [];
- }
- if (verticalWalls[x].indexOf(y) === -1) {
- verticalWalls[x].push(y);
- }
- } else if (dX === 1) {
- if (verticalWalls[x + 1] === undefined) {
- verticalWalls[x + 1] = [];
- }
- if (verticalWalls[x + 1].indexOf(y) === -1) {
- verticalWalls[x + 1].push(y);
- }
- } else if (dY === -1) {
- if (horizontalWalls[y] === undefined) {
- horizontalWalls[y] = [];
- }
- if (horizontalWalls[y].indexOf(x) === -1) {
- horizontalWalls[y].push(x);
- }
- } else if (dY === 1) {
- if (horizontalWalls[y + 1] === undefined) {
- horizontalWalls[y + 1] = [];
- }
- if (horizontalWalls[y + 1].indexOf(x) === -1) {
- horizontalWalls[y + 1].push(x);
- }
+ if (dX === -1) {
+ if (verticalWalls[x] === undefined) {
+ verticalWalls[x] = [];
+ }
+ if (verticalWalls[x].indexOf(y) === -1) {
+ verticalWalls[x].push(y);
+ }
+ } else if (dX === 1) {
+ if (verticalWalls[x + 1] === undefined) {
+ verticalWalls[x + 1] = [];
+ }
+ if (verticalWalls[x + 1].indexOf(y) === -1) {
+ verticalWalls[x + 1].push(y);
+ }
+ } else if (dY === -1) {
+ if (horizontalWalls[y] === undefined) {
+ horizontalWalls[y] = [];
+ }
+ if (horizontalWalls[y].indexOf(x) === -1) {
+ horizontalWalls[y].push(x);
+ }
+ } else if (dY === 1) {
+ if (horizontalWalls[y + 1] === undefined) {
+ horizontalWalls[y + 1] = [];
+ }
+ if (horizontalWalls[y + 1].indexOf(x) === -1) {
+ horizontalWalls[y + 1].push(x);
}
}
}
}
});
+ // Merge walls into longer segments
+
const result = [];
const walls = [verticalWalls, horizontalWalls];
for (let i = 0; i < 2; i++) {
@@ -65,10 +70,10 @@ export function deriveWallLocations(room) {
let startPos = wallList[a][0];
const isHorizontal = i === 1;
- const startPosX = isHorizontal ? startPos : a;
- const startPosY = isHorizontal ? a : startPos;
if (wallList[a].length === 1) {
+ const startPosX = isHorizontal ? startPos : a;
+ const startPosY = isHorizontal ? a : startPos;
result.push({
startPosX,
startPosY,
@@ -80,6 +85,8 @@ export function deriveWallLocations(room) {
for (let b = 0; b < wallList[a].length - 1; b++) {
if (b + 1 === wallList[a].length - 1) {
if (wallList[a][b + 1] - wallList[a][b] > 1) {
+ const startPosX = isHorizontal ? startPos : a;
+ const startPosY = isHorizontal ? a : startPos;
result.push({
startPosX,
startPosY,
@@ -89,6 +96,8 @@ export function deriveWallLocations(room) {
consecutiveCount = 0;
startPos = wallList[a][b + 1];
}
+ const startPosX = isHorizontal ? startPos : a;
+ const startPosY = isHorizontal ? a : startPos;
result.push({
startPosX,
startPosY,
@@ -97,6 +106,8 @@ export function deriveWallLocations(room) {
});
break;
} else if (wallList[a][b + 1] - wallList[a][b] > 1) {
+ const startPosX = isHorizontal ? startPos : a;
+ const startPosY = isHorizontal ? a : startPos;
result.push({
startPosX,
startPosY,