예제 #1
0
        public void SetNodePassable(int x, int y, bool passable)
        {
            Grid2DNode node = GetNode(x, y);

            if (node != null)
            {
                node.blockValue = (ushort)(passable ? 0 : 1);
            }
        }
예제 #2
0
 public Grid2DNode GetNode(int x, int y)
 {
     if (x >= 0 && x < width && y >= 0 && y <= height)
     {
         Grid2DNode node = nodes[x, y];
         return(node);
     }
     return(null);
 }
예제 #3
0
        protected override int CalCostH(ProcessingNode node, AStarContext context)
        {
            Grid2DNode endNode = context.GetTargetNode().astarNode as Grid2DNode;
            Grid2DNode nd      = (Grid2DNode)(node.astarNode);
            int        dx      = Math.Abs(endNode.x - nd.x);
            int        dy      = Math.Abs(endNode.y - nd.y);
            int        dist    = dx > dy ? 14 * dy + 10 * (dx - dy) : 14 * dx + 10 * (dy - dx);

            return(dist);
        }
예제 #4
0
        protected override int CalCostG(ProcessingNode prevNode, ProcessingNode currentNode, AStarContext context)
        {
            Grid2DNode pre  = (Grid2DNode)(prevNode.astarNode);
            Grid2DNode cur  = (Grid2DNode)(currentNode.astarNode);
            int        dx   = Math.Abs(pre.x - cur.x);
            int        dy   = Math.Abs(pre.y - cur.y);
            int        dist = dx > dy ? 14 * dy + 10 * (dx - dy) : 14 * dx + 10 * (dy - dx);

            return(prevNode.g + dist);
        }
예제 #5
0
        public bool IsNodePassable(int x, int y)
        {
            Grid2DNode node = GetNode(x, y);

            if (node != null)
            {
                return(node.blockValue == 0);
            }
            return(false);
        }
예제 #6
0
        public void FindPath(ProcessingNode start, ProcessingNode end, AStarContext context)
        {
            ProcessingNode endNode = DoAStar(context);

            Grid2DGraph    graph    = context.map as Grid2DGraph;
            ProcessingNode pathNode = endNode;

            while (pathNode != null)
            {
                Grid2DNode navNode = pathNode.astarNode as Grid2DNode;
                context.rawPathNodeCache.Add(navNode);
                context.rawPathPoints.Add(graph.Int2ToFixVector3(new Int2(navNode.x, navNode.y)));
                pathNode = pathNode.prev;
            }
        }
예제 #7
0
 public override AStarNode GetNeighbourNode(AStarNode node, int index)
 {
     if (index >= 0 && index < NEIGHBOUR_COUNT && node != null)
     {
         Grid2DNode gridNode = node as Grid2DNode;
         int        x        = gridNode.x + xOffset[index];
         int        y        = gridNode.y + yOffset[index];
         if (x >= 0 && x < width && y >= 0 && y < height)
         {
             Grid2DNode toNode = nodes[x, y] as Grid2DNode;
             if (IsNeighbourPassable(gridNode, toNode))
             {
                 return(toNode);
             }
         }
     }
     return(null);
 }
예제 #8
0
        public override void Init(INavData data)
        {
            navData       = data as Grid2DNavData;
            nodeIdCounter = 0;
            this.width    = navData.Width;
            this.height   = navData.Height;

            nodes = new Grid2DNode[width, height];

            for (int x = 0; x < width; ++x)
            {
                for (int y = 0; y < height; ++y)
                {
                    Grid2DNode node = new Grid2DNode(nodeIdCounter++);
                    nodes[x, y]      = node;
                    node.x           = (ushort)x;
                    node.y           = (ushort)y;
                    node.blockValue  = navData.GetMask(x, y);
                    node.terrainType = navData.GetTerrain(x, y);
                }
            }
        }
예제 #9
0
 private bool IsNeighbourPassable(Grid2DNode from, Grid2DNode to)
 {
     return((nodes[from.x, to.y].blockValue < 1 && nodes[to.x, from.y].blockValue < 1) ||
            (from.x == to.x && from.y == to.y));
 }