From bb431b8490e923d68a7a6b00e888c199bc1a3e16 Mon Sep 17 00:00:00 2001 From: tobspr Date: Mon, 10 Aug 2020 22:13:26 +0200 Subject: [PATCH] Reduce savegame size by not storing the tileSize in the static entity --- src/js/game/blueprint.js | 8 +- src/js/game/building_codes.js | 4 + src/js/game/components/static_map_entity.js | 85 +++++++++------------ src/js/game/hud/parts/building_placer.js | 1 - src/js/game/systems/belt.js | 7 +- src/js/game/systems/static_map_entity.js | 6 +- 6 files changed, 45 insertions(+), 66 deletions(-) diff --git a/src/js/game/blueprint.js b/src/js/game/blueprint.js index e1ac2dec..ecffdc31 100644 --- a/src/js/game/blueprint.js +++ b/src/js/game/blueprint.js @@ -92,13 +92,7 @@ export class Blueprint { parameters.context.globalAlpha = 1; } - staticComp.drawSpriteOnFullEntityBounds( - parameters, - staticComp.getBlueprintSprite(), - 0, - true, - newPos - ); + staticComp.drawSpriteOnFullEntityBounds(parameters, staticComp.getBlueprintSprite(), 0, newPos); } parameters.context.globalAlpha = 1; } diff --git a/src/js/game/building_codes.js b/src/js/game/building_codes.js index 370660bc..05c27f57 100644 --- a/src/js/game/building_codes.js +++ b/src/js/game/building_codes.js @@ -1,6 +1,7 @@ /* typehints:start */ import { MetaBuilding } from "./meta_building"; import { AtlasSprite } from "../core/sprites"; +import { Vector } from "../core/vector"; /* typehints:end */ /** @@ -9,6 +10,7 @@ import { AtlasSprite } from "../core/sprites"; * metaInstance?: MetaBuilding, * variant?: string, * rotationVariant?: number, + * tileSize?: Vector, * sprite?: AtlasSprite, * blueprintSprite?: AtlasSprite, * silhouetteColor?: string @@ -41,6 +43,8 @@ export function registerBuildingVariant( metaClass: meta, variant, rotationVariant, + // @ts-ignore + tileSize: new meta().getDimensions(variant), }; } diff --git a/src/js/game/components/static_map_entity.js b/src/js/game/components/static_map_entity.js index 6494aba1..2c80b43b 100644 --- a/src/js/game/components/static_map_entity.js +++ b/src/js/game/components/static_map_entity.js @@ -24,6 +24,14 @@ export class StaticMapEntityComponent extends Component { }; } + /** + * Returns the effective tile size + * @returns {Vector} + */ + getTileSize() { + return getBuildingDataFromCode(this.code).tileSize; + } + /** * Returns the sprite * @returns {AtlasSprite} @@ -51,7 +59,6 @@ export class StaticMapEntityComponent extends Component { duplicateWithoutContents() { return new StaticMapEntityComponent({ origin: this.origin.copy(), - tileSize: this.tileSize.copy(), rotation: this.rotation, originalRotation: this.originalRotation, code: this.code, @@ -81,7 +88,6 @@ export class StaticMapEntityComponent extends Component { ); this.origin = origin; - this.tileSize = tileSize; this.rotation = rotation; this.code = code; this.originalRotation = originalRotation; @@ -92,30 +98,16 @@ export class StaticMapEntityComponent extends Component { * @returns {Rectangle} */ getTileSpaceBounds() { + const size = this.getTileSize(); switch (this.rotation) { case 0: - return new Rectangle(this.origin.x, this.origin.y, this.tileSize.x, this.tileSize.y); + return new Rectangle(this.origin.x, this.origin.y, size.x, size.y); case 90: - return new Rectangle( - this.origin.x - this.tileSize.y + 1, - this.origin.y, - this.tileSize.y, - this.tileSize.x - ); + return new Rectangle(this.origin.x - size.y + 1, this.origin.y, size.y, size.x); case 180: - return new Rectangle( - this.origin.x - this.tileSize.x + 1, - this.origin.y - this.tileSize.y + 1, - this.tileSize.x, - this.tileSize.y - ); + return new Rectangle(this.origin.x - size.x + 1, this.origin.y - size.y + 1, size.x, size.y); case 270: - return new Rectangle( - this.origin.x, - this.origin.y - this.tileSize.x + 1, - this.tileSize.y, - this.tileSize.x - ); + return new Rectangle(this.origin.x, this.origin.y - size.x + 1, size.y, size.x); default: assert(false, "Invalid rotation"); } @@ -186,34 +178,35 @@ export class StaticMapEntityComponent extends Component { let y = 0; let w = 0; let h = 0; + const size = this.getTileSize(); switch (this.rotation) { case 0: { x = this.origin.x; y = this.origin.y; - w = this.tileSize.x; - h = this.tileSize.y; + w = size.x; + h = size.y; break; } case 90: { - x = this.origin.x - this.tileSize.y + 1; + x = this.origin.x - size.y + 1; y = this.origin.y; - w = this.tileSize.y; - h = this.tileSize.x; + w = size.y; + h = size.x; break; } case 180: { - x = this.origin.x - this.tileSize.x + 1; - y = this.origin.y - this.tileSize.y + 1; - w = this.tileSize.x; - h = this.tileSize.y; + x = this.origin.x - size.x + 1; + y = this.origin.y - size.y + 1; + w = size.x; + h = size.y; break; } case 270: { x = this.origin.x; - y = this.origin.y - this.tileSize.x + 1; - w = this.tileSize.y; - h = this.tileSize.x; + y = this.origin.y - size.x + 1; + w = size.y; + h = size.x; break; } default: @@ -233,19 +226,13 @@ export class StaticMapEntityComponent extends Component { * @param {DrawParameters} parameters * @param {AtlasSprite} sprite * @param {number=} extrudePixels How many pixels to extrude the sprite - * @param {boolean=} clipping Whether to clip * @param {Vector=} overridePosition Whether to drwa the entity at a different location */ - drawSpriteOnFullEntityBounds( - parameters, - sprite, - extrudePixels = 0, - clipping = true, - overridePosition = null - ) { + drawSpriteOnFullEntityBounds(parameters, sprite, extrudePixels = 0, overridePosition = null) { if (!this.shouldBeDrawn(parameters) && !overridePosition) { return; } + const size = this.getTileSize(); let worldX = this.origin.x * globalConfig.tileSize; let worldY = this.origin.y * globalConfig.tileSize; @@ -258,10 +245,10 @@ export class StaticMapEntityComponent extends Component { // Early out, is faster sprite.drawCached( parameters, - worldX - extrudePixels * this.tileSize.x, - worldY - extrudePixels * this.tileSize.y, - globalConfig.tileSize * this.tileSize.x + 2 * extrudePixels * this.tileSize.x, - globalConfig.tileSize * this.tileSize.y + 2 * extrudePixels * this.tileSize.y, + worldX - extrudePixels * size.x, + worldY - extrudePixels * size.y, + globalConfig.tileSize * size.x + 2 * extrudePixels * size.x, + globalConfig.tileSize * size.y + 2 * extrudePixels * size.y, false ); } else { @@ -273,10 +260,10 @@ export class StaticMapEntityComponent extends Component { sprite.drawCached( parameters, - -globalConfig.halfTileSize - extrudePixels * this.tileSize.x, - -globalConfig.halfTileSize - extrudePixels * this.tileSize.y, - globalConfig.tileSize * this.tileSize.x + 2 * extrudePixels * this.tileSize.x, - globalConfig.tileSize * this.tileSize.y + 2 * extrudePixels * this.tileSize.y, + -globalConfig.halfTileSize - extrudePixels * size.x, + -globalConfig.halfTileSize - extrudePixels * size.y, + globalConfig.tileSize * size.x + 2 * extrudePixels * size.x, + globalConfig.tileSize * size.y + 2 * extrudePixels * size.y, false ); diff --git a/src/js/game/hud/parts/building_placer.js b/src/js/game/hud/parts/building_placer.js index 9198c69a..a6a16aad 100644 --- a/src/js/game/hud/parts/building_placer.js +++ b/src/js/game/hud/parts/building_placer.js @@ -312,7 +312,6 @@ export class HUDBuildingPlacer extends HUDBuildingPlacerLogic { const staticComp = this.fakeEntity.components.StaticMapEntity; staticComp.origin = mouseTile; staticComp.rotation = rotation; - staticComp.tileSize = metaBuilding.getDimensions(this.currentVariant.get()); metaBuilding.updateVariants(this.fakeEntity, rotationVariant, this.currentVariant.get()); staticComp.code = getCodeFromBuildingData( this.currentMetaBuilding.get(), diff --git a/src/js/game/systems/belt.js b/src/js/game/systems/belt.js index 7f09a873..a581343c 100644 --- a/src/js/game/systems/belt.js +++ b/src/js/game/systems/belt.js @@ -507,12 +507,7 @@ export class BeltSystem extends GameSystemWithFilter { const direction = entity.components.Belt.direction; const sprite = this.beltAnimations[direction][animationIndex % BELT_ANIM_COUNT]; - entity.components.StaticMapEntity.drawSpriteOnFullEntityBounds( - parameters, - sprite, - 0, - false - ); + entity.components.StaticMapEntity.drawSpriteOnFullEntityBounds(parameters, sprite, 0); } } } diff --git a/src/js/game/systems/static_map_entity.js b/src/js/game/systems/static_map_entity.js index c5089eb3..7e931575 100644 --- a/src/js/game/systems/static_map_entity.js +++ b/src/js/game/systems/static_map_entity.js @@ -47,7 +47,7 @@ export class StaticMapEntitySystem extends GameSystem { const beltComp = entity.components.Belt; if (beltComp) { const sprite = this.beltOverviewSprites[beltComp.direction]; - staticComp.drawSpriteOnFullEntityBounds(parameters, sprite, 0, false); + staticComp.drawSpriteOnFullEntityBounds(parameters, sprite, 0); } else { parameters.context.fillRect( rect.x * globalConfig.tileSize, @@ -59,7 +59,7 @@ export class StaticMapEntitySystem extends GameSystem { } else { const sprite = staticComp.getSprite(); if (sprite) { - staticComp.drawSpriteOnFullEntityBounds(parameters, sprite, 2, false); + staticComp.drawSpriteOnFullEntityBounds(parameters, sprite, 2); } } } @@ -91,7 +91,7 @@ export class StaticMapEntitySystem extends GameSystem { const sprite = staticComp.getSprite(); if (sprite) { - staticComp.drawSpriteOnFullEntityBounds(parameters, sprite, 2, false); + staticComp.drawSpriteOnFullEntityBounds(parameters, sprite, 2); } } }