Пример #1
0
        private bool ObjectsColliding(Powerup powerup, Sprite dinosaur)
        {
            float powerupLeft = powerup.Position.X;
            float powerupRight = powerupLeft + 50;
            float powerupTop = powerup.Position.Y;
            float powerupBottom = powerupTop + 50;

            float dinosaurLeft = dinosaur.Position.X - 30;
            float dinosaurRight = dinosaurLeft + 60;
            float dinosaurTop = dinosaur.Position.Y - 60;
            float dinosaurBottom = dinosaurTop + 120;

            if (powerupBottom >= dinosaurTop)
            {
                if (powerupTop <= dinosaurBottom)
                {
                    if (powerupLeft <= dinosaurRight && powerupRight >= dinosaurLeft)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
Пример #2
0
        private void CheckIfPlayerHitSomething(Game game, SpriteBatch spriteBatch, ContentManager Content, List<HealthBar> healthBars)
        {
            for (int enemyCounter = enemies.Count - 1; enemyCounter >= 0; enemyCounter--)
            {
                Sprite enemy = enemies[enemyCounter];
                enemy.IsHit = false;
                for (int i = playerShots.shotsOne.Count - 1; i >= 0; i--)
                {
                    //don't check for collision below half screen height
                    if (playerShots.shotsOne[i].Y > _halfScreenHeight)
                        continue;

                    if ((Vector2.DistanceSquared(playerShots.shotsOne[i], enemy.Position) < ((enemy.width * enemy.height) / 11f)))
                    {
                        enemy.IsHit = true;
                        enemy._currentHealth -= 10;
                        playerShots.shotsOne.RemoveAt(i);
                        if (enemy._currentHealth <= 0)
                        {
                            AnimatedSprite explode = new AnimatedSprite(12, 0, 0, 134, 134, game, explodeImage,
                                enemy.Position,
                                spriteBatch, new Enemies.EnemyType(Content, Enemies.EnemyType.Etype.Simple));
                            explode.Name = "Explode";
                            explode.delay = 75f;

                            healthBars.RemoveAt(healthBars.Count - 1);

                            explosionSound.Play();
                            dinosaurOne.Points += enemy.enemyType.PointValue;
                            Random r = new Random();
                            if (r.NextDouble() > 0.75)
                            {
                                Vector2 powerupStartingPosition = enemy.Position;
                                Powerup p = new Powerup(gdm, sb, cm, powerupStartingPosition);
                                powerups.Add(p);
                            }

                            enemies.RemoveAt(enemyCounter);
                            explosions.Add(explode);
                        }
                        return;
                    }
                }
            }

            if (playerShots.shotsTwo != null)
            {

                for (int enemyCounter = enemies.Count - 1; enemyCounter >= 0; enemyCounter--)
                {
                    Sprite enemy = enemies[enemyCounter];
                    enemy.IsHit = false;
                    for (int i = playerShots.shotsTwo.Count - 1; i >= 0; i--)
                    {
                        //don't check for collision below half screen height
                        if (playerShots.shotsTwo[i].Y > _halfScreenHeight)
                            continue;

                        if ((Vector2.DistanceSquared(playerShots.shotsTwo[i], enemy.Position) < ((enemy.width * enemy.height) / 11f)))
                        {
                            enemy.IsHit = true;
                            enemy._currentHealth -= 10;
                            playerShots.shotsTwo.RemoveAt(i);
                            if (enemy._currentHealth <= 0)
                            {
                                AnimatedSprite explode = new AnimatedSprite(12, 0, 0, 134, 134, game, explodeImage,
                                    enemy.Position,
                                    spriteBatch, new Enemies.EnemyType(Content, Enemies.EnemyType.Etype.Simple));
                                explode.Name = "Explode";
                                explode.delay = 75f;

                                healthBars.RemoveAt(healthBars.Count - 1);

                                explosionSound.Play();
                                dinosaurTwo.Points += enemy.enemyType.PointValue;
                                Random r = new Random();
                                if (r.NextDouble() > 0.75)
                                {
                                    Vector2 powerupStartingPosition = enemy.Position;
                                    Powerup p = new Powerup(gdm, sb, cm, powerupStartingPosition);
                                    powerups.Add(p);
                                }

                                enemies.RemoveAt(enemyCounter);
                                explosions.Add(explode);
                            }
                            return;
                        }
                    }
                }
            }
        }