示例#1
0
        protected void GamePlayUpdate(GameTime gameTime, KeyboardState keyboard)
        {
            // Spawn EnemyTank if appropriate
            if (enemyTankCanSpawn && enemyTanksData.Count > 0)
            {
                PopUpEnemyTank popUpEnemyTank = new PopUpEnemyTank(Content, GameConstants.TILE_WIDTH * enemyTankRespawnPlace[enemyTankRespawnCounter], 0);

                popUpEnemyTanks.Add(popUpEnemyTank);

                enemyTankCanSpawn = false;
            }

            if (!enemyTankCanSpawn)
            {
                enemyTankElapsedSpawnTime += gameTime.ElapsedGameTime.Milliseconds;
                if (enemyTanks.Count < GameConstants.ENEMY_TANK_MAX_COUNT && enemyTankElapsedSpawnTime > enemyTankRespawnTime)
                {
                    enemyTankElapsedSpawnTime = 0;
                    enemyTankCanSpawn         = true;
                }
            }


            foreach (Explosion explosion in explosions)
            {
                explosion.Update(gameTime);
            }

            if (bonusTimerIsActive)
            {
                elapsedGameTimeBonusTimer -= gameTime.ElapsedGameTime.Milliseconds;
                if (elapsedGameTimeBonusTimer < 0)
                {
                    elapsedGameTimeBonusTimer = 0;
                    bonusTimerIsActive        = false;
                }
            }

            if (bonusEagleArmorIsActive)
            {
                BonusEagleArmor(gameTime);
            }


            foreach (EnemyTank enemyTank in enemyTanks)
            {
                enemyTank.CanMoveLeft  = enemyTank.MovementSpeed;
                enemyTank.CanMoveRight = enemyTank.MovementSpeed;
                enemyTank.CanMoveUp    = enemyTank.MovementSpeed;
                enemyTank.CanMoveDown  = enemyTank.MovementSpeed;

                enemyTank.RefreshMovementSpeed();

                // проверить возможность движения enemyTank во всех направлениях

                foreach (Tile tile in tiles)
                {
                    // et Left
                    if (!tile.Crossable && (tile.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Left - enemyTank.MovementSpeed, enemyTank.CollisionRectangle.Top) ||
                                            tile.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Left - enemyTank.MovementSpeed, enemyTank.CollisionRectangle.Bottom - 1)))
                    {
                        if ((enemyTank.CollisionRectangle.Left - tile.CollisionRectangle.Right) < enemyTank.CanMoveLeft)
                        {
                            enemyTank.CanMoveLeft = enemyTank.CollisionRectangle.Left - tile.CollisionRectangle.Right;
                        }
                    }
                    // et Right
                    if (!tile.Crossable && (tile.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Right + enemyTank.MovementSpeed, enemyTank.CollisionRectangle.Top) ||
                                            tile.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Right + enemyTank.MovementSpeed, enemyTank.CollisionRectangle.Bottom - 1)))
                    {
                        if ((tile.CollisionRectangle.Left - enemyTank.CollisionRectangle.Right) < enemyTank.CanMoveRight)
                        {
                            enemyTank.CanMoveRight = tile.CollisionRectangle.Left - enemyTank.CollisionRectangle.Right;
                        }
                    }
                    // et Up
                    if (!tile.Crossable && (tile.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Left, enemyTank.CollisionRectangle.Top - enemyTank.MovementSpeed) ||
                                            tile.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Right - 1, enemyTank.CollisionRectangle.Top - enemyTank.MovementSpeed)))
                    {
                        if ((enemyTank.CollisionRectangle.Top - tile.CollisionRectangle.Bottom) < enemyTank.CanMoveUp)
                        {
                            enemyTank.CanMoveUp = enemyTank.CollisionRectangle.Top - tile.CollisionRectangle.Bottom;
                        }
                    }
                    // et Down
                    if (!tile.Crossable && (tile.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Left, enemyTank.CollisionRectangle.Bottom + enemyTank.MovementSpeed) ||
                                            tile.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Right - 1, enemyTank.CollisionRectangle.Bottom + enemyTank.MovementSpeed)))
                    {
                        if ((tile.CollisionRectangle.Top - enemyTank.CollisionRectangle.Bottom) < enemyTank.CanMoveDown)
                        {
                            enemyTank.CanMoveDown = tile.CollisionRectangle.Top - enemyTank.CollisionRectangle.Bottom;
                        }
                    }
                }


                // проверка et на столкновение с pt
                if (playerTank.IsActive)
                {
                    // et Left
                    if (playerTank.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Left - enemyTank.MovementSpeed, enemyTank.CollisionRectangle.Top) ||
                        playerTank.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Left - enemyTank.MovementSpeed, enemyTank.CollisionRectangle.Bottom - 1))
                    {
                        if ((enemyTank.CollisionRectangle.Left - playerTank.CollisionRectangle.Right) < enemyTank.CanMoveLeft)
                        {
                            enemyTank.CanMoveLeft = enemyTank.CollisionRectangle.Left - playerTank.CollisionRectangle.Right;
                        }
                    }
                    // et Right
                    if (playerTank.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Right + enemyTank.MovementSpeed, enemyTank.CollisionRectangle.Top) ||
                        playerTank.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Right + enemyTank.MovementSpeed, enemyTank.CollisionRectangle.Bottom - 1))
                    {
                        if ((playerTank.CollisionRectangle.Left - enemyTank.CollisionRectangle.Right) < enemyTank.CanMoveRight)
                        {
                            enemyTank.CanMoveRight = playerTank.CollisionRectangle.Left - enemyTank.CollisionRectangle.Right;
                        }
                    }
                    // et Up
                    if (playerTank.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Left, enemyTank.CollisionRectangle.Top - enemyTank.MovementSpeed) ||
                        playerTank.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Right - 1, enemyTank.CollisionRectangle.Top - enemyTank.MovementSpeed))
                    {
                        if ((enemyTank.CollisionRectangle.Top - playerTank.CollisionRectangle.Bottom) < enemyTank.CanMoveUp)
                        {
                            enemyTank.CanMoveUp = enemyTank.CollisionRectangle.Top - playerTank.CollisionRectangle.Bottom;
                        }
                    }
                    // et Down
                    if (playerTank.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Left, enemyTank.CollisionRectangle.Bottom + enemyTank.MovementSpeed) ||
                        playerTank.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Right - 1, enemyTank.CollisionRectangle.Bottom + enemyTank.MovementSpeed))
                    {
                        if ((playerTank.CollisionRectangle.Top - enemyTank.CollisionRectangle.Bottom) < enemyTank.CanMoveDown)
                        {
                            enemyTank.CanMoveDown = playerTank.CollisionRectangle.Top - enemyTank.CollisionRectangle.Bottom;
                        }
                    }
                }


                foreach (EnemyTank enemyTank2 in enemyTanks)
                {
                    // проверка et на столкновение с et
                    // et Left
                    if (enemyTank2.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Left - enemyTank.MovementSpeed, enemyTank.CollisionRectangle.Top) ||
                        enemyTank2.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Left - enemyTank.MovementSpeed, enemyTank.CollisionRectangle.Bottom - 1))
                    {
                        if ((enemyTank.CollisionRectangle.Left - enemyTank2.CollisionRectangle.Right) < enemyTank.CanMoveLeft)
                        {
                            enemyTank.CanMoveLeft = enemyTank.CollisionRectangle.Left - enemyTank2.CollisionRectangle.Right;
                        }
                    }
                    // et Right
                    if (enemyTank2.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Right + enemyTank.MovementSpeed, enemyTank.CollisionRectangle.Top) ||
                        enemyTank2.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Right + enemyTank.MovementSpeed, enemyTank.CollisionRectangle.Bottom - 1))
                    {
                        if ((enemyTank2.CollisionRectangle.Left - enemyTank.CollisionRectangle.Right) < enemyTank.CanMoveRight)
                        {
                            enemyTank.CanMoveRight = enemyTank2.CollisionRectangle.Left - enemyTank.CollisionRectangle.Right;
                        }
                    }
                    // et Up
                    if (enemyTank2.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Left, enemyTank.CollisionRectangle.Top - enemyTank.MovementSpeed) ||
                        enemyTank2.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Right - 1, enemyTank.CollisionRectangle.Top - enemyTank.MovementSpeed))
                    {
                        if ((enemyTank.CollisionRectangle.Top - enemyTank2.CollisionRectangle.Bottom) < enemyTank.CanMoveUp)
                        {
                            enemyTank.CanMoveUp = enemyTank.CollisionRectangle.Top - enemyTank2.CollisionRectangle.Bottom;
                        }
                    }
                    // et Down
                    if (enemyTank2.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Left, enemyTank.CollisionRectangle.Bottom + enemyTank.MovementSpeed) ||
                        enemyTank2.CollisionRectangle.Contains(enemyTank.CollisionRectangle.Right - 1, enemyTank.CollisionRectangle.Bottom + enemyTank.MovementSpeed))
                    {
                        if ((enemyTank2.CollisionRectangle.Top - enemyTank.CollisionRectangle.Bottom) < enemyTank.CanMoveDown)
                        {
                            enemyTank.CanMoveDown = enemyTank2.CollisionRectangle.Top - enemyTank.CollisionRectangle.Bottom;
                        }
                    }
                }

                enemyTank.Destination = new Vector2(playerTank.CollisionRectangleX, playerTank.CollisionRectangleY);

                enemyTank.Update(gameTime, bonusTimerIsActive);
            }


            playerTank.RefreshMovementSpeed();

            foreach (EnemyTank enemyTank in enemyTanks)
            {
                // проверить возможность движения playerTank во всех направлениях
                // pt Left
                if (enemyTank.CollisionRectangle.Contains(playerTank.CollisionRectangle.Left - GameConstants.PLAYER_TANK_MOVEMENT_SPEED, playerTank.CollisionRectangle.Top) ||
                    enemyTank.CollisionRectangle.Contains(playerTank.CollisionRectangle.Left - GameConstants.PLAYER_TANK_MOVEMENT_SPEED, playerTank.CollisionRectangle.Bottom - 1))
                {
                    if ((playerTank.CollisionRectangle.Left - enemyTank.CollisionRectangle.Right) < playerTank.CanMoveLeft)
                    {
                        playerTank.CanMoveLeft = playerTank.CollisionRectangle.Left - enemyTank.CollisionRectangle.Right;
                    }
                }
                // pt Right
                if (enemyTank.CollisionRectangle.Contains(playerTank.CollisionRectangle.Right + GameConstants.PLAYER_TANK_MOVEMENT_SPEED, playerTank.CollisionRectangle.Top) ||
                    enemyTank.CollisionRectangle.Contains(playerTank.CollisionRectangle.Right + GameConstants.PLAYER_TANK_MOVEMENT_SPEED, playerTank.CollisionRectangle.Bottom - 1))
                {
                    if ((enemyTank.CollisionRectangle.Left - playerTank.CollisionRectangle.Right) < playerTank.CanMoveRight)
                    {
                        playerTank.CanMoveRight = enemyTank.CollisionRectangle.Left - playerTank.CollisionRectangle.Right;
                    }
                }
                // pt Up
                if (enemyTank.CollisionRectangle.Contains(playerTank.CollisionRectangle.Left, playerTank.CollisionRectangle.Top - GameConstants.PLAYER_TANK_MOVEMENT_SPEED) ||
                    enemyTank.CollisionRectangle.Contains(playerTank.CollisionRectangle.Right - 1, playerTank.CollisionRectangle.Top - GameConstants.PLAYER_TANK_MOVEMENT_SPEED))
                {
                    if ((playerTank.CollisionRectangle.Top - enemyTank.CollisionRectangle.Bottom) < playerTank.CanMoveUp)
                    {
                        playerTank.CanMoveUp = playerTank.CollisionRectangle.Top - enemyTank.CollisionRectangle.Bottom;
                    }
                }
                // pt Down
                if (enemyTank.CollisionRectangle.Contains(playerTank.CollisionRectangle.Left, playerTank.CollisionRectangle.Bottom + GameConstants.PLAYER_TANK_MOVEMENT_SPEED) ||
                    enemyTank.CollisionRectangle.Contains(playerTank.CollisionRectangle.Right - 1, playerTank.CollisionRectangle.Bottom + GameConstants.PLAYER_TANK_MOVEMENT_SPEED))
                {
                    if ((enemyTank.CollisionRectangle.Top - playerTank.CollisionRectangle.Bottom) < playerTank.CanMoveDown)
                    {
                        playerTank.CanMoveDown = enemyTank.CollisionRectangle.Top - playerTank.CollisionRectangle.Bottom;
                    }
                }
            }

            playerTank.Update(gameTime, keyboard, soundBank, cuePlayerMove);


            foreach (Tile tile in tiles)
            {
                switch (playerTank.LastKey)
                {
                case "Left":
                    if (!tile.Crossable && tile.CollisionRectangle.Intersects(playerTank.CollisionRectangle))
                    {
                        playerTank.CollisionRectangleX -= playerTank.CollisionRectangle.Left - tile.CollisionRectangle.Right;
                    }
                    break;

                case "Right":
                    if (!tile.Crossable && tile.CollisionRectangle.Intersects(playerTank.CollisionRectangle))
                    {
                        playerTank.CollisionRectangleX -= playerTank.CollisionRectangle.Right - tile.CollisionRectangle.Left;
                    }
                    break;

                case "Up":
                    if (!tile.Crossable && tile.CollisionRectangle.Intersects(playerTank.CollisionRectangle))
                    {
                        playerTank.CollisionRectangleY -= playerTank.CollisionRectangle.Top - tile.CollisionRectangle.Bottom;
                    }
                    break;

                case "Down":
                    if (!tile.Crossable && tile.CollisionRectangle.Intersects(playerTank.CollisionRectangle))
                    {
                        playerTank.CollisionRectangleY -= playerTank.CollisionRectangle.Bottom - tile.CollisionRectangle.Top;
                    }
                    break;

                default:
                    break;
                }

                // столкновение пули со стеной
                foreach (Bullet bullet in bullets)
                {
                    if (tile.CollisionRectangle.Intersects(bullet.CollisionRectangle) && tile.TileNumber > 0 && tile.TileNumber < 17)
                    {
                        bullet.IsActive = false;

                        //взрыв
                        Explosion explosion = new Explosion(bulletSprite, bullet.CollisionRectangle.Center.X, bullet.CollisionRectangle.Center.Y);
                        explosions.Add(explosion);

                        if (!(tile.TileNumber == 16 && bullet.BulletPower == BulletPower.Low))     // броня не разбивается слабой пулей
                        {
                            tile.TileChangeState(GameData.TilesChangeState[bullet.Direction][tile.TileNumber]);
                            if (bullet.Type == BulletType.Player)
                            {
                                soundBank.PlayCue("w12_bullet2brick");
                            }
                        }
                        else
                        {
                            if (bullet.Type == BulletType.Player)
                            {
                                soundBank.PlayCue("w15_bullet2wall");
                            }
                        }
                    }

                    // уничтожение штаба
                    if (tile.TileNumber > 19 && tile.CollisionRectangle.Intersects(bullet.CollisionRectangle))
                    {
                        // конец игры
                        bullet.IsActive     = false;
                        gameState           = GameState.End;
                        globalGameState     = GameState.End;
                        playerTank.IsActive = false;

                        soundBank.PlayCue("w14_playerExplosion");
                    }
                }
            }

            // переход на следующий уровень
            if (enemyTanksData.Count == 0 && enemyTanks.Count == 0 && playerTank.Lives > 0)
            {
                // счетчик для автом окончания
                levelEndTimer += gameTime.ElapsedGameTime.Milliseconds;
                if (levelEndTimer > 4000)
                {
                    scoreScreen.UpdateData(currentLevel);
                    levelEndTimer = 0;

                    if (!cuePlayerMove.IsPaused)
                    {
                        cuePlayerMove.Pause();
                    }

                    gameState = GameState.ScoreScreen;
                }

                if (!cueEnemyMove.IsPaused)
                {
                    cueEnemyMove.Pause();
                }
            }

            foreach (Bullet bullet in bullets)
            {
                bullet.Update(gameTime, soundBank);
            }

            foreach (Bullet bullet in bullets)
            {
                // уничтожение playerTank
                if (playerTank.IsActive && bullet.Type == BulletType.Enemy && bullet.CollisionRectangle.Intersects(playerTank.CollisionRectangle))
                {
                    bullet.IsActive = false;

                    if (!playerTank.ShieldIsActive)
                    {
                        Explosion explosion = new Explosion(bulletSprite, playerTank.CollisionRectangle.Center.X, playerTank.CollisionRectangle.Center.Y);
                        explosions.Add(explosion);

                        soundBank.PlayCue("w14_playerExplosion");

                        playerTank.Lives -= 1;
                        scoreField.ChangeLives(playerTank.Lives);
                        playerTank.Visible  = false;
                        playerTank.IsActive = false;

                        if (playerTank.Lives > 0)
                        {
                            playerTank.Respawn("death");
                        }
                        else
                        {
                            playerTank.IsActive = false;

                            if (!cuePlayerMove.IsPaused)
                            {
                                cuePlayerMove.Pause();
                            }

                            gameState       = GameState.End;
                            globalGameState = GameState.End;
                        }
                    }
                }

                // end game
                if (gameState == GameState.End)
                {
                    foreach (Tile tile in tiles)
                    {
                        if (tile.TileNumber == 25)
                        {
                            Explosion explosion = new Explosion(bulletSprite, tile.CollisionRectangle.X, tile.CollisionRectangle.Y);
                            explosions.Add(explosion);
                        }
                        if (tile.TileNumber > 19 && tile.TileNumber < 28)
                        {
                            tile.TileChangeState(GameData.TilesChangeState["Left"][tile.TileNumber]);
                        }
                    }
                }

                // попадание пули во вражеский танк
                foreach (EnemyTank enemyTank in enemyTanks)
                {
                    if (bullet.Type == BulletType.Player && bullet.CollisionRectangle.Intersects(enemyTank.CollisionRectangle))
                    {
                        bullet.IsActive  = false;
                        enemyTank.Lives -= 1;
                        if (enemyTank.HasBonus)
                        {
                            soundBank.PlayCue("w4_popUpBonus");
                            bonuses.Clear();
                            Bonus bonus = new Bonus(bulletSprite);
                            bonuses.Add(bonus);

                            enemyTank.DisableBonus();
                        }

                        if (enemyTank.Lives == 0)
                        {
                            scoreScreen.DestroyedTanksCount[enemyTank.TankType] += 1;
                            scoreScreen.PlayerScore += enemyTank.TankType * 100 + 100;

                            Explosion explosion = new Explosion(bulletSprite, enemyTank.CollisionRectangle.Center.X, enemyTank.CollisionRectangle.Center.Y);
                            explosions.Add(explosion);
                            soundBank.PlayCue("w7_tankExplosion");
                        }
                        else
                        {
                            soundBank.PlayCue("w15_bullet2wall");
                        }
                    }
                }
            }

            // взятие бонуса
            foreach (Bonus bonus in bonuses)
            {
                bonus.Update(gameTime);

                if (bonus.CollisionRectangle.Intersects(playerTank.CollisionRectangle))
                {
                    if (bonus.Type == 5)
                    {
                        soundBank.PlayCue("w10_bonusLife");
                    }
                    else
                    {
                        soundBank.PlayCue("w2_pickBonus");
                        if (bonus.Type == 4)
                        {
                            soundBank.PlayCue("w7_tankExplosion");
                        }
                    }

                    scoreScreen.PlayerScore += 500;
                    BonusApply(bonus.Type);
                    bonus.IsActive = false;
                }
            }

            // столкновение двух пуль
            for (int i = 0; i < bullets.Count; i++)
            {
                for (int j = 0; j < bullets.Count; j++)
                {
                    if (i != j && bullets[i].CollisionRectangle.Intersects(bullets[j].CollisionRectangle))
                    {
                        bullets[i].IsActive = false;
                        bullets[j].IsActive = false;
                    }
                }
            }

            // мерцание при появлении вражеского танка
            foreach (PopUpEnemyTank popUpEnemyTank in popUpEnemyTanks)
            {
                popUpEnemyTank.Update(gameTime);
            }

            // появление вражеского танка
            for (int i = popUpEnemyTanks.Count - 1; i >= 0; i--)
            {
                if (popUpEnemyTanks[i].PopUpTimer == 0)
                {
                    popUpEnemyTanks.RemoveAt(i);
                    SpawnEnemyTank();
                }
            }

            // смена спрайта воды
            waterElapsedCooldownTime += gameTime.ElapsedGameTime.Milliseconds;
            if (waterElapsedCooldownTime > 800)
            {
                waterElapsedCooldownTime = 0;

                foreach (Tile tile in tiles)
                {
                    if (tile.TileNumber == 17)
                    {
                        tile.WaterChangeState(waterChangeSprite);
                    }
                }

                waterChangeSprite = waterChangeSprite ? false : true;
            }

            for (int i = enemyTanks.Count - 1; i >= 0; i--)
            {
                if (enemyTanks[i].Lives == 0)
                {
                    enemyTanks.RemoveAt(i);
                }
            }

            for (int i = tiles.Count - 1; i >= 0; i--)
            {
                if (!tiles[i].IsActive)
                {
                    tiles.RemoveAt(i);
                }
            }

            for (int i = bullets.Count - 1; i >= 0; i--)
            {
                if (!bullets[i].IsActive)
                {
                    bullets.RemoveAt(i);
                }
            }

            for (int i = explosions.Count - 1; i >= 0; i--)
            {
                if (explosions[i].Finished)
                {
                    explosions.RemoveAt(i);
                }
            }

            for (int i = bonuses.Count - 1; i >= 0; i--)
            {
                if (!bonuses[i].IsActive)
                {
                    bonuses.RemoveAt(i);
                }
            }
        }
示例#2
0
 private void GenerateRandomBonus()
 {
     CurrentBonus = _bonuses[GameRandom.RandNumber(0, _bonuses.Count - 1)];
 }