示例#1
0
文件: Game.cs 项目: mshurst/BotGame
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            KeyboardState newKeyState = Keyboard.GetState();

            foreach (ToggleAction ta in toggleActions)
            {
                if (oldKeyboardState.IsKeyUp(ta.Key) && newKeyState.IsKeyDown(ta.Key))
                {
                    ta.IsActive = !ta.IsActive;
                }
            }

            if (oldKeyboardState.IsKeyDown(Keys.P) && newKeyState.IsKeyUp(Keys.P))
            {
                SimulateHit(spriteBatch);
            }

            if (oldKeyboardState.IsKeyUp(Keys.NumPad1) && newKeyState.IsKeyDown(Keys.NumPad1))
            {
                selectedBot = allBots[0];
            }
            else if (oldKeyboardState.IsKeyUp(Keys.NumPad2) && newKeyState.IsKeyDown(Keys.NumPad2))
            {
                selectedBot = allBots[1];
            }

            if (oldKeyboardState.IsKeyDown(Keys.Space) && newKeyState.IsKeyUp(Keys.Space))
            {
                bot1.Fire();
            }

            float frameRate = (float)gameTime.ElapsedGameTime.TotalSeconds;

            bot1.Update(frameRate);
            bot2.Update(frameRate);

            oldKeyboardState = newKeyState;

            shotManager.Update(frameRate);

            base.Update(gameTime);
        }