Beispiel #1
0
        public override void Update(GameTime gameTime)
        {
            KeyboardState ks = Keyboard.GetState();

            // If spacecraft (player) does not exist,
            //  In other words, If Game is end
            if (Game.Services.GetService <Spacecraft>() == null)
            {
                // Stop to create enemies
                ClearManagers();

                // If current score is in 5th,
                // Call HighScoreInput for Writing new score to a file with player name
                int score = Game.Services.GetService <ScoreDisplay>().GetScore();
                if (!Game.Components.OfType <HighScoreInput>().Any())
                {
                    if (GameScore.CheckHighScore(score))
                    {
                        this.AddComponent(new HighScoreInput(Game));
                    }

                    // Enter key: New game
                    if (ks.IsKeyDown(Keys.Enter))
                    {
                        ClearGame();
                        StartNewGame();
                    }
                }
            }

            // ESC key: exit this menu
            if (ks.IsKeyDown(Keys.Escape))
            {
                // If Game end, delete all current enemies
                if (Game.Services.GetService <Spacecraft>() == null)
                {
                    ClearGame();
                }

                ((Game1)Game).HideAllScenes();
                Game.Services.GetService <StartScene>().Show();
            }

            base.Update(gameTime);
        }
        public override void Update(GameTime gameTime)
        {
            KeyboardState keyState = Keyboard.GetState();

            // Get all keys currently pressed
            Keys[] pressedKeys = keyState.GetPressedKeys();

            if (pressedKeys.Length > 0)
            {
                // Iterate through all pressed keys
                foreach (Keys key in pressedKeys)
                {
                    if (prevKS.IsKeyDown(key))
                    {
                        continue;
                    }
                    switch (key)
                    {
                    case Keys.Space:
                        enteredString += " ";
                        break;

                    case Keys.Back:
                        if (enteredString.Length > 0)
                        {
                            enteredString = enteredString.Remove(enteredString.Length - 1);
                        }
                        break;

                    case Keys.A:
                    case Keys.B:
                    case Keys.C:
                    case Keys.D:
                    case Keys.E:
                    case Keys.F:
                    case Keys.G:
                    case Keys.H:
                    case Keys.I:
                    case Keys.J:
                    case Keys.K:
                    case Keys.L:
                    case Keys.M:
                    case Keys.N:
                    case Keys.O:
                    case Keys.P:
                    case Keys.Q:
                    case Keys.R:
                    case Keys.S:
                    case Keys.T:
                    case Keys.U:
                    case Keys.V:
                    case Keys.W:
                    case Keys.X:
                    case Keys.Y:
                    case Keys.Z:
                        if (Keyboard.GetState().IsKeyDown(Keys.LeftShift) ||
                            Keyboard.GetState().IsKeyDown(Keys.RightShift))
                        {
                            enteredString += key.ToString();
                        }
                        else
                        {
                            enteredString += key.ToString().ToLower();
                        }
                        break;

                    case Keys.OemPeriod:
                        enteredString += ".";
                        break;

                    case Keys.OemComma:
                        enteredString += ",";
                        break;

                    case Keys.OemQuestion:
                        enteredString += "?";
                        break;

                    case Keys.D1:
                        enteredString += "!";
                        break;

                    case Keys.OemQuotes:
                        enteredString += "\"";
                        break;

                    case Keys.Enter:
                        // Add current score to High Score List File
                        int score = Game.Services.GetService <ScoreDisplay>().GetScore();
                        GameScore.WriteScore(new ScoreData(enteredString, score));
                        // Idle timer is on for stable transition from menu to game
                        idleTimerOn = true;
                        break;

                    case Keys.OemMinus:
                        enteredString += "-";
                        break;

                    default:
                        break;
                    }
                }
            }

            // Limit Maximum player name characters
            if (enteredString.Length > MAX_NAME_CHARS)
            {
                enteredString = enteredString.Remove(enteredString.Length - 1);
            }

            prevKS = keyState;

            // Check Idle Timer
            // If Idle Timer is over, reset score and remove this instance
            if (idleTimerOn)
            {
                idleTimer += gameTime.ElapsedGameTime.TotalSeconds;
                if (idleTimer >= IDLE_DURATION)
                {
                    Game.Services.GetService <ScoreDisplay>().ResetScore();
                    Game.Components.Remove(this);
                }
            }
            else
            {
                idleTimer = 0;
            }

            base.Update(gameTime);
        }