Пример #1
0
        private static int GetColOffsetDirection(Cell current, Cell target)
        {
            var rezult = BoardController.GetCellCol(target.num) - BoardController.GetCellCol(current.num);

            if (rezult == 0) return rezult;

            return rezult / Mathf.Abs(rezult);
        }
Пример #2
0
        public static List<int> GetPath(Cell start, Cell target)
        {
            List<int> path = new List<int>();

            Open = new List<Cell>();
            Closed = new List<Cell>();

            Cell current = start;
            current.fromStartDistance = 0;
            current.fromTargetDistance = GetDistance(current, target);
            current.distance = current.fromStartDistance + current.fromTargetDistance;

            Open.Add(current);

            while (Open.Count > 0)
            {
                current = GetMinDistanceCell();
                path.Add(current.num);

                if (current.num == target.num)
                    return path;

                foreach (int connectionNum in current.connections)
                {
                    Cell connectionCell = BoardController.GetCell(connectionNum);

                    if ((Open.Contains(connectionCell) || Closed.Contains(connectionCell)) && GetDistance(current, connectionCell) <= current.fromStartDistance + 1)
                        break;

                    connectionCell.fromStartDistance = current.fromStartDistance + 1;
                    connectionCell.fromTargetDistance = GetDistance(connectionCell, target);
                    connectionCell.distance = connectionCell.fromStartDistance + connectionCell.fromTargetDistance;

                    if (Closed.Contains(connectionCell))
                        Closed.Remove(connectionCell);

                    if (!Open.Contains(connectionCell))
                        Open.Add(connectionCell);
                }

                Closed.Add(current);
            }

            return path;
        }
Пример #3
0
        private static float GetDistance(Cell start, Cell target)
        {
            Cell current = start;
            int rowDir = GetRowOffsetDirection(current, target);
            int colDir = GetColOffsetDirection(current, target);

            float stepCount = 0;

            while (!(rowDir == 0 && colDir == 0))
            {
                current = BoardController.GetCell(current.num, new Vector2(colDir, rowDir));

                rowDir = GetRowOffsetDirection(current, target);
                colDir = GetColOffsetDirection(current, target);

                stepCount += 0.9f;
            }

            return stepCount;
        }