Exemplo n.º 1
0
    private void ReplaceWithWire(ElementBase element)
    {
        var firstPos  = new Vector3Int(ConnectionsMaker.GetConnectPosition(true, element).x, ConnectionsMaker.GetConnectPosition(true, element).y, Scheme.GetWiresCount() + 2);
        var secondPos = new Vector3Int(ConnectionsMaker.GetConnectPosition(false, element).x, ConnectionsMaker.GetConnectPosition(false, element).y, Scheme.GetWiresCount() + 2);
        var wire      = new Wire(firstPos, secondPos, element.angle);

        Scheme.AddElement(wire);
        var texture = Resources.Load <Texture2D>("Sprites/HalfWireSprite");

        var tile = new Tile
        {
            sprite = Sprite.Create(texture,
                                   new Rect(0, 0, texture.width, texture.height),
                                   new Vector2(0.0f, 0.5f),
                                   100,
                                   1,
                                   SpriteMeshType.Tight,
                                   Vector4.zero
                                   )
        };

        var scale = wire.pivotPosition.x == wire.secondPosition.x
            ? (Math.Abs(wire.pivotPosition.y - wire.secondPosition.y) + 0.01f) / 2
            : (Math.Abs(wire.pivotPosition.x - wire.secondPosition.x) + 0.01f) / 2;
        var rotation = Quaternion.Euler(0, 0, element.angle);

        var m = tile.transform;

        m.SetTRS(Vector3.zero, rotation, new Vector3(scale, 1, 1));
        tile.transform = m;

        tile.name = "Wire";

        map.GetComponent <Tilemap>().SetTile(wire.pivotPosition, tile);
    }
Exemplo n.º 2
0
        private List <Wire> GetConnectedWires(ElementBase conductor)
        {
            var markedElements = new List <ElementBase>();

            var result = new List <Wire>();

            var elementsInIteration = new List <ElementBase> {
                conductor
            };

            while (elementsInIteration.Count > 0)
            {
                var elementsInNextIteration = new List <Wire>();

                foreach (var element in elementsInIteration)
                {
                    markedElements.Add(element);

                    Vector2Int leftPos, rightPos;

                    if (element is Wire temp)
                    {
                        leftPos  = (Vector2Int)temp.pivotPosition;
                        rightPos = (Vector2Int)temp.secondPosition;
                    }
                    else
                    {
                        leftPos  = ConnectionsMaker.GetConnectPosition(true, element);
                        rightPos = ConnectionsMaker.GetConnectPosition(false, element);
                    }

                    var tempList = new List <Wire>();

                    if (_connectionGraph[leftPos].Values.Count <= 2)
                    {
                        foreach (var connectedElement in _connectionGraph[leftPos].Values)
                        {
                            if (!(connectedElement is Wire) && connectedElement != conductor)
                            {
                                tempList.Clear();
                                break;
                            }
                            if (!markedElements.Contains(connectedElement))
                            {
                                tempList.Add((Wire)connectedElement);
                            }
                        }
                    }

                    elementsInNextIteration.AddRange(tempList);
                    tempList.Clear();

                    if (_connectionGraph[rightPos].Values.Count <= 2)
                    {
                        foreach (var connectedElement in _connectionGraph[rightPos].Values)
                        {
                            if (!(connectedElement is Wire) && connectedElement != conductor)
                            {
                                tempList.Clear();
                                break;
                            }
                            if (!markedElements.Contains(connectedElement))
                            {
                                tempList.Add((Wire)connectedElement);
                            }
                        }
                    }

                    elementsInNextIteration.AddRange(tempList);
                }

                elementsInIteration = new List <ElementBase>(elementsInNextIteration);
                result.AddRange(elementsInNextIteration);
            }

            return(result);
        }