From fbd8e2e7e71476c4ca74a4f65259fe8c85d45c0c Mon Sep 17 00:00:00 2001 From: Christopher-Robin Date: Sun, 18 Oct 2020 21:37:53 +1100 Subject: [PATCH] Fixed bug with Transistors and Formatted Code --- src/js/game/buildings/wire_tunnel.js | 105 +++++++++++++------ src/js/game/components/wire_tunnel.js | 144 ++++++++++++-------------- src/js/game/logic.js | 9 +- src/js/game/systems/wire.js | 57 +++++----- 4 files changed, 177 insertions(+), 138 deletions(-) diff --git a/src/js/game/buildings/wire_tunnel.js b/src/js/game/buildings/wire_tunnel.js index 5e93d602..8585a65f 100644 --- a/src/js/game/buildings/wire_tunnel.js +++ b/src/js/game/buildings/wire_tunnel.js @@ -1,5 +1,11 @@ import { generateMatrixRotations } from "../../core/utils"; -import { Vector } from "../../core/vector"; +import { + arrayAllDirections, + enumDirection, + enumDirectionToVector, + enumInvertedDirections, + Vector, +} from "../../core/vector"; import { WireTunnelComponent } from "../components/wire_tunnel"; import { Entity } from "../entity"; import { MetaBuilding, defaultBuildingVariant } from "../meta_building"; @@ -21,13 +27,20 @@ const wireTunnelsOverlayMatrix = { }; /** - * @enum {Array>} + * Enum of Objects containing the Tunnel Variant Connections + * @enum {Object.} */ export const ConnectionDirections = { - [defaultBuildingVariant]: [[new Vector(0, 1), new Vector(0, 1)], [new Vector(1, 0), new Vector(1, 0)]], - [enumWireTunnelVariants.DoubleElbow]: [[new Vector(0, 1), new Vector(1, 0)], [new Vector(0, -1), new Vector(-1, 0)]], - [enumWireTunnelVariants.Elbow]: [[new Vector(0, 1), new Vector(1, 0)]], - [enumWireTunnelVariants.Straight]: [[new Vector(0, 1), new Vector(0, 1)]], + [defaultBuildingVariant]: BuildConnections([ + [new Vector(0, 1), new Vector(0, 1)], + [new Vector(1, 0), new Vector(1, 0)], + ]), + [enumWireTunnelVariants.DoubleElbow]: BuildConnections([ + [new Vector(0, 1), new Vector(1, 0)], + [new Vector(0, -1), new Vector(-1, 0)], + ]), + [enumWireTunnelVariants.Elbow]: BuildConnections([[new Vector(0, 1), new Vector(1, 0)]]), + [enumWireTunnelVariants.Straight]: BuildConnections([[new Vector(0, 1), new Vector(0, 1)]]), }; export class MetaWireTunnelBuilding extends MetaBuilding { @@ -44,22 +57,21 @@ export class MetaWireTunnelBuilding extends MetaBuilding { */ getIsUnlocked(root) { return root.hubGoals.isRewardUnlocked(enumHubGoalRewards.reward_wires_painter_and_levers); - } - - /** - * - * @param {GameRoot} root - */ - getAvailableVariants(root) { - return [defaultBuildingVariant, enumWireTunnelVariants.Elbow, enumWireTunnelVariants.Straight, enumWireTunnelVariants.DoubleElbow]; - // if (root.hubGoals.isRewardUnlocked(enumHubGoalRewards.reward_miner_chainable)) { - // return [enumMinerVariants.chainable]; - // } - // return super.getAvailableVariants(root); } /** - * + * @param {GameRoot} root + */ + getAvailableVariants(root) { + return [ + defaultBuildingVariant, + enumWireTunnelVariants.Elbow, + enumWireTunnelVariants.Straight, + enumWireTunnelVariants.DoubleElbow, + ]; + } + + /** * @param {number} rotation * @param {number} rotationVariant * @param {string} variant @@ -73,10 +85,10 @@ export class MetaWireTunnelBuilding extends MetaBuilding { return true; } - getStayInPlacementMode() { + getStayInPlacementMode() { return true; - } - + } + getDimensions() { return new Vector(1, 1); } @@ -91,19 +103,50 @@ export class MetaWireTunnelBuilding extends MetaBuilding { * @param {Entity} entity */ setupEntityComponents(entity) { - entity.addComponent(new WireTunnelComponent({Variant: defaultBuildingVariant, Connections: ConnectionDirections[defaultBuildingVariant]})); - } - - /** + entity.addComponent( + new WireTunnelComponent({ + Connections: ConnectionDirections[defaultBuildingVariant], + }) + ); + } + + /** * * @param {Entity} entity * @param {number} rotationVariant * @param {string} variant */ updateVariants(entity, rotationVariant, variant) { - if(entity.components.WireTunnel){ - //a.rotateInplaceFastMultipleOf90(rotationVariant); - entity.components.WireTunnel.UpdateConnections(variant, ConnectionDirections[variant]) - } - } + if (entity.components.WireTunnel) { + entity.components.WireTunnel.UpdateConnections(ConnectionDirections[variant]); + } + } +} + +/** + * Builds the Connection Graph object from the input Array + * @param {Array>} Connections + * @returns {Object.} + */ +function BuildConnections(Connections) { + /** + * @type {Object.} + */ + let res = {}; + for (let i = 0; i < Connections.length; ++i) { + assert(Connections[i].length == 2, "Connection Wasn't Continuos"); + let [a, b] = Connections[i]; + + const ahash = a.toString(); + if (!res[ahash]) { + res[ahash] = b; + } + let alta = a.rotateFastMultipleOf90(180); + let altb = b.rotateFastMultipleOf90(180); + const bhash = altb.toString(); + if (!res[bhash]) { + res[bhash] = alta; + } + } + return res; } diff --git a/src/js/game/components/wire_tunnel.js b/src/js/game/components/wire_tunnel.js index 4b356573..447a5478 100644 --- a/src/js/game/components/wire_tunnel.js +++ b/src/js/game/components/wire_tunnel.js @@ -1,89 +1,77 @@ -import { Vector } from "../../core/vector"; +import { + arrayAllDirections, + enumDirection, + enumDirectionToVector, + enumInvertedDirections, + Vector, +} from "../../core/vector"; import { Component } from "../component"; import { defaultBuildingVariant } from "../meta_building"; export class WireTunnelComponent extends Component { - static getId() { - return "WireTunnel"; + static getId() { + return "WireTunnel"; + } - } + /** + * + * @param {{Connections: Object.}} Elements + */ + constructor({ Connections = {} }) { + super(); + /** + * @type {Object.} + */ + this.Connections = Connections; - constructor({ Variant, Connections = [] }) { - super(); + /** + * Linked network, only if its not multiple directions + * @type {Array} + */ + this.linkedNetworks = []; + } - this.Variant = Variant; - // /** - // * All Connection Directions - // * @type {Object.>} Possibility for a T piece. Should be Irrelevant - // */ - /** - * @type {Object.} - */ - this.Connections = {}; + /** + * @param {Object.} Connections + */ + UpdateConnections(Connections) { + this.Connections = Connections; + } - this.RebuildConnections(Connections); + /** + * Returns if the Tunnel accepts inputs from the given direction + * @param {Vector} dir + * Local Space Vector into the Tunnel + */ + CanConnect(dir) { + return !!this.Connections[dir.toString()]; + } - /** - * Linked network, only if its not multiple directions - * @type {Array} - */ - this.linkedNetworks = []; - } + /** + * Returns if the Tunnel accepts inputs from the given direction + * @param {import("./static_map_entity").StaticMapEntityComponent} staticComp + * Static Map Entity Component + * @param {Vector} dir + * World space Vector into the Tunnel + */ + CanConnectWorld(staticComp, dir) { + const inputDir = staticComp.unapplyRotationToVector(dir); + return !!this.Connections[inputDir.toString()]; + } - /** - * @param {import("../buildings/wire_tunnel").ConnectionDirections} Connections - */ - RebuildConnections(Connections) { - - this.Connections = {}; - for(let i = 0; i < Connections.length; ++i) { - assert(Connections[i].length == 2, "Connection Wasn't Continuos"); - let [a, b] = Connections[i]; - - const ahash = a.toString(); - if(!this.Connections[ahash]) { - this.Connections[ahash] = b; - } - let alta = a.rotateFastMultipleOf90(180); - let altb = b.rotateFastMultipleOf90(180); - const bhash = altb.toString(); - if(!this.Connections[bhash]) { - this.Connections[bhash] = alta; - } - } - console.log(this.Connections); - } - - /** - * @param {string} Variant - * @param {import("../buildings/wire_tunnel").ConnectionDirections} Connections - */ - UpdateConnections(Variant, Connections) { - if(this.Variant !== Variant){ - this.Variant = Variant; - this.RebuildConnections(Connections) - } - } - - /** - * Local Space Direction the connection is coming from - * @param {Vector} dir - */ - CanConnect(dir) { - return !!this.Connections[dir.toString()]; - } - - /** - * @param {import("./static_map_entity").StaticMapEntityComponent} staticComp - * @param {Vector} input - * LocalSpace Direction into the Tunnel - */ - GetOutputDirection(staticComp, input) { - const inputDir = staticComp.unapplyRotationToVector(input); //TODO: Fix the Wierd Shit - if(this.CanConnect(inputDir)){ - let out = this.Connections[inputDir.toString()]; - return staticComp.applyRotationToVector(out); - } - return null; - } + /** + * Returns the Worldspace Vector out from the Tunnel or Null + * @param {import("./static_map_entity").StaticMapEntityComponent} staticComp + * Static Map Entity Component + * @param {Vector|null} input + * Worldspace Direction into the Tunnel + */ + GetOutputDirection(staticComp, input) { + const inputDir = staticComp.unapplyRotationToVector(input); + if (this.CanConnect(inputDir)) { + let out = this.Connections[inputDir.toString()]; + return staticComp.applyRotationToVector(out); + } + return null; + } } diff --git a/src/js/game/logic.js b/src/js/game/logic.js index e624447b..9951ca36 100644 --- a/src/js/game/logic.js +++ b/src/js/game/logic.js @@ -193,9 +193,9 @@ export class GameLogic { * @param {enumDirection} param0.edge The edge to check for */ computeWireEdgeStatus({ wireVariant, tile, edge }) { - /** - * @type {Vector} - */ + /** + * @type {Vector} + */ const offset = enumDirectionToVector[edge]; const targetTile = tile.add(offset); @@ -246,8 +246,7 @@ export class GameLogic { // Check if its a tunnel const wireTunnelComp = targetEntity.components.WireTunnel; if (wireTunnelComp) { - const inputDir = targetStaticComp.unapplyRotationToVector(offset); - return wireTunnelComp.CanConnect(inputDir); + return wireTunnelComp.CanConnectWorld(targetStaticComp, offset); } // Check if its a wire diff --git a/src/js/game/systems/wire.js b/src/js/game/systems/wire.js index 656f7c79..b38a467f 100644 --- a/src/js/game/systems/wire.js +++ b/src/js/game/systems/wire.js @@ -314,12 +314,12 @@ export class WireSystem extends GameSystemWithFilter { newSearchDirections = [staticComp.localDirectionToWorld(slot.direction)]; newSearchTile = staticComp.localTileToWorld(slot.pos); } - } - - const tunnelComp = nextEntity.components.WireTunnel; - if(tunnelComp){ - //const outputDir = tunnelComp.GetOutputDirection(staticComp, offset); - } + } + + const tunnelComp = nextEntity.components.WireTunnel; + if (tunnelComp) { + //const outputDir = tunnelComp.GetOutputDirection(staticComp, offset); + } if (newSearchTile) { // Find new surrounding wire targets @@ -369,7 +369,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) { @@ -406,8 +406,8 @@ export class WireSystem extends GameSystemWithFilter { for (let j = 0; j < initialContents.length; ++j) { contents.push({ entity: initialContents[j], - tile: initialSearchTile, - dir: offset + tile: initialSearchTile, + dir: offset, }); } @@ -444,8 +444,17 @@ export class WireSystem extends GameSystemWithFilter { } // Check if the direction (inverted) matches - const pinDirection = staticComp.localDirectionToWorld(slot.direction); - if (pinDirection !== enumInvertedDirections[direction]) { + // 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)) { continue; } @@ -469,15 +478,15 @@ export class WireSystem extends GameSystemWithFilter { 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); + 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 @@ -497,15 +506,15 @@ export class WireSystem extends GameSystemWithFilter { forwardedTile.x, forwardedTile.y ); - + // Attach the entities and the tile we search at, because it may change for (let h = 0; h < connectedContents.length; ++h) { contents.push({ entity: connectedContents[h], - tile: forwardedTile, - dir: outputDir + tile: forwardedTile, + dir: outputDir, }); - } + } // Add the tunnel to the network if (tunnelComp.linkedNetworks.indexOf(network) < 0) {