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
|
* Creates the entity at the given location
|
||||||
* @param {Entity} entity
|
* @param {Entity} entity
|
||||||
* @param {GameRoot} root
|
|
||||||
*/
|
*/
|
||||||
setupEntityComponents(entity, root) {
|
setupEntityComponents(entity) {
|
||||||
let itemBelow = null;
|
entity.addComponent(new MinerComponent({}));
|
||||||
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 }],
|
||||||
|
@ -12,10 +12,10 @@ 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,
|
||||||
minedItem: types.nullable(types.obj(gItemRegistry)),
|
|
||||||
itemChainBuffer: types.array(types.obj(gItemRegistry)),
|
itemChainBuffer: types.array(types.obj(gItemRegistry)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -23,14 +23,10 @@ export class MinerComponent extends Component {
|
|||||||
duplicateWithoutContents() {
|
duplicateWithoutContents() {
|
||||||
return new MinerComponent({
|
return new MinerComponent({
|
||||||
chainable: this.chainable,
|
chainable: this.chainable,
|
||||||
minedItem: this.minedItem,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
constructor({ chainable = false }) {
|
||||||
* @param {{chainable?: boolean, minedItem?: BaseItem}} param0
|
|
||||||
*/
|
|
||||||
constructor({ chainable = false, minedItem = null }) {
|
|
||||||
super();
|
super();
|
||||||
this.lastMiningTime = 0;
|
this.lastMiningTime = 0;
|
||||||
this.chainable = chainable;
|
this.chainable = chainable;
|
||||||
@ -45,7 +41,7 @@ export class MinerComponent extends Component {
|
|||||||
/**
|
/**
|
||||||
* @type {BaseItem}
|
* @type {BaseItem}
|
||||||
*/
|
*/
|
||||||
this.minedItem = minedItem;
|
this.cachedMinedItem = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,9 +25,17 @@ export class MinerSystem extends GameSystemWithFilter {
|
|||||||
|
|
||||||
const minerComp = entity.components.Miner;
|
const minerComp = entity.components.Miner;
|
||||||
|
|
||||||
if (!minerComp.minedItem) {
|
if (!minerComp.cachedMinedItem) {
|
||||||
|
const staticComp = entity.components.StaticMapEntity;
|
||||||
|
const tileBelow = this.root.map.getLowerLayerContentXY(
|
||||||
|
staticComp.origin.x,
|
||||||
|
staticComp.origin.y
|
||||||
|
);
|
||||||
|
if (!tileBelow) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
minerComp.cachedMinedItem = tileBelow;
|
||||||
|
}
|
||||||
|
|
||||||
// First, try to get rid of chained items
|
// First, try to get rid of chained items
|
||||||
if (minerComp.itemChainBuffer.length > 0) {
|
if (minerComp.itemChainBuffer.length > 0) {
|
||||||
@ -38,9 +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)) {
|
||||||
if (this.tryPerformMinerEject(entity, minerComp.minedItem)) {
|
if (this.tryPerformMinerEject(entity, minerComp.cachedMinedItem)) {
|
||||||
// Analytics hook
|
// Analytics hook
|
||||||
this.root.signals.itemProduced.dispatch(minerComp.minedItem);
|
this.root.signals.itemProduced.dispatch(minerComp.cachedMinedItem);
|
||||||
|
|
||||||
// Actually mine
|
// Actually mine
|
||||||
minerComp.lastMiningTime = this.root.time.now();
|
minerComp.lastMiningTime = this.root.time.now();
|
||||||
@ -105,10 +113,13 @@ export class MinerSystem extends GameSystemWithFilter {
|
|||||||
if (!staticComp.shouldBeDrawn(parameters)) {
|
if (!staticComp.shouldBeDrawn(parameters)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (!minerComp.cachedMinedItem) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (minerComp.minedItem) {
|
if (minerComp.cachedMinedItem) {
|
||||||
const padding = 3;
|
const padding = 3;
|
||||||
parameters.context.fillStyle = minerComp.minedItem.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,
|
||||||
@ -117,8 +128,8 @@ export class MinerSystem extends GameSystemWithFilter {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (minerComp.minedItem) {
|
if (minerComp.cachedMinedItem) {
|
||||||
minerComp.minedItem.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