Exemplo n.º 1
0
        public void Enter(SmartFarmer i)
        {
            if (i.patrolRoute.Count > 0)
            {
                AStarSearch patrolStart = new AStarSearch(
                    AStarGame.GameMap.NavigationGraph,
                    AStarGame.GameMap.ClosestNodeIndex(i.Position),
                    AStarGame.GameMap.ClosestNodeIndex(i.patrolRoute[0]),
                    AStarHeuristics.Distance);

                List<int> patrolSearchNodes;
                patrolStart.PathToTarget(out patrolSearchNodes);

                List<Vector2> patrolSearchPos;
                patrolSearchPos = AStarGame.GameMap.getWorldfromNodes(patrolSearchNodes);

                i.FollowPath(patrolSearchPos, false);
            }
        }
Exemplo n.º 2
0
        public void Enter(SmartFarmer i)
        {
            bushes = BushRadar.getBushes();

            if (bushes.Count == 0)
                i.doneSearching = true;
            else
            {
                bushSearch = new AStarSearch(
                    AStarGame.GameMap.NavigationGraph,
                    AStarGame.GameMap.ClosestNodeIndex(i.Position + (15.0f * i.Heading)),
                    AStarGame.GameMap.ClosestNodeIndex(bushes[0]),
                    AStarHeuristics.Distance);

                bushSearch.PathToTarget(out bushSearchNodes);
                bushSearchPos = AStarGame.GameMap.getWorldfromNodes(bushSearchNodes);

                i.FollowPath(bushSearchPos, false);
            }
        }
Exemplo n.º 3
0
        public void Execute(SmartFarmer i)
        {
            if (!i.doneSearching)
            {
                bool reachedBush = AStarGame.ApproximatelyEqual(i.Position, bushes[nextBush]);
                if (reachedBush)
                {
                    nextBush++;
                    i.FollowingPath = false;

                    if (nextBush >= bushes.Count)
                    {
                        i.doneSearching = true;
                        return;
                    }
                }

                if (!i.FollowingPath)
                {
                    if (!AStarGame.GameMap.WallsBetween(i.Position, bushes[nextBush]))
                    {
                        Vector2 force = i.Seek(bushes[nextBush]);
                        i.Velocity += force;
                    }
                    else
                    {
                        bushSearch = new AStarSearch(
                            AStarGame.GameMap.NavigationGraph,
                            AStarGame.GameMap.ClosestNodeIndex(i.Position),
                            AStarGame.GameMap.ClosestNodeIndex(bushes[nextBush]),
                            AStarHeuristics.Distance);

                        bushSearch.PathToTarget(out bushSearchNodes);
                        bushSearchPos = AStarGame.GameMap.getWorldfromNodes(bushSearchNodes);

                        i.FollowPath(bushSearchPos, false);
                    }
                }
            }
        }
Exemplo n.º 4
0
        // Depending on whether or not source and target nodes have
        // been selected, this method will display the A* path between
        // source and target, or instruct the player's entity to move
        // along the A* path to the target.
        private void updatePathDisplay()
        {
            IGameEntity player = EntityManager.Instance.GetPlayer();

            if (SourceNode == null && TargetNode != null && !player.FollowingPath)
            {
                PathNodeRadar nodeRadar = new PathNodeRadar(player.Position, g);

                List<PositionalNode> adjacentNodes;
                nodeRadar.AdjacentNodes(out adjacentNodes);

                PositionalNode nodeClosestToPlayer = null;
                float minDistSquared = float.MaxValue;
                foreach (PositionalNode n in adjacentNodes)
                {
                    float distSquared = (n.Position - player.Position).LengthSquared();

                    if (distSquared < minDistSquared)
                    {
                        minDistSquared = distSquared;
                        nodeClosestToPlayer = n;
                    }
                }

                AStarSearch search = new AStarSearch(g, nodeClosestToPlayer.Index,
                    TargetNode.Index, AStarHeuristics.Distance);

                if (search.TargetFound)
                {
                    // Change old path back to default curEdge color.
                    for (int i = 0; i < path.Count - 1; i++)
                        ChangeEdgeColor(g.GetEdge(path[i], path[i + 1]), EdgeColor);

                    search.PathToTarget(out path);

                    // Change new path to contrasting curEdge color.
                    for (int i = 0; i < path.Count - 1; i++)
                        ChangeEdgeColor(g.GetEdge(path[i], path[i + 1]), Color.Red);

                    List<Vector2> nodePositions = new List<Vector2>();
                    for (int i = 0; i < path.Count; i++)
                        nodePositions.Add(g.GetNode(path[i]).Position);

                    player.FollowPath(nodePositions, false);
                }
            }
            else if (SourceNode != null && TargetNode != null)
            {
                AStarSearch search = new AStarSearch(g, SourceNode.Index,
                    TargetNode.Index, AStarHeuristics.Distance);

                if (search.TargetFound)
                {
                    // Change old path back to default curEdge color.
                    for (int i = 0; i < path.Count - 1; i++)
                        ChangeEdgeColor(g.GetEdge(path[i], path[i + 1]), EdgeColor);

                    search.PathToTarget(out path);

                    // Change new path to contrasting curEdge color.
                    for (int i = 0; i < path.Count - 1; i++)
                        ChangeEdgeColor(g.GetEdge(path[i], path[i + 1]), Color.Red);
                }
            }
        }