Skip to content

Commit

Permalink
fix: Fix map layer dimensions calculation for empty layers
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypfer committed Jan 18, 2021
1 parent a81b8e9 commit 216c347
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 48 deletions.
107 changes: 61 additions & 46 deletions lib/entities/map/MapLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,58 +37,73 @@ class MapLayer extends SerializableEntity {
* @private
*/
calculateDimensions() {
this.dimensions = {
x: {
min: undefined,
max: undefined,
mid: undefined
},
y: {
min: undefined,
max: undefined,
mid: undefined
}
};
if (this.pixels.length > 0) {
this.dimensions = {
x: {
min: undefined,
max: undefined,
mid: undefined
},
y: {
min: undefined,
max: undefined,
mid: undefined
}
};

for (let i = 0; i < this.pixels.length; i = i + 2) {
if (
!this.dimensions.x.min ||
this.pixels[i] < this.dimensions.x.min
) {
this.dimensions.x.min = this.pixels[i];
}
for (let i = 0; i < this.pixels.length; i = i + 2) {
if (
!this.dimensions.x.min ||
this.pixels[i] < this.dimensions.x.min
) {
this.dimensions.x.min = this.pixels[i];
}

if (
!this.dimensions.x.max ||
this.pixels[i] > this.dimensions.x.max
) {
this.dimensions.x.max = this.pixels[i];
}
if (
!this.dimensions.x.max ||
this.pixels[i] > this.dimensions.x.max
) {
this.dimensions.x.max = this.pixels[i];
}

if (
!this.dimensions.y.min ||
this.pixels[i+1] < this.dimensions.y.min
) {
this.dimensions.y.min = this.pixels[i+1];
}
if (
!this.dimensions.y.min ||
this.pixels[i+1] < this.dimensions.y.min
) {
this.dimensions.y.min = this.pixels[i+1];
}

if (
!this.dimensions.y.max ||
this.pixels[i+1] > this.dimensions.y.max
) {
this.dimensions.y.max = this.pixels[i+1];
if (
!this.dimensions.y.max ||
this.pixels[i+1] > this.dimensions.y.max
) {
this.dimensions.y.max = this.pixels[i+1];
}
}
}

this.dimensions.x.mid = Math.round((
this.dimensions.x.max +
this.dimensions.x.min
) / 2);
this.dimensions.x.mid = Math.round((
this.dimensions.x.max +
this.dimensions.x.min
) / 2);

this.dimensions.y.mid = Math.round((
this.dimensions.y.max +
this.dimensions.y.min
) / 2);
this.dimensions.y.mid = Math.round((
this.dimensions.y.max +
this.dimensions.y.min
) / 2);
} else {
this.dimensions = {
x: {
min: 0,
max: 0,
mid: 0
},
y: {
min: 0,
max: 0,
mid: 0
}
};
}
}
}

Expand All @@ -103,4 +118,4 @@ MapLayer.TYPE = Object.freeze({
SEGMENT: "segment"
});

module.exports = MapLayer;
module.exports = MapLayer;
8 changes: 6 additions & 2 deletions test/lib/DreameMapParser/d9_1058_with_segments.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@
"pixels": [],
"dimensions": {
"x": {
"mid": null
"max": 0,
"mid": 0,
"min": 0
},
"y": {
"mid": null
"max": 0,
"mid": 0,
"min": 0
}
}
},
Expand Down

0 comments on commit 216c347

Please sign in to comment.