From 909b1215a2d548166b2718be403169f72cb6549b Mon Sep 17 00:00:00 2001 From: cha0s Date: Sun, 28 Mar 2021 11:23:17 -0500 Subject: [PATCH] refactor: hulls --- packages/math/src/vertice.js | 4 +-- packages/topdown/src/resources/tiles.js | 31 +++++++-------------- packages/topdown/src/tiles-view.js | 37 ++++++++++--------------- 3 files changed, 26 insertions(+), 46 deletions(-) diff --git a/packages/math/src/vertice.js b/packages/math/src/vertice.js index 578c760..6b6acc7 100644 --- a/packages/math/src/vertice.js +++ b/packages/math/src/vertice.js @@ -191,10 +191,8 @@ export function removeCollinear(vs) { return trimmed; } -export function smooth(vs) { +export function smooth(sorted) { const smoothed = []; - // eslint-disable-next-line no-use-before-define - const sorted = ortho(unique(vs)); if (0 === sorted.length) { return []; } diff --git a/packages/topdown/src/resources/tiles.js b/packages/topdown/src/resources/tiles.js index 0ba4ff0..f91cfa9 100644 --- a/packages/topdown/src/resources/tiles.js +++ b/packages/topdown/src/resources/tiles.js @@ -66,29 +66,18 @@ export default (latus) => { const xx = k % w; const yy = Math.floor(k / w); const [xxs, yys] = [xx * 4, yy * 4]; - const u = k - w; - if ((u < 0 || yy < 1) || !indices.has(this.data[u])) { - body.push([xxs, yys]); - body.push([xxs + 4, yys]); - } - const r = k + 1; - if ((r < 0 || xx + 1 >= w) || !indices.has(this.data[r])) { - body.push([xxs + 4, yys]); - body.push([xxs + 4, yys + 4]); - } - const d = k + w; - if ((d < 0 || yy + 1 >= h) || !indices.has(this.data[d])) { - body.push([xxs + 4, yys + 4]); - body.push([xxs, yys + 4]); - } - const l = k - 1; - if ((l < 0 || xx < 1) || !indices.has(this.data[l])) { - body.push([xxs, yys + 4]); - body.push([xxs, yys]); - } + body.push([xxs + 1, yys + 1]); + body.push([xxs + 3, yys + 1]); + body.push([xxs + 3, yys + 3]); + body.push([xxs + 1, yys + 3]); } } - const hull = Vertice.removeCollinear(Vertice.ortho(Vertice.unique(body), 4)); + const hull = Vertice.removeCollinear( + Vertice.ortho( + Vertice.unique(body), + 2, + ), + ); for (let i = 0; i < hull.length; i++) { hull[i] = Vector.scale(hull[i], 0.25); } diff --git a/packages/topdown/src/tiles-view.js b/packages/topdown/src/tiles-view.js index 87415d2..d39b588 100644 --- a/packages/topdown/src/tiles-view.js +++ b/packages/topdown/src/tiles-view.js @@ -23,7 +23,7 @@ export default class TilesView extends Container { this.tileset = tileset; this.renderer = renderer; this.rendered = []; - // this.hulls = this.tiles.indexHulls(0); + // this.hulls = this.tiles.indexHulls(new Set([342, 456, 103, 104])); // const mask = this.renderMask(Vector.mul(this.tiles.size, this.tileset.tileSize)); // if (mask) { // this.addChild(mask); @@ -184,7 +184,7 @@ export default class TilesView extends Container { renderMask(size) { const {hulls} = this; - if (!hulls.length > 0) { + if (0 === hulls.length) { return undefined; } const {tileSize} = this.tileset; @@ -192,9 +192,9 @@ export default class TilesView extends Container { const canvas = window.document.createElement('canvas'); [canvas.width, canvas.height] = canvasSize; const ctx = canvas.getContext('2d'); - ctx.fillStyle = 'rgba(255, 255, 255, 1)'; - ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = 'rgba(0, 0, 0, 1)'; + ctx.fillRect(0, 0, canvas.width, canvas.height); + ctx.fillStyle = 'rgba(255, 255, 255, 1)'; for (let i = 0; i < hulls.length; ++i) { const scaled = []; for (let j = 0; j < hulls[i].length; j++) { @@ -211,31 +211,24 @@ export default class TilesView extends Container { (p0[0] > p1[0] && points[0][0] < points[points.length - 1][0]) || (p0[1] > p1[1] && points[0][1] < points[points.length - 1][1]) ); - const u = Vector.scale( - Vector.fromRadians((Math.PI * 0.5) + Vector.toRadians(Vector.sub(p1, p0))), - 2, - ); for ( let k = (isReversed ? points.length - 1 : 0); (isReversed ? k >= 0 : k < points.length); k += (isReversed ? -1 : 1) ) { const [x, y] = points[k]; - const shift = Vector.add( - u, - Vector.scale( - Vector.mul( - [ - noise(Vector.scale([x, y], 10)), - noise(Vector.scale([y, x], 10)), - ], - [ - noise(Vector.scale([x, y], 20)), - noise(Vector.scale([y, x], 20)), - ], - ), - 5, + const shift = Vector.scale( + Vector.mul( + [ + noise(Vector.scale([x, y], 10)), + noise(Vector.scale([y, x], 10)), + ], + [ + noise(Vector.scale([x, y], 20)), + noise(Vector.scale([y, x], 20)), + ], ), + 5, ); const [vx, vy] = Vector.add( points[k],