Beispiel #1
0
        private void checkForClickedNode(Vector2 clickPosition,
            bool leftAltPressed, bool rightAltPressed)
        {
            PositionalNode nodeClicked = null;
            PathNodeRadar nodeRadar = new PathNodeRadar(clickPosition, g);

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

            foreach (PositionalNode n in adjacentNodes)
            {
                Vector2 mouseToNode = n.Position - clickPosition;

                if (mouseToNode.LengthSquared() < nodeRadius * nodeRadius)
                {
                    nodeClicked = n;
                    break;
                }
            }

            if (nodeClicked != null)
            {
                if (leftAltPressed && !rightAltPressed)
                {
                    if (SourceNode != null)
                        ChangeNodeColor(SourceNode, NodeColor);

                    SourceNode = nodeClicked;
                    ChangeNodeColor(nodeClicked, Color.White);
                }
                else if (!leftAltPressed && rightAltPressed)
                {
                    if (TargetNode != null)
                        ChangeNodeColor(TargetNode, NodeColor);

                    TargetNode = nodeClicked;
                    ChangeNodeColor(nodeClicked, Color.Black);
                }
            }
        }
Beispiel #2
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);
                }
            }
        }