/// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// 
        //������� ��� ��������� �������� ����������
        public void controlPlayer(GameTime gameTime)
        {
            KeyboardState keyboard = Keyboard.GetState();
            MouseState mst = Mouse.GetState();

            if (mst.X != prevMouseState.X || mst.Y != prevMouseState.Y)
                sightPosition = new Vector2(mst.X - cursorTexture.Width / 2, mst.Y - cursorTexture.Height / 2);
            prevMouseState = mst;

            //���������� �������� ���������� �� ������ ���� �����
            if (keyboard.IsKeyDown(Keys.Escape))
            {
                currentGameState = GameState.pause;
            }

            if (keyboard.IsKeyDown(Keys.A))
            {
                personPosition.X -= PersonMoveSpeed;
                background.BackgroundOffset -= 1;
                background.ParallaxOffset -= 2;
            }

            if (keyboard.IsKeyDown(Keys.D))
            {
                personPosition.X += PersonMoveSpeed;
                background.BackgroundOffset += 1;
                background.ParallaxOffset += 2;
            }

            if (keyboard.IsKeyDown(Keys.W))
            {
                personPosition.Y -= PersonMoveSpeed;

            }

            if (keyboard.IsKeyDown(Keys.S))
            {
                personPosition.Y += PersonMoveSpeed;

            }

            //���������� ������ ��� ��������� �������
            if (mst.LeftButton == ButtonState.Pressed || bullets.Count != 0)
            {
                time++;
                if (time == stime)
                {
                    time = 0;
                    float angle;
                    if (mst.X != ScreenSize.X)
                    {
                        //���������� ������� ������� ��� (�� ������)
                        angle = (float)Math.Atan((-personPosition.Y + mst.Y) / (mst.X - personPosition.X));
                        if (mst.X < personPosition.X)
                            angle += (float)Math.PI;
                    }
                    else
                    {
                        if (personPosition.Y < mst.Y)
                            angle = (float)Math.PI / 2;
                        else
                            angle = -(float)Math.PI / 2;
                    }
                    if (mst.LeftButton == ButtonState.Pressed)
                        add(angle);
                }

                //��������� ����, �� ������ �� ������� �������� ������
                rdelete();
                foreach (bullet item in bullets)
                {
                    item.Update(gameTime);
                }
                base.Update(gameTime);
            }

            //�������� ��������� ��������� ������� �� ������ �������� ������
            personPosition.X = MathHelper.Clamp(personPosition.X,
                safeBounds.Left - 10, safeBounds.Right + 10);

            personPosition.Y = MathHelper.Clamp(personPosition.Y,
                safeBounds.Top + 5, safeBounds.Bottom - 5);

            Matrix personTransform =
                Matrix.CreateTranslation(new Vector3(personPosition, 0.0f));

            //��������� ���������� ������ ������� ���� ������
            if (random.NextDouble() < BlockSpawnProbability)
            {
                Block newBlock = new Block();

                float x = (float)random.NextDouble() *
                     (Window.ClientBounds.Width - blockTexture.Width);

                newBlock.Position = new Vector2(x, -blockTexture.Height);

                //��������� ������ ������� �� ��
                newBlock.Rotation = (float)random.NextDouble() * MathHelper.TwoPi;
                blocks.Add(newBlock);
            }

            //������ ������ ���������� ������� ����� ��������� ���������
            Rectangle personRectangle =
                new Rectangle((int)personPosition.X, (int)personPosition.Y,
                personTexture.Width, personTexture.Height);

            for (int i = 0; i < blocks.Count - 1; i++)
            {
                // Animate this block falling
                if (blocks[i].Position.X < personPosition.X)
                    blocks[i].Position += new Vector2(2f, BlockFallSpeed);
                else if (blocks[i].Position.X > personPosition.X)
                    blocks[i].Position += new Vector2(-2f, BlockFallSpeed);
                else
                    blocks[i].Position += new Vector2(0f, BlockFallSpeed);

                blocks[i].Rotation += BlockRotateSpeed;

                Matrix blockTransform =
                    Matrix.CreateTranslation(new Vector3(-blockOrigin, 0.0f)) *
                    // Matrix.CreateScale(block.Scale) *  would go here
                    Matrix.CreateRotationZ(blocks[i].Rotation) *
                    Matrix.CreateTranslation(new Vector3(blocks[i].Position, 0.0f));

                if (blocks[i].Position.Y > Window.ClientBounds.Height + blockOrigin.Length())
                {
                    blocks.RemoveAt(i);
                    i--;
                }
            }

            //�������� �������� ������ ����������� ����� �� ������� �������
            rotation = TurnToFace(new Vector2(personPosition.X - personTexture.Width, personPosition.Y + personTexture.Height),
                new Vector2(sightPosition.X - cursorTexture.Width, sightPosition.Y + cursorTexture.Height), rotation, 1f);

            //���������� ������� ��������� � ��������, ��������� ������,
            //����������� ����, ������ ������'� ���������, ������ ���� ���� ���
            Rectangle ship = new Rectangle((int)personPosition.X, (int)personPosition.Y, personTexture.Width, personTexture.Height);
            for (int i = 0; i < blocks.Count - 1; i++)
            {
                Rectangle blockRect = new Rectangle((int)blocks[i].Position.X, (int)blocks[i].Position.Y, blockTexture.Width, blockTexture.Height);
                if (blockRect.Intersects(ship))
                {
                    mHealth -= 10;
                    Score += 100;
                    AddExplosion(blocks[i].Position);
                    explosionSound.Play();
                    blocks.RemoveAt(i);
                    this.EndDraw();
                    if (mHealth <= 0 && ship.Intersects(blockRect))
                    {
                        AddExplosion(blocks[i].Position);
                        AddExplosion(personPosition);
                        blocks.RemoveAt(i);
                        currentGameState = GameState.gameOver;
                    }
                }
            }

            //���������� ��������� ���, ����������� ����, ��������� ��������� ���, �  ������ ������ �������
            for (int i = 0; i <= bullets.Count - 1; i++)
            {
                Rectangle bulletRect = new Rectangle((int)bullets[i].values.X, (int)bullets[i].values.Y, texture.Width, texture.Height);

                for (int j = 0; j <= blocks.Count - 1; j++)
                {
                    Rectangle blockRect = new Rectangle((int)blocks[j].Position.X, (int)blocks[j].Position.Y, blockTexture.Width, blockTexture.Height);

                    if (blockRect.Y > Window.ClientBounds.Height)
                        blocks.RemoveAt(j);

                    if (blockRect.Intersects(bulletRect))
                    {
                        blocks[j].Health -= bullets[i].Damage;
                        bullets.RemoveAt(i);
                        if (blocks[j].Health <= 0)
                        {
                            AddExplosion(blocks[j].Position);
                            explosionSound.Play();
                            blocks.RemoveAt(j);
                            Score += 100;
                            BlockSpawnProbability += 0.0007f;
                        }

                    }

                }
            }

            UpdateExplosions(gameTime);
            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)
        {
            // Get input
            KeyboardState keyboard = Keyboard.GetState();
            GamePadState gamePad = GamePad.GetState(PlayerIndex.One);

            // Allows the game to exit
            if (gamePad.Buttons.Back == ButtonState.Pressed ||
                keyboard.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            // Move the player left and right with arrow keys or d-pad
            if (keyboard.IsKeyDown(Keys.Left) ||
                gamePad.DPad.Left == ButtonState.Pressed)
            {
                personPosition.X -= PersonMoveSpeed;
            }
            if (keyboard.IsKeyDown(Keys.Right) ||
                gamePad.DPad.Right == ButtonState.Pressed)
            {
                personPosition.X += PersonMoveSpeed;
            }

            // Prevent the person from moving off of the screen
            personPosition.X = MathHelper.Clamp(personPosition.X,
                safeBounds.Left, safeBounds.Right - personTexture.Width);

            // Update the person's transform
            Matrix personTransform =
                Matrix.CreateTranslation(new Vector3(personPosition, 0.0f));

            // Spawn new falling blocks
            if (random.NextDouble() < BlockSpawnProbability)
            {
                Block newBlock = new Block();

                // at a random position just above the screen
                float x = (float)random.NextDouble() *
                    (Window.ClientBounds.Width - blockTexture.Width);
                newBlock.Position = new Vector2(x, -blockTexture.Height);

                // with a random rotation
                newBlock.Rotation = (float)random.NextDouble() * MathHelper.TwoPi;

                blocks.Add(newBlock);
            }

            // Get the bounding rectangle of the person
            Rectangle personRectangle =
                new Rectangle((int)personPosition.X, (int)personPosition.Y,
                personTexture.Width, personTexture.Height);

            // Update each block
            personHit = false;
            for (int i = 0; i < blocks.Count; i++)
            {
                // Animate this block falling
                blocks[i].Position += new Vector2(0.0f, BlockFallSpeed);
                blocks[i].Rotation += BlockRotateSpeed;

                // Build the block's transform
                Matrix blockTransform =
                    Matrix.CreateTranslation(new Vector3(-blockOrigin, 0.0f)) *
                    // Matrix.CreateScale(block.Scale) *  would go here
                    Matrix.CreateRotationZ(blocks[i].Rotation) *
                    Matrix.CreateTranslation(new Vector3(blocks[i].Position, 0.0f));

                // Calculate the bounding rectangle of this block in world space
                Rectangle blockRectangle = CalculateBoundingRectangle(
                         new Rectangle(0, 0, blockTexture.Width, blockTexture.Height),
                         blockTransform);

                // The per-pixel check is expensive, so check the bounding rectangles
                // first to prevent testing pixels when collisions are impossible.
                if (personRectangle.Intersects(blockRectangle))
                {
                    // Check collision with person
                    if (IntersectPixels(personTransform, personTexture.Width,
                                        personTexture.Height, personTextureData,
                                        blockTransform, blockTexture.Width,
                                        blockTexture.Height, blockTextureData))
                    {
                        personHit = true;
                    }
                }

                // Remove this block if it have fallen off the screen
                if (blocks[i].Position.Y >
                    Window.ClientBounds.Height + blockOrigin.Length())
                {
                    blocks.RemoveAt(i);

                    // When removing a block, the next block will have the same index
                    // as the current block. Decrement i to prevent skipping a block.
                    i--;
                }
            }

            base.Update(gameTime);
        }