Exemple #1
0
        public Main()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferHeight = BackBufferHeight;
            graphics.PreferredBackBufferWidth = BackBufferWidth;
            Window.Title = "Pathfinding Profiler v1.0";
            Content.RootDirectory = "content";
            this.IsMouseVisible = true;

            // Set frame rate
            TargetElapsedTime = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / TargetFrameRate);

            // Add Components
            menu = new ProfilerMenu(this);
            input = new InputHandler(this);
            levelHandler = new LevelHandler(this);

            GameUtils.AddUtil<GameComponentCollection>(Components);
            GameUtils.GetUtil<GameComponentCollection>().Add(menu);
            GameUtils.GetUtil<GameComponentCollection>().Add(input);
            GameUtils.GetUtil<GameComponentCollection>().Add(levelHandler);

            levelHandler.Enabled = false;
            levelHandler.Visible = false;

            SetState(typeof(ProfilerMenu));
        }
        /// <summary>
        /// A background worker, used to run testing on a background thread.
        /// </summary>
        protected override void OnDoWork(DoWorkEventArgs e)
        {
            base.OnDoWork(e);

            for (int i = 0; i < config.NumberOfTestRuns; i++)
            {
                if (!CheckCancellation())
                {
                    // Calculate positions for this test run
                    Coord2 targetPos = new Coord2(0, 0);
                    Coord2 botPos    = new Coord2(0, 0);

                    // Assign a random bot position
                    do
                    {
                        botPos.X = rand.Next(0, LevelHandler.Level.Map.GridSize);
                        botPos.Y = rand.Next(0, LevelHandler.Level.Map.GridSize);
                    } while (!LevelHandler.Level.Map.ValidPosition(botPos));

                    // Assign a target position the correct distance from the bot
                    do
                    {
                    } while (!LevelHandler.Level.Map.ValidPosition(targetPos));

                    // Run test on the loaded level
                    results.Add(LevelHandler.RunTest(config.Algorithm, new Coord2(0, 0), new Coord2(0, 0)));

                    // Report the thread's current progress
                    ReportProgress((i / config.NumberOfTestRuns) * 100);
                }
                else
                {
                    break;
                }
            }
        }
        /// <summary>
        /// Update function for this component.
        /// </summary>
        public override void Update(GameTime gameTime)
        {
            if (isActive)
            {
                foreach (Button b in buttons)
                {
                    b.Update();

                    if (b.IsPressed)
                    {
                        if (b.Command == MenuCommand.OpenDijkstra || b.Command == MenuCommand.OpenAStar || b.Command == MenuCommand.OpenScentMap)
                        {
                            if (LoadMap())
                            {
                                if (b.Command == MenuCommand.OpenDijkstra)
                                {
                                    LevelHandler.SetPathfindingAlgorithm(PathfinderAlgorithm.Dijkstra);
                                }
                                else if (b.Command == MenuCommand.OpenAStar)
                                {
                                    LevelHandler.SetPathfindingAlgorithm(PathfinderAlgorithm.AStar);
                                }
                                else if (b.Command == MenuCommand.OpenScentMap)
                                {
                                    LevelHandler.SetPathfindingAlgorithm(PathfinderAlgorithm.ScentMap);
                                }
                                else
                                {
                                    Console.WriteLine("Menu.cs: Error, attempted to set unrecognized pathfinding algorithm. Defaulting to Dijkstra.");
                                    LevelHandler.SetPathfindingAlgorithm(PathfinderAlgorithm.Dijkstra);
                                }
                                Main.SetState(typeof(LevelHandler));
                            }
                        }
                        else if (b.Command == MenuCommand.OpenMapFile)
                        {
                            OpenMap();
                        }
                        else if (b.Command == MenuCommand.OpenTestConfig)
                        {
                            if (LoadMap())
                            {
                                RunTestConfig();
                            }
                        }
                        else if (b.Command == MenuCommand.OpenGenerateMap)
                        {
                            RunGenerateMap();
                        }
                        else
                        {
                            Console.WriteLine("Unrecognized menu button command called.\n");
                        }
                    }
                }
            }
            else
            {
                if (InputHandler.IsMouseButtonPressed(MouseButton.LeftButton) && game.IsActive)
                {
                    if (mapGenActive)
                    {
                        if (Form.ActiveForm != mapGen)
                        {
                            mapGen.Activate();
                            SystemSounds.Exclamation.Play();
                        }
                    }
                    if (configTestActive && !testActive)
                    {
                        if (Form.ActiveForm != testConfigBox)
                        {
                            testConfigBox.Activate();
                            SystemSounds.Exclamation.Play();
                        }
                    }
                }
            }

            base.Update(gameTime);
        }