private bool ensureGoalIntegrity(AStarComponent aStarComponent)
        {
            if (aStarComponent.Follow)
            {
                GameObject entity = aStarComponent.EntityToFollow;
                Transform2DComponent entityTransformComponent =
                    (Transform2DComponent)entity.GetComponent(ComponentType.Transform2D);

                if (!aStarComponent.DoSearch)
                {
                    if (aStarComponent.EntityToFollowPosBuffer != entityTransformComponent.GetTranslation() ||
                        aStarComponent.SwitchedEntity)
                    {
                        return false;
                    }

                    return true;
                }

                aStarComponent.DoSearch = false;
                return true;
            }

            return !aStarComponent.DoSearch;
        }
        private void resolvePath(AStarComponent aStarComponent, AStarNode currentNode_in, AStarNode goalNode_in, AStarGraph graph_in)
        {
            BinaryPriorityQueue<AStarNode> openset =
                new BinaryPriorityQueue<AStarNode>(AStarNode.sortFScoreAscending());
            AStarNode currentNode = currentNode_in;
            AStarNode goalNode = goalNode_in;
            AStarGraph graph = graph_in;

            if (currentNode != null)
            {
                aStarComponent.ClearPath();

                for (int i = 0; i < graph.Nodes.Count; i++)
                {
                    graph.Nodes[i].Initialize();
                }

                openset.Push(currentNode);
                currentNode.FScore = this.getFScore(currentNode, goalNode);

                while (openset.Count > 0)
                {
                    AStarNode current;

                    current = openset.Pop();

                    List<AStarNode> neighbors = current.GetNeighbors();

                    if (current.GetPosition() == goalNode.GetPosition())
                    {
                        AStarNode node = current;

                        while (node.GetOptimalPathParent() != null)
                        {
                            aStarComponent.Path.Push(node);
                            node = node.GetOptimalPathParent();
                        }

                        //aStarComponent.Path.Push(node);

                        break;
                    }

                    current.Processed = true;

                    for (int i = 0; i < neighbors.Count; i++)
                    {
                        AStarNode neighbor = neighbors[i];

                        if (!neighbor.Processed)
                        {
                            float tentative_g_score =
                                current.GScore + (current.GetPosition() - neighbor.GetPosition()).Length();

                            if (!neighbor.Visited || tentative_g_score <= neighbor.GScore)
                            {
                                neighbor.SetOptimalPathParent(current);
                                neighbor.GScore = tentative_g_score;
                                neighbor.FScore = neighbor.GScore + this.getFScore(neighbor, goalNode);

                                if (!neighbor.Visited)
                                {
                                    neighbor.Visited = true;
                                }
                                openset.Push(neighbor);
                            }
                        }
                    }
                }
            }
        }
Пример #3
0
        public static GameObject BuildComputerControlledEntity(
            Game1 game,
            int playerNumber,
            Color playerColor,
            Vector2 position,
            float rotation,
            Vector2 scale,
            float maxGroundSpeed,
            float maxAirSpeed,
            SpriteType spriteType,
            List<Shape> boundingBoxes,
            Dictionary<ActionDefinition, ActionInfo> actionsInformationList,
            GameObject opponent)
        {
            GameObject entity = DynamicEntityFactory.BuildCharacterEntity(
                game,
                playerNumber,
                playerColor,
                position,
                rotation,
                scale,
                maxGroundSpeed,
                maxAirSpeed,
                spriteType,
                boundingBoxes,
                actionsInformationList
                );

            AIControllerComponent aiComponent = new AIControllerComponent(entity);
            AStarComponent aStarComponent = new AStarComponent(entity);
            BehaviourComponent behaviourComponent = new BehaviourComponent(
                entity,
                BehaviourFactory.Easy(
                    game,
                    entity,
                    opponent
                ));
            SoundComponent soundComponent = new SoundComponent(entity);

            entity.AddComponent(aiComponent);
            entity.AddComponent(aStarComponent);
            entity.AddComponent(behaviourComponent);

            return entity;
        }