Пример #1
0
        public void PlaceElement(ref string line, string type)
        {
            int id = Int32.Parse(GetSubString(ref line, line.IndexOf(' ')));

            int row    = Int32.Parse(GetSubString(ref line, line.IndexOf(' ')));
            int column = Int32.Parse(GetSubString(ref line, line.IndexOf(' ')));

            string direction = GetSubString(ref line, 1);

            int node1Id = Int32.Parse(GetSubString(ref line, line.IndexOf(' ')));
            int node2Id = Int32.Parse(GetSubString(ref line, line.Length));

            Node node1 = gameController.scheme.GetNode(node1Id);
            Node node2 = gameController.scheme.GetNode(node2Id);

            BaseElement element;

            switch (type)
            {
            case "Re":
                element = new Resistor(node1, node2, id);
                break;

            case "No":
                element = new Norator(node1, node2, id);
                break;

            case "Nu":
                element = new Nullator(node1, node2, id);
                break;

            default:
                element = new Conductor(node1, node2, id);
                break;
            }

            gameController.scheme.AddElement(element);

            element.GetImage().SetValue(Grid.RowProperty, row);
            element.GetImage().SetValue(Grid.ColumnProperty, column);
            element.GetImage().SetValue(Panel.ZIndexProperty, 1);

            if (direction == "R")
            {
                element.ChangeImageDirectionToLand();

                element.GetImage().SetValue(Grid.RowSpanProperty, 3);
                element.GetImage().SetValue(Grid.ColumnSpanProperty, 9);
            }
            else
            {
                element.GetImage().SetValue(Grid.RowSpanProperty, 9);
                element.GetImage().SetValue(Grid.ColumnSpanProperty, 3);
            }

            gameController.gameWindow.GameGrid.Children.Add(element.GetImage());
        }
