コード例 #1
0
        private static void CheckWires()
        {
            foreach (var pair in wiresToCheck
                     .Select(GetConnectedWiresWith)
                     .SelectMany(connectedWires => connectedWires))
            {
                foreach (var wire in pair.Value.Where(wire => !_marked.ContainsKey(wire.pivotPosition) ||
                                                      !_marked[wire.pivotPosition].Contains(wire.secondPosition)))
                {
                    if (!_marked.ContainsKey(wire.pivotPosition))
                    {
                        _marked.Add(wire.pivotPosition, new List <Vector3Int> {
                            wire.secondPosition
                        });
                    }
                    else
                    {
                        _marked[wire.pivotPosition].Add(wire.secondPosition);
                    }
                    if (!_marked.ContainsKey(wire.secondPosition))
                    {
                        _marked.Add(wire.secondPosition, new List <Vector3Int> {
                            wire.pivotPosition
                        });
                    }
                    else
                    {
                        _marked[wire.secondPosition].Add(wire.pivotPosition);
                    }

                    g[(Vector2Int)wire.pivotPosition].
                    Remove((Vector2Int)wire.secondPosition);
                    g[(Vector2Int)wire.secondPosition].
                    Remove((Vector2Int)wire.pivotPosition);

                    var z             = Scheme.GetWiresCount() + 2;
                    var wireFirstPos  = new Vector3Int(wire.pivotPosition.x, wire.pivotPosition.y, z);
                    var wireSecondPos = new Vector3Int(pair.Key.x, pair.Key.y, z);
                    var wire1         = new Wire(wireFirstPos, wireSecondPos, wire.angle);
                    PlaceWire(wireFirstPos, wireSecondPos);
                    Scheme.AddElement(wire1);

                    z             = Scheme.GetWiresCount() + 2;
                    wireFirstPos  = new Vector3Int(pair.Key.x, pair.Key.y, z);
                    wireSecondPos = new Vector3Int(wire.secondPosition.x, wire.secondPosition.y, z);
                    var wire2 = new Wire(wireFirstPos, wireSecondPos, wire.angle);
                    PlaceWire(wireFirstPos, wireSecondPos);
                    Scheme.AddElement(wire2);

                    RemoveWire(wire);

                    if (!g[(Vector2Int)wire.pivotPosition].ContainsKey((Vector2Int)pair.Key))
                    {
                        g[(Vector2Int)wire.pivotPosition].Add((Vector2Int)pair.Key, wire1);
                    }
                    if (!g[(Vector2Int)wire.secondPosition].ContainsKey((Vector2Int)pair.Key))
                    {
                        g[(Vector2Int)wire.secondPosition].Add((Vector2Int)pair.Key, wire2);
                    }

                    if (!g[(Vector2Int)pair.Key].ContainsKey((Vector2Int)wire.pivotPosition))
                    {
                        g[(Vector2Int)pair.Key].Add((Vector2Int)wire.pivotPosition, wire1);
                    }
                    if (!g[(Vector2Int)pair.Key].ContainsKey((Vector2Int)wire.secondPosition))
                    {
                        g[(Vector2Int)pair.Key].Add((Vector2Int)wire.secondPosition, wire2);
                    }
                }
            }
        }
