Exemple #1
0
        private List <Point> FindAStarPath(Point currentPosition, Point targetPosition)
        {
            var graph = new AstarGridGraph(mover.collisionLayer);
            var path  = graph.search(currentPosition, targetPosition);

            return(path);
        }
        public void update()
        {
            Point   currentNode   = new Point((int)entity.position.X / 16, (int)entity.position.Y / 16);
            Vector2 mousePosition = entity.scene.camera.mouseToWorldPoint();
            Point   mouseNode     = new Point((int)mousePosition.X / 16, (int)mousePosition.Y / 16);
            Point   nextNode      = currentNode;

            if (currentNode == mouseNode)
            {
                entity.destroy();
            }

            if (mouseNode != _goal)
            {
                if (!_grid.walls.Contains(mouseNode))
                {
                    _goal      = mouseNode;
                    _waypoints = _grid.search(currentNode, mouseNode);
                    _count     = 1;
                }
            }

            if (_waypoints != null && _waypoints.Count > _count)
            {
                nextNode = _waypoints[_count];
                if (currentNode == _waypoints[_count])
                {
                    if (!_grid.walls.Contains(mouseNode))
                    {
                        _waypoints = _grid.search(currentNode, mouseNode);
                    }
                    else
                    {
                        _count++;
                    }
                }
            }
            _mover.move(new Vector2((nextNode.X - currentNode.X) * 16 * _velocity * Time.deltaTime, (nextNode.Y - currentNode.Y) * 16 * _velocity * Time.deltaTime), out CollisionResult res);
        }
Exemple #3
0
        public Pathfinder(TiledMap tilemap)
        {
            _tilemap = tilemap;
            var layer = tilemap.getLayer <TiledTileLayer>("main");

            _start = new Point(1, 1);
            _end   = new Point(10, 10);

            _gridGraph         = new UnweightedGridGraph(layer);
            _breadthSearchPath = _gridGraph.search(_start, _end);

            _weightedGraph      = new WeightedGridGraph(layer);
            _weightedSearchPath = _weightedGraph.search(_start, _end);

            _astarGraph      = new AstarGridGraph(layer);
            _astarSearchPath = _astarGraph.search(_start, _end);

            Debug.drawTextFromBottom = true;
        }
Exemple #4
0
        void IUpdatable.update()
        {
            // on left click set our path end time
            if (Input.leftMouseButtonPressed)
            {
                _end = _tilemap.worldToTilePosition(Input.mousePosition);
            }

            // on right click set our path start time
            if (Input.rightMouseButtonPressed)
            {
                _start = _tilemap.worldToTilePosition(Input.mousePosition);
            }

            // regenerate the path on either click
            if (Input.leftMouseButtonPressed || Input.rightMouseButtonPressed)
            {
                // time both path generations
                var first = Debug.timeAction(() =>
                {
                    _breadthSearchPath = _gridGraph.search(_start, _end);
                });

                var second = Debug.timeAction(() =>
                {
                    _weightedSearchPath = _weightedGraph.search(_start, _end);
                });

                var third = Debug.timeAction(() =>
                {
                    _astarSearchPath = _astarGraph.search(_start, _end);
                });

                // debug draw the times
                Debug.drawText("Breadth First: {0}\nDijkstra: {1}\nAstar: {2}", first, second, third);
                Debug.log("\nBreadth First: {0}\nDijkstra: {1}\nAstar: {2}", first, second, third);
            }
        }