From f15cd5bcffbbdd27332e1ff6c4a68e8f860e6947 Mon Sep 17 00:00:00 2001 From: Phlosioneer Date: Wed, 17 Jun 2020 15:36:53 -0400 Subject: [PATCH] Rework to cache instead of serialize Using lessons from other PRs --- src/js/game/buildings/miner.js | 10 ++-------- src/js/game/components/miner.js | 10 +++------- src/js/game/systems/miner.js | 27 +++++++++++++++++++-------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/js/game/buildings/miner.js b/src/js/game/buildings/miner.js index 2baf3fd4..ed87bc85 100644 --- a/src/js/game/buildings/miner.js +++ b/src/js/game/buildings/miner.js @@ -44,15 +44,9 @@ export class MetaMinerBuilding extends MetaBuilding { /** * Creates the entity at the given location * @param {Entity} entity - * @param {GameRoot} root */ - setupEntityComponents(entity, root) { - let itemBelow = null; - if (root) { - const staticComp = entity.components.StaticMapEntity; - itemBelow = root.map.getLowerLayerContentXY(staticComp.origin.x, staticComp.origin.y); - } - entity.addComponent(new MinerComponent({ minedItem: itemBelow })); + setupEntityComponents(entity) { + entity.addComponent(new MinerComponent({})); entity.addComponent( new ItemEjectorComponent({ slots: [{ pos: new Vector(0, 0), direction: enumDirection.top }], diff --git a/src/js/game/components/miner.js b/src/js/game/components/miner.js index ae64223a..74a4b616 100644 --- a/src/js/game/components/miner.js +++ b/src/js/game/components/miner.js @@ -12,10 +12,10 @@ export class MinerComponent extends Component { } static getSchema() { + // cachedMinedItem is not serialized. return { lastMiningTime: types.ufloat, chainable: types.bool, - minedItem: types.nullable(types.obj(gItemRegistry)), itemChainBuffer: types.array(types.obj(gItemRegistry)), }; } @@ -23,14 +23,10 @@ export class MinerComponent extends Component { duplicateWithoutContents() { return new MinerComponent({ chainable: this.chainable, - minedItem: this.minedItem, }); } - /** - * @param {{chainable?: boolean, minedItem?: BaseItem}} param0 - */ - constructor({ chainable = false, minedItem = null }) { + constructor({ chainable = false }) { super(); this.lastMiningTime = 0; this.chainable = chainable; @@ -45,7 +41,7 @@ export class MinerComponent extends Component { /** * @type {BaseItem} */ - this.minedItem = minedItem; + this.cachedMinedItem = null; } /** diff --git a/src/js/game/systems/miner.js b/src/js/game/systems/miner.js index 41786603..b195e6ab 100644 --- a/src/js/game/systems/miner.js +++ b/src/js/game/systems/miner.js @@ -25,8 +25,16 @@ export class MinerSystem extends GameSystemWithFilter { const minerComp = entity.components.Miner; - if (!minerComp.minedItem) { - continue; + if (!minerComp.cachedMinedItem) { + const staticComp = entity.components.StaticMapEntity; + const tileBelow = this.root.map.getLowerLayerContentXY( + staticComp.origin.x, + staticComp.origin.y + ); + if (!tileBelow) { + continue; + } + minerComp.cachedMinedItem = tileBelow; } // First, try to get rid of chained items @@ -38,9 +46,9 @@ export class MinerSystem extends GameSystemWithFilter { } if (this.root.time.isIngameTimerExpired(minerComp.lastMiningTime, 1 / miningSpeed)) { - if (this.tryPerformMinerEject(entity, minerComp.minedItem)) { + if (this.tryPerformMinerEject(entity, minerComp.cachedMinedItem)) { // Analytics hook - this.root.signals.itemProduced.dispatch(minerComp.minedItem); + this.root.signals.itemProduced.dispatch(minerComp.cachedMinedItem); // Actually mine minerComp.lastMiningTime = this.root.time.now(); @@ -105,10 +113,13 @@ export class MinerSystem extends GameSystemWithFilter { if (!staticComp.shouldBeDrawn(parameters)) { continue; } + if (!minerComp.cachedMinedItem) { + continue; + } - if (minerComp.minedItem) { + if (minerComp.cachedMinedItem) { const padding = 3; - parameters.context.fillStyle = minerComp.minedItem.getBackgroundColorAsResource(); + parameters.context.fillStyle = minerComp.cachedMinedItem.getBackgroundColorAsResource(); parameters.context.fillRect( staticComp.origin.x * globalConfig.tileSize + padding, staticComp.origin.y * globalConfig.tileSize + padding, @@ -117,8 +128,8 @@ export class MinerSystem extends GameSystemWithFilter { ); } - if (minerComp.minedItem) { - minerComp.minedItem.draw( + if (minerComp.cachedMinedItem) { + minerComp.cachedMinedItem.draw( (0.5 + staticComp.origin.x) * globalConfig.tileSize, (0.5 + staticComp.origin.y) * globalConfig.tileSize, parameters