コード例 #2
0
        private void PlaceElement(ElementBase element, string texturePath)
        {
            Scheme.AddElement(element);
            Texture2D texture = Resources.Load <Texture2D>(texturePath);

            Tile tile;

            if (element is Wire)
            {
                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
                                           )
                }
            }
            ;
            else
            {
                tile = new Tile
                {
                    sprite = Sprite.Create(texture,
                                           new Rect(0, 0, texture.width, texture.height),
                                           new Vector2(0.5f, 0.5f),
                                           100,
                                           1,
                                           SpriteMeshType.Tight,
                                           Vector4.zero
                                           )
                }
            };

            if (element is LabeledChainElement)
            {
                //GameObject.Find("Labels").Add //(((LabeledChainElement)element).label);
                Debug.Log("JsonReader: LabeledChainElement encountered");

                //((LabeledChainElement)element).label.transform.parent = GameObject.Find("Labels").transform;
                ((LabeledChainElement)element).AddLabel(((LabeledChainElement)element).labelStr, element.pivotPosition);
                if (element is Resistor)
                {
                    ((Resistor)element).FixLabel();
                }
                if (element is Conductor)
                {
                    ((Conductor)element).FixLabel();
                }
            }

            float      scale    = 1;
            Quaternion rotation = Quaternion.Euler(0, 0, element.angle);

            if (element is Wire)
            {
                Wire temp = (Wire)element;

                scale = temp.pivotPosition.x == temp.secondPosition.x
                    ? (Math.Abs(temp.pivotPosition.y - temp.secondPosition.y) + 0.01f) / 2
                    : (Math.Abs(temp.pivotPosition.x - temp.secondPosition.x) + 0.01f) / 2;
            }

            var m = tile.transform;

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

            switch (texturePath)
            {
            case "Sprites/ResistorSprite":
                tile.name = "Resistor";
                break;

            case "Sprites/ConductorSprite":
                tile.name = "Conductor";
                break;

            case "Sprites/HalfWireSprite":
                tile.name = "Wire";
                break;

            case "Sprites/NullatorSprite":
                tile.name = "Nullator";
                break;

            case "Sprites/NoratorSprite":
                tile.name = "Norator";
                break;
            }

            map.SetTile(element.pivotPosition, tile);
        }
コード例 #3
0
        private static Dictionary <Vector3Int, List <Wire> > GetConnectedWiresWith(Wire wire)
        {
            var pos1 = wire.pivotPosition;
            var pos2 = wire.secondPosition;

            var result = new Dictionary <Vector3Int, List <Wire> >();

            foreach (var wireToCheck in g.Values.SelectMany(
                         graphPair => graphPair.Values.Where(
                             element => element is Wire)).Cast <Wire>())
            {
                if (InCenter(pos1, wireToCheck))
                {
                    if (result.ContainsKey(pos1) && !result[pos1].Contains(wireToCheck))
                    {
                        result[pos1].Add(wireToCheck);
                    }
                    else if (!result.ContainsKey(pos1))
                    {
                        result.Add(pos1, new List <Wire>());
                        result[pos1].Add(wireToCheck);
                    }
                }
                if (InCenter(pos2, wireToCheck))
                {
                    if (result.ContainsKey(pos2) && !result[pos2].Contains(wireToCheck))
                    {
                        result[pos2].Add(wireToCheck);
                    }
                    else if (!result.ContainsKey(pos2))
                    {
                        result.Add(pos2, new List <Wire>());
                        result[pos2].Add(wireToCheck);
                    }
                }

                if (InCenter(wireToCheck.pivotPosition, wire))
                {
                    if (result.ContainsKey(wireToCheck.pivotPosition) && !result[wireToCheck.pivotPosition].Contains(wire))
                    {
                        result[wireToCheck.pivotPosition].Add(wire);
                    }
                    else if (!result.ContainsKey(wireToCheck.pivotPosition))
                    {
                        result.Add(wireToCheck.pivotPosition, new List <Wire>());
                        result[wireToCheck.pivotPosition].Add(wire);
                    }
                }
                if (InCenter(wireToCheck.secondPosition, wire))
                {
                    if (result.ContainsKey(wireToCheck.secondPosition) && !result[wireToCheck.secondPosition].Contains(wire))
                    {
                        result[wireToCheck.secondPosition].Add(wire);
                    }
                    else if (!result.ContainsKey(wireToCheck.secondPosition))
                    {
                        result.Add(wireToCheck.secondPosition, new List <Wire>());
                        result[wireToCheck.secondPosition].Add(wire);
                    }
                }
            }

            return(result);
        }