コード例 #1
0
        private void UpdateGameplay(GameTime gameTime)
        {
            if (scroll1.location.Y + scroll1.location.Height >= 8192)
            {
                scroll1.location.Y = scroll2.location.Y - scroll2.backgroundTexture.Height;
            }
            if (scroll2.location.Y + scroll2.backgroundTexture.Height >= 8192)
            {
                scroll2.location.Y = scroll1.location.Y - scroll1.backgroundTexture.Height;
            }
            scroll1.Update();
            scroll2.Update();

            player.UpdateMovement();

            #region ButtonFunctionality

            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                isPausePressed      = true;
                this.IsMouseVisible = true;
                Pause();

                //TODO napisać funkcje pauzująca grę
            }

            var mouseState = Mouse.GetState();
            var mousePoint = new Point(mouseState.X, mouseState.Y);

            if (!showUpgradeWindow)
            {
                if (exitToMainMenuButton.location.Contains(mousePoint))
                {
                    exitToMainMenuButton.hover();
                }
                else
                {
                    exitToMainMenuButton.unhover();
                }
                if (resumeButton.location.Contains(mousePoint))
                {
                    resumeButton.hover();
                }
                else
                {
                    resumeButton.unhover();
                }
                if (restartButton.location.Contains(mousePoint))
                {
                    restartButton.hover();
                }
                else
                {
                    restartButton.unhover();
                }
                if (upgradeButton.location.Contains(mousePoint))
                {
                    upgradeButton.hover();
                }
                else
                {
                    upgradeButton.unhover();
                }

                if (exitToMainMenuButton.location.Contains(mousePoint) && mouseState.LeftButton == ButtonState.Pressed)
                {
                    exitToMainMenuButton.press();
                }
                else if (resumeButton.location.Contains(mousePoint) && mouseState.LeftButton == ButtonState.Pressed)
                {
                    resumeButton.press();
                }
                else if (restartButton.location.Contains(mousePoint) && mouseState.LeftButton == ButtonState.Pressed)
                {
                    restartButton.press();
                }
                else if (upgradeButton.location.Contains(mousePoint) && mouseState.LeftButton == ButtonState.Pressed)
                {
                    upgradeButton.press();
                }
                coinBackgroundButton.hideButton();
                oneShotButton.hideButton();
                piercingButton.hideButton();
                movementSpeedButton.hideButton();
                fireRateButton.hideButton();
            }
            if (showUpgradeWindow)
            {
                if (oneShotButton.location.Contains(mousePoint))
                {
                    oneShotButton.hover();
                }
                else
                {
                    oneShotButton.unhover();
                }
                if (piercingButton.location.Contains(mousePoint))
                {
                    piercingButton.hover();
                }
                else
                {
                    piercingButton.unhover();
                }
                if (movementSpeedButton.location.Contains(mousePoint))
                {
                    movementSpeedButton.hover();
                }
                else
                {
                    movementSpeedButton.unhover();
                }
                if (fireRateButton.location.Contains(mousePoint))
                {
                    fireRateButton.hover();
                }
                else
                {
                    fireRateButton.unhover();
                }

                if (oneShotButton.location.Contains(mousePoint) && mouseState.LeftButton == ButtonState.Pressed)
                {
                    oneShotButton.press();
                }
                else if (piercingButton.location.Contains(mousePoint) && mouseState.LeftButton == ButtonState.Pressed)
                {
                    piercingButton.press();
                }
                else if (movementSpeedButton.location.Contains(mousePoint) && mouseState.LeftButton == ButtonState.Pressed)
                {
                    movementSpeedButton.press();
                }
                else if (fireRateButton.location.Contains(mousePoint) && mouseState.LeftButton == ButtonState.Pressed)
                {
                    fireRateButton.press();
                }

                coinBackgroundButton.showHiddenButton();
                oneShotButton.showHiddenButton();
                piercingButton.showHiddenButton();
                movementSpeedButton.showHiddenButton();
                fireRateButton.showHiddenButton();
                if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                {
                    showUpgradeWindow = false;
                }
            }

            if (exitToMainMenuButton.isPressed)
            {
                if (exitToMainMenuButton.location.Contains(mousePoint) && mouseState.LeftButton == ButtonState.Released)
                {
                    MediaPlayer.Stop();
                    MediaPlayer.Play(menuSong);
                    exitToMainMenuButton.unpress();
                    isPausePressed    = false;
                    _currentGameState = GameState.MainMenu;
                }
            }
            else if (resumeButton.isPressed)
            {
                if (resumeButton.location.Contains(mousePoint) && mouseState.LeftButton == ButtonState.Released)
                {
                    resumeButton.unpress();
                    isPausePressed = false;
                    Resume();
                }
            }
            else if (restartButton.isPressed)
            {
                if (restartButton.location.Contains(mousePoint) && mouseState.LeftButton == ButtonState.Released)
                {
                    restartButton.unpress();
                    isPausePressed = false;
                    RestartGame();
                }
            }
            else if (upgradeButton.isPressed)
            {
                if (upgradeButton.location.Contains(mousePoint) && mouseState.LeftButton == ButtonState.Released)
                {
                    upgradeButton.unpress();
                    showUpgradeWindow = true;
                }
            }
            else if (oneShotButton.isPressed)
            {
                if (oneShotButton.location.Contains(mousePoint) && mouseState.LeftButton == ButtonState.Released)
                {
                    oneShotButton.unpress();
                    if (coinCount >= 5 && !isOneShotBought)
                    {
                        missileTexture = oneShotMissileTexture;
                        coinCount     -= 5;
                    }
                }
            }
            else if (piercingButton.isPressed)
            {
                if (piercingButton.location.Contains(mousePoint) && mouseState.LeftButton == ButtonState.Released)
                {
                    piercingButton.unpress();
                    if (coinCount >= 3 && !isPiercingBought)
                    {
                        missileTexture = piercingMissileTexture;
                        coinCount     -= 3;
                    }
                }
            }
            else if (movementSpeedButton.isPressed)
            {
                if (movementSpeedButton.location.Contains(mousePoint) && mouseState.LeftButton == ButtonState.Released)
                {
                    movementSpeedButton.unpress();
                    if (coinCount >= 1)
                    {
                        player.speed++;
                        coinCount--;
                    }
                }
            }
            else if (fireRateButton.isPressed)
            {
                if (fireRateButton.location.Contains(mousePoint) && mouseState.LeftButton == ButtonState.Released)
                {
                    fireRateButton.unpress();
                    if (coinCount >= 1)
                    {
                        shootingSpeed++;
                        coinCount--;
                    }
                }
            }
            #endregion

            if (shooting)
            {
                //waves config
                if (actualScore > 100 && _currentEnemyWave == EnemyWave.First)
                {
                    throwSomeCoins    = true;
                    _currentEnemyWave = EnemyWave.Second;
                    enemySpeed        = 2;
                }
                else if (actualScore > 200 && _currentEnemyWave == EnemyWave.Second)
                {
                    throwSomeCoins    = true;
                    _currentEnemyWave = EnemyWave.Third;
                    enemySpeed        = 2;
                }
                else if (actualScore > 300 && _currentEnemyWave == EnemyWave.Third)
                {
                    throwSomeCoins    = true;
                    _currentEnemyWave = EnemyWave.Fourth;
                    enemySpeed        = 2;
                }
                else if (actualScore > 400 && _currentEnemyWave == EnemyWave.Fourth)
                {
                    throwSomeCoins    = true;
                    _currentEnemyWave = EnemyWave.Fifth;
                    enemySpeed        = 2;
                }
                //drawing missiles
                if (missiles.Count == 0)
                {
                    missiles.Add(new Missile(missileTexture, new Rectangle(player.playerLocation.X + player.playerLocation.Width / 2 - 5, player.playerLocation.Y, 10, 32), shootingSpeed));
                }
                else if (missiles[missiles.Count - 1].isNextReady(fireRate))
                {
                    missiles.Add(new Missile(missileTexture, new Rectangle(player.playerLocation.X + player.playerLocation.Width / 2 - 5, player.playerLocation.Y, 10, 32), shootingSpeed));
                }
                foreach (Missile m in missiles)
                {
                    //clearing destroyed missiles
                    if (m.isDestroyed)
                    {
                        missiles.Remove(m);
                        break;
                    }
                    else
                    {
                        m.Update();
                    }
                }

                //drawing enemies
                if (enemies.Count == 0)
                {
                    enemies.Add(new Enemy(enemyTexture1, enemyTexture2, enemyTexture3, enemyTexture4, enemyTexture5, new Rectangle(rnd.Next(500), -100, 100, 100), enemySpeed, _currentEnemyWave));
                }
                else if (enemies[enemies.Count - 1].isNextReady())
                {
                    enemies.Add(new Enemy(enemyTexture1, enemyTexture2, enemyTexture3, enemyTexture4, enemyTexture5, new Rectangle(rnd.Next(500), -100, 100, 100), enemySpeed, _currentEnemyWave));
                }
                foreach (Enemy e in enemies)
                {
                    //clearing destroyed enemies
                    if (e.isDestroyed)
                    {
                        enemies.Remove(e);
                        actualScore++;
                        break;
                    }
                    else
                    {
                        e.Update();
                    }

                    if (e.enemyLocation.Y > 800)
                    {
                        player.Hit();
                        e.isDestroyed = true;
                    }

                    if (player.health == 0)
                    {
                        Pause();
                        _currentGameState = GameState.EndOfGame;
                    }
                }

                //missiles collision detection
                foreach (Missile m in missiles)
                {
                    foreach (Enemy e in enemies)
                    {
                        if (e.enemyLocation.Contains(m.missileLocation))
                        {
                            if (missileTexture == oneShotMissileTexture)
                            {
                                e.Hit(500);
                            }
                            else if (missileTexture == piercingMissileTexture)
                            {
                                e.Hit(50);
                            }
                            else
                            {
                                e.Hit(25);
                            }

                            m.isDestroyed = true;
                            if (actualScore % 10 == 0 && enemySpeed != 5)
                            {
                                enemySpeed++;
                            }
                        }
                    }

                    foreach (TossACoin c in coins)
                    {
                        if (c.coinLocation.Contains(m.missileLocation))
                        {
                            c.isDestroyed = true;
                            coinCount++;
                        }
                    }
                }
                //update coins, check if out of screen
                foreach (TossACoin c in coins)
                {
                    if (c.isDestroyed)
                    {
                        coins.Remove(c);
                        break;
                    }
                    else
                    {
                        c.Update();
                    }
                }
                //drawing coins
                if (throwSomeCoins)
                {
                    coins.Add(new TossACoin(coinTexture, new Rectangle(rnd.Next(500), -90, 45, 55)));
                    coins.Add(new TossACoin(coinTexture, new Rectangle(rnd.Next(500), -150, 45, 55)));
                    coins.Add(new TossACoin(coinTexture, new Rectangle(rnd.Next(500), -210, 45, 55)));

                    throwSomeCoins = false;
                }
            }
            else if (isPausePressed == false)
            {
                shooting = true;
            }
        }
