Beispiel #1
0
        static public List <SpotNode> returnURDL(List <SpotNode> openNodes, List <SpotNode> closedNodes, List <BasicTile> tiles, SpotNode selectedNode)
        {
            BasicTile       tileAbove = tiles.Find(t => t.mapPosition.Location == new Point(selectedNode.nodeLoc.X, selectedNode.nodeLoc.Y - 64));
            BasicTile       tileBelow = tiles.Find(t => t.mapPosition.Location == new Point(selectedNode.nodeLoc.X, selectedNode.nodeLoc.Y + 64));
            BasicTile       tileLeft  = tiles.Find(t => t.mapPosition.Location == new Point(selectedNode.nodeLoc.X - 64, selectedNode.nodeLoc.Y));
            BasicTile       tileRight = tiles.Find(t => t.mapPosition.Location == new Point(selectedNode.nodeLoc.X + 64, selectedNode.nodeLoc.Y));
            List <SpotNode> temp      = new List <SpotNode>();

            if (tileAbove != null)
            {
                temp.Add(new SpotNode(selectedNode.NodeCost + 1, new Point(selectedNode.nodeLoc.X, selectedNode.nodeLoc.Y - 64)));
                var temp1 = openNodes.Find(on => on.nodeLoc == tileAbove.mapPosition.Location);
                var temp2 = closedNodes.Find(on => on.nodeLoc == tileAbove.mapPosition.Location);
                if (temp1 != null || temp2 != null)
                {
                    temp.RemoveAt(temp.Count - 1);
                }
            }

            if (tileBelow != null)
            {
                temp.Add(new SpotNode(selectedNode.NodeCost + 1, new Point(selectedNode.nodeLoc.X, selectedNode.nodeLoc.Y + 64)));
                var temp1 = openNodes.Find(on => on.nodeLoc == tileBelow.mapPosition.Location);
                var temp2 = closedNodes.Find(on => on.nodeLoc == tileBelow.mapPosition.Location);
                if (temp1 != null || temp2 != null)
                {
                    temp.RemoveAt(temp.Count - 1);
                }
            }

            if (tileLeft != null)
            {
                temp.Add(new SpotNode(selectedNode.NodeCost + 1, new Point(selectedNode.nodeLoc.X - 64, selectedNode.nodeLoc.Y)));
                var temp1 = openNodes.Find(on => on.nodeLoc == tileLeft.mapPosition.Location);
                var temp2 = closedNodes.Find(on => on.nodeLoc == tileLeft.mapPosition.Location);
                if (temp1 != null || temp2 != null)
                {
                    temp.RemoveAt(temp.Count - 1);
                }
            }

            if (tileRight != null)
            {
                temp.Add(new SpotNode(selectedNode.NodeCost + 1, new Point(selectedNode.nodeLoc.X + 64, selectedNode.nodeLoc.Y)));
                var temp1 = openNodes.Find(on => on.nodeLoc == tileRight.mapPosition.Location);
                var temp2 = closedNodes.Find(on => on.nodeLoc == tileRight.mapPosition.Location);
                if (temp1 != null || temp2 != null)
                {
                    temp.RemoveAt(temp.Count - 1);
                }
            }

            return(temp);
        }
Beispiel #2
0
        static public List <BasicTile> returnValidMapRadius2(int r, List <BasicTile> bt, Vector2 bs)
        {
            Point            loc       = ((bs / 64).ToPoint().ToVector2() * 64).ToPoint();
            List <Rectangle> temp      = new List <Rectangle>();
            List <BasicTile> tempTiles = new List <BasicTile>();

            for (int i = 0; i < r + 1; i++)
            {
                for (int j = -(r - i); j < (r - i) + 1; j++)
                {
                    temp.Add(new Rectangle(loc.X + i * 64, loc.Y + j * 64, 64, 64));
                }
            }

            for (int i = -r; i != 0; i++)
            {
                for (int j = -(i + r); j < i + r + 1; j++)
                {
                    temp.Add(new Rectangle(loc.X + i * 64, loc.Y + j * 64, 64, 64));
                }
            }

            foreach (var item in temp)
            {
                BasicTile tempTile = bt.Find(t => t.mapPosition == item);
                if (tempTile != null)
                {
                    tempTiles.Add(tempTile);
                }
            }

            List <SpotNode> openNodes   = new List <SpotNode>();
            List <SpotNode> closedNodes = new List <SpotNode>();
            SpotNode        startNode   = new SpotNode(0, ((bs / 64).ToPoint().ToVector2() * 64).ToPoint());
            SpotNode        selectedNode;

            openNodes.AddRange(returnURDL(openNodes, closedNodes, tempTiles, startNode));
            while (openNodes.Count != 0)
            {
                for (int i = 1; i < r + 1; i++)
                {
                    selectedNode = openNodes.Find(on => on.NodeCost == i);
                    if (selectedNode != null)
                    {
                        openNodes.Remove(selectedNode);
                        closedNodes.Add(selectedNode);
                        if (i != r)
                        {
                            openNodes.AddRange(returnURDL(openNodes, closedNodes, tempTiles, selectedNode));
                        }
                        break;
                    }
                }
            }

            List <BasicTile> toDeleteTiles = new List <BasicTile>();

            foreach (var tile in tempTiles)
            {
                bool bCanReach = false;
                foreach (var cn in closedNodes)
                {
                    if (tile.mapPosition.Location == cn.nodeLoc)
                    {
                        bCanReach = true;
                    }
                }

                if (!bCanReach)
                {
                    toDeleteTiles.Add(tile);
                }
            }

            foreach (var item in toDeleteTiles)
            {
                tempTiles.Remove(item);
            }

            return(tempTiles);
        }