1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2024-10-27 20:34:29 +00:00

Fix click and drag

Clicking and dragging can trigger up to 4 add/destroy signals, and it's
a common case.
This commit is contained in:
Phlosioneer 2020-06-16 22:08:46 -04:00
parent 2341772425
commit da24c472d7

View File

@ -65,9 +65,8 @@ export class BeltSystem extends GameSystemWithFilter {
this.root.signals.entityDestroyed.add(this.updateSurroundingBeltPlacement, this);
this.cacheNeedsUpdate = true;
/** @type {Rectangle} */
this.singleUpdateArea = null;
this.isMultiUpdate = false;
/** @type {Rectangle[]} */
this.smallUpdateAreas = [];
}
/**
@ -119,15 +118,10 @@ export class BeltSystem extends GameSystemWithFilter {
}
}
// Optimize for the common case of adding or removing one belt.
// Optimize for the common case of adding or removing buildings one at a time.
// Clicking and dragging can fire up to 4 create/destroy signals.
if (this.cacheNeedsUpdate) {
if (this.isMultiUpdate || this.singleUpdateArea) {
// This isn't the common case.
// The singleUpdateArea will be cleared by the cache update.
this.isMultiUpdate = true;
} else {
this.singleUpdateArea = affectedArea.expandedInAllDirections(1);
}
this.smallUpdateAreas.push(affectedArea.expandedInAllDirections(1));
}
}
@ -176,23 +170,25 @@ export class BeltSystem extends GameSystemWithFilter {
computeBeltCache() {
logger.log("Updating belt cache");
if (this.singleUpdateArea && !this.isMultiUpdate) {
for (let x = this.singleUpdateArea.x; x < this.singleUpdateArea.right(); ++x) {
for (let y = this.singleUpdateArea.y; y < this.singleUpdateArea.bottom(); ++y) {
if (this.smallUpdateAreas.length <= 4) {
for (let i = 0; i < this.smallUpdateAreas.length; ++i) {
const area = this.smallUpdateAreas[i];
for (let x = area.x; x < area.right(); ++x) {
for (let y = area.y; y < area.bottom(); ++y) {
const tile = this.root.map.getTileContentXY(x, y);
if (tile && tile.components.Belt) {
tile.components.Belt.followUpCache = this.findFollowUpEntity(tile);
}
}
}
}
} else {
for (let i = 0; i < this.allEntities.length; ++i) {
const entity = this.allEntities[i];
entity.components.Belt.followUpCache = this.findFollowUpEntity(entity);
}
}
this.isMultiUpdate = false;
this.singleUpdateArea = null;
this.smallUpdateAreas = [];
}
update() {