Exemplo n.º 1
0
        protected override void Update(GameTime gameTime)
        {
            KeyboardState CurrKeys = Keyboard.GetState();
            MouseState    CurrMouse = Mouse.GetState();
            Particle2D    BulletInfo, EnemyInfo;
            List <int>    ParticlesToRemove = new List <int>();
            int           Ctr, Cnt;

            if (cAliveSince == 0)
            {
                cAliveSince = (uint)gameTime.TotalGameTime.TotalSeconds;
            }

            if (cLastAsteroid < gameTime.TotalGameTime.TotalMilliseconds)               //Create new asteroid
            {
                if ((cSpawnNum % 3 != 0) && (cEnemyKills >= cAsteroids.ParticleList.Count))
                {
                    for (Ctr = 0; Ctr < 1 + (cEnemyKills / 20); Ctr++)
                    {
                        CreateNewAsteroid(100, new Vector2(-1, -1));
                    }
                }
                else
                {
                    Vector2 StartPos;

                    StartPos.X = (float)(cRandom.NextDouble() * cGraphDevMgr.GraphicsDevice.Viewport.Width);
                    StartPos.Y = cRandom.Next(-2, 1) * 60;

                    if (StartPos.Y == 0)
                    {
                        StartPos.Y = cGraphDevMgr.GraphicsDevice.Viewport.Height;
                    }

                    CreateNewHunter(60, StartPos);
                }

                cSpawnNum++;
                cLastAsteroid = gameTime.TotalGameTime.TotalMilliseconds + 2000;
            }

            //Check for player input
            Ctr = (int)(500 - cEnemyKillsMax);             //Calculate the minimum time between shots
            if (Ctr > 400)
            {
                Ctr = 400;
            }
            else if (Ctr < 100)
            {
                Ctr = 50;
            }

            if (((CurrKeys.IsKeyDown(Keys.Space) == true) || (CurrMouse.LeftButton == ButtonState.Pressed)) && (cLastShot < gameTime.TotalGameTime.TotalMilliseconds - Ctr))
            {
                PlayerFireBullet();

                cLastShot = gameTime.TotalGameTime.TotalMilliseconds;
            }

            if ((CurrKeys.IsKeyDown(Keys.OemTilde) == true) && (cPriorKeyState.IsKeyDown(Keys.OemTilde) == false))
            {
                cDevConsole.ToggleVisible();
            }

            cUFOs.Update(gameTime);
            cAsteroids.Update(gameTime);
            cEnemyBullets.Update(gameTime);
            cPlayerBullets.Update(gameTime);
            cPlayerShip.Update(gameTime, CurrKeys, CurrMouse);
            cSparkles.Update(gameTime);
            cDevConsole.Update(gameTime, CurrKeys, CurrMouse);

            //Collision detection
            cPlayerShip.ImageTint = Color.White;
            for (Cnt = 0; Cnt < cAsteroids.ParticleList.Count; Cnt++)
            {
                EnemyInfo = (Particle2D)cAsteroids.ParticleList[Cnt];

                //Are bullts hitting the asteroid?
                for (Ctr = 0; Ctr < cPlayerBullets.ParticleList.Count; Ctr++)
                {
                    BulletInfo = (Particle2D)cPlayerBullets.ParticleList[Ctr];

                    if (BulletInfo.TestCollision(EnemyInfo.GetCollisionRegions()) == true)
                    {
                        CreateParticleBurst(new Vector2(EnemyInfo.TopLeft.X + (EnemyInfo.Width / 2), EnemyInfo.TopLeft.Y + (EnemyInfo.Height / 2)), 25 * EnemyInfo.Height / 6, EnemyInfo.Height / 3, Color.SaddleBrown, cTextureDict[Textures.Dust]);

                        //Spawn little asteroids
                        if (EnemyInfo.Height > 50)
                        {
                            Vector2 TopLeft;

                            TopLeft.X = EnemyInfo.TopLeft.X + (EnemyInfo.Width / 2) - (EnemyInfo.Width * 0.35f);
                            TopLeft.Y = EnemyInfo.TopLeft.Y + (EnemyInfo.Height / 2) - (EnemyInfo.Height * 0.35f);

                            CreateNewAsteroid((int)(EnemyInfo.Width * 0.7f), TopLeft);
                            CreateNewAsteroid((int)(EnemyInfo.Width * 0.7f), TopLeft);
                        }

                        //Destroy shot and large asteroid
                        ParticlesToRemove.Add(Cnt);
                        cPlayerBullets.ParticleList.RemoveAt(Ctr);
                        cEnemyKills++;

                        break;                         //Exit inner loop so each bullet ony gets 1 asteroid
                    }
                }

                //Is the asteroid hitting the player?
                if (EnemyInfo.TestCollision(cPlayerShip) == true)
                {
                    cPlayerShip.ImageTint = Color.Red;

                    CreateParticleBurst(new Vector2(EnemyInfo.TopLeft.X + (EnemyInfo.Width / 2), EnemyInfo.TopLeft.Y + (EnemyInfo.Height / 2)), 25 * EnemyInfo.Height / 6, EnemyInfo.Height / 3, Color.SaddleBrown, cTextureDict[Textures.Dust]);

                    //Spawn little asteroids
                    if (EnemyInfo.Height > 50)
                    {
                        Vector2 TopLeft;

                        TopLeft.X = EnemyInfo.TopLeft.X + (EnemyInfo.Width / 2) - (EnemyInfo.Width * 0.35f);
                        TopLeft.Y = EnemyInfo.TopLeft.Y + (EnemyInfo.Height / 2) - (EnemyInfo.Height * 0.35f);

                        CreateNewAsteroid((int)(EnemyInfo.Width * 0.7f), TopLeft);
                        CreateNewAsteroid((int)(EnemyInfo.Width * 0.7f), TopLeft);
                    }

                    //Destroy asteroid
                    ParticlesToRemove.Add(Cnt);

                    PlayerHit(gameTime);
                }
            }

            for (Cnt = ParticlesToRemove.Count - 1; Cnt >= 0; Cnt--)
            {
                cAsteroids.ParticleList.RemoveAt(ParticlesToRemove[Cnt]);
            }
            ParticlesToRemove.Clear();

            for (Cnt = 0; Cnt < cUFOs.ParticleList.Count; Cnt++)
            {
                EnemyInfo = (Particle2D)cUFOs.ParticleList[Cnt];

                //Are bullts hitting the UFO?
                for (Ctr = 0; Ctr < cPlayerBullets.ParticleList.Count; Ctr++)
                {
                    BulletInfo = (Particle2D)cPlayerBullets.ParticleList[Ctr];

                    if (BulletInfo.TestCollision(EnemyInfo.GetCollisionRegions()) == true)
                    {
                        //Destroy shot and UFO
                        cPlayerBullets.ParticleList.RemoveAt(Ctr);
                        ParticlesToRemove.Add(Cnt);
                        cEnemyKills++;

                        CreateParticleBurst(new Vector2(EnemyInfo.TopLeft.X + (EnemyInfo.Width / 2), EnemyInfo.TopLeft.Y + (EnemyInfo.Height / 2)), 200, Color.OrangeRed);

                        break;                         //Exit inner loop so each bullet ony gets 1 enemy
                    }
                }

                //Is the UFO hitting the player?
                if (EnemyInfo.TestCollision(cPlayerShip) == true)
                {
                    cPlayerShip.ImageTint = Color.Red;

                    PlayerHit(gameTime);
                }
            }

            for (Cnt = ParticlesToRemove.Count - 1; Cnt >= 0; Cnt--)
            {
                cUFOs.ParticleList.RemoveAt(ParticlesToRemove[Cnt]);
            }

            for (Cnt = 0; Cnt < cEnemyBullets.ParticleList.Count; Cnt++)
            {
                BulletInfo = (Particle2D)cEnemyBullets.ParticleList[Cnt];

                //Is the bullet hitting the player?
                if (BulletInfo.TestCollision(cPlayerShip) == true)
                {
                    cEnemyBullets.ParticleList.RemoveAt(Cnt);

                    PlayerHit(gameTime);
                }
            }

            cPriorKeyState = CurrKeys;

            //Use monogame update
            base.Update(gameTime);
        }