Ejemplo n.º 1
0
        /// <summary>
        /// Permite al juego ejecutar lógica para, por ejemplo, actualizar el mundo,
        /// buscar colisiones, recopilar entradas y reproducir audio.
        /// </summary>
        /// <param name="gameTime">Proporciona una instantánea de los valores de tiempo.</param>
        protected override void Update(GameTime gameTime)
        {
            ButtonState bs = GamePad.GetState(PlayerIndex.One).Buttons.Back;

            var touch = TouchPanel.GetState().FirstOrDefault();

            bool pressed  = touch.State == TouchLocationState.Pressed;
            bool released = touch.State == TouchLocationState.Released;

            // About transitions ---------------------------->
            if (fadingScreen < 255)
            {
                fadingScreen += rateFade;
            }

            switch (STATE)
            {
                #region MENU
            case GameState.MENU:
            {
                if (bs == ButtonState.Pressed)
                {
                    GameSettings.Save(CurrentProgress);
                    Exit();
                }
                else if (touch.State == TouchLocationState.Pressed)
                {
                    if (recPlay.Contains((int)touch.Position.X, (int)touch.Position.Y))
                    {
                        STATE = GameState.WORLD;
                        LockExchangeScreens = true;
                    }
                    else if (recSettings.Contains((int)touch.Position.X, (int)touch.Position.Y))
                    {
                        CurrentProgress.LevelsPuntuations = new Dictionary <int, int>();
                        CurrentProgress.CurrentLevel      = 1;
                        GameSettings.SetCurrentProgress(CurrentProgress);
                        STATE       = GameState.WORLD;
                        PathTexture = Content.Load <Texture2D>("Path/Camino1");
                    }
                    else if (recAbout.Contains((int)touch.Position.X, (int)touch.Position.Y))
                    {
                    }
                }
                DrawMenu(gameTime);
                break;
            }

                #endregion
                #region WORLD
            case GameState.WORLD:
            {
                if (bs == ButtonState.Pressed)
                {
                    STATE = GameState.MENU;
                    break;
                }
                else
                {
                    if (released)
                    {
                        if (!LockExchangeScreens)
                        {
                            dragging = false;
                            if (!tooDragNoClick)
                            {
                                int l = cacheLevelsPositions.Count();

                                for (int i = 0; i < CurrentProgress.CurrentLevel; i++)
                                {
                                    if (i < LAST_LEVEL)
                                    {
                                        if (cacheLevelsPositions[i].Contains((int)touch.Position.X,
                                                                             (int)(touch.Position.Y + touchLocation)))
                                        {
                                            STATE = GameState.LEVEL;
                                            LoadLevel(i + 1);
                                        }
                                    }
                                }
                            }
                            tooDragNoClick = false;
                        }
                        LockExchangeScreens = false;
                    }
                    else if (pressed)
                    {
                        onDrag   = true;
                        dragging = true;
                    }

                    if (onDrag)
                    {
                        touchWorld = touch;
                        onDrag     = false;
                    }
                    if (dragging)
                    {
                        float difference = (touch.Position.Y - touchWorld.Position.Y);
                        touchLocation -= difference;
                        if (touchLocation > MAX_POS_START_WORLD_MAP_Y)
                        {
                            touchLocation = MAX_POS_START_WORLD_MAP_Y;
                        }
                        else if (touchLocation < 0)
                        {
                            touchLocation = 0;
                        }
                        touchWorld = touch;
                        if (Math.Abs(difference) > 1)
                        {
                            tooDragNoClick = true;
                        }
                    }

                    DrawWorld(gameTime);
                    break;
                }
            }

                #endregion
                #region LEVEL
            case GameState.LEVEL:
            {
                if (bs == ButtonState.Pressed)
                {
                    STATE = GameState.WORLD;
                }
                else if (touch.State == TouchLocationState.Pressed)
                {
                    int pushX = ((int)touch.Position.X - ScreenDistanceX) / CurrentLevel.SizeSquare,
                        pushY = ((int)touch.Position.Y - ScreenDistanceY) / CurrentLevel.SizeSquare;

                    var tappedPoint = new Point
                    {
                        X = (int)touch.Position.X,
                        Y = (int)touch.Position.Y
                    };

                    if ((pushX >= 0) && (pushX < CurrentLevel.X) && (pushY >= 0) && (pushY < CurrentLevel.Y))
                    {
                        SquareExploding = CurrentLevel.squares[pushX, pushY];
                        if (ItemPushed != -1)
                        {
                            if ((ItemPushed == 0) && (SquareExploding.Content < 2))
                            {
                                transitionStatus = 4;
                                soundEffect.Play();
                                SquareExploding.RecentChanged = true;
                                SquareExploding.Switch();
                                CurrentLevel.moves++;
                                CurrentProgress.GameItems[ItemPushed].Substract();
                                ItemPushed = -1;

                                if (CurrentLevel.IsLevelCompleted(1))
                                {
                                    ActionsWhenLevelCompleted();
                                }
                            }
                            else if (ItemPushed == 1)
                            {
                                transitionStatus = 4;
                                soundEffect.Play();
                                var squareList = CurrentLevel.Get5X5AreaSquares(pushX, pushY);
                                ExplodeSquareList(squareList, false);
                                CurrentProgress.GameItems[ItemPushed].Substract();
                                ItemPushed = -1;

                                if (CurrentLevel.IsLevelCompleted(1))
                                {
                                    ActionsWhenLevelCompleted();
                                }
                            }
                            else if (ItemPushed == 5)
                            {
                                dragginItemThunder = true;
                            }
                        }
                        else
                        {
                            if (SquareExploding.Content < 2)
                            {
                                transitionStatus = 4;

                                var squareList = CurrentLevel.GetSquareAndAdjacent(pushX, pushY);

                                ExplodeSquareList(squareList, true);
                            }
                        }
                    }
                    if (RecItemsContainer.Contains(tappedPoint))
                    {
                        var relativePushed = (tappedPoint.X - startXItems) / 80;
                        ItemPushed = (ItemPushed != relativePushed) &&
                                     (CurrentProgress.GameItems[relativePushed].Amount > 0)
                                    ? relativePushed
                                    : -1;
                    }
                    else if (recReset.Contains(tappedPoint))
                    {
                        CurrentLevel = SetCurrentLevel(Levels.Level[CurrentLevel.LevelNumber]);
                    }
                    else if (StarsRectangle.Contains(tappedPoint))
                    {
                        if (ItemPushed == 2)
                        {
                            if (CurrentLevel.ONE_STAR > CurrentLevel.moves)
                            {
                                CurrentLevel.moves = 0;
                            }
                            else if (CurrentLevel.TWO_STAR > CurrentLevel.moves)
                            {
                                CurrentLevel.moves = CurrentLevel.ONE_STAR / 2;
                            }
                            else if (CurrentLevel.TWO_STAR == CurrentLevel.moves)
                            {
                                CurrentLevel.moves = 0;
                            }
                            else if (CurrentLevel.THREE_STAR > CurrentLevel.moves)
                            {
                                CurrentLevel.moves = CurrentLevel.TWO_STAR / 2;
                            }
                            else if (CurrentLevel.THREE_STAR == CurrentLevel.moves)
                            {
                                CurrentLevel.moves = CurrentLevel.ONE_STAR;
                            }
                            CurrentProgress.GameItems[ItemPushed].Substract();
                            ItemPushed = -1;
                        }
                        else if (ItemPushed == 3)
                        {
                            if (CurrentLevel.ONE_STAR > CurrentLevel.moves)
                            {
                                CurrentLevel.moves = 0;
                            }
                            else if (CurrentLevel.TWO_STAR > CurrentLevel.moves)
                            {
                                CurrentLevel.moves = CurrentLevel.ONE_STAR;
                            }
                            else if (CurrentLevel.TWO_STAR == CurrentLevel.moves)
                            {
                                CurrentLevel.moves = 0;
                            }
                            else if (CurrentLevel.THREE_STAR > CurrentLevel.moves)
                            {
                                CurrentLevel.moves = CurrentLevel.TWO_STAR;
                            }
                            else if (CurrentLevel.THREE_STAR == CurrentLevel.moves)
                            {
                                CurrentLevel.moves = CurrentLevel.ONE_STAR;
                            }
                            CurrentProgress.GameItems[ItemPushed].Substract();
                            ItemPushed = -1;
                        }
                    }
                }
                if (transitionStatus > 0)
                {
                    transitionStatus--;
                }
                else
                {
                    SquareExploding = null;
                    SquaresAdjacent = EmptySquares;
                }
                DrawLevel(gameTime);
                break;
            }

                #endregion
                #region ENDLEVEL
            case GameState.ENDLEVEL:
            {
                if (bs == ButtonState.Pressed || touch.State == TouchLocationState.Released)
                {
                    if (ScreenChange)
                    {
                        ScreenChange = false;
                    }
                    else
                    {
                        if (!gameOver)
                        {
                            if (CurrentProgress.CurrentLevel < CurrentLevel.LevelNumber)
                            {
                                TotalMoves += CurrentLevel.moves;
                                var next = CurrentLevel.LevelNumber + 1;
                                SetCurrentLevel(Levels.Level[next]);
                            }
                        }
                        STATE    = GameState.WORLD;
                        gameOver = false;
                        drawEnd  = false;
                    }
                }
                else
                {
                }
                DrawLevel(gameTime);
                break;
            }
            }
            #endregion
            base.Update(gameTime);
        }