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); }
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); } } } }