Exemplo n.º 1
0
        public override void Update(GameTime gameTime)
        {
            level.Update(gameTime);

            if (Game.IsActive)
            {
                // Check if user wishes to return to menu
                if (InputHandler.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Escape))
                {
                    if (MessageBox.Show("Return to the menu?", "Return to Menu", MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        Main.SetState(typeof(ProfilerMenu));
                        SetBotPosition(new Coord2(0, 0));
                        SetPlayerPosition(new Coord2(1, 0));
                    }
                }

                // Check for mouse clicks
                if (InputHandler.IsMouseInWindow(GraphicsDevice.Viewport.Bounds))
                {
                    // Check for mouse clicks
                    if (InputHandler.IsMouseButtonPressed(MouseButton.LeftButton) || InputHandler.IsMouseButtonPressed(MouseButton.RightButton))
                    {
                        ClearAll();

                        Coord2 mp = new Coord2(InputHandler.MousePosition().X / level.Map.TileSize, InputHandler.MousePosition().Y / level.Map.TileSize);

                        if (level.Map.ValidPosition(mp))
                        {
                            if (InputHandler.IsMouseButtonPressed(MouseButton.LeftButton))
                            {
                                if (level.Bot.GridPosition != mp)
                                {
                                    level.SetPlayerPosition(mp);
                                }
                            }
                            else
                            {
                                if (level.Player.GridPosition != mp)
                                {
                                    level.SetBotPosition(mp);
                                }
                            }
                        }
                    }

                    // Update mouse position
                    mousePos = new Point(InputHandler.MousePosition().X / level.Map.TileSize, InputHandler.MousePosition().Y / level.Map.TileSize);
                }

                // Check for hide/show instructions
                if (InputHandler.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.Tab))
                {
                    showInstructions = !showInstructions;
                }
            }

            base.Update(gameTime);
        }
Exemplo n.º 2
0
        public void Update(GameTime gameTime)
        {
            // Handle player input for movement
            HandlePlayerMovement();

            // Update the pathfinder
            if (map.pathfinder.GetAlgorithm() == PathfinderAlgorithm.ScentMap)
            {
                map.pathfinder.Build(bot.GridPosition, player.GridPosition);
            }
            else
            {
                if (InputHandler.IsKeyPressed(Keys.Enter))
                {
                    map.pathfinder.Build(bot.GridPosition, player.GridPosition);
                }
            }

            // Update bot and player
            bot.Update(gameTime, map, player);
            player.Update(gameTime, map);
        }
Exemplo n.º 3
0
        public void Build(Coord2 startPos, Coord2 targetPos)
        {
            if (InputHandler.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.Enter))
            {
                this.startPos  = startPos;
                this.targetPos = targetPos;
                path           = new List <Coord2>();

                nodes = new NodeCollection(GridSize);

                // Initialize bot position
                nodes.Get(startPos.X, startPos.Y).cost = 0;
                bool firstLoop = true;

                while (nodes.Get(targetPos.X, targetPos.Y).closed == false)
                {
                    if (firstLoop)
                    {
                        currentLowestPos = startPos;
                        firstLoop        = false;
                    }
                    else
                    {
                        FindLowestCost(); // Find lowest cost
                    }
                    // Mark lowest cost as closed
                    nodes.Get(currentLowestPos.X, currentLowestPos.Y).closed = true;

                    // Find the neigbour positions
                    Coord2[] neighbours = GetNeighbours(currentLowestPos);

                    // Recalculate Costs
                    RecalculateCosts(neighbours, currentLowestPos);
                }

                // Trace the completed path
                TracePath();
            }
        }