示例#1
0
        public static WeightedGraph <double> GetWeightedGraph(ICellMap map, NeighbourMode neighbourMode)
        {
            WeightedGraphNode <double>[,] nodes = new WeightedGraphNode <double> [map.Width, map.Height];

            for (int x = 0; x < map.Width; x++)
            {
                for (int y = 0; y < map.Height; y++)
                {
                    if (map.IsPassable(x, y))
                    {
                        WeightedGraphNode <double> connectionNode;
                        WeightedGraphNode <double> currentNode = new WeightedGraphNode <double>(Double.PositiveInfinity);
                        nodes[x, y]          = currentNode;
                        currentNode.Position = new Vector2Int(x, y);

                        int prevX = x - 1;
                        int prevY = y - 1;
                        int nextY = y + 1;

                        if (prevX >= 0)
                        {
                            connectionNode = nodes[prevX, y];
                            ConnectNodes(currentNode, connectionNode, 1);
                        }
                        if (prevY >= 0)
                        {
                            connectionNode = nodes[x, prevY];
                            ConnectNodes(currentNode, connectionNode, 1);
                        }

                        if (prevX >= 0 && prevY >= 0 && neighbourMode == NeighbourMode.SidesAndDiagonals)
                        {
                            connectionNode = nodes[prevX, prevY];
                            ConnectNodes(currentNode, connectionNode, Sqrt2);
                        }

                        if (nextY < map.Height && prevX >= 0 && neighbourMode == NeighbourMode.SidesAndDiagonals)
                        {
                            connectionNode = nodes[prevX, nextY];
                            ConnectNodes(currentNode, connectionNode, Sqrt2);
                        }
                    }
                    else
                    {
                        nodes[x, y] = null;
                    }
                }
            }

            WeightedGraph <double> weightedGraph = new WeightedGraph <double>(nodes, Double.PositiveInfinity);

            return(weightedGraph);

            ;
        }
示例#2
0
        public static GraphNode[,] GetGraph(ICellMap map, NeighbourMode neighbourMode)
        {
            GraphNode[,] nodes = new GraphNode[map.Width, map.Height];
            for (int x = 0; x < map.Width; x++)
            {
                for (int y = 0; y < map.Height; y++)
                {
                    if (map.IsPassable(x, y))
                    {
                        GraphNode connectionNode;
                        GraphNode currentNode = new GraphNode();
                        nodes[x, y]          = currentNode;
                        currentNode.Position = new Vector2Int(x, y);

                        int prevX = x - 1;
                        int prevY = y - 1;
                        int nextY = y + 1;

                        if (prevX >= 0)
                        {
                            connectionNode = nodes[prevX, y];
                            ConnectNodes(currentNode, connectionNode);
                        }

                        if (prevY >= 0)
                        {
                            connectionNode = nodes[x, prevY];
                            ConnectNodes(currentNode, connectionNode);
                        }

                        if (prevX >= 0 && prevY >= 0 && neighbourMode == NeighbourMode.SidesAndDiagonals)
                        {
                            connectionNode = nodes[prevX, prevY];
                            ConnectNodes(currentNode, connectionNode);
                        }

                        if (nextY < map.Height && prevX >= 0 && neighbourMode == NeighbourMode.SidesAndDiagonals)
                        {
                            connectionNode = nodes[prevX, nextY];
                            ConnectNodes(currentNode, connectionNode);
                        }
                    }
                    else
                    {
                        nodes[x, y] = null;
                    }
                }
            }

            return(nodes);
        }
示例#3
0
        private IList <Vector2Int> ForwardSmoothing(ICellMap map, IList <Vector2Int> rawPath)
        {
            List <Vector2Int> smoothedPath = new List <Vector2Int>(rawPath.Count);
            bool lineCasterFailed = false;
            BresenhamLinePlotter lineCaster = new BresenhamLinePlotter();
            int        currentIndex = 0, nextIndex = 1, lastValidIndex = 1;
            Vector2Int lastObstacle = default;

            bool IsCellPassable(int x, int y)
            {
                bool isPassable = map.IsPassable(x, y);

                if (!isPassable)
                {
                    OnObstacleDetectedEvent?.Invoke(x, y);
                    lastObstacle     = new Vector2Int(x, y);
                    lineCasterFailed = true;
                }

                return(isPassable);
            }

            bool SetPathCell(int x, int y)
            {
                smoothedPath.Add(new Vector2Int(x, y));
                return(true);
            }

            while (currentIndex < rawPath.Count - 1)
            {
                nextIndex      = currentIndex + 1;
                lastValidIndex = currentIndex;
                while (nextIndex < rawPath.Count)
                {
                    lineCasterFailed = false;
                    lineCaster.CastLine(rawPath[currentIndex], rawPath[nextIndex], IsCellPassable);

                    if (!lineCasterFailed)
                    {
                        lastValidIndex = nextIndex;
                    }
                    nextIndex++;
                }

                lineCaster.CastLine(rawPath[currentIndex], rawPath[lastValidIndex], SetPathCell);
                currentIndex = lastValidIndex + 1;
            }

            return(smoothedPath);
        }
