Skip to content

Commit

Permalink
Remove doors and walls as part of a single state change.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcglincy committed Nov 22, 2021
1 parent 4104520 commit f70ae5f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
23 changes: 21 additions & 2 deletions scripts/dungeon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions scripts/dungeonlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f70ae5f

Please sign in to comment.