示例#1
0
        /*************************************************************************************************************************/

        /// <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)
        {
            physics.Update((float)gameTime.ElapsedGameTime.TotalSeconds);
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                camera.LookRight();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                camera.LookLeft();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                camera.LookUp();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Down))
            {
                camera.LookDown();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.A))
            {
                camera.MoveLeft();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.D))
            {
                camera.MoveRight();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.W))
            {
                camera.MoveForward();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.S))
            {
                camera.MoveBackward();
            }

            camera.UpdateCamera();
            base.Update(gameTime);
        }
示例#2
0
        /*************************************************************************************************************************/

        /// <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 ((ship.IsAlive && ((float)gameTime.TotalGameTime.TotalSeconds - gameStartTime) > (Score.level * GameConstants.secondsPerLevel)))
            {
                Score.LevelUp();
            }
            if (!ship.IsAlive)
            {
                asteroids.Reset();
            }
            else
            {
                nextLevelUpTime = GameConstants.secondsPerLevel - ((int)(gameTime.TotalGameTime.TotalSeconds - gameStartTime) % GameConstants.secondsPerLevel);
            }

            // Update physics and audio engines
            physics.Update((float)gameTime.ElapsedGameTime.TotalSeconds);
            audioEngine.Update();

            // Update game constructs
            bullets.Update();
            asteroids.Update();
            perimeter.Update();
            explosions.Update(gameTime);

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            bool noInput = true;

            if (Keyboard.GetState().IsKeyDown(Keys.Tab) && !tabDownLastUpdate)
            {
                GameConstants.drawScore = !GameConstants.drawScore;
                tabDownLastUpdate       = true;
            }
            else if (!Keyboard.GetState().IsKeyDown(Keys.Tab))
            {
                tabDownLastUpdate = false;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.A) || Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                PlayEngineSound();
                ship.Left(GameConstants.movementAmount);
                noInput = false;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.D) || Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                PlayEngineSound();
                ship.Right(GameConstants.movementAmount);
                noInput = false;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.W) || Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                PlayEngineSound();
                ship.Up(GameConstants.movementAmount);
                noInput = false;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.S) || Keyboard.GetState().IsKeyDown(Keys.Down))
            {
                PlayEngineSound();
                ship.Down(GameConstants.movementAmount);
                noInput = false;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.R) && !ship.IsAlive)
            {
                ship.Reset();
                Score.Reset();
                gameStartTime = (float)gameTime.TotalGameTime.TotalSeconds;
            }

            if (noInput && engineSound != null && engineSound.IsPlaying)
            {
                engineSound.Pause();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Space) && ship.IsAlive)
            {
                if (((float)gameTime.TotalGameTime.TotalMilliseconds - lastShot) > GameConstants.timeBetweenBullets)
                {
                    bullets.Shoot(new Vector3(ship.Position.X, ship.Position.Y, ship.Position.Z - 100.0f));
                    soundBank.PlayCue("tx0_fire1");
                    lastShot = (float)gameTime.TotalGameTime.TotalMilliseconds;
                }
            }
            if (!asteroids.IsFull() && ((float)gameTime.TotalGameTime.TotalMilliseconds - lastAsteroid) > GameConstants.timeBetweenAsteroids)
            {
                asteroids.AddAsteroid();
                lastAsteroid = (float)gameTime.TotalGameTime.TotalMilliseconds;
            }

            base.Update(gameTime);
        }
