Make miners cache mined item
Saves two chunk+tile lookups per update, and one chunk+tile lookup per draw.
This commit is contained in:
parent
b753187cde
commit
83a4928be5
|
@ -44,9 +44,15 @@ export class MetaMinerBuilding extends MetaBuilding {
|
||||||
/**
|
/**
|
||||||
* Creates the entity at the given location
|
* Creates the entity at the given location
|
||||||
* @param {Entity} entity
|
* @param {Entity} entity
|
||||||
|
* @param {GameRoot} root
|
||||||
*/
|
*/
|
||||||
setupEntityComponents(entity) {
|
setupEntityComponents(entity, root) {
|
||||||
entity.addComponent(new MinerComponent({}));
|
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 }));
|
||||||
entity.addComponent(
|
entity.addComponent(
|
||||||
new ItemEjectorComponent({
|
new ItemEjectorComponent({
|
||||||
slots: [{ pos: new Vector(0, 0), direction: enumDirection.top }],
|
slots: [{ pos: new Vector(0, 0), direction: enumDirection.top }],
|
||||||
|
|
|
@ -15,6 +15,7 @@ export class MinerComponent extends Component {
|
||||||
return {
|
return {
|
||||||
lastMiningTime: types.ufloat,
|
lastMiningTime: types.ufloat,
|
||||||
chainable: types.bool,
|
chainable: types.bool,
|
||||||
|
minedItem: types.nullable(types.obj(gItemRegistry)),
|
||||||
itemChainBuffer: types.array(types.obj(gItemRegistry)),
|
itemChainBuffer: types.array(types.obj(gItemRegistry)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -22,12 +23,15 @@ export class MinerComponent extends Component {
|
||||||
duplicateWithoutContents() {
|
duplicateWithoutContents() {
|
||||||
return new MinerComponent({
|
return new MinerComponent({
|
||||||
chainable: this.chainable,
|
chainable: this.chainable,
|
||||||
|
minedItem: this.minedItem
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
|
* @param {{chainable?: boolean, minedItem?: BaseItem}} param0
|
||||||
*/
|
*/
|
||||||
constructor({ chainable = false }) {
|
constructor({ chainable = false, minedItem = null }) {
|
||||||
super();
|
super();
|
||||||
this.lastMiningTime = 0;
|
this.lastMiningTime = 0;
|
||||||
this.chainable = chainable;
|
this.chainable = chainable;
|
||||||
|
@ -38,6 +42,11 @@ export class MinerComponent extends Component {
|
||||||
* @type {Array<BaseItem>}
|
* @type {Array<BaseItem>}
|
||||||
*/
|
*/
|
||||||
this.itemChainBuffer = [];
|
this.itemChainBuffer = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {BaseItem}
|
||||||
|
*/
|
||||||
|
this.minedItem = minedItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -24,10 +24,8 @@ 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.minedItem) {
|
||||||
if (!tileBelow) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,20 +38,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.minedItem)) {
|
||||||
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.minedItem);
|
||||||
|
|
||||||
// Actually mine
|
// Actually mine
|
||||||
minerComp.lastMiningTime = this.root.time.now();
|
minerComp.lastMiningTime = this.root.time.now();
|
||||||
|
@ -114,18 +101,14 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
const lowerLayerItem = this.root.map.getLowerLayerContentXY(
|
if (minerComp.minedItem) {
|
||||||
staticComp.origin.x,
|
|
||||||
staticComp.origin.y
|
|
||||||
);
|
|
||||||
|
|
||||||
if (lowerLayerItem) {
|
|
||||||
const padding = 3;
|
const padding = 3;
|
||||||
parameters.context.fillStyle = lowerLayerItem.getBackgroundColorAsResource();
|
parameters.context.fillStyle = minerComp.minedItem.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 +117,8 @@ export class MinerSystem extends GameSystemWithFilter {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lowerLayerItem) {
|
if (minerComp.minedItem) {
|
||||||
lowerLayerItem.draw(
|
minerComp.minedItem.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
|
||||||
|
|
Reference in New Issue