コード例 #2
0
ファイル: Game1.cs プロジェクト: kbuczkowski/SpaceShooter
        private void UpdateGameplay(GameTime deltaTime)
        {
            //game unpaused
            if (_gameplayState == GameplayState.Unpaused)
            {
                //update actual wave
                if (actualScore > 100 && actualWave == EnemyWave.First)
                {
                    actualWave = EnemyWave.Second;
                    startTime  = (int)deltaTime.TotalGameTime.TotalSeconds + 3;
                    enemySpeed = 2;
                    fireRate   = 210;
                }
                else if (actualScore > 200 && actualWave == EnemyWave.Second)
                {
                    actualWave = EnemyWave.Third;
                    startTime  = (int)deltaTime.TotalGameTime.TotalSeconds + 3;
                    enemySpeed = 2;
                    fireRate   = 190;
                }
                else if (actualScore > 300 && actualWave == EnemyWave.Third)
                {
                    actualWave = EnemyWave.Fourth;
                    startTime  = (int)deltaTime.TotalGameTime.TotalSeconds + 3;
                    enemySpeed = 2;
                    fireRate   = 150;
                }
                else if (actualScore > 400 && actualWave == EnemyWave.Fourth)
                {
                    actualWave = EnemyWave.Fifth;
                    startTime  = (int)deltaTime.TotalGameTime.TotalSeconds + 3;
                    enemySpeed = 2;
                    fireRate   = 120;
                }

                //deleting missiles
                for (int i = 0; i < missiles.Count; i++)
                {
                    if (missiles[i].isDestroyed)
                    {
                        missiles.RemoveAt(i);
                    }
                }

                //deleting enemies
                for (int i = 0; i < enemies.Count; i++)
                {
                    if (enemies[i].isDestroyed)
                    {
                        enemies.RemoveAt(i);
                    }
                }

                //creating missiles
                if (missiles.Count == 0)
                {
                    missiles.Add(new Missile(player.missileTexture, player.location.X, (int)(player.missileSpeed * skillCheck.multiplier)));
                }
                else if (missiles[missiles.Count - 1].isNextReady(fireRate))
                {
                    missiles.Add(new Missile(player.missileTexture, player.location.X, (int)(player.missileSpeed * skillCheck.multiplier)));
                }

                //creating enemies
                if (deltaTime.TotalGameTime.TotalSeconds > startTime)
                {
                    if (enemies.Count == 0)
                    {
                        enemies.Add(new Enemy(enemyTextures, r.Next(4), enemySpeed, actualWave));
                    }
                    else if (enemies[enemies.Count - 1].isNextReady())
                    {
                        enemies.Add(new Enemy(enemyTextures, r.Next(4), enemySpeed, actualWave));
                    }
                }

                if (deltaTime.TotalGameTime.TotalSeconds > nextSkilCheck)
                {
                    skillCheck.Run();
                    nextSkilCheck = r.Next((int)deltaTime.TotalGameTime.TotalSeconds + 20, (int)deltaTime.TotalGameTime.TotalSeconds + 40);
                }

                //updateplayer health
                foreach (Enemy e in enemies)
                {
                    if (e.location.Y >= 2340 && !e.isDestroyed)
                    {
                        e.isDestroyed   = true;
                        heartBlinkLeft += 10;
                        if (player.Hit())
                        {
                            if (actualScore > highScore)
                            {
                                highScore    = actualScore;
                                newHighScore = true;
                                SaveSettings();
                            }
                            else
                            {
                                newHighScore = false;
                            }
                            _gameState = GameState.Menu;
                            _menuState = MenuState.EndOfGame;

                            PlaySong();
                        }
                    }
                }

                //enemy-missile collision handling
                foreach (Missile m in missiles)
                {
                    foreach (Enemy e in enemies)
                    {
                        if (e.location.Contains(new Point(m.location.X + 8, m.location.Y + 135)) && !(e.isDestroyed || m.isDestroyed))
                        {
                            if (e.Hit(skillCheck.damage))
                            {
                                actualScore++;
                                if (actualScore % 5 == 0)
                                {
                                    enemySpeed++;
                                }
                            }
                            m.isDestroyed = true;
                        }
                    }
                }

                //hearts blinking
                if (heartBlinkLeft > 0)
                {
                    if (deltaTime.TotalGameTime.TotalMilliseconds >= nextBlinkTime)
                    {
                        player.heartVisible = !player.heartVisible;

                        nextBlinkTime = deltaTime.TotalGameTime.TotalMilliseconds + 100;
                        heartBlinkLeft--;
                    }
                }

                if (skillCheck.isActive)
                {
                    //updating enemies, missiles and background position
                    foreach (Enemy e in enemies)
                    {
                        e.UpdateSlow();
                    }
                    foreach (Missile m in missiles)
                    {
                        m.UpdateSlow();
                    }
                    gameplayBackground.UpdateSlow();
                    skillCheck.Update(deltaTime);

                    if (!skillCheck.isPressed)
                    {
                        touchCollection = TouchPanel.GetState();
                        foreach (TouchLocation tl in touchCollection)
                        {
                            if (tl.State == TouchLocationState.Pressed)
                            {
                                skillCheck.Press(deltaTime);
                            }
                        }
                    }
                }
                else
                {
                    //updating enemies, missiles and background position
                    foreach (Enemy e in enemies)
                    {
                        e.Update();
                    }
                    foreach (Missile m in missiles)
                    {
                        m.Update();
                    }
                    gameplayBackground.Update();

                    //controls / touch handling
                    touchCollection = TouchPanel.GetState();

                    foreach (TouchLocation tl in touchCollection)
                    {
                        Vector2 position      = new Vector2((int)tl.Position.X, (int)tl.Position.Y);
                        Vector2 touchPosition = Vector2.Transform(position, Matrix.Invert(matrix));

                        if (pauseButton.isPressed)
                        {
                            if (tl.State == TouchLocationState.Released && pauseButton.location.Contains(touchPosition))
                            {
                                pauseButton.unpress();
                                _gameplayState = GameplayState.Paused;
                            }
                            else if (tl.State == TouchLocationState.Released)
                            {
                                pauseButton.unpress();
                            }
                        }
                        else
                        {
                            if (pauseButton.location.Contains(touchPosition) && tl.State == TouchLocationState.Pressed)
                            {
                                pauseButton.press();
                            }
                            else
                            {
                                //player ship controls
                                if (touchPosition.X < 150)
                                {
                                    player.location.X = 0;
                                }
                                else if (touchPosition.X > 930)
                                {
                                    player.location.X = 780;
                                }
                                else
                                {
                                    player.location.X = (int)touchPosition.X - 150;
                                }
                            }
                        }
                    }

                    //pressing Android back button
                    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                    {
                        _gameplayState = GameplayState.Paused;
                    }
                }
            }
            //game paused
            else if (_gameplayState == GameplayState.Paused)
            {
                touchCollection = TouchPanel.GetState();

                foreach (TouchLocation tl in touchCollection)
                {
                    Vector2 position      = new Vector2((int)tl.Position.X, (int)tl.Position.Y);
                    Vector2 touchPosition = Vector2.Transform(position, Matrix.Invert(matrix));

                    foreach (Button b in pauseMenuButtons)
                    {
                        if (b.isPressed)
                        {
                            if (tl.State == TouchLocationState.Released && b.location.Contains(touchPosition))
                            {
                                switch (b.Name)
                                {
                                case "resume":
                                    b.unpress();
                                    _gameplayState = GameplayState.Unpaused;
                                    break;

                                case "exit":
                                    b.unpress();
                                    _gameState = GameState.Menu;
                                    PlaySong();
                                    break;
                                }
                            }
                            else if (tl.State == TouchLocationState.Released)
                            {
                                b.unpress();
                            }
                        }
                        else
                        {
                            if (b.location.Contains(touchPosition) && tl.State == TouchLocationState.Pressed)
                            {
                                b.press();
                            }
                        }
                    }
                }
            }
        }