Пример #2
0
        public void FindPlaceAndCreateNullor(Node node1, Node node2, string type)
        {
            string line      = "";
            int    row       = 0;
            int    column    = 0;
            int    id        = gameController.scheme.GetElementMaxId() + 1;
            string direction = "";

            var blockingElements = FindBlockingElements(node1, node2, ref direction);

            blockingElements.Remove(node1);
            blockingElements.Remove(node2);

            Norator  norator  = null;
            Nullator nullator = null;

            foreach (var elem in blockingElements)
            {
                if (elem is Norator)
                {
                    norator = (Norator)elem;
                }
                if (elem is Nullator)
                {
                    nullator = (Nullator)elem;
                }
            }

            blockingElements.Remove(norator);
            blockingElements.Remove(nullator);

            row    = direction == "U" ? node2.GetY() : node1.GetY();
            column = direction == "L" ? node2.GetX() : node1.GetX();

            if (!blockingElements.Any())
            {
                line = (direction == "R") || (direction == "L")
                    ? id + " " + (row - 1) + " " + (column + 4) + " R " + node1.GetId() + " " + node2.GetId()
                    : id + " " + (row + 4) + " " + (column - 1) + " D " + node1.GetId() + " " + node2.GetId();

                PlaceElement(ref line, type);

                int wireId = gameController.scheme.GetWireMaxId() + 1;
                int id1    = direction == "U" || direction == "L" ? node1.GetId() : node2.GetId();

                line = (direction == "R" || direction == "L")
                    ? wireId + " " + row + " " + column + " " + 6 + " R " + id1 + " N " + id + " E"
                    : wireId + " " + row + " " + column + " " + 6 + " D " + id1 + " N " + id + " E";

                PlaceWire(ref line);

                int length = (direction == "R") || (direction == "L")
                    ? Math.Abs(node1.GetX() - node2.GetX()) - 4
                    : Math.Abs(node1.GetY() - node2.GetY()) - 4;

                //Если элемент слишком короткий(т.е. он не дотягивается до второго узла), то лучше пририсовать ему провод, чтобы нормально все выглядело
                if (length > 8)
                {
                    wireId = gameController.scheme.GetWireMaxId() + 1;

                    id1 = direction == "U" || direction == "L" ? node2.GetId() : node1.GetId();

                    line = (direction == "R") || (direction == "L")
                        ? wireId + " " + row + " " + (column + 12) + " " + (length - 7) + " R " + id1 + " N " + id + " E"
                        : wireId + " " + (row + 12) + " " + column + " " + (length - 7) + " D " + id1 + " N " + id + " E";

                    PlaceWire(ref line);
                    line = "";
                }

                //Если у узлов не совпадают ни X, ни Y (здесь нужен будет еще один провод)
                if ((node1.GetX() != node2.GetX()) && (node1.GetY() != node2.GetY()))
                {
                    wireId = gameController.scheme.GetWireMaxId();

                    int wireRow    = 0;
                    int wireColumn = 0;

                    if ((direction == "R") || (direction == "L"))
                    {
                        wireRow    = node2.GetY() > node1.GetY() ? node1.GetY() : node2.GetY();
                        wireColumn = node2.GetX();
                    }
                    else
                    {
                        wireRow    = node2.GetY();
                        wireColumn = node2.GetX() > node1.GetX() ? node1.GetX() : node2.GetX();
                    }

                    length = (direction == "R") || (direction == "L")
                             ? Math.Abs(node1.GetY() - node2.GetY()) + 1
                             : Math.Abs(node1.GetX() - node2.GetX()) + 1;

                    line = (direction == "R") || (direction == "L")
                        ? (wireId + 1) + " " + wireRow + " " + wireColumn + " " + length + " D " + node1.GetId() + " N " + node2.GetId() + " N"
                        : (wireId + 1) + " " + wireRow + " " + wireColumn + " " + length + " R " + node1.GetId() + " N " + node2.GetId() + " N";

                    PlaceWire(ref line);
                    line = "";

                    line = (direction == "R") || (direction == "L")
                        ? (gameController.scheme.GetNodeMaxId() + 1) + " " + node1.GetY() + " " + node2.GetX()
                        : (gameController.scheme.GetNodeMaxId() + 1) + " " + node2.GetY() + " " + node1.GetX();
                    PlaceNode(ref line);
                    node2 = gameController.scheme.GetNode(gameController.scheme.GetNodeMaxId());
                }
            }
            else if (blockingElements.Count == 1)
            {
                //Если есть блокирующие элементы и нужно как-то считать положение элемента
                //TO DO
                direction = node1.GetY() == node2.GetY() ? "R" : "D";

                if (direction == "R" && node1.GetX() > node2.GetX())
                {
                    var temp = node1;
                    node1 = node2;
                    node2 = temp;
                }
                if (direction == "D" && node1.GetY() > node2.GetY())
                {
                    var temp = node1;
                    node1 = node2;
                    node2 = temp;
                }

                id   = gameController.scheme.GetElementMaxId() + 1;
                line = direction == "R"
                    ? id + " " + (node1.GetY() - 4) + " " + (node1.GetX() + 4) + " R " + node1.GetId() + " " + node2.GetId()
                    : id + " " + (node1.GetY() + 4) + " " + (node1.GetX() + 2) + " D " + node1.GetId() + " " + node2.GetId();
                PlaceElement(ref line, type);

                int wireId = gameController.scheme.GetWireMaxId() + 1;
                line = direction == "R"
                    ? wireId + " " + (node1.GetY() - 3) + " " + node1.GetX() + " 6 R " + id + " E"
                    : wireId + " " + node1.GetY() + " " + (node1.GetX() + 3) + " 6 D " + id + " E";
                PlaceWire(ref line);

                Wire wire1 = gameController.scheme.GetWire(wireId);

                wireId++;
                line = direction == "R"
                    ? wireId + " " + (node1.GetY() - 3) + " " + (node1.GetX() + 12) + " 6 R " + id + " E"
                    : wireId + " " + (node1.GetY() + 12) + " " + (node1.GetX() + 3) + " 6 D " + id + " E";
                PlaceWire(ref line);

                Wire wire2 = gameController.scheme.GetWire(wireId);

                wireId++;
                line = direction == "R"
                    ? wireId + " " + (node1.GetY() - 3) + " " + node1.GetX() + " 4 D " + node1.GetId() + " N"
                    : wireId + " " + node1.GetY() + " " + node1.GetX() + " 4 R " + node1.GetId() + " N";
                PlaceWire(ref line);

                Wire wire3 = gameController.scheme.GetWire(wireId);
                gameController.scheme.AddWireToObject(wire1, wire3);

                wireId++;
                line = direction == "R"
                    ? wireId + " " + (node2.GetY() - 3) + " " + node2.GetX() + " 4 D " + node2.GetId() + " N"
                    : wireId + " " + node2.GetY() + " " + node2.GetX() + " 4 R " + node2.GetId() + " N";
                PlaceWire(ref line);

                Wire wire4 = gameController.scheme.GetWire(wireId);
                gameController.scheme.AddWireToObject(wire2, wire4);
            }
        }