Exemplo n.º 1
0
        /// <summary>Возвращает узлы, напрямую соединённые с указанным узлом.</summary>
        /// <param name="node">Опорный узел, относительно которого выбираются соседние узлы.</param>
        /// <returns>Возвращает набор соседних узлов.</returns>
        public override IEnumerable <IGraphNode> GetNext(IGraphNode node)
        {
            if (node is null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            var hexCurrent   = (HexNode)node;
            var offsetCoords = hexCurrent.OffsetCoords;
            var segmentX     = offsetCoords.X / _segmentSize;

            if (offsetCoords.X < 0)
            {
                segmentX--;
            }

            var segmentY = offsetCoords.Y / _segmentSize;

            if (offsetCoords.Y < 0)
            {
                segmentY--;
            }

            var localOffsetX = NormalizeNeighborCoord(offsetCoords.X % _segmentSize);
            var localOffsetY = NormalizeNeighborCoord(offsetCoords.Y % _segmentSize);

            var segmentKey = new SegmentKey(segmentX, segmentY);
            var matrix     = _segmentDict[segmentKey];

            return(GetNextFromMatrix(localOffsetX, localOffsetY, segmentX, segmentY, matrix));
        }
Exemplo n.º 2
0
        private void CreateSegment(int segmentX, int segmentY)
        {
            var matrix = new IMapNode[_segmentSize, _segmentSize];

            var key = new SegmentKey(segmentX, segmentY);

            _segmentDict[key] = matrix;
        }
Exemplo n.º 3
0
        private void CreateSegment(int segmentX, int segmentY)
        {
            //TODO Отказаться от многомерного массива. Вместо этого сделать одномерный и адресацию через смещение.
            var matrix = new IGraphNode[_segmentSize, _segmentSize];

            var key = new SegmentKey(segmentX, segmentY);

            _segmentDict[key] = matrix;
        }
Exemplo n.º 4
0
        protected HexNode?GetByCoords(int x, int y)
        {
            var segmentKey = new SegmentKey(0, 0);
            var segment    = _segmentDict[segmentKey];

            if (x < 0 || y < 0 || x >= _segmentSize || y >= _segmentSize)
            {
                return(null);
            }

            var node = segment[x, y];

            return((HexNode)node);
        }
Exemplo n.º 5
0
        protected HexNode GetByCoords(int x, int y)
        {
            var segmentKey = new SegmentKey(0, 0);
            var segment    = _segmentDict[segmentKey];

            try
            {
                var node = segment[x, y];
                return((HexNode)node);
            }
            catch (IndexOutOfRangeException)
            {
                return(null);
            }
        }
Exemplo n.º 6
0
        private ITerrainNode[,] CreateSegment(int segmentX, int segmentY)
        {
            var matrix = new ITerrainNode[_segmentSize, _segmentSize];

            for (var i = 0; i < _segmentSize; i++)
            {
                for (var j = 0; j < _segmentSize; j++)
                {
                    matrix[i, j] = new HexNode(i + segmentX * _segmentSize, j + segmentY * _segmentSize);
                }
            }

            var key = new SegmentKey(segmentX, segmentY);

            _segmentDict[key] = matrix;
            return(matrix);
        }
Exemplo n.º 7
0
        private bool AddDistanceInner(ISegment segment)
        {
            var result = false;

            if (null != segment)
            {
                var newKey = new SegmentKey()
                {
                    Start = segment.Start, End = segment.End
                };
                if (!_distances.ContainsKey(newKey))
                {
                    _distances.Add(newKey, segment);
                    result = true;
                }
            }
            return(result);
        }
Exemplo n.º 8
0
        public IEnumerable <ITerrainNode> GetNeighborNodes(ITerrainNode node)
        {
            var offsetCoords = node.Offset;
            var segmentX     = offsetCoords.X / _segmentSize;

            if (offsetCoords.X < 0)
            {
                segmentX--;
            }

            var segmentY = offsetCoords.Y / _segmentSize;

            if (offsetCoords.Y < 0)
            {
                segmentY--;
            }

            var localOffsetX = NormalizeNeighborCoord(offsetCoords.X % _segmentSize);
            var localOffsetY = NormalizeNeighborCoord(offsetCoords.Y % _segmentSize);

            var segmentKey = new SegmentKey(segmentX, segmentY);
            var matrix     = _segmentDict[segmentKey];

            var directions        = HexHelper.GetOffsetClockwise();
            var currentCubeCoords = HexHelper.ConvertToCube(localOffsetX, localOffsetY);

            for (var i = 0; i < 6; i++)
            {
                var dir = directions[i];
                var neighborLocalCube = new CubeCoords(dir.X + currentCubeCoords.X,
                                                       dir.Y + currentCubeCoords.Y,
                                                       dir.Z + currentCubeCoords.Z);

                var neighborLocalOffset = HexHelper.ConvertToOffset(neighborLocalCube);

                var neighborSegmentX = segmentX;
                var neighborSegmentY = segmentY;

                if (neighborLocalOffset.X < 0)
                {
                    neighborSegmentX--;
                }
                else if (neighborLocalOffset.X >= _segmentSize)
                {
                    neighborSegmentX++;
                }

                if (neighborLocalOffset.Y < 0)
                {
                    neighborSegmentY--;
                }
                else if (neighborLocalOffset.Y >= _segmentSize)
                {
                    neighborSegmentY++;
                }

                if (neighborSegmentX == segmentX &&
                    neighborSegmentY == segmentY)
                {
                    yield return(matrix[neighborLocalOffset.X, neighborLocalOffset.Y]);
                }
                else
                {
                    var segmentMatrix = CreateSegment(neighborSegmentX, neighborSegmentY);
                    var neighborX     = NormalizeNeighborCoord(neighborLocalOffset.X);
                    var neighborY     = NormalizeNeighborCoord(neighborLocalOffset.Y);

                    yield return(segmentMatrix[neighborX, neighborY]);
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>Возвращает узлы, напрямую соединённые с указанным узлом.</summary>
        /// <param name="node">Опорный узел, относительно которого выбираются соседние узлы.</param>
        /// <returns>Возвращает набор соседних узлов.</returns>
        public override IEnumerable <IMapNode> GetNext(IMapNode node)
        {
            var hexCurrent   = (HexNode)node;
            var offsetCoords = new OffsetCoords(hexCurrent.OffsetX, hexCurrent.OffsetY);
            var segmentX     = offsetCoords.X / _segmentSize;

            if (offsetCoords.X < 0)
            {
                segmentX--;
            }

            var segmentY = offsetCoords.Y / _segmentSize;

            if (offsetCoords.Y < 0)
            {
                segmentY--;
            }

            var localOffsetX = NormalizeNeighborCoord(offsetCoords.X % _segmentSize);
            var localOffsetY = NormalizeNeighborCoord(offsetCoords.Y % _segmentSize);

            var segmentKey = new SegmentKey(segmentX, segmentY);
            var matrix     = _segmentDict[segmentKey];

            var directions        = HexHelper.GetOffsetClockwise();
            var currentCubeCoords = HexHelper.ConvertToCube(localOffsetX, localOffsetY);

            for (var i = 0; i < 6; i++)
            {
                var dir = directions[i];
                var neighborLocalCube = new CubeCoords(dir.X + currentCubeCoords.X,
                                                       dir.Y + currentCubeCoords.Y,
                                                       dir.Z + currentCubeCoords.Z);

                var neighborLocalOffset = HexHelper.ConvertToOffset(neighborLocalCube);

                var neighborSegmentX = segmentX;
                var neighborSegmentY = segmentY;

                if (neighborLocalOffset.X < 0)
                {
                    neighborSegmentX--;
                }
                else if (neighborLocalOffset.X >= _segmentSize)
                {
                    neighborSegmentX++;
                }

                if (neighborLocalOffset.Y < 0)
                {
                    neighborSegmentY--;
                }
                else if (neighborLocalOffset.Y >= _segmentSize)
                {
                    neighborSegmentY++;
                }

                IMapNode currentNeibour;
                if (neighborSegmentX == segmentX &&
                    neighborSegmentY == segmentY)
                {
                    currentNeibour = matrix[neighborLocalOffset.X, neighborLocalOffset.Y];

                    if (currentNeibour == null)
                    {
                        continue;
                    }

                    yield return(currentNeibour);
                }
            }
        }