예제 #1
0
        public static void Reset(GameWindow window, ContentManager content, GameTime gameTime)
        {
            //sätter allt till orginal värden när player dör
            player.Reset(50, 200, 4f, 4f);
            enemies.Clear();
            bossD1.Clear();
            bossD2.Clear();
            wave          = 1;
            lv            = 1;
            player.points = 0;
            player.PW     = 0;
            j             = 0;
            int w = 0;

            timeSinceLastWave = 0;
            #region Enemy Wave
            Random random = new Random();

            if (gameTime.TotalGameTime.TotalSeconds > timeSinceLastWave + 9)
            {
                if (wave < 4)
                {
                    while (w < 1)
                    {
                        wave++; w++;
                    }
                }
                if (wave > 4 && wave < 9)
                {
                    while (w < 1)
                    {
                        wave++; w++;
                    }
                }
                timeSinceLastWave = gameTime.TotalGameTime.TotalSeconds;
            }

            #region Level 1
            if (lv == 1)
            {
                if (wave == 1)
                {
                    while (j < 1)
                    {
                        Texture2D tmpSprite = content.Load <Texture2D>("images/enemies/TIE-Defender");
                        for (int i = 0; i < 10; i++)
                        {
                            int      rndX = random.Next(600, window.ClientBounds.Width);
                            int      rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Defender temp = new Defender(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        tmpSprite = content.Load <Texture2D>("images/enemies/TIE-fighter");
                        for (int i = 0; i < 10; i++)
                        {
                            int     rndX = random.Next(600, window.ClientBounds.Width);
                            int     rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Fighter temp = new Fighter(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        j = 1;
                    }
                }
                if (wave == 2)
                {
                    while (j < 2)
                    {
                        Texture2D tmpSprite = content.Load <Texture2D>("images/enemies/TIE-Defender");
                        for (int i = 0; i < 20; i++)
                        {
                            int      rndX = random.Next(600, window.ClientBounds.Width);
                            int      rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Defender temp = new Defender(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        tmpSprite = content.Load <Texture2D>("images/enemies/TIE-fighter");
                        for (int i = 0; i < 20; i++)
                        {
                            int     rndX = random.Next(600, window.ClientBounds.Width);
                            int     rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Fighter temp = new Fighter(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        j = 2;
                    }
                }
                if (wave == 3)
                {
                    while (j < 3)
                    {
                        Texture2D tmpSprite = content.Load <Texture2D>("images/enemies/TIE-Defender");
                        for (int i = 0; i < 30; i++)
                        {
                            int      rndX = random.Next(600, window.ClientBounds.Width);
                            int      rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Defender temp = new Defender(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        tmpSprite = content.Load <Texture2D>("images/enemies/TIE-fighter");
                        for (int i = 0; i < 30; i++)
                        {
                            int     rndX = random.Next(600, window.ClientBounds.Width);
                            int     rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Fighter temp = new Fighter(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        j = 3;
                    }
                }
                if (wave == 4)
                {
                    while (j < 4)
                    {
                        Texture2D tmpSprite = content.Load <Texture2D>("images/enemies/Boss/The_Death_Star");
                        for (int i = 0; i < 1; i++)
                        {
                            int  rndX = random.Next(650, window.ClientBounds.Width - tmpSprite.Width);
                            int  rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Boss temp = new Boss(tmpSprite, rndX, rndY);
                            bossD1.Add(temp);
                        }
                        j = 4;
                    }
                }
            }
            #endregion
            #region Level 2
            if (lv == 2)
            {
                if (wave == 6)
                {
                    while (j < 5)
                    {
                        Texture2D tmpSprite = content.Load <Texture2D>("images/enemies/TIE-Defender");
                        for (int i = 0; i < 10; i++)
                        {
                            int      rndX = random.Next(600, window.ClientBounds.Width);
                            int      rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Defender temp = new Defender(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        tmpSprite = content.Load <Texture2D>("images/enemies/TIE-Advanced");
                        for (int i = 0; i < 10; i++)
                        {
                            int      rndX = random.Next(600, window.ClientBounds.Width);
                            int      rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Advanced temp = new Advanced(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        j = 5;
                    }
                }
                if (wave == 7)
                {
                    while (j < 6)
                    {
                        Texture2D tmpSprite = content.Load <Texture2D>("images/enemies/TIE-Defender");
                        for (int i = 0; i < 20; i++)
                        {
                            int      rndX = random.Next(600, window.ClientBounds.Width);
                            int      rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Defender temp = new Defender(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        tmpSprite = content.Load <Texture2D>("images/enemies/TIE-Advanced");
                        for (int i = 0; i < 20; i++)
                        {
                            int      rndX = random.Next(600, window.ClientBounds.Width);
                            int      rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Advanced temp = new Advanced(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        j = 6;
                    }
                }
                if (wave == 8)
                {
                    while (j < 7)
                    {
                        Texture2D tmpSprite = content.Load <Texture2D>("images/enemies/TIE-Defender");
                        for (int i = 0; i < 30; i++)
                        {
                            int      rndX = random.Next(600, window.ClientBounds.Width);
                            int      rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Defender temp = new Defender(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        tmpSprite = content.Load <Texture2D>("images/enemies/TIE-Advanced");
                        for (int i = 0; i < 30; i++)
                        {
                            int      rndX = random.Next(600, window.ClientBounds.Width);
                            int      rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Advanced temp = new Advanced(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        j = 7;
                    }
                }
                if (wave == 9)
                {
                    while (j < 8)
                    {
                        Texture2D tmpSprite = content.Load <Texture2D>("images/enemies/Boss/Death-Star-2");
                        for (int i = 0; i < 1; i++)
                        {
                            int  rndX = random.Next(650, window.ClientBounds.Width - tmpSprite.Width);
                            int  rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Boss temp = new Boss(tmpSprite, rndX, rndY);
                            bossD2.Add(temp);
                        }
                        j = 8;
                    }
                }
            }
            #endregion
            #endregion
        }
예제 #2
0
        public static state RunUpdate(ContentManager content, GameWindow window, GameTime gameTime)
        {
            player.update(window, gameTime, Lazer, Proton);
            background.Update(window);
            #region Enemy Wave
            Random random = new Random();
            int    w      = 0;
            // besämmer hur länga det är mellan varige Wave
            if (gameTime.TotalGameTime.TotalSeconds > timeSinceLastWave + 9)
            {
                if (wave < 4)//ökar wave om wave < 4
                {
                    while (w < 1)
                    {
                        wave++; w++;
                    }
                }
                if (wave > 4 && wave < 9)
                {
                    while (w < 1)
                    {
                        wave++; w++;
                    }
                }
                timeSinceLastWave = gameTime.TotalGameTime.TotalSeconds;
            }

            #region Level 1
            if (lv == 1)
            {
                if (wave == 1)
                {
                    while (j < 1)//ser till att det bara körs en gång och inte lika många gånger som spelet updateras
                    {
                        // anger vilker bild som tillhör denna fiende
                        Texture2D tmpSprite = content.Load <Texture2D>("images/enemies/TIE-Defender");
                        for (int i = 0; i < 10; i++)
                        {
                            //sätter fiender på random platser på x 600
                            int      rndX = random.Next(600, window.ClientBounds.Width);
                            int      rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Defender temp = new Defender(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        tmpSprite = content.Load <Texture2D>("images/enemies/TIE-fighter");
                        for (int i = 0; i < 10; i++)
                        {
                            int     rndX = random.Next(600, window.ClientBounds.Width);
                            int     rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Fighter temp = new Fighter(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        j = 1;
                    }
                }
                if (wave == 2)
                {
                    while (j < 2)
                    {
                        Texture2D tmpSprite = content.Load <Texture2D>("images/enemies/TIE-Defender");
                        for (int i = 0; i < 20; i++)
                        {
                            int      rndX = random.Next(600, window.ClientBounds.Width);
                            int      rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Defender temp = new Defender(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        tmpSprite = content.Load <Texture2D>("images/enemies/TIE-fighter");
                        for (int i = 0; i < 20; i++)
                        {
                            int     rndX = random.Next(600, window.ClientBounds.Width);
                            int     rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Fighter temp = new Fighter(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        j = 2;
                    }
                }
                if (wave == 3)
                {
                    while (j < 3)
                    {
                        Texture2D tmpSprite = content.Load <Texture2D>("images/enemies/TIE-Defender");
                        for (int i = 0; i < 30; i++)
                        {
                            int      rndX = random.Next(600, window.ClientBounds.Width);
                            int      rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Defender temp = new Defender(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        tmpSprite = content.Load <Texture2D>("images/enemies/TIE-fighter");
                        for (int i = 0; i < 30; i++)
                        {
                            int     rndX = random.Next(600, window.ClientBounds.Width);
                            int     rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Fighter temp = new Fighter(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        j = 3;
                    }
                }
                if (wave == 4)
                {
                    while (j < 4)
                    {
                        Texture2D tmpSprite = content.Load <Texture2D>("images/enemies/Boss/The_Death_Star");
                        for (int i = 0; i < 1; i++)
                        {
                            int  rndX = random.Next(650, window.ClientBounds.Width - tmpSprite.Width);
                            int  rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Boss temp = new Boss(tmpSprite, rndX, rndY);
                            bossD1.Add(temp);
                        }
                        j = 4;
                    }
                }
            }
            #endregion
            #region Level 2
            if (lv == 2)
            {
                if (wave == 6)
                {
                    while (j < 5)
                    {
                        Texture2D tmpSprite = content.Load <Texture2D>("images/enemies/TIE-Defender");
                        for (int i = 0; i < 10; i++)
                        {
                            int      rndX = random.Next(600, window.ClientBounds.Width);
                            int      rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Defender temp = new Defender(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        tmpSprite = content.Load <Texture2D>("images/enemies/TIE-Advanced");
                        for (int i = 0; i < 10; i++)
                        {
                            int      rndX = random.Next(600, window.ClientBounds.Width);
                            int      rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Advanced temp = new Advanced(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        j = 5;
                    }
                }
                if (wave == 7)
                {
                    while (j < 6)
                    {
                        Texture2D tmpSprite = content.Load <Texture2D>("images/enemies/TIE-Defender");
                        for (int i = 0; i < 20; i++)
                        {
                            int      rndX = random.Next(600, window.ClientBounds.Width);
                            int      rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Defender temp = new Defender(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        tmpSprite = content.Load <Texture2D>("images/enemies/TIE-Advanced");
                        for (int i = 0; i < 20; i++)
                        {
                            int      rndX = random.Next(600, window.ClientBounds.Width);
                            int      rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Advanced temp = new Advanced(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        j = 6;
                    }
                }
                if (wave == 8)
                {
                    while (j < 7)
                    {
                        Texture2D tmpSprite = content.Load <Texture2D>("images/enemies/TIE-Defender");
                        for (int i = 0; i < 30; i++)
                        {
                            int      rndX = random.Next(600, window.ClientBounds.Width);
                            int      rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Defender temp = new Defender(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        tmpSprite = content.Load <Texture2D>("images/enemies/TIE-Advanced");
                        for (int i = 0; i < 30; i++)
                        {
                            int      rndX = random.Next(600, window.ClientBounds.Width);
                            int      rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Advanced temp = new Advanced(tmpSprite, rndX, rndY);
                            enemies.Add(temp);
                        }
                        j = 7;
                    }
                }
                if (wave == 9)
                {
                    while (j < 8)
                    {
                        Texture2D tmpSprite = content.Load <Texture2D>("images/enemies/Boss/Death-Star-2");
                        for (int i = 0; i < 1; i++)
                        {
                            int  rndX = random.Next(650, window.ClientBounds.Width - tmpSprite.Width);
                            int  rndY = random.Next(0, window.ClientBounds.Height - tmpSprite.Height);
                            Boss temp = new Boss(tmpSprite, rndX, rndY);
                            bossD2.Add(temp);
                        }
                        j = 8;
                    }
                }
            }
            #endregion
            #endregion
            #region Collision
            // anger vadsom ska hända när objekt kolliderar med varandra
            #region Collision Enemy

            foreach (Enemy e in enemies.ToList())
            {
                foreach (Bullet b in player.Bullets)
                {
                    // kollision mellan fiende och skott
                    if (e.CheckCollision(b))
                    {
                        // vid kollision dödar finede ohc skott
                        e.IsAlive = false;
                        b.IsAlive = false;
                        // vid kollision ger dig 10 poeng
                        player.points += 10;
                    }
                }
                foreach (ProtonTorpedo pt in player.Torpedo)
                {
                    if (e.CheckCollision(pt))
                    {
                        // vid kollision dödar finede
                        e.IsAlive = false;
                        // vid kollision ger dig 10 poeng
                        player.points += 10;
                    }
                }
                if (e.IsAlive)
                {
                    // om player har sköld
                    if (player.shields > 0)
                    {
                        //kollision mellen fiende och player
                        if (e.CheckCollision(player))
                        {
                            // tar bort sköld
                            player.shields -= 1;
                            //om sköld fins kvar så dör inte player
                            player.IsAlive = true;
                            if (player.points > 0)
                            {
                                // tar bort poeng om kollision inträffar
                                player.points -= 1;
                            }
                        }
                        e.Update(window);
                    }
                    else
                    {
                        // döda player om sköld är 0
                        player.IsAlive = false;
                    }
                }
                else
                {
                    enemies.Remove(e);
                }
            }
            #endregion
            #region Collision BossD1
            foreach (Enemy boss in bossD1.ToList())
            {
                foreach (Bullet b in player.Bullets)
                {
                    if (boss.IsAlive)
                    {
                        if (HPD1 > 0)
                        {
                            if (boss.CheckCollision(b))
                            {
                                b.IsAlive      = false;
                                player.points += 10;
                                HPD1--;
                            }
                        }
                        else
                        {
                            boss.IsAlive = false;
                        }
                    }
                    foreach (ProtonTorpedo pt in player.Torpedo)
                    {
                        if (boss.IsAlive)
                        {
                            if (HPD1 > 0)
                            {
                                if (boss.CheckCollision(pt))
                                {
                                    pt.IsAlive     = false;
                                    player.points += 50;
                                    HPD1--;
                                }
                            }
                            else
                            {
                                boss.IsAlive = false;
                            }
                        }
                    }
                }
                if (boss.IsAlive)
                {
                    if (boss.CheckCollision(player))
                    {
                        player.IsAlive = false;
                    }
                    boss.Update(window);
                }
                else
                {
                    bossD1.Remove(boss);
                    lv++;
                    wave++;
                }
            }
            #endregion
            #region Collision BossD2
            foreach (Enemy boss in bossD2.ToList())
            {
                foreach (Bullet b in player.Bullets)
                {
                    if (boss.IsAlive)
                    {
                        if (HPD2 > 0)
                        {
                            if (boss.CheckCollision(b))
                            {
                                b.IsAlive      = false;
                                player.points += 10;
                                HPD2--;
                            }
                        }
                        else
                        {
                            boss.IsAlive = false;
                        }
                    }
                    foreach (ProtonTorpedo pt in player.Torpedo)
                    {
                        if (boss.IsAlive)
                        {
                            if (HPD2 > 0)
                            {
                                if (boss.CheckCollision(pt))
                                {
                                    pt.IsAlive     = false;
                                    player.points += 50;
                                    HPD2--;
                                }
                            }
                            else
                            {
                                boss.IsAlive = false;
                            }
                        }
                    }
                }
                if (boss.IsAlive)
                {
                    if (boss.CheckCollision(player))
                    {
                        player.IsAlive = false;
                    }
                    boss.Update(window);
                }
                else
                {
                    bossD2.Remove(boss);
                    return(state.EnterHighScore);
                }
            }
            #endregion
            #region Coin Collision
            int newCoin = random.Next(1, 200);
            if (newCoin == 1)
            {
                int rndX = random.Next(0, window.ClientBounds.Width - goldCoinSprite.Width);
                int rndY = random.Next(0, window.ClientBounds.Height - goldCoinSprite.Height);

                goldCoins.Add(new GoldCoin(goldCoinSprite, rndX, rndY, gameTime));
            }
            foreach (GoldCoin gc in goldCoins.ToList())
            {
                if (gc.IsAlive)
                {
                    gc.Update(gameTime);
                    if (gc.CheckCollision(player))
                    {
                        goldCoins.Remove(gc);
                        player.points += 10;
                    }
                }
                else
                {
                    goldCoins.Remove(gc);
                }
            }
            #endregion
            #region Shield Collision
            int newshield = random.Next(1, 200);
            if (wave < 4)
            {
                if (newshield == 1)
                {
                    int rndX = random.Next(0, window.ClientBounds.Width - shieldSprite.Width);
                    int rndY = random.Next(0, window.ClientBounds.Height - shieldSprite.Height);

                    goldShield.Add(new GoldCoin(shieldSprite, rndX, rndY, gameTime));
                }
            }

            foreach (GoldCoin gs in goldShield.ToList())
            {
                if (gs.IsAlive)
                {
                    gs.Update(gameTime);
                    if (gs.CheckCollision(player))
                    {
                        goldShield.Remove(gs);
                        player.shields += 50;
                    }
                }
                else
                {
                    goldShield.Remove(gs);
                }
            }
            #endregion
            #region Torpedo Collision
            int newTorpedo = random.Next(1, 200);
            if (newTorpedo == 1)
            {
                int rndX = random.Next(0, window.ClientBounds.Width - Torpedo.Width);
                int rndY = random.Next(0, window.ClientBounds.Height - Torpedo.Height);

                pwTorpedo.Add(new pwTorpedo(Torpedo, rndX, rndY, gameTime));
            }
            foreach (pwTorpedo pw in pwTorpedo.ToList())
            {
                if (pw.IsAlive)
                {
                    pw.Update(gameTime);
                    if (pw.CheckCollision(player))
                    {
                        pwTorpedo.Remove(pw);
                        player.PW++;
                    }
                }
                else
                {
                    pwTorpedo.Remove(pw);
                }
            }
            #endregion
            #endregion
            #region Died
            // anger vad som ska hända när spelaren dör
            if (!player.IsAlive)
            {
                // stoppa och starta en ny song
                MediaPlayer.Stop();
                MediaPlayer.Play(hsSound);
                MediaPlayer.IsRepeating = true;
                hsItem.Points           = player.points;
                //anroppar Reset
                Reset(window, content, gameTime);
                // gör så att du kan ange ditt namn och till highscorelistan
                return(state.EnterHighScore);
            }
            #endregion
            return(state.Run);
        }