mirror of
https://github.com/tobspr/shapez.io.git
synced 2024-10-27 20:34:29 +00:00
Merge pull request #193 from Phlosioneer/miner-optimization
[Minor Opt] Make miners cache mined item
This commit is contained in:
commit
cbdc302760
@ -12,6 +12,7 @@ export class MinerComponent extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static getSchema() {
|
static getSchema() {
|
||||||
|
// cachedMinedItem is not serialized.
|
||||||
return {
|
return {
|
||||||
lastMiningTime: types.ufloat,
|
lastMiningTime: types.ufloat,
|
||||||
chainable: types.bool,
|
chainable: types.bool,
|
||||||
@ -25,8 +26,6 @@ export class MinerComponent extends Component {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*/
|
|
||||||
constructor({ chainable = false }) {
|
constructor({ chainable = false }) {
|
||||||
super();
|
super();
|
||||||
this.lastMiningTime = 0;
|
this.lastMiningTime = 0;
|
||||||
@ -38,6 +37,11 @@ export class MinerComponent extends Component {
|
|||||||
* @type {Array<BaseItem>}
|
* @type {Array<BaseItem>}
|
||||||
*/
|
*/
|
||||||
this.itemChainBuffer = [];
|
this.itemChainBuffer = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {BaseItem}
|
||||||
|
*/
|
||||||
|
this.cachedMinedItem = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -24,11 +24,17 @@ export class MinerSystem extends GameSystemWithFilter {
|
|||||||
// Check if miner is above an actual tile
|
// Check if miner is above an actual tile
|
||||||
|
|
||||||
const minerComp = entity.components.Miner;
|
const minerComp = entity.components.Miner;
|
||||||
const staticComp = entity.components.StaticMapEntity;
|
|
||||||
|
|
||||||
const tileBelow = this.root.map.getLowerLayerContentXY(staticComp.origin.x, staticComp.origin.y);
|
if (!minerComp.cachedMinedItem) {
|
||||||
if (!tileBelow) {
|
const staticComp = entity.components.StaticMapEntity;
|
||||||
continue;
|
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
|
// First, try to get rid of chained items
|
||||||
@ -40,20 +46,9 @@ export class MinerSystem extends GameSystemWithFilter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this.root.time.isIngameTimerExpired(minerComp.lastMiningTime, 1 / miningSpeed)) {
|
if (this.root.time.isIngameTimerExpired(minerComp.lastMiningTime, 1 / miningSpeed)) {
|
||||||
const lowerLayerItem = this.root.map.getLowerLayerContentXY(
|
if (this.tryPerformMinerEject(entity, minerComp.cachedMinedItem)) {
|
||||||
staticComp.origin.x,
|
|
||||||
staticComp.origin.y
|
|
||||||
);
|
|
||||||
|
|
||||||
// TODO: Should not be required actually
|
|
||||||
if (!lowerLayerItem) {
|
|
||||||
// Nothing below;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.tryPerformMinerEject(entity, lowerLayerItem)) {
|
|
||||||
// Analytics hook
|
// Analytics hook
|
||||||
this.root.signals.itemProduced.dispatch(lowerLayerItem);
|
this.root.signals.itemProduced.dispatch(minerComp.cachedMinedItem);
|
||||||
|
|
||||||
// Actually mine
|
// Actually mine
|
||||||
minerComp.lastMiningTime = this.root.time.now();
|
minerComp.lastMiningTime = this.root.time.now();
|
||||||
@ -114,18 +109,17 @@ export class MinerSystem extends GameSystemWithFilter {
|
|||||||
|
|
||||||
if (entity && entity.components.Miner) {
|
if (entity && entity.components.Miner) {
|
||||||
const staticComp = entity.components.StaticMapEntity;
|
const staticComp = entity.components.StaticMapEntity;
|
||||||
|
const minerComp = entity.components.Miner;
|
||||||
if (!staticComp.shouldBeDrawn(parameters)) {
|
if (!staticComp.shouldBeDrawn(parameters)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (!minerComp.cachedMinedItem) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
const lowerLayerItem = this.root.map.getLowerLayerContentXY(
|
if (minerComp.cachedMinedItem) {
|
||||||
staticComp.origin.x,
|
|
||||||
staticComp.origin.y
|
|
||||||
);
|
|
||||||
|
|
||||||
if (lowerLayerItem) {
|
|
||||||
const padding = 3;
|
const padding = 3;
|
||||||
parameters.context.fillStyle = lowerLayerItem.getBackgroundColorAsResource();
|
parameters.context.fillStyle = minerComp.cachedMinedItem.getBackgroundColorAsResource();
|
||||||
parameters.context.fillRect(
|
parameters.context.fillRect(
|
||||||
staticComp.origin.x * globalConfig.tileSize + padding,
|
staticComp.origin.x * globalConfig.tileSize + padding,
|
||||||
staticComp.origin.y * globalConfig.tileSize + padding,
|
staticComp.origin.y * globalConfig.tileSize + padding,
|
||||||
@ -134,8 +128,8 @@ export class MinerSystem extends GameSystemWithFilter {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lowerLayerItem) {
|
if (minerComp.cachedMinedItem) {
|
||||||
lowerLayerItem.draw(
|
minerComp.cachedMinedItem.draw(
|
||||||
(0.5 + staticComp.origin.x) * globalConfig.tileSize,
|
(0.5 + staticComp.origin.x) * globalConfig.tileSize,
|
||||||
(0.5 + staticComp.origin.y) * globalConfig.tileSize,
|
(0.5 + staticComp.origin.y) * globalConfig.tileSize,
|
||||||
parameters
|
parameters
|
||||||
|
Loading…
Reference in New Issue
Block a user