Esempio n. 1
0
 public void AddEnemy(Enemy enemy)
 {
     enemies.Add(enemy);
 }
Esempio n. 2
0
        public void Update(GameTime gameTime, KeyboardState keystate)
        {
            for (int i = 0; i < soundManagers.Count; i++)
                soundManagers[i].isPlaying = false;

            dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

            player.Update(dt, keystate);

            // TEST : Adding test enemies
                newEnemyWait -= dt;
                if (newEnemyWait <= newEnemyDelay)
                {
                    newEnemyWait += newEnemyDelay;
                    int testx = (int)(random.NextDouble() * (width - 40.0) + 20.0);
                    Enemy testenemy = new Enemy(this.game, this, testx, 16, 0, 50, 3);
                    AddEnemy(testenemy);

                }
            // :

            // Update each explosion on the screen
            Effect.Explosion explosion;
            for (int i = 0; i < explosions.Count; i++)
            {
                explosion = explosions[i];
                explosion.Update(dt);

                if (explosion.destroyed == true)
                {
                    explosions.RemoveAt(i);
                    i--;
                }
            }

            // Update each enemy on the screen
            Enemy enemy;
            for (int i = 0; i < enemies.Count; i++)
            {
                enemy = enemies[i];
                enemy.Update(dt);

                // Check enemy collisions with the player

                // Find the distance of the enemy from the player
                double distance = Math.Sqrt(
                    (player.GetCenterX() - enemy.GetCenterX()) * (player.GetCenterX() - enemy.GetCenterX()) +
                    (player.GetCenterY() - enemy.GetCenterY()) * (player.GetCenterY() - enemy.GetCenterY())
                    );

                if (distance <= enemy.radius + player.radius)
                {
                    player.Kill();
                }

                if (enemy.destroyed == true)
                {
                    enemies.RemoveAt(i);
                    i--;
                }
            }

            //Update each bullet on the screen.
            Bullet bullet;
            for (int i = 0; i < playerBullets.Count; i++)
            {
                bullet = playerBullets[i];
                bullet.Update(dt);

                // Remove any off-screen bullets
                if (bullet.position.X < 0 || bullet.position.X > screenWidth
                    || bullet.position.Y < 0 || bullet.position.Y > screenHeight)
                {
                    playerBullets.RemoveAt(i);
                    i--;
                }

                else
                {
                    // Check any bullets for collision
                    for (int ii = 0; ii < enemies.Count; ii++)
                    {
                        enemy = enemies[ii];
                        // Find the distance from the bullet point to the center of the enemy circle
                        double distance = Math.Sqrt(
                            (bullet.position.X - enemy.GetCenterX()) * (bullet.position.X - enemy.GetCenterX()) +
                            (bullet.position.Y - enemy.GetCenterY()) * (bullet.position.Y - enemy.GetCenterY())
                            );

                        if (distance <= enemy.radius + bullet.radius)
                        {
                            enemy.Damage(bullet);
                            playerBullets.RemoveAt(i);
                            i--;
                            if (enemy.destroyed == true)
                            {
                                enemies.RemoveAt(ii);
                                ii--;
                            }
                            break;
                        }
                    }
                }
            }

            for (int i = 0; i < enemyBullets.Count; i++)
            {
                bullet = enemyBullets[i];
                bullet.Update(dt);

                // Remove any off-screen bullets
                if (bullet.position.X < 0 || bullet.position.X > screenWidth
                    || bullet.position.Y < 0 || bullet.position.Y > screenHeight)
                {
                    enemyBullets.RemoveAt(i);
                    i--;
                }

                else
                {
                    // Check each enemy bullet for collision with the game player
                    double distance = Math.Sqrt(
                        (bullet.position.X - player.GetCenterX()) * (bullet.position.X - player.GetCenterX()) +
                        (bullet.position.Y - player.GetCenterY()) * (bullet.position.Y - player.GetCenterY())
                        );

                    if (distance <= player.radius + bullet.radius)
                    {
                        enemyBullets.RemoveAt(i);
                        i--;
                        player.Kill();
                    }
                }
            }

            if (keystate.IsKeyDown(Keys.Escape))
                game.Exit();
        }