示例#4
0
        public static Bitmap GetBitmap(ICellMap map, int scale)
        {
            Bitmap bitmap = new Bitmap(map.Width * scale, map.Height * scale);

            for (int x = 0; x < map.Width; x++)
            {
                for (int y = 0; y < map.Height; y++)
                {
                    if (!map.IsPassable(x, y))
                    {
                        DrawScaledPixel(bitmap, Color.Black, x, y, scale);
                    }
                }
            }

            return(bitmap);
        }
示例#5
0
        private bool IsLayerCellPassable(int index, int x, int y)
        {
            ICellMap   layer  = _layers[index];
            Vector2Int offset = _layerOffsets[index];

            int shiftedX = x - offset.X;
            int shiftedY = y - offset.Y;

            if (!layer.IsInBounds(shiftedX, shiftedY))
            {
                return(true);
            }
            else
            {
                bool layerIsPassable = layer.IsPassable(shiftedX, shiftedY);
                return(layerIsPassable);
            }
        }
示例#6
0
        private void UpdateCellSurroundings(ICellMap map, int[,] matrix, int x, int y, int d, NeighbourMode neighbourMode)
        {
            foreach (var step in Steps.GetSteps(neighbourMode))
            {
                int xNew = x + step.X;
                int yNew = y + step.Y;

                if (!map.IsInBounds(xNew, yNew))
                {
                    continue;
                }

                if (map.IsPassable(xNew, yNew) && matrix[xNew, yNew] == -1)
                {
                    matrix[xNew, yNew] = d;
                    OnCellViewedEvent?.Invoke(this, xNew, yNew, d);
                }
            }
        }
示例#7
0
        public bool IsPassable(int x, int y)
        {
            if (!IsInBounds(x, y))
            {
                return(false);
            }

            if (!_layerBase.IsPassable(x, y))
            {
                return(false);
            }

            for (var i = 0; i < _layers.Count; i++)
            {
                if (!IsLayerCellPassable(i, x, y))
                {
                    return(false);
                }
            }

            return(true);
        }
示例#8
0
        private static void GetTransitionCells(CellCluster clusterA, CellCluster clusterB, Vector2Int start,
                                               Vector2Int delta, Vector2Int bOffset, List <Vector2Int> aPoints, List <Vector2Int> bPoints)
        {
            int xCurrent = start.X;
            int yCurrent = start.Y;

            ICellMap map = clusterA.Map;

            bool IsInsideCluster(int x, int y)
            {
                int clusterRelativeX = x - clusterA.LeftBottom.X;
                int clusterRelativeY = y - clusterA.LeftBottom.Y;

                if (clusterRelativeX < 0 || clusterRelativeY < 0 || clusterRelativeX >= clusterA.Width ||
                    clusterRelativeY >= clusterB.Height)
                {
                    return(false);
                }
                return(true);
            }

            while (IsInsideCluster(xCurrent, yCurrent))
            {
                while (!map.IsPassable(xCurrent, yCurrent) ||
                       !map.IsPassable(xCurrent + bOffset.X, yCurrent + bOffset.Y))
                {
                    xCurrent += delta.X;
                    yCurrent += delta.Y;
                    if (!IsInsideCluster(xCurrent, yCurrent))
                    {
                        return;
                    }
                }

                aPoints.Add(new Vector2Int(xCurrent, yCurrent));
                bPoints.Add(new Vector2Int(xCurrent + bOffset.X, yCurrent + bOffset.Y));


                xCurrent += delta.X;
                yCurrent += delta.Y;

                if (!IsInsideCluster(xCurrent, yCurrent))
                {
                    break;
                }

                while (map.IsPassable(xCurrent, yCurrent) && map.IsPassable(xCurrent + bOffset.X, yCurrent + bOffset.Y))
                {
                    xCurrent += delta.X;
                    yCurrent += delta.Y;
                    if (!IsInsideCluster(xCurrent, yCurrent))
                    {
                        break;
                    }
                }

                int prevPassableX = xCurrent - delta.X;
                int prevPassableY = yCurrent - delta.Y;
                //int prevPassableX = xCurrent;
                //int prevPassableY = yCurrent;
                Vector2Int aPoint = new Vector2Int(prevPassableX, prevPassableY);
                Vector2Int bPoint = new Vector2Int(prevPassableX + bOffset.X, prevPassableY + bOffset.Y);
                if (aPoints[aPoints.Count - 1] != aPoint)
                {
                    aPoints.Add(aPoint);
                    bPoints.Add(bPoint);
                }
            }
        }
示例#9
0
 public bool IsPassable(int x, int y)
 {
     return(Map.IsPassable(x, y));
 }