Exemplo n.º 1
0
        Compass(RailwayChain chain)
        {
            var m = new[] { RailwayType.T8L, RailwayType.T8R }.Contains(((Railway)chain[0]).Type) ? 2 : 1;

            return(
                chain[1 * m - 1] as Railway, chain[2 * m - 1] as Railway, chain[3 * m - 1] as Railway,
                chain[0] as Railway, /*                                */ chain[4 * m - 1] as Railway,
                chain[7 * m - 1] as Railway, chain[6 * m - 1] as Railway, chain[5 * m - 1] as Railway);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Цепочка блоков рельсов на основе модели
        /// </summary>
        /// <param name="model">Модель</param>
        /// <param name="reduceCount">Уменьшать количество доступных блоков в модели</param>
        /// <returns></returns>
        public static RailwayChain FromModel(Model model, bool reduceCount = false)
        {
            var railways = new List <IRailwayTemplate>()
            {
                new Railway(RailwayType.L0)
            };

            foreach (var item in model.Topology)
            {
                if (item.SecondBlock == 0)
                {
                    continue;
                }

                var block = model.Order[item.SecondBlock - 1];

                if (reduceCount)
                {
                    var blocksCount = model.Blocks.FirstOrDefault(_ => _.Name == block);

                    if (blocksCount != null)
                    {
                        blocksCount.Count--;
                    }
                }

                if (item.Direction == -1 && block.StartsWith("T"))
                {
                    block = block.ToLower();
                }

                railways.Add(Railway.From(block));
            }

            var chain = new RailwayChain(railways.ToArray());

            return(chain);
        }
Exemplo n.º 3
0
        /// <summary>
        ///  Создать стартовую трассу - кольцо
        /// </summary>
        /// <param name="turnDirection">Направление кольца (T4R, T4L, T8R или T8L)</param>
        /// <param name="model">Модель</param>
        /// <returns>Указатель на начало трассы - блок L0</returns>
        public static RailwayChain CreateCircle(RailwayType turnDirection, Model model = null)
        {
            // Добавляем блок L0
            var head = new Railway(RailwayType.L0)
            {
                Start = new Point(0, 0),
            };

            var count = turnDirection == RailwayType.T4L || turnDirection == RailwayType.T4R ? 8 : 16;

            var list = new List <IRailwayTemplate>()
            {
                head
            };

            var blueprint =
                turnDirection == RailwayType.T4L ? "t4" :
                turnDirection == RailwayType.T4R ? "T4" :
                turnDirection == RailwayType.T8L ? "t8" :
                turnDirection == RailwayType.T8R ? "T8" : "";

            // Добавляем 8 блоков T4 или 16 T8
            for (var i = 0; i < count; i++)
            {
                if (!RailwayFactory.Default.TryBuildTemplate(out var turn, out var error, blueprint, model))
                {
                    turn = new RailwayChain(new Railway(turnDirection));
                }

                list.AddRange(turn.GetRailways());
            }
            var chain = new RailwayChain(list.ToArray());

            //head.Append(chain);

            // Закольцовываем список
            //chain.Next = chain[0];

            // Связываем симметричные блоки
            for (var i = 0; i < list.Count; i++)
            {
                var block = list[i];

                if (!(DirectionExtensions.FromAngle(block.End.Angle) is Direction direction))
                {
                    continue;
                }

                if (direction == Direction.S)
                {
                    //var last = list.Last();
                    //last.Symmetric = block;

                    // TODO: задаём в качестве симметричного элемента для ↓ саму цепочку,
                    // чтобы при расширении блоки добавлялись после неё.
                    // По-хорошему надо добавлять внутрь цепочки в конец,
                    // но при этом ломается конвертация в модель,
                    // т.к. при добавлении а конец цепочки _tail не меняется

                    block.Symmetric = chain;
                    continue;
                }

                if (block.Symmetric != null)
                {
                    break;
                }

                var sym = direction.GetSymmetric();

                for (var j = i + 1; j < list.Count; j++)
                {
                    var other = list[j];

                    if (DirectionExtensions.FromAngle(other.End.Angle) is Direction otherDirection &&
                        otherDirection == sym)
                    {
                        other.Symmetric = block;
                        block.Symmetric = other;

                        break;
                    }
                }
            }

            //head.Symmetric = chain[count / 2 - 1];
            //chain[count / 2 - 1].Symmetric = head;

            return(chain);
        }