From bb12e2a0003d36b27d01a02b32746e7caa2a9284 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=97=D0=BB=20=D0=93=D1=80=D0=B8?= =?UTF-8?q?=D0=B3=D0=BE=D1=80=27=D1=94=D0=B2?= Date: Sun, 2 May 2021 10:14:54 +0300 Subject: [PATCH] Make trim center buildings --- .../game/hud/parts/puzzle_editor_settings.js | 44 +++++++++++++------ 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/src/js/game/hud/parts/puzzle_editor_settings.js b/src/js/game/hud/parts/puzzle_editor_settings.js index 0fb41dde..1adc6981 100644 --- a/src/js/game/hud/parts/puzzle_editor_settings.js +++ b/src/js/game/hud/parts/puzzle_editor_settings.js @@ -56,31 +56,49 @@ export class HUDPuzzleEditorSettings extends BaseHUDPart { trim() { const mode = /** @type {PuzzleGameMode} */ (this.root.gameMode); - let w = mode.zoneWidth; - let h = mode.zoneHeight; - if (this.anyBuildingOutsideZone(w, h)) { - logger.error("Trim: Zone is already too small"); + const sourceRect = Rectangle.centered(mode.zoneWidth, mode.zoneHeight); + const entities = this.root.entityMgr.entities; + + if (entities.length == 0) { + logger.log("Zone trim: no buildings found"); return; } - logger.log("Zone trim: Starts at", w, h); + logger.log("Zone trim: Starts at", sourceRect.toString()); - while (!this.anyBuildingOutsideZone(w - 1, h)) { - --w; + const initialRect = entities[0].components.StaticMapEntity.getTileSpaceBounds(); + + // Get union of all entity rectangles + let destRect = entities.reduce((rect, current) => { + const staticComp = current.components.StaticMapEntity; + return rect.getUnion(staticComp.getTileSpaceBounds()); + }, initialRect); + + // Make sure the new zone matches min requirement + const minRect = Rectangle.fromSquare(destRect.x, destRect.y, globalConfig.puzzleMinBoundsSize); + destRect = destRect.getUnion(minRect); + + // Now find center of the new zone and align entities to (0, 0) + const center = destRect.getCenter().ceil(); + + // We need two loops to make sure entities don't corrupt each other + for (const entity of entities) { + this.root.map.removeStaticEntity(entity); } - while (!this.anyBuildingOutsideZone(w, h - 1)) { - --h; + for (const entity of entities) { + entity.components.StaticMapEntity.origin.subInplace(center); + this.root.map.placeStaticEntity(entity); } - logger.log("Zone trim: After height pass at", w, h); - if (this.anyBuildingOutsideZone(w, h)) { + logger.log("Zone trim: Finished at", destRect.toString()); + if (this.anyBuildingOutsideZone(destRect.w, destRect.h)) { logger.error("Trim: Zone is too small *after* trim"); return; } - mode.zoneWidth = w; - mode.zoneHeight = h; + mode.zoneWidth = destRect.w; + mode.zoneHeight = destRect.h; this.updateZoneValues(); }