示例#3
0
        /// <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)
        {
            physics.Update((float)gameTime.ElapsedGameTime.TotalSeconds);

            //-------------------------------------------------------------------------------------
            // Permanent controls (controls that are available regardless of game mode
            //-------------------------------------------------------------------------------------

            if (controls.Exit())
            {
                this.Exit();
            }
            if (controls.SwitchToDebug())
            {
                gameMode = GameMode.Debug;
            }
            if (controls.Reset() && !resetLastUpdate)
            {
                gameMode = GameVariables.initialGameMode;
                controls = new PlayerControls();
                camera.Reset();
                balls.Reset();
                camera.TargetBall = balls.CueBall;
                gameMode          = GameMode.Aim;
                score             = new Score();
                resetLastUpdate   = true;
            }
            else if (!controls.Reset() && resetLastUpdate)
            {
                resetLastUpdate = false;
            }

            if (controls.SwitchPerspective() && !switchPerspectiveLastUpdate)
            {
                GameVariables.orthographicProj = !GameVariables.orthographicProj;
                switchPerspectiveLastUpdate    = true;
            }
            else if (!controls.SwitchPerspective() && switchPerspectiveLastUpdate)
            {
                switchPerspectiveLastUpdate = false;
            }

            controls.Mode = gameMode;

            //-------------------------------------------------------------------------------------
            // Aiming and shooting
            //-------------------------------------------------------------------------------------

            if (gameMode == GameMode.Aim)
            {
                camera.Mode = CameraMode.RotationalAim;

                if (controls.FastAdjustment())
                {
                    GameVariables.angleAdjustment = GameVariables.fastAngleAdjustment;
                }
                else
                {
                    GameVariables.angleAdjustment = GameVariables.slowAngleAdjustment;
                }

                if (balls.CueBall.IsOutOfCube())
                {
                    balls.CueBall.Position = GameVariables.cueBallPosition;
                    score.CueBallSunk();
                }

                if (controls.AimUp() && !shooting)
                {
                    if (controls.SlowDown())
                    {
                        camera.LookUp((float)gameTime.ElapsedGameTime.TotalSeconds);
                    }
                    else
                    {
                        camera.LookUp(GameVariables.speedMultipier * (float)gameTime.ElapsedGameTime.TotalSeconds);
                    }
                }

                if (controls.AimDown() && !shooting)
                {
                    if (controls.SlowDown())
                    {
                        camera.LookDown((float)gameTime.ElapsedGameTime.TotalSeconds);
                    }
                    else
                    {
                        camera.LookDown(GameVariables.speedMultipier * (float)gameTime.ElapsedGameTime.TotalSeconds);
                    }
                }

                if (controls.AimLeft() && !shooting)
                {
                    if (controls.SlowDown())
                    {
                        camera.LookLeft((float)gameTime.ElapsedGameTime.TotalSeconds);
                    }
                    else
                    {
                        camera.LookLeft(GameVariables.speedMultipier * (float)gameTime.ElapsedGameTime.TotalSeconds);
                    }
                }

                if (controls.AimRight() && !shooting)
                {
                    if (controls.SlowDown())
                    {
                        camera.LookRight((float)gameTime.ElapsedGameTime.TotalSeconds);
                    }
                    else
                    {
                        camera.LookRight(GameVariables.speedMultipier * (float)gameTime.ElapsedGameTime.TotalSeconds);
                    }
                }

                if (controls.ZoomIn() && !shooting)
                {
                    if (controls.SlowDown())
                    {
                        camera.MoveForward((float)gameTime.ElapsedGameTime.TotalSeconds);
                    }
                    else
                    {
                        camera.MoveForward(GameVariables.speedMultipier * (float)gameTime.ElapsedGameTime.TotalSeconds);
                    }
                }

                if (controls.ZoomOut() && !shooting)
                {
                    if (controls.SlowDown())
                    {
                        camera.MoveBackward((float)gameTime.ElapsedGameTime.TotalSeconds);
                    }
                    else
                    {
                        camera.MoveBackward(GameVariables.speedMultipier * (float)gameTime.ElapsedGameTime.TotalSeconds);
                    }
                }

                if (controls.Shoot())
                {
                    if (!shooting)
                    {
                        shooting = true;
                    }
                    shotPower += GameVariables.shotPowerIncreasePerSecond * (float)gameTime.ElapsedGameTime.TotalSeconds;
                    if (shotPower > 1.0f)
                    {
                        shotPower = 1.0f;
                    }
                }
                else
                {
                    if (shooting)
                    {
                        Vector3 direction = camera.CameraFocusOn - camera.CameraPosition;
                        direction.Normalize();

                        Vector3 velocity = direction * GameVariables.defaultBallSpeed * shotPower;

                        balls.CueBall.Velocity = velocity;

                        gameMode    = GameMode.Observe;
                        camera.Mode = CameraMode.Observe;
                        shooting    = false;
                        shotPower   = 0.0f;
                        score.ShotTaken();
                    }
                }
            }

            //-------------------------------------------------------------------------------------
            // Simulation
            //-------------------------------------------------------------------------------------

            else if (gameMode == GameMode.Observe)
            {
                camera.Mode = CameraMode.Observe;

                // Check for sunk balls
                Ball[] ballsOut = balls.BallsOutOfCube();
                foreach (Ball ball in ballsOut)
                {
                    score.BallSunk(ball.Number);
                    balls.Remove(ball);
                }
                if (balls.CueBall.IsOutOfCube())
                {
                    balls.CueBall.Stop();
                }

                // Check whether balls are slow enough to stop simulation
                if (balls.IsStopped())
                {
                    balls.Stop();
                    gameMode    = GameMode.Aim;
                    camera.Mode = CameraMode.RotationalAim;
                }
            }

            //-------------------------------------------------------------------------------------
            // Debug mode
            //-------------------------------------------------------------------------------------

            else if (gameMode == GameMode.Debug)
            {
                if (controls.DebugSwitchToSimpleCamera())
                {
                    camera.Mode = CameraMode.Simple;
                }
                else if (controls.DebugSwitchToAimCamera())
                {
                    camera.Mode = CameraMode.RotationalAim;
                }
                else if (controls.DebugSwitchToObserveCamera())
                {
                    camera.Mode = CameraMode.Observe;
                }

                if (controls.DebugMoveForward())
                {
                    camera.MoveForward((float)gameTime.ElapsedGameTime.TotalSeconds);
                }

                if (controls.DebugMoveBack())
                {
                    camera.MoveBackward((float)gameTime.ElapsedGameTime.TotalSeconds);
                }

                if (controls.DebugMoveLeft())
                {
                    camera.MoveLeft((float)gameTime.ElapsedGameTime.TotalSeconds);
                }

                if (controls.DebugMoveRight())
                {
                    camera.MoveRight((float)gameTime.ElapsedGameTime.TotalSeconds);
                }

                if (controls.DebugMoveUp())
                {
                    camera.MoveUp((float)gameTime.ElapsedGameTime.TotalSeconds);
                }

                if (controls.DebugMoveDown())
                {
                    camera.MoveDown((float)gameTime.ElapsedGameTime.TotalSeconds);
                }

                if (controls.DebugLookUp())
                {
                    camera.LookUp((float)gameTime.ElapsedGameTime.TotalSeconds);
                }

                if (controls.DebugLookDown())
                {
                    camera.LookDown((float)gameTime.ElapsedGameTime.TotalSeconds);
                }

                if (controls.DebugLookLeft())
                {
                    camera.LookLeft((float)gameTime.ElapsedGameTime.TotalSeconds);
                }

                if (controls.DebugLookRight())
                {
                    camera.LookRight((float)gameTime.ElapsedGameTime.TotalSeconds);
                }
            }

            camera.UpdateCamera();

            base.Update(gameTime);
        }