Exemple #1
0
        /// <summary>
        /// Update all the game component
        /// </summary>
        /// <param name="gameTime"></param>
        /// <param name="otherScreenHasFocus"></param>
        /// <param name="coveredByOtherScreen"></param>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            // Do not advance to the highscore screen if sounds are playing
            if (moveToHighScore && !AudioManager.AreSoundsPlaying())
            {
                ScreenManager.Game.Components.Remove(currentLevel);

                foreach (GameScreen screen in ScreenManager.GetScreens())
                {
                    screen.ExitScreen();
                }

                ScreenManager.AddScreen(new BackgroundScreen(true), null);
                ScreenManager.AddScreen(new HighScoreScreen(), null);
            }

            // Do not perform advance update logic if the game is inactive or we are
            // moving to the highscore screen
            if (!IsActive || moveToHighScore)
            {
                base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
                return;
            }

            if ((inputState == TouchInputState.GracePeriod) && (isActive))
            {
                inputTimeMeasure += gameTime.ElapsedGameTime;

                // if the input grace period is over, handle the touch input
                if (inputTimeMeasure >= inputGracePeriod)
                {
                    currentLevel.RegisterTouch(lastPressInput);
                    inputState = TouchInputState.Idle;
                }
            }

            // If the user passed the level, advance to the next or finish the game if
            // the current level was last
            if (currentLevel.CurrentState == LevelState.FinishedOk && isActive)
            {
                AudioManager.PlaySound("success");

                if (currentLevelNumber < maxLevelNumber)
                {
                    currentLevelNumber++;
                    isLevelChange = true;
                }
                else
                {
                    FinishCurrentGame();
                }
            }
            // If the user failed to pass the level, revert to level one, allowing the
            // user to register a highscore if he reached a high enough level
            else if (currentLevel.CurrentState == LevelState.FinishedFail)
            {
                isActive = false;

                if (HighScoreScreen.IsInHighscores(currentLevelNumber))
                {
                    // The player has a highscore - show the device's keyboard
                    Guide.BeginShowKeyboardInput(PlayerIndex.One,
                                                 Constants.HighscorePopupTitle, Constants.HighscorePopupText,
                                                 Constants.HighscorePopupDefault, ShowHighscorePromptEnded,
                                                 false);
                }
                else
                {
                    AudioManager.PlaySound("fail");
                    isActive           = true;
                    currentLevelNumber = 1;
                    isLevelChange      = true;
                }
            }

            if (isLevelChange)
            {
                ScreenManager.Game.Components.Remove(currentLevel);

                currentLevel = new Level(ScreenManager.Game,
                                         ScreenManager.SpriteBatch,
                                         currentLevelNumber, buttonsTexture);
                currentLevel.IsActive = true;

                ScreenManager.Game.Components.Add(currentLevel);

                isLevelChange = false;
            }

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }
Exemple #2
0
        /// <summary>
        /// Handle user presses.
        /// </summary>
        /// <param name="touchPoints">The locations touched by the user. The list
        /// is expected to contain at least one member.</param>
        public void RegisterTouch(List <TouchLocation> touchPoints)
        {
            if ((CurrentState == LevelState.Started ||
                 CurrentState == LevelState.InProcess))
            {
                ButtonColors[] stepColors = sequence.First.Value;

                bool validTouchRegistered = false;

                if (touchPoints.Count > 0)
                {
                    // Reset current touch sample
                    for (int i = 0; i < Settings.ButtonAmount; i++)
                    {
                        currentTouchSampleColors[i] = null;
                    }

                    // Go over the touch points and populate the current touch sample
                    for (int i = 0; i < touchPoints.Count; i++)
                    {
                        var gestureBox = new BoundingBox(
                            new Vector3(touchPoints[i].Position.X - 5,
                                        touchPoints[i].Position.Y - 5, 0),
                            new Vector3(touchPoints[i].Position.X + 10,
                                        touchPoints[i].Position.Y + 10, 0));

                        if (redShpere.Intersects(gestureBox))
                        {
                            currentTouchSampleColors[i] = ButtonColors.Red;
                            AudioManager.PlaySound("red");
                        }
                        else if (yellowShpere.Intersects(gestureBox))
                        {
                            currentTouchSampleColors[i] = ButtonColors.Yellow;
                            AudioManager.PlaySound("yellow");
                        }
                        else if (blueShpere.Intersects(gestureBox))
                        {
                            currentTouchSampleColors[i] = ButtonColors.Blue;
                            AudioManager.PlaySound("blue");
                        }
                        else if (greenShpere.Intersects(gestureBox))
                        {
                            currentTouchSampleColors[i] = ButtonColors.Green;
                            AudioManager.PlaySound("green");
                        }

                        CurrentState = LevelState.InProcess;
                    }

                    List <ButtonColors> colorsHit =
                        new List <ButtonColors>(currentTouchSampleColors.Length);

                    // Check if the user pressed at least one of the colored buttons
                    foreach (var hitColor in currentTouchSampleColors)
                    {
                        if (hitColor.HasValue)
                        {
                            validTouchRegistered = true;
                            colorsHit.Add(hitColor.Value);
                        }
                    }

                    // Find the buttons which the user failed to touch
                    List <ButtonColors> missedColors =
                        new List <ButtonColors>(stepColors.Length);

                    foreach (var stepColor in stepColors)
                    {
                        if (!colorsHit.Contains(stepColor))
                        {
                            missedColors.Add(stepColor);
                        }
                    }

                    // If the user failed to perform the current move, fail the level
                    // Do nothing if no buttons were touched
                    if (((missedColors.Count > 0) ||
                         (touchPoints.Count != stepColors.Length)) && validTouchRegistered)
                    {
                        CurrentState = LevelState.Fault;
                    }

                    if (validTouchRegistered)
                    {
                        // Show user pressed buttons, reset timeout period
                        // for button flash
                        drawUserInput    = true;
                        inputFlashPeriod = TimeSpan.Zero;

                        MovesPerformed++;
                        sequence.Remove(stepColors);

                        if ((sequence.Count == 0) && (CurrentState != LevelState.Fault))
                        {
                            CurrentState = LevelState.Success;
                        }
                    }
                }
            }
        }