1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2026-03-02 03:39:21 +00:00

Improve performance by getting rid of instanceof

This commit is contained in:
tobspr
2020-06-27 10:38:11 +02:00
parent d8bf4f9cd8
commit 419401b9be
21 changed files with 239 additions and 199 deletions

View File

@@ -102,7 +102,9 @@ export class BeltSystem extends GameSystemWithFilter {
logger.warn("Restored", this.beltPaths.length, "belt paths");
}
this.verifyBeltPaths();
if (G_IS_DEV && globalConfig.debug.checkBeltPaths) {
this.debug_verifyBeltPaths();
}
}
/**
@@ -171,12 +173,16 @@ export class BeltSystem extends GameSystemWithFilter {
this.addEntityToPaths(targetEntity);
// Sanity
this.verifyBeltPaths();
if (G_IS_DEV && globalConfig.debug.checkBeltPaths) {
this.debug_verifyBeltPaths();
}
}
}
}
this.verifyBeltPaths();
if (G_IS_DEV && globalConfig.debug.checkBeltPaths) {
this.debug_verifyBeltPaths();
}
}
/**
@@ -195,7 +201,9 @@ export class BeltSystem extends GameSystemWithFilter {
const assignedPath = entity.components.Belt.assignedPath;
assert(assignedPath, "Entity has no belt path assigned");
this.deleteEntityFromPath(assignedPath, entity);
this.verifyBeltPaths();
if (G_IS_DEV && globalConfig.debug.checkBeltPaths) {
this.debug_verifyBeltPaths();
}
}
/**
@@ -281,7 +289,9 @@ export class BeltSystem extends GameSystemWithFilter {
}
this.addEntityToPaths(entity);
this.verifyBeltPaths();
if (G_IS_DEV && globalConfig.debug.checkBeltPaths) {
this.debug_verifyBeltPaths();
}
}
/**
@@ -297,21 +307,19 @@ export class BeltSystem extends GameSystemWithFilter {
/**
* Verifies all belt paths
*/
verifyBeltPaths() {
if (G_IS_DEV && true) {
for (let i = 0; i < this.beltPaths.length; ++i) {
this.beltPaths[i].debug_checkIntegrity("general-verify");
}
debug_verifyBeltPaths() {
for (let i = 0; i < this.beltPaths.length; ++i) {
this.beltPaths[i].debug_checkIntegrity("general-verify");
}
const belts = this.root.entityMgr.getAllWithComponent(BeltComponent);
for (let i = 0; i < belts.length; ++i) {
const path = belts[i].components.Belt.assignedPath;
if (!path) {
throw new Error("Belt has no path: " + belts[i].uid);
}
if (this.beltPaths.indexOf(path) < 0) {
throw new Error("Path of entity not contained: " + belts[i].uid);
}
const belts = this.root.entityMgr.getAllWithComponent(BeltComponent);
for (let i = 0; i < belts.length; ++i) {
const path = belts[i].components.Belt.assignedPath;
if (!path) {
throw new Error("Belt has no path: " + belts[i].uid);
}
if (this.beltPaths.indexOf(path) < 0) {
throw new Error("Path of entity not contained: " + belts[i].uid);
}
}
}
@@ -450,13 +458,17 @@ export class BeltSystem extends GameSystemWithFilter {
* Updates all belts
*/
update() {
this.verifyBeltPaths();
if (G_IS_DEV && globalConfig.debug.checkBeltPaths) {
this.debug_verifyBeltPaths();
}
for (let i = 0; i < this.beltPaths.length; ++i) {
this.beltPaths[i].update();
}
this.verifyBeltPaths();
if (G_IS_DEV && globalConfig.debug.checkBeltPaths) {
this.debug_verifyBeltPaths();
}
}
/**

View File

@@ -21,20 +21,23 @@ export class ItemAcceptorSystem extends GameSystemWithFilter {
}
update() {
const progress =
this.root.dynamicTickrate.deltaSeconds *
this.root.hubGoals.getBeltBaseSpeed() *
2 * // * 2 because its only a half tile
globalConfig.itemSpacingOnBelts;
for (let i = 0; i < this.allEntities.length; ++i) {
const entity = this.allEntities[i];
const aceptorComp = entity.components.ItemAcceptor;
const animations = aceptorComp.itemConsumptionAnimations;
// Process item consumption animations to avoid items popping from the belts
for (let animIndex = 0; animIndex < aceptorComp.itemConsumptionAnimations.length; ++animIndex) {
const anim = aceptorComp.itemConsumptionAnimations[animIndex];
anim.animProgress +=
this.root.dynamicTickrate.deltaSeconds *
this.root.hubGoals.getBeltBaseSpeed() *
2 *
globalConfig.itemSpacingOnBelts;
for (let animIndex = 0; animIndex < animations.length; ++animIndex) {
const anim = animations[animIndex];
anim.animProgress += progress;
if (anim.animProgress > 1) {
aceptorComp.itemConsumptionAnimations.splice(animIndex, 1);
animations.splice(animIndex, 1);
animIndex -= 1;
}
}

View File

@@ -193,18 +193,19 @@ export class ItemEjectorSystem extends GameSystemWithFilter {
if (!sourceEjectorComp.cachedConnectedSlots) {
continue;
}
for (let j = 0; j < sourceEjectorComp.cachedConnectedSlots.length; j++) {
const sourceSlot = sourceEjectorComp.cachedConnectedSlots[j];
const destSlot = sourceSlot.cachedDestSlot;
const targetEntity = sourceSlot.cachedTargetEntity;
const slots = sourceEjectorComp.cachedConnectedSlots;
for (let j = 0; j < slots.length; ++j) {
const sourceSlot = slots[j];
const item = sourceSlot.item;
if (!item) {
// No item available to be ejected
continue;
}
const destSlot = sourceSlot.cachedDestSlot;
const targetEntity = sourceSlot.cachedTargetEntity;
// Advance items on the slot
sourceSlot.progress = Math_min(1, sourceSlot.progress + progressGrowth);
@@ -248,15 +249,8 @@ export class ItemEjectorSystem extends GameSystemWithFilter {
if (path.tryAcceptItem(item)) {
return true;
}
}
const storageComp = receiver.components.Storage;
if (storageComp) {
// It's a storage
if (storageComp.canAcceptItem(item)) {
storageComp.takeItem(item);
return true;
}
// Belt can have nothing else
return false;
}
const itemProcessorComp = receiver.components.ItemProcessor;
@@ -265,6 +259,8 @@ export class ItemEjectorSystem extends GameSystemWithFilter {
if (itemProcessorComp.tryTakeItem(item, slotIndex)) {
return true;
}
// Item processor can have nothing else
return false;
}
const undergroundBeltComp = receiver.components.UndergroundBelt;
@@ -278,6 +274,21 @@ export class ItemEjectorSystem extends GameSystemWithFilter {
) {
return true;
}
// Underground belt can have nothing else
return false;
}
const storageComp = receiver.components.Storage;
if (storageComp) {
// It's a storage
if (storageComp.canAcceptItem(item)) {
storageComp.takeItem(item);
return true;
}
// Storage can't have anything else
return false;
}
const energyGeneratorComp = receiver.components.EnergyGenerator;
@@ -286,6 +297,9 @@ export class ItemEjectorSystem extends GameSystemWithFilter {
// Passed it over
return true;
}
// Energy generator comp can't have anything else
return false;
}
return false;

View File

@@ -30,21 +30,21 @@ export class UndergroundBeltSystem extends GameSystemWithFilter {
}
update() {
const delta = this.root.dynamicTickrate.deltaSeconds;
for (let i = 0; i < this.allEntities.length; ++i) {
const entity = this.allEntities[i];
const undergroundComp = entity.components.UndergroundBelt;
const pendingItems = undergroundComp.pendingItems;
// Decrease remaining time of all items in belt
for (let k = 0; k < undergroundComp.pendingItems.length; ++k) {
const item = undergroundComp.pendingItems[k];
item[1] = Math_max(0, item[1] - this.root.dynamicTickrate.deltaSeconds);
for (let k = 0; k < pendingItems.length; ++k) {
const item = pendingItems[k];
item[1] = Math_max(0, item[1] - delta);
if (G_IS_DEV && globalConfig.debug.instantBelts) {
item[1] = 0;
}
}
if (undergroundComp.mode === enumUndergroundBeltMode.sender) {
this.handleSender(entity);
} else {