Minor performance improvements, improve underground belt performance

This commit is contained in:
tobspr 2020-09-18 19:21:12 +02:00
parent 746f4935ad
commit 0238de1260
4 changed files with 370 additions and 428 deletions

View File

@ -553,25 +553,7 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
const bonusTimeToApply = Math.min(originalTime, processorComp.bonusTime); const bonusTimeToApply = Math.min(originalTime, processorComp.bonusTime);
const timeToProcess = originalTime - bonusTimeToApply; const timeToProcess = originalTime - bonusTimeToApply;
// Substract one tick because we already process it this frame
// if (processorComp.bonusTime > originalTime) {
// if (processorComp.type === enumItemProcessorTypes.reader) {
// console.log(
// "Bonus time",
// round4Digits(processorComp.bonusTime),
// "Original time",
// round4Digits(originalTime),
// "Overcomit by",
// round4Digits(processorComp.bonusTime - originalTime),
// "->",
// round4Digits(timeToProcess),
// "reduced by",
// round4Digits(bonusTimeToApply)
// );
// }
// }
processorComp.bonusTime -= bonusTimeToApply; processorComp.bonusTime -= bonusTimeToApply;
processorComp.ongoingCharges.push({ processorComp.ongoingCharges.push({
items: outItems, items: outItems,
remainingTime: timeToProcess, remainingTime: timeToProcess,

View File

@ -1,6 +1,6 @@
import { globalConfig } from "../../core/config"; import { globalConfig } from "../../core/config";
import { Loader } from "../../core/loader"; import { Loader } from "../../core/loader";
import { smoothPulse, round4Digits } from "../../core/utils"; import { smoothPulse } from "../../core/utils";
import { enumItemProcessorRequirements, enumItemProcessorTypes } from "../components/item_processor"; import { enumItemProcessorRequirements, enumItemProcessorTypes } from "../components/item_processor";
import { Entity } from "../entity"; import { Entity } from "../entity";
import { GameSystem } from "../game_system"; import { GameSystem } from "../game_system";
@ -17,7 +17,6 @@ export class ItemProcessorOverlaysSystem extends GameSystem {
this.readerOverlaySprite = Loader.getSprite("sprites/misc/reader_overlay.png"); this.readerOverlaySprite = Loader.getSprite("sprites/misc/reader_overlay.png");
this.drawnUids = new Set(); this.drawnUids = new Set();
this.root.signals.gameFrameStarted.add(this.clearDrawnUids, this); this.root.signals.gameFrameStarted.add(this.clearDrawnUids, this);
} }
@ -40,7 +39,6 @@ export class ItemProcessorOverlaysSystem extends GameSystem {
} }
const requirement = processorComp.processingRequirement; const requirement = processorComp.processingRequirement;
if (!requirement && processorComp.type !== enumItemProcessorTypes.reader) { if (!requirement && processorComp.type !== enumItemProcessorTypes.reader) {
continue; continue;
} }

View File

@ -46,7 +46,6 @@ export class MinerSystem extends GameSystemWithFilter {
} }
// Check if miner is above an actual tile // Check if miner is above an actual tile
if (!minerComp.cachedMinedItem) { if (!minerComp.cachedMinedItem) {
const staticComp = entity.components.StaticMapEntity; const staticComp = entity.components.StaticMapEntity;
const tileBelow = this.root.map.getLowerLayerContentXY( const tileBelow = this.root.map.getLowerLayerContentXY(

View File

@ -1,406 +1,369 @@
import { globalConfig } from "../../core/config"; import { globalConfig } from "../../core/config";
import { Loader } from "../../core/loader"; import { Loader } from "../../core/loader";
import { createLogger } from "../../core/logging"; import { createLogger } from "../../core/logging";
import { Rectangle } from "../../core/rectangle"; import { Rectangle } from "../../core/rectangle";
import { import { StaleAreaDetector } from "../../core/stale_area_detector";
enumAngleToDirection, import { fastArrayDelete } from "../../core/utils";
enumDirection, import {
enumDirectionToAngle, enumAngleToDirection,
enumDirectionToVector, enumDirection,
enumInvertedDirections, enumDirectionToAngle,
} from "../../core/vector"; enumDirectionToVector,
import { enumUndergroundBeltMode, UndergroundBeltComponent } from "../components/underground_belt"; enumInvertedDirections,
import { Entity } from "../entity"; } from "../../core/vector";
import { GameSystemWithFilter } from "../game_system_with_filter"; import { enumUndergroundBeltMode, UndergroundBeltComponent } from "../components/underground_belt";
import { fastArrayDelete } from "../../core/utils"; import { Entity } from "../entity";
import { GameSystemWithFilter } from "../game_system_with_filter";
const logger = createLogger("tunnels");
const logger = createLogger("tunnels");
export class UndergroundBeltSystem extends GameSystemWithFilter {
constructor(root) { export class UndergroundBeltSystem extends GameSystemWithFilter {
super(root, [UndergroundBeltComponent]); constructor(root) {
super(root, [UndergroundBeltComponent]);
this.beltSprites = {
[enumUndergroundBeltMode.sender]: Loader.getSprite( this.beltSprites = {
"sprites/buildings/underground_belt_entry.png" [enumUndergroundBeltMode.sender]: Loader.getSprite(
), "sprites/buildings/underground_belt_entry.png"
[enumUndergroundBeltMode.receiver]: Loader.getSprite( ),
"sprites/buildings/underground_belt_exit.png" [enumUndergroundBeltMode.receiver]: Loader.getSprite(
), "sprites/buildings/underground_belt_exit.png"
}; ),
};
this.root.signals.entityManuallyPlaced.add(this.onEntityManuallyPlaced, this);
this.staleAreaWatcher = new StaleAreaDetector({
/** root: this.root,
* @type {Rectangle} name: "underground-belt",
*/ recomputeMethod: this.recomputeArea.bind(this),
this.areaToRecompute = null; });
this.root.signals.entityAdded.add(this.onEntityChanged, this); this.root.signals.entityManuallyPlaced.add(this.onEntityManuallyPlaced, this);
this.root.signals.entityDestroyed.add(this.onEntityChanged, this);
} // NOTICE: Once we remove a tunnel, we need to update the whole area to
// clear outdated handles
/** this.staleAreaWatcher.recomputeOnComponentsChanged(
* Called when an entity got added or removed [UndergroundBeltComponent],
* @param {Entity} entity globalConfig.undergroundBeltMaxTilesByTier[globalConfig.undergroundBeltMaxTilesByTier.length - 1]
*/ );
onEntityChanged(entity) { }
if (!this.root.gameInitialized) {
return; /**
} * Callback when an entity got placed, used to remove belts between underground belts
const undergroundComp = entity.components.UndergroundBelt; * @param {Entity} entity
if (!undergroundComp) { */
return; onEntityManuallyPlaced(entity) {
} if (!this.root.app.settings.getAllSettings().enableTunnelSmartplace) {
// Smart-place disabled
const affectedArea = entity.components.StaticMapEntity.getTileSpaceBounds().expandedInAllDirections( return;
globalConfig.undergroundBeltMaxTilesByTier[ }
globalConfig.undergroundBeltMaxTilesByTier.length - 1
] + 1 const undergroundComp = entity.components.UndergroundBelt;
); if (undergroundComp && undergroundComp.mode === enumUndergroundBeltMode.receiver) {
const staticComp = entity.components.StaticMapEntity;
if (this.areaToRecompute) { const tile = staticComp.origin;
this.areaToRecompute = this.areaToRecompute.getUnion(affectedArea);
} else { const direction = enumAngleToDirection[staticComp.rotation];
this.areaToRecompute = affectedArea; const inverseDirection = enumInvertedDirections[direction];
} const offset = enumDirectionToVector[inverseDirection];
}
let currentPos = tile.copy();
/**
* Callback when an entity got placed, used to remove belts between underground belts const tier = undergroundComp.tier;
* @param {Entity} entity const range = globalConfig.undergroundBeltMaxTilesByTier[tier];
*/
onEntityManuallyPlaced(entity) { // FIND ENTRANCE
if (!this.root.app.settings.getAllSettings().enableTunnelSmartplace) { // Search for the entrance which is furthes apart (this is why we can't reuse logic here)
// Smart-place disabled let matchingEntrance = null;
return; for (let i = 0; i < range; ++i) {
} currentPos.addInplace(offset);
const contents = this.root.map.getTileContent(currentPos, entity.layer);
const undergroundComp = entity.components.UndergroundBelt; if (!contents) {
if (undergroundComp && undergroundComp.mode === enumUndergroundBeltMode.receiver) { continue;
const staticComp = entity.components.StaticMapEntity; }
const tile = staticComp.origin;
const contentsUndergroundComp = contents.components.UndergroundBelt;
const direction = enumAngleToDirection[staticComp.rotation]; const contentsStaticComp = contents.components.StaticMapEntity;
const inverseDirection = enumInvertedDirections[direction]; if (
const offset = enumDirectionToVector[inverseDirection]; contentsUndergroundComp &&
contentsUndergroundComp.tier === undergroundComp.tier &&
let currentPos = tile.copy(); contentsUndergroundComp.mode === enumUndergroundBeltMode.sender &&
enumAngleToDirection[contentsStaticComp.rotation] === direction
const tier = undergroundComp.tier; ) {
const range = globalConfig.undergroundBeltMaxTilesByTier[tier]; matchingEntrance = {
entity: contents,
// FIND ENTRANCE range: i,
// Search for the entrance which is furthes apart (this is why we can't reuse logic here) };
let matchingEntrance = null; }
for (let i = 0; i < range; ++i) { }
currentPos.addInplace(offset);
const contents = this.root.map.getTileContent(currentPos, entity.layer); if (!matchingEntrance) {
if (!contents) { // Nothing found
continue; return;
} }
const contentsUndergroundComp = contents.components.UndergroundBelt; // DETECT OBSOLETE BELTS BETWEEN
const contentsStaticComp = contents.components.StaticMapEntity; // Remove any belts between entrance and exit which have the same direction,
if ( // but only if they *all* have the right direction
contentsUndergroundComp && currentPos = tile.copy();
contentsUndergroundComp.tier === undergroundComp.tier && let allBeltsMatch = true;
contentsUndergroundComp.mode === enumUndergroundBeltMode.sender && for (let i = 0; i < matchingEntrance.range; ++i) {
enumAngleToDirection[contentsStaticComp.rotation] === direction currentPos.addInplace(offset);
) {
matchingEntrance = { const contents = this.root.map.getTileContent(currentPos, entity.layer);
entity: contents, if (!contents) {
range: i, allBeltsMatch = false;
}; break;
} }
}
const contentsStaticComp = contents.components.StaticMapEntity;
if (!matchingEntrance) { const contentsBeltComp = contents.components.Belt;
// Nothing found if (!contentsBeltComp) {
return; allBeltsMatch = false;
} break;
}
// DETECT OBSOLETE BELTS BETWEEN
// Remove any belts between entrance and exit which have the same direction, // It's a belt
// but only if they *all* have the right direction if (
currentPos = tile.copy(); contentsBeltComp.direction !== enumDirection.top ||
let allBeltsMatch = true; enumAngleToDirection[contentsStaticComp.rotation] !== direction
for (let i = 0; i < matchingEntrance.range; ++i) { ) {
currentPos.addInplace(offset); allBeltsMatch = false;
break;
const contents = this.root.map.getTileContent(currentPos, entity.layer); }
if (!contents) { }
allBeltsMatch = false;
break; currentPos = tile.copy();
} if (allBeltsMatch) {
// All belts between this are obsolete, so drop them
const contentsStaticComp = contents.components.StaticMapEntity; for (let i = 0; i < matchingEntrance.range; ++i) {
const contentsBeltComp = contents.components.Belt; currentPos.addInplace(offset);
if (!contentsBeltComp) { const contents = this.root.map.getTileContent(currentPos, entity.layer);
allBeltsMatch = false; assert(contents, "Invalid smart underground belt logic");
break; this.root.logic.tryDeleteBuilding(contents);
} }
}
// It's a belt
if ( // REMOVE OBSOLETE TUNNELS
contentsBeltComp.direction !== enumDirection.top || // Remove any double tunnels, by checking the tile plus the tile above
enumAngleToDirection[contentsStaticComp.rotation] !== direction currentPos = tile.copy().add(offset);
) { for (let i = 0; i < matchingEntrance.range - 1; ++i) {
allBeltsMatch = false; const posBefore = currentPos.copy();
break; currentPos.addInplace(offset);
}
} const entityBefore = this.root.map.getTileContent(posBefore, entity.layer);
const entityAfter = this.root.map.getTileContent(currentPos, entity.layer);
currentPos = tile.copy();
if (allBeltsMatch) { if (!entityBefore || !entityAfter) {
// All belts between this are obsolete, so drop them continue;
for (let i = 0; i < matchingEntrance.range; ++i) { }
currentPos.addInplace(offset);
const contents = this.root.map.getTileContent(currentPos, entity.layer); const undergroundBefore = entityBefore.components.UndergroundBelt;
assert(contents, "Invalid smart underground belt logic"); const undergroundAfter = entityAfter.components.UndergroundBelt;
this.root.logic.tryDeleteBuilding(contents);
} if (!undergroundBefore || !undergroundAfter) {
} // Not an underground belt
continue;
// REMOVE OBSOLETE TUNNELS }
// Remove any double tunnels, by checking the tile plus the tile above
currentPos = tile.copy().add(offset); if (
for (let i = 0; i < matchingEntrance.range - 1; ++i) { // Both same tier
const posBefore = currentPos.copy(); undergroundBefore.tier !== undergroundAfter.tier ||
currentPos.addInplace(offset); // And same tier as our original entity
undergroundBefore.tier !== undergroundComp.tier
const entityBefore = this.root.map.getTileContent(posBefore, entity.layer); ) {
const entityAfter = this.root.map.getTileContent(currentPos, entity.layer); // Mismatching tier
continue;
if (!entityBefore || !entityAfter) { }
continue;
} if (
undergroundBefore.mode !== enumUndergroundBeltMode.sender ||
const undergroundBefore = entityBefore.components.UndergroundBelt; undergroundAfter.mode !== enumUndergroundBeltMode.receiver
const undergroundAfter = entityAfter.components.UndergroundBelt; ) {
// Not the right mode
if (!undergroundBefore || !undergroundAfter) { continue;
// Not an underground belt }
continue;
} // Check rotations
const staticBefore = entityBefore.components.StaticMapEntity;
if ( const staticAfter = entityAfter.components.StaticMapEntity;
// Both same tier
undergroundBefore.tier !== undergroundAfter.tier || if (
// And same tier as our original entity enumAngleToDirection[staticBefore.rotation] !== direction ||
undergroundBefore.tier !== undergroundComp.tier enumAngleToDirection[staticAfter.rotation] !== direction
) { ) {
// Mismatching tier // Wrong rotation
continue; continue;
} }
if ( // All good, can remove
undergroundBefore.mode !== enumUndergroundBeltMode.sender || this.root.logic.tryDeleteBuilding(entityBefore);
undergroundAfter.mode !== enumUndergroundBeltMode.receiver this.root.logic.tryDeleteBuilding(entityAfter);
) { }
// Not the right mode }
continue; }
}
/**
// Check rotations * Recomputes the cache in the given area, invalidating all entries there
const staticBefore = entityBefore.components.StaticMapEntity; * @param {Rectangle} area
const staticAfter = entityAfter.components.StaticMapEntity; */
recomputeArea(area) {
if ( for (let x = area.x; x < area.right(); ++x) {
enumAngleToDirection[staticBefore.rotation] !== direction || for (let y = area.y; y < area.bottom(); ++y) {
enumAngleToDirection[staticAfter.rotation] !== direction const entities = this.root.map.getLayersContentsMultipleXY(x, y);
) { for (let i = 0; i < entities.length; ++i) {
// Wrong rotation const entity = entities[i];
continue; const undergroundComp = entity.components.UndergroundBelt;
} if (!undergroundComp) {
continue;
// All good, can remove }
this.root.logic.tryDeleteBuilding(entityBefore); undergroundComp.cachedLinkedEntity = null;
this.root.logic.tryDeleteBuilding(entityAfter); }
} }
} }
} }
/** update() {
* Recomputes the cache in the given area, invalidating all entries there this.staleAreaWatcher.update();
*/
recomputeArea() { const delta = this.root.dynamicTickrate.deltaSeconds;
const area = this.areaToRecompute;
logger.log("Recomputing area:", area.x, area.y, "/", area.w, area.h); for (let i = 0; i < this.allEntities.length; ++i) {
if (G_IS_DEV && globalConfig.debug.renderChanges) { const entity = this.allEntities[i];
this.root.hud.parts.changesDebugger.renderChange("tunnels", this.areaToRecompute, "#fc03be"); const undergroundComp = entity.components.UndergroundBelt;
} const pendingItems = undergroundComp.pendingItems;
for (let x = area.x; x < area.right(); ++x) { // Decrease remaining time of all items in belt
for (let y = area.y; y < area.bottom(); ++y) { for (let k = 0; k < pendingItems.length; ++k) {
const entities = this.root.map.getLayersContentsMultipleXY(x, y); const item = pendingItems[k];
for (let i = 0; i < entities.length; ++i) { item[1] = Math.max(0, item[1] - delta);
const entity = entities[i]; if (G_IS_DEV && globalConfig.debug.instantBelts) {
const undergroundComp = entity.components.UndergroundBelt; item[1] = 0;
if (!undergroundComp) { }
continue; }
} if (undergroundComp.mode === enumUndergroundBeltMode.sender) {
this.handleSender(entity);
undergroundComp.cachedLinkedEntity = null; } else {
} this.handleReceiver(entity);
} }
} }
} }
update() { /**
if (this.areaToRecompute) { * Finds the receiver for a given sender
this.recomputeArea(); * @param {Entity} entity
this.areaToRecompute = null; * @returns {import("../components/underground_belt").LinkedUndergroundBelt}
} */
findRecieverForSender(entity) {
const delta = this.root.dynamicTickrate.deltaSeconds; const staticComp = entity.components.StaticMapEntity;
const undergroundComp = entity.components.UndergroundBelt;
for (let i = 0; i < this.allEntities.length; ++i) { const searchDirection = staticComp.localDirectionToWorld(enumDirection.top);
const entity = this.allEntities[i]; const searchVector = enumDirectionToVector[searchDirection];
const undergroundComp = entity.components.UndergroundBelt; const targetRotation = enumDirectionToAngle[searchDirection];
const pendingItems = undergroundComp.pendingItems; let currentTile = staticComp.origin;
// Decrease remaining time of all items in belt // Search in the direction of the tunnel
for (let k = 0; k < pendingItems.length; ++k) { for (
const item = pendingItems[k]; let searchOffset = 0;
item[1] = Math.max(0, item[1] - delta); searchOffset < globalConfig.undergroundBeltMaxTilesByTier[undergroundComp.tier];
if (G_IS_DEV && globalConfig.debug.instantBelts) { ++searchOffset
item[1] = 0; ) {
} currentTile = currentTile.add(searchVector);
}
if (undergroundComp.mode === enumUndergroundBeltMode.sender) { const potentialReceiver = this.root.map.getTileContent(currentTile, "regular");
this.handleSender(entity); if (!potentialReceiver) {
} else { // Empty tile
this.handleReceiver(entity); continue;
} }
} const receiverUndergroundComp = potentialReceiver.components.UndergroundBelt;
} if (!receiverUndergroundComp || receiverUndergroundComp.tier !== undergroundComp.tier) {
// Not a tunnel, or not on the same tier
/** continue;
* Finds the receiver for a given sender }
* @param {Entity} entity
* @returns {import("../components/underground_belt").LinkedUndergroundBelt} const receiverStaticComp = potentialReceiver.components.StaticMapEntity;
*/ if (receiverStaticComp.rotation !== targetRotation) {
findRecieverForSender(entity) { // Wrong rotation
const staticComp = entity.components.StaticMapEntity; continue;
const undergroundComp = entity.components.UndergroundBelt; }
const searchDirection = staticComp.localDirectionToWorld(enumDirection.top);
const searchVector = enumDirectionToVector[searchDirection]; if (receiverUndergroundComp.mode !== enumUndergroundBeltMode.receiver) {
const targetRotation = enumDirectionToAngle[searchDirection]; // Not a receiver, but a sender -> Abort to make sure we don't deliver double
let currentTile = staticComp.origin; break;
}
// Search in the direction of the tunnel
for ( return { entity: potentialReceiver, distance: searchOffset };
let searchOffset = 0; }
searchOffset < globalConfig.undergroundBeltMaxTilesByTier[undergroundComp.tier];
++searchOffset // None found
) { return { entity: null, distance: 0 };
currentTile = currentTile.add(searchVector); }
const potentialReceiver = this.root.map.getTileContent(currentTile, "regular"); /**
if (!potentialReceiver) { *
// Empty tile * @param {Entity} entity
continue; */
} handleSender(entity) {
const receiverUndergroundComp = potentialReceiver.components.UndergroundBelt; const undergroundComp = entity.components.UndergroundBelt;
if (!receiverUndergroundComp || receiverUndergroundComp.tier !== undergroundComp.tier) {
// Not a tunnel, or not on the same tier // Find the current receiver
continue; let cacheEntry = undergroundComp.cachedLinkedEntity;
} if (!cacheEntry) {
// Need to recompute cache
const receiverStaticComp = potentialReceiver.components.StaticMapEntity; cacheEntry = undergroundComp.cachedLinkedEntity = this.findRecieverForSender(entity);
if (receiverStaticComp.rotation !== targetRotation) { }
// Wrong rotation
continue; if (!cacheEntry.entity) {
} // If there is no connection to a receiver, ignore this one
return;
if (receiverUndergroundComp.mode !== enumUndergroundBeltMode.receiver) { }
// Not a receiver, but a sender -> Abort to make sure we don't deliver double
break; // Check if we have any item
} if (undergroundComp.pendingItems.length > 0) {
assert(undergroundComp.pendingItems.length === 1, "more than 1 pending");
return { entity: potentialReceiver, distance: searchOffset }; const nextItemAndDuration = undergroundComp.pendingItems[0];
} const remainingTime = nextItemAndDuration[1];
const nextItem = nextItemAndDuration[0];
// None found
return { entity: null, distance: 0 }; // Check if the item is ready to be emitted
} if (remainingTime === 0) {
// Check if the receiver can accept it
/** if (
* cacheEntry.entity.components.UndergroundBelt.tryAcceptTunneledItem(
* @param {Entity} entity nextItem,
*/ cacheEntry.distance,
handleSender(entity) { this.root.hubGoals.getUndergroundBeltBaseSpeed()
const undergroundComp = entity.components.UndergroundBelt; )
) {
// Find the current receiver // Drop this item
let receiver = undergroundComp.cachedLinkedEntity; fastArrayDelete(undergroundComp.pendingItems, 0);
if (!receiver) { }
// We don't have a receiver, compute it }
receiver = undergroundComp.cachedLinkedEntity = this.findRecieverForSender(entity); }
}
if (G_IS_DEV && globalConfig.debug.renderChanges) {
this.root.hud.parts.changesDebugger.renderChange( /**
"sender", *
entity.components.StaticMapEntity.getTileSpaceBounds(), * @param {Entity} entity
"#fc03be" */
); handleReceiver(entity) {
} const undergroundComp = entity.components.UndergroundBelt;
}
// Try to eject items, we only check the first one because it is sorted by remaining time
if (!receiver.entity) { const items = undergroundComp.pendingItems;
// If there is no connection to a receiver, ignore this one if (items.length > 0) {
return; const nextItemAndDuration = undergroundComp.pendingItems[0];
} const remainingTime = nextItemAndDuration[1];
const nextItem = nextItemAndDuration[0];
// Check if we have any item
if (undergroundComp.pendingItems.length > 0) { if (remainingTime <= 0) {
assert(undergroundComp.pendingItems.length === 1, "more than 1 pending"); const ejectorComp = entity.components.ItemEjector;
const nextItemAndDuration = undergroundComp.pendingItems[0];
const remainingTime = nextItemAndDuration[1]; const nextSlotIndex = ejectorComp.getFirstFreeSlot();
const nextItem = nextItemAndDuration[0]; if (nextSlotIndex !== null) {
if (ejectorComp.tryEject(nextSlotIndex, nextItem)) {
// Check if the item is ready to be emitted items.shift();
if (remainingTime === 0) { }
// Check if the receiver can accept it }
if ( }
receiver.entity.components.UndergroundBelt.tryAcceptTunneledItem( }
nextItem, }
receiver.distance, }
this.root.hubGoals.getUndergroundBeltBaseSpeed()
)
) {
// Drop this item
fastArrayDelete(undergroundComp.pendingItems, 0);
}
}
}
}
/**
*
* @param {Entity} entity
*/
handleReceiver(entity) {
const undergroundComp = entity.components.UndergroundBelt;
// Try to eject items, we only check the first one because it is sorted by remaining time
const items = undergroundComp.pendingItems;
if (items.length > 0) {
const nextItemAndDuration = undergroundComp.pendingItems[0];
const remainingTime = nextItemAndDuration[1];
const nextItem = nextItemAndDuration[0];
if (remainingTime <= 0) {
const ejectorComp = entity.components.ItemEjector;
const nextSlotIndex = ejectorComp.getFirstFreeSlot();
if (nextSlotIndex !== null) {
if (ejectorComp.tryEject(nextSlotIndex, nextItem)) {
items.shift();
}
}
}
}
}
}