Пример #1
0
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            KeyboardState prevKeyboard = keyboard;

            keyboard = Keyboard.GetState();

            GamePadState prevGamepad = gamepad;

            gamepad = GamePad.GetState(PlayerIndex.One);

            Random random = new Random();

            if (keyboard.IsKeyDown(Keys.F2) && prevKeyboard.IsKeyUp(Keys.F2))
            {
                particles.Add(new particle(new Vector2(320, 240), 1, "red", random.Next(5, 10), 0.1f, random.Next(360), 4));
            }

            switch (gameState)
            {
            case GameState.startScreen:
                if (keyboard.IsKeyDown(Keys.Space) || gamepad.Buttons.A == ButtonState.Pressed)
                {
                    gameState = GameState.menu;
                }
                break;

            case GameState.menu:
                menu.Update();
                if (keyboard.IsKeyDown(Keys.Escape) && prevKeyboard.IsKeyUp(Keys.Escape) || gamepad.Buttons.Start == ButtonState.Pressed && prevGamepad.Buttons.Start == ButtonState.Released)
                {
                    if (startedGame)
                    {
                        gameState = GameState.game;
                    }
                }
                if (keyboard.IsKeyDown(Keys.Space) && prevKeyboard.IsKeyUp(Keys.Space) || gamepad.Buttons.A == ButtonState.Pressed && prevGamepad.Buttons.A == ButtonState.Released)
                {
                    switch (menu.selected)
                    {
                    case 1:
                        gameState = GameState.game;
                        if (!startedGame)
                        {
                            Initialize();
                            startedGame = true;
                        }
                        break;

                    case 2:
                        gameState = GameState.howTo;
                        break;

                    case 3:
                        this.Exit();
                        break;
                    }
                }
                break;

            case GameState.game:
                player.Input(projectiles);
                player.Update();
                player.CheckHealth(healthbar, particles);
                spawnManager.Spawn(level, backgroundObjects, enemies);
                gui.update(player, level);
                foreach (enemyProjectile ep in enemyProjectiles)
                {
                    ep.Movment();
                }
                foreach (enemy e in enemies)
                {
                    e.Attack(enemyProjectiles);
                    e.Movment(player);
                    e.Animation();
                    e.CheckHealth(projectiles, explosions, ref player);
                }
                foreach (explosion ex in explosions)
                {
                    ex.Animation();
                }
                foreach (backgroundObject bo in backgroundObjects)
                {
                    bo.Movment();
                }
                foreach (projectile p in projectiles)
                {
                    p.Movment();
                }
                foreach (particle p in particles)
                {
                    p.Movemnt();
                }
                if (keyboard.IsKeyDown(Keys.Escape) && prevKeyboard.IsKeyUp(Keys.Escape) || gamepad.Buttons.Start == ButtonState.Pressed && prevGamepad.Buttons.Start == ButtonState.Released)
                {
                    gameState     = GameState.menu;
                    menu.selected = 1;
                }
                for (int i = 0; i < projectiles.Count; i++)
                {
                    if (projectiles[i].destroy)
                    {
                        projectiles.RemoveAt(i);
                    }
                }
                for (int i = 0; i < enemyProjectiles.Count; i++)
                {
                    if (enemyProjectiles[i].destroy)
                    {
                        enemyProjectiles.RemoveAt(i);
                    }
                }
                for (int i = 0; i < particles.Count; i++)
                {
                    if (particles[i].destroy)
                    {
                        particles.RemoveAt(i);
                    }
                }
                for (int i = 0; i < enemies.Count; i++)
                {
                    if (enemies[i].destroy)
                    {
                        enemies.RemoveAt(i);
                    }
                }
                for (int i = 0; i < backgroundObjects.Count; i++)
                {
                    if (backgroundObjects[i].destroy)
                    {
                        backgroundObjects.RemoveAt(i);
                    }
                }
                for (int i = 0; i < explosions.Count; i++)
                {
                    if (explosions[i].destroy)
                    {
                        explosions.RemoveAt(i);
                    }
                }
                break;

            case GameState.howTo:
                if (keyboard.IsKeyDown(Keys.Space) && prevKeyboard.IsKeyUp(Keys.Space) || gamepad.Buttons.A == ButtonState.Pressed && prevGamepad.Buttons.A == ButtonState.Released)
                {
                    gameState = GameState.menu;
                }
                break;
            }

            base.Update(gameTime);
        }
Пример #2
0
        protected override void Update(GameTime gameTime)
        {
            KeyboardState prevKeyboard = keyboard;

            keyboard = Keyboard.GetState();

            GamePadState prevGamepad = gamepad;

            gamepad = GamePad.GetState(PlayerIndex.One);

            Random random = new Random();

            if (keyboard.IsKeyDown(Keys.F1) && prevKeyboard.IsKeyUp(Keys.F1) || gamepad.Buttons.Back == ButtonState.Pressed && prevGamepad.Buttons.Back == ButtonState.Released)
            {
                graphics.ToggleFullScreen();
            }

            switch (gameState)
            {
            case GameState.startScreen:
                if (keyboard.IsKeyDown(Keys.Space) && prevKeyboard.IsKeyUp(Keys.Space) || keyboard.IsKeyDown(Keys.Enter) && prevKeyboard.IsKeyUp(Keys.Enter) || gamepad.Buttons.A == ButtonState.Pressed && prevGamepad.Buttons.A == ButtonState.Released)
                {
                    gameState = GameState.menu;
                }
                break;

            case GameState.menu:
                menu.Update();
                if (keyboard.IsKeyDown(Keys.Escape) && prevKeyboard.IsKeyUp(Keys.Escape) || gamepad.Buttons.Start == ButtonState.Pressed && prevGamepad.Buttons.Start == ButtonState.Released)
                {
                    if (startedGame)
                    {
                        gameState = GameState.game;
                    }
                }
                if (keyboard.IsKeyDown(Keys.Space) && prevKeyboard.IsKeyUp(Keys.Space) || keyboard.IsKeyDown(Keys.Enter) && prevKeyboard.IsKeyUp(Keys.Enter) || gamepad.Buttons.A == ButtonState.Pressed && prevGamepad.Buttons.A == ButtonState.Released)
                {
                    switch (menu.selected)
                    {
                    case 1:
                        gameState = GameState.game;
                        if (!startedGame)
                        {
                            Initialize();
                            startedGame = true;
                        }
                        break;

                    case 2:
                        gameState = GameState.howTo;
                        break;

                    case 3:
                        this.Exit();
                        break;
                    }
                }
                break;

            case GameState.levelTransition:
                if (keyboard.IsKeyDown(Keys.Space) && prevKeyboard.IsKeyUp(Keys.Space) || keyboard.IsKeyDown(Keys.Enter) && prevKeyboard.IsKeyUp(Keys.Enter) || gamepad.Buttons.A == ButtonState.Pressed && prevGamepad.Buttons.A == ButtonState.Released)
                {
                    resetLevel();
                    gameState = GameState.game;
                }
                break;

            case GameState.gameOver:
                gibs.Clear();
                if (keyboard.IsKeyDown(Keys.Space) && prevKeyboard.IsKeyUp(Keys.Space) || keyboard.IsKeyDown(Keys.Enter) && prevKeyboard.IsKeyUp(Keys.Enter) || gamepad.Buttons.A == ButtonState.Pressed && prevGamepad.Buttons.A == ButtonState.Released)
                {
                    resetLevel();
                    Initialize();
                    gameState = GameState.menu;
                }
                break;

            case GameState.game:

                if (!this.IsActive)
                {
                    gameState = GameState.menu;
                }
                player.Input(projectiles, particles);
                player.Update();
                if (player.lives <= -1)
                {
                    gameState = GameState.gameOver;
                }
                player.CheckHealth(healthbar, particles);
                spawnManager.Spawn(level, backgroundObjects, enemies, powerUps, bosses);
                gui.Update(player, level);

                foreach (gib g in gibs)
                {
                    g.movment();
                }
                foreach (boss b in bosses)
                {
                    b.Attack(enemyProjectiles, player);
                    b.Movment(player);
                    b.checkHealth(projectiles, particles, ref player, ref level);
                    b.Update();
                    if (b.nextLevelCount >= 128 * 2)
                    {
                        gameState = GameState.levelTransition;
                    }
                }
                foreach (enemyProjectile ep in enemyProjectiles)
                {
                    ep.Movment(player);
                    ep.Update(player, projectiles, gibs);
                }
                foreach (hitEffect he in hitEffects)
                {
                    he.Update();
                }
                foreach (enemy e in enemies)
                {
                    e.Attack(enemyProjectiles, player);
                    e.Movment(player);
                    e.Animation();
                    e.CheckHealth(projectiles, explosions, hitEffects, gibs, ref player);
                }
                foreach (explosion ex in explosions)
                {
                    ex.Animation();
                }
                foreach (powerUp pu in powerUps)
                {
                    pu.Update(ref player);
                    pu.Movment();
                }
                foreach (backgroundObject bo in backgroundObjects)
                {
                    bo.Movment();
                }
                foreach (projectile p in projectiles)
                {
                    p.Movment();
                    p.Update(particles);
                }
                foreach (particle p in particles)
                {
                    p.Movemnt();
                }
                if (keyboard.IsKeyDown(Keys.Escape) && prevKeyboard.IsKeyUp(Keys.Escape) || gamepad.Buttons.Start == ButtonState.Pressed && prevGamepad.Buttons.Start == ButtonState.Released)
                {
                    gameState     = GameState.menu;
                    menu.selected = 1;
                }
                for (int i = 0; i < projectiles.Count; i++)
                {
                    if (projectiles[i].destroy)
                    {
                        projectiles.RemoveAt(i);
                    }
                }
                for (int i = 0; i < gibs.Count; i++)
                {
                    if (gibs[i].destroy)
                    {
                        gibs.RemoveAt(i);
                    }
                }
                for (int i = 0; i < hitEffects.Count; i++)
                {
                    if (hitEffects[i].destroy)
                    {
                        hitEffects.RemoveAt(i);
                    }
                }
                for (int i = 0; i < enemyProjectiles.Count; i++)
                {
                    if (enemyProjectiles[i].destroy)
                    {
                        enemyProjectiles.RemoveAt(i);
                    }
                }
                for (int i = 0; i < particles.Count; i++)
                {
                    if (particles[i].destroy)
                    {
                        particles.RemoveAt(i);
                    }
                }
                for (int i = 0; i < enemies.Count; i++)
                {
                    if (enemies[i].destroy)
                    {
                        enemies.RemoveAt(i);
                    }
                }
                for (int i = 0; i < backgroundObjects.Count; i++)
                {
                    if (backgroundObjects[i].destroy)
                    {
                        backgroundObjects.RemoveAt(i);
                    }
                }
                for (int i = 0; i < powerUps.Count; i++)
                {
                    if (powerUps[i].destroy)
                    {
                        powerUps.RemoveAt(i);
                    }
                }
                for (int i = 0; i < explosions.Count; i++)
                {
                    if (explosions[i].destroy)
                    {
                        explosions.RemoveAt(i);
                    }
                }
                for (int i = 0; i < bosses.Count; i++)
                {
                    if (bosses[i].destroy)
                    {
                        bosses.RemoveAt(i);
                    }
                }
                break;

            case GameState.howTo:
                if (keyboard.IsKeyDown(Keys.Space) && prevKeyboard.IsKeyUp(Keys.Space) || keyboard.IsKeyDown(Keys.Enter) && prevKeyboard.IsKeyUp(Keys.Enter) || gamepad.Buttons.A == ButtonState.Pressed && prevGamepad.Buttons.A == ButtonState.Released)
                {
                    gameState = GameState.menu;
                }
                break;
            }

            base.Update(gameTime);
        }