From f70ae5fc0654ec6354b460d78244d4d002eb777f Mon Sep 17 00:00:00 2001 From: mcglincy Date: Mon, 22 Nov 2021 09:35:49 -0600 Subject: [PATCH] Remove doors and walls as part of a single state change. --- scripts/dungeon.js | 23 +++++++++++++++++++++-- scripts/dungeonlayer.js | 3 +-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/scripts/dungeon.js b/scripts/dungeon.js index 2d9e6ed..4003968 100644 --- a/scripts/dungeon.js +++ b/scripts/dungeon.js @@ -187,10 +187,10 @@ export class Dungeon extends PlaceableObject { } async subtractInteriorWalls(rect) { - const poly = geo.rectToPolygon(rect); + const rectPoly = geo.rectToPolygon(rect); const wallsToKeep = this.history[this.historyIndex].interiorWalls.filter(w => { const wallPoly = geo.twoPointsToLineString(w[0], w[1], w[2], w[3]); - return !poly.intersects(wallPoly); + return !rectPoly.intersects(wallPoly); }); if (wallsToKeep.length != this.history[this.historyIndex].interiorWalls.length) { const newState = this.history[this.historyIndex].clone(); @@ -199,6 +199,25 @@ export class Dungeon extends PlaceableObject { } } + async subtractDoorsAndInteriorWalls(rect) { + const rectPoly = geo.rectToPolygon(rect); + const oldState = this.history[this.historyIndex]; + const doorsToKeep = oldState.doors.filter(d => { + const doorPoly = geo.twoPointsToLineString(d[0], d[1], d[2], d[3]); + return !rectPoly.intersects(doorPoly); + }); + const wallsToKeep = oldState.interiorWalls.filter(w => { + const wallPoly = geo.twoPointsToLineString(w[0], w[1], w[2], w[3]); + return !rectPoly.intersects(wallPoly); + }); + if (doorsToKeep.length != oldState.doors.length || wallsToKeep.length != oldState.interiorWalls.length) { + const newState = oldState.clone(); + newState.doors = doorsToKeep; + newState.interiorWalls = wallsToKeep; + await this.pushState(newState); + } + } + async _addPoly(poly) { const oldState = this.history[this.historyIndex]; const newState = oldState.clone(); diff --git a/scripts/dungeonlayer.js b/scripts/dungeonlayer.js index d761c8b..b65baf0 100644 --- a/scripts/dungeonlayer.js +++ b/scripts/dungeonlayer.js @@ -316,8 +316,7 @@ export class DungeonLayer extends PlaceablesLayer { height: createData.height, width: createData.width }; - await this.dungeon.subtractInteriorWalls(rect); - await this.dungeon.subtractDoors(rect); + await this.dungeon.subtractDoorsAndInteriorWalls(rect); } else if (game.activeTool === "addpoly") { const offsetPoints = createData.points.map(p => [p[0] + createData.x, p[1] + createData.y]); await this.dungeon.addPolygon(offsetPoints);