Esempio n. 1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            GameTime = gameTime;

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }
            foreach (var entity in EntityManager.Entities)
            {
                if (entity is MainPlayer)
                {
                    tempPlayer = (MainPlayer)entity;
                }
            }
            // for menu
            mouseState    = Mouse.GetState();
            keyboardState = Keyboard.GetState();

            switch (gameState.state)
            {
            case "MAINMENU":
                timer = 0;
                if (StartGameButton.update(new Vector2(mouseState.X, mouseState.Y)) == true && mouseState != previousMouseState && mouseState.LeftButton == ButtonState.Pressed)
                {
                    gameState.changeToState("PLAYGAME");
                }
                else if (LevelSelectButton.update(new Vector2(mouseState.X, mouseState.Y)) == true && mouseState.LeftButton == ButtonState.Pressed)
                {
                    gameState.changeToState("LEVEL");
                }
                break;

            case "PAUSE":
                if (InGameMainMenuButton.update(new Vector2(mouseState.X, mouseState.Y)) == true && mouseState != previousMouseState && mouseState.LeftButton == ButtonState.Pressed)
                {
                    gameState.changeToState("MAINMENU");
                    //EntityManager.Entities.Clear();
                    timer = 0;
                }
                if (InGamePlayButton.update(new Vector2(mouseState.X, mouseState.Y)) == true && mouseState != previousMouseState && mouseState.LeftButton == ButtonState.Pressed)
                {
                    gameState.changeToState("PLAYGAME");
                }
                break;

            case "PLAYGAME":
                //var tempPlayer = (MainPlayer)EntityManager.Entities[0];
                if (tempPlayer.Lives <= 0)
                {
                    gameState.changeToState("GAMEOVER");
                    break;
                }
                if (keyboardState.IsKeyDown(Keys.P))
                {
                    gameState.changeToState("PAUSE");
                    break;
                }
                else
                {
                    if (keyboardState.IsKeyDown(Keys.Space) && gameTime.TotalGameTime - timeOfShot > timeBetweenShots)
                    {
                        timeOfShot = gameTime.TotalGameTime;
                        EntityManager.Add(PlayerBullet.Instance);
                    }

                    // we need to add certain enemies to entity manager here

                    if (timer <= EntityManager.waveEndTimes[0])
                    {
                        if (timer >= EntityManager.enemyIntervals[0] - 0.01 && timer <= EntityManager.enemyIntervals[0] + 0.01)
                        {
                            // we are in interval of first wave
                            EntityManager.AddStandardEnemyQuater(150f, 20, MovePattern.QuarterTurnLeft);

                            for (; _indexer <= 10 && _indexer < EntityManager.EntitiesFromJson.Count; _indexer++)
                            {
                                EntityManager.Entities.Add(EntityManager.EntitiesFromJson[_indexer]);
                            }
                        }
                    }
                    if (timer >= EntityManager.waveStartTimes[1] && timer <= EntityManager.waveEndTimes[1])
                    {
                        if (timer >= EntityManager.enemyIntervals[1] - 0.01 && timer <= EntityManager.enemyIntervals[1] + 0.01)
                        {
                            // we are in interval of second wave
                            EntityManager.AddStandardEnemyQuater(150f, 10, MovePattern.QuarterTurnRight);
                            EntityManager.AddButterflyEnemy("Random", 30);

                            //EntityManager.Entities.Add(EntityManager.EntitiesFromJson[_indexer]);
                            //_indexer++;

                            for (; _indexer <= 20 && _indexer < EntityManager.EntitiesFromJson.Count; _indexer++)
                            {
                                EntityManager.Entities.Add(EntityManager.EntitiesFromJson[_indexer]);
                            }
                        }
                    }
                    if (timer >= EntityManager.waveStartTimes[2] && timer <= EntityManager.waveEndTimes[2])
                    {
                        if (timer >= EntityManager.enemyIntervals[2] - 0.01 && timer <= EntityManager.enemyIntervals[2] + 0.01)
                        {
                            // we are in interval of third wave
                            EntityManager.Add(GameFactory.CreateGameBoss());

                            //EntityManager.Entities.Add(EntityManager.EntitiesFromJson[_indexer]);
                            //_indexer++;

                            for (; _indexer < EntityManager.EntitiesFromJson.Count; _indexer++)
                            {
                                EntityManager.Entities.Add(EntityManager.EntitiesFromJson[_indexer]);
                            }
                        }
                    }



                    //Stop game at 60 seconds
                    if (gameTime.TotalGameTime.TotalSeconds > 120)
                    {
                        EntityManager.ClearScreen();
                        gameState.changeToState("ENDGAME");
                    }

                    EntityManager.Update(gameTime, timer);
                    foreach (var bullet in playerBullets)
                    {
                        bullet.Update(gameTime, EntityManager.Entities);
                    }


                    base.Update(gameTime);
                }
                break;

            case "LEVEL":
                if (level1.update(new Vector2(mouseState.X, mouseState.Y)) == true && mouseState != previousMouseState && mouseState.LeftButton == ButtonState.Pressed)
                {
                    EntityManager.WavesParser = new GameWavesRepository(this, "LevelOne");
                    gameState.changeToState("PLAYGAME");
                }
                else if (level2.update(new Vector2(mouseState.X, mouseState.Y)) == true && mouseState != previousMouseState && mouseState.LeftButton == ButtonState.Pressed)
                {
                    EntityManager.WavesParser = new GameWavesRepository(this, "LevelTwo");
                    EntityManager.InitializeGame(this, "LevelTwo");
                    gameState.changeToState("PLAYGAME");
                }
                else if (level3.update(new Vector2(mouseState.X, mouseState.Y)) == true && mouseState != previousMouseState && mouseState.LeftButton == ButtonState.Pressed)
                {
                    EntityManager.WavesParser = new GameWavesRepository(this, "LevelThree");
                    EntityManager.InitializeGame(this, "LevelThree");
                    gameState.changeToState("PLAYGAME");
                }
                break;

            case "ENDGAME":
                // display game over message then return to main menu
                break;
            }
            if (gameState.state != "PAUSE" && gameState.state != "ENDGAME" && gameState.state != "GAMEOVER" && gameState.state != "LEVEL")
            {
                timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
            previousMouseState = mouseState;
        }