Esempio n. 1
0
        //Check that a node can be created here
        private int GetTerrainCost(int x, int y)
        {
            //Check the boundary
            if (!_boundary.Contains(x, y))
            {
                return(-1);
            }

            TerrainPoint node = _terrain.Get(x, y);

            if (node.IsEmpty)
            {
                return(0);
            }

            return(-1);
        }
Esempio n. 2
0
        //Add a node to a parent node, ignoring if impassable or already closed,
        //adding if new, updating if new and better route
        private void AddAdjacentNode(int dx, int dy, int cost, RouteNode parent)
        {
            int x       = parent.X + dx;
            int y       = parent.Y + dy;
            int newCost = parent.MovementCost + cost;

            //Get terrain cost. -1 is not passable, 0 no cost, 1,2,3 etc higher cost
            int terraincost = GetTerrainCost(x, y);

            if (terraincost == -1)
            {
                return;
            }

            //Check if item has been added to the grid already
            RouteNode existing = _grid.Get(x, y);

            if (existing != null)
            {
                if (!existing.Closed && newCost < existing.MovementCost)
                {
                    existing.MovementCost = newCost;
                    existing.TotalCost    = newCost + existing.Heuristic;
                    existing.Parent       = parent;

                    if (_movementList.Contains(existing))
                    {
                        _movementList.Update(existing);
                    }
                    else
                    {
                        _openList.Update(existing);
                    }
                }
                return;
            }

            //Create a new node
            RouteNode node = new RouteNode();

            node.X = x;
            node.Y = y;

            //Set the parent after the location so that the Direction can be set correctly
            node.Parent = parent;

            node.MovementCost = newCost;        //Add the cost to the parent cost
            node.Heuristic    = GetHeuristic(node);
            node.TotalCost    = newCost + node.Heuristic;

            PushNode(node);

            //Add to the grid
            _grid.Add(x, y, node);
        }