mirror of
https://github.com/tobspr/shapez.io.git
synced 2024-10-27 20:34:29 +00:00
Rework to cache instead of serialize
Using lessons from other PRs
This commit is contained in:
parent
d4fc2e6dcd
commit
f15cd5bcff
@ -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 }],
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user