From f69687a6477fb083d8287bfcd1a4d5b85d8853b4 Mon Sep 17 00:00:00 2001 From: TcePrepK <56453014+TcePrepK@users.noreply.github.com> Date: Sat, 7 Nov 2020 23:48:05 +0300 Subject: [PATCH] Update wire.js --- src/js/game/systems/wire.js | 46 ++++++++++++------------------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/src/js/game/systems/wire.js b/src/js/game/systems/wire.js index 73fcf3ac..4d0e6de4 100644 --- a/src/js/game/systems/wire.js +++ b/src/js/game/systems/wire.js @@ -316,11 +316,6 @@ export class WireSystem extends GameSystemWithFilter { } } - const tunnelComp = nextEntity.components.WireTunnel; - if (tunnelComp) { - //const outputDir = tunnelComp.GetOutputDirection(staticComp, offset); - } - if (newSearchTile) { // Find new surrounding wire targets const newTargets = this.findSurroundingWireTargets( @@ -369,7 +364,7 @@ export class WireSystem extends GameSystemWithFilter { * @param {Vector} initialTile * @param {Array} directions * @param {WireNetwork} network - * @param {enumWireVariant} variantMask Only accept connections to this mask + * @param {enumWireVariant=} variantMask Only accept connections to this mask * @returns {Array} */ findSurroundingWireTargets(initialTile, directions, network, variantMask = null) { @@ -391,6 +386,9 @@ export class WireSystem extends GameSystemWithFilter { const offset = enumDirectionToVector[direction]; const initialSearchTile = initialTile.add(offset); + // Store which tunnels we already visited to avoid infinite loops + const visitedTunnels = new Set(); + // First, find the initial connected entities const initialContents = this.root.map.getLayersContentsMultipleXY( initialSearchTile.x, @@ -398,18 +396,17 @@ export class WireSystem extends GameSystemWithFilter { ); // Link the initial tile to the initial entities, since it may change - /** @type {Array<{entity: Entity, tile: Vector, dir: Vector}>} */ + /** @type {Array<{entity: Entity, tile: Vector}>} */ const contents = []; for (let j = 0; j < initialContents.length; ++j) { contents.push({ entity: initialContents[j], tile: initialSearchTile, - dir: offset, }); } for (let k = 0; k < contents.length; ++k) { - const { entity, tile, dir } = contents[k]; + const { entity, tile } = contents[k]; const wireComp = entity.components.Wire; // Check for wire @@ -441,17 +438,8 @@ export class WireSystem extends GameSystemWithFilter { } // Check if the direction (inverted) matches - // const pinDirection = staticComp.localDirectionToWorld(slot.direction); - // if (pinDirection !== enumInvertedDirections[direction]) { - // continue; - // } - // /** - // * @type {Vector} - // */ - const worldDir = staticComp.localDirectionToWorld(slot.direction); - const invDir = enumInvertedDirections[worldDir]; - const pinDirection = enumDirectionToVector[invDir]; - if (!pinDirection.equals(dir)) { + const pinDirection = staticComp.localDirectionToWorld(slot.direction); + if (pinDirection !== enumInvertedDirections[direction]) { continue; } @@ -470,20 +458,14 @@ export class WireSystem extends GameSystemWithFilter { // Check if it's a tunnel, if so, go to the forwarded item const tunnelComp = entity.components.WireTunnel; if (tunnelComp) { + if (visitedTunnels.has(entity.uid)) { + continue; + } const staticComp = entity.components.StaticMapEntity; - //const localDir = staticComp.worldToLocalTile(tile.sub(offset)); - //staticComp.localDirectionToWorld(); - const outputDir = tunnelComp.GetOutputDirection(staticComp, dir); - if (!outputDir) { - continue; - } - const forwardedTile = staticComp.origin.add(outputDir); - - //TODO: Alter to Allow for different tunnel Types // Compute where this tunnel connects to - //const forwardedTile = staticComp.origin.add(offset); + const forwardedTile = staticComp.origin.add(offset); VERBOSE_WIRES && logger.log( " Found tunnel", @@ -505,7 +487,6 @@ export class WireSystem extends GameSystemWithFilter { contents.push({ entity: connectedContents[h], tile: forwardedTile, - dir: outputDir, }); } @@ -516,6 +497,9 @@ export class WireSystem extends GameSystemWithFilter { if (network.tunnels.indexOf(entity) < 0) { network.tunnels.push(entity); } + + // Remember this tunnel + visitedTunnels.add(entity.uid); } } }