Пример #1
0
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            fontPosition = new Vector2(_position.X - 720, _position.Y - 380);   // Font position set to follow player

            MouseState mouse = Mouse.GetState();

            IsMouseVisible = true;

            //rotation = (float)Math.Atan2(distance.Y, distance.X);     Set for player to look at mouse and rotate with it accordingly



            if (Keyboard.GetState().IsKeyDown(Keys.A))
            {
                rotation -= 0.1f;       // Rotates ship to the left when A is pressed
            }
            if (Keyboard.GetState().IsKeyDown(Keys.D))
            {
                rotation += 0.1f;       // Rotates ship to the right when D is pressed
            }


            _rectangle = new Rectangle((int)_position.X, (int)_position.Y, _texture.Width, _texture.Height); // Rectangle is set to player position and texture size
            _position  = _velocity + _position;
            _origin    = new Vector2(_rectangle.Width / 2, _rectangle.Height / 2);                           // Center of player texture



            switch (currentGameState)
            {
            case gameStates.Waiting:

                camera.center = new Vector2(0, 0);

                if (Keyboard.GetState().IsKeyDown(Keys.D1))      // If 1 is pressed in character selection menu
                {
                    currentGameState = gameStates.InGame;
                    _texture         = Content.Load <Texture2D>("Sprites/Player"); // Sets player texture to this
                    ammoFont         = 15;                                         // This is there to keep count of current ammo
                    _position        = new Vector2(300, 250);
                }

                if (Keyboard.GetState().IsKeyDown(Keys.D2))     // If 2 is pressed in character selection menu
                {
                    currentGameState = gameStates.InGame;
                    _texture         = Content.Load <Texture2D>("Sprites/Player2"); // Sets player texture to this
                    ammoFont         = 10;
                    _position        = new Vector2(400, 350);                       // Sets position
                }
                if (Keyboard.GetState().IsKeyDown(Keys.D3))
                {
                    currentGameState = gameStates.InGame;
                    _texture         = Content.Load <Texture2D>("Sprites/Player3");
                    ammoFont         = 3;
                    _position        = new Vector2(500, 450);
                }
                if (Keyboard.GetState().IsKeyDown(Keys.D4))
                {
                    currentGameState = gameStates.InGame;
                    _texture         = Content.Load <Texture2D>("Sprites/Player4");
                    ammoFont         = 30;
                    _position        = new Vector2(600, 550);
                }
                break;

            case gameStates.InGame:

                if (Keyboard.GetState().IsKeyDown(Keys.W))
                {
                    _velocity.X = (float)Math.Cos(rotation) * tangentialVelocity;       // Lets the player move forward according to the direction it is facing
                    _velocity.Y = (float)Math.Sin(rotation) * tangentialVelocity;
                }
                else if (_velocity != Vector2.Zero)
                {
                    Vector2 i = _velocity;

                    _velocity = i -= friction * i;     // Lets the ship drift for a while after W key has been let go
                }
                if (Keyboard.GetState().IsKeyDown(Keys.Space) && pastKey.IsKeyUp(Keys.Space))
                {
                    ammoFont--;
                    if (ammoFont <= 0)          // Changes ammo count for font
                    {
                        ammoFont = 0;
                    }
                    Shooting();     // Updates the Shooting method
                }
                if (Keyboard.GetState().IsKeyDown(Keys.Enter))
                {
                    currentGameState = gameStates.Pause;
                }
                break;

            case gameStates.Pause:

                pauseMenu.Update(gameTime, this);
                break;

            case gameStates.Main:

                mainMenu.Update(this);
                break;
            }


            pastKey = Keyboard.GetState();  // Updates the variable
            UpdateProjectiles();

            camera.Update(gameTime, this);

            #region Boundaries
            if (_position.X < -275)
            {
                _position.X = -275;
            }
            if (_position.Y < -510)          // Keeps the player from going off screen
            {
                _position.Y = -510;
            }

            if (_position.X > 2525)
            {
                _position.X = 2525;
            }

            if (_position.Y > 1500)
            {
                _position.Y = 1500;
            }
            #endregion

            base.Update(gameTime);
        }