Exemple #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)
        {
            // TODO: Add your update logic here
            InputManager.Instance.Update();



            //deltaTime is in seconds, so calculations = pixels per second.
            deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

            //Press enter to start game/level
            if (!startGame)
            {
                if (InputManager.Instance.KeyPressed(Keys.Enter))
                {
                    startGame = true;
                }


                return;
            }

            timer += deltaTime;

            //update player
            spritePlayer.Update(gameTime);

            //update backgrounds
            scrollBack.Update(gameTime);
            scrollFore.Update(gameTime);
            scrollBack.setScrollSpeed(playerSpeeeeeed - 2);
            scrollFore.setScrollSpeed(playerSpeeeeeed);

            //limit scroll speed
            if (playerSpeeeeeed > -1)
            {
                playerSpeeeeeed = -1;
            }
            else if (playerSpeeeeeed < -10)
            {
                playerSpeeeeeed = -10;
            }

            //Every 2 seconds, spawn an enemy randomly.
            if (timer > spawnInterval)
            {
                AddEnemy(enemyList);
                timer = 0;
            }

            //inputs
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            if (InputManager.Instance.KeyPressed(Keys.B))
            {
                showbb = !showbb;
            }

            //PLAYER MOVEMENT
            if (InputManager.Instance.KeyDown(Keys.Left))
            {
                if (spritePlayer.getPosX() > playArea.Left)
                {
                    spritePlayer.setPosX(spritePlayer.getPosX() - playerSpeed);
                }
                playerSpeeeeeed += 0.1f;
            }
            if (InputManager.Instance.KeyDown(Keys.Right))
            {
                if (spritePlayer.getPosX() < playArea.Right - spritePlayer.getWidth())
                {
                    spritePlayer.setPosX(spritePlayer.getPosX() + playerSpeed);
                }
                playerSpeeeeeed += -0.1f;
            }
            if (InputManager.Instance.KeyDown(Keys.Up))
            {
                if (spritePlayer.getPosY() > playArea.Bottom)
                {
                    spritePlayer.setPosY(spritePlayer.getPosY() - playerSpeed);
                }
            }
            if (InputManager.Instance.KeyDown(Keys.Down))
            {
                if (spritePlayer.getPosY() < playArea.Top - spritePlayer.getHeight())
                {
                    spritePlayer.setPosY(spritePlayer.getPosY() + playerSpeed);
                }
            }
            //PLAYER SHOOT
            if (InputManager.Instance.KeyPressed(Keys.Space))
            {
                AddBullet((int)spritePlayer.getPosX(), (int)spritePlayer.getPosY());
            }

            //Update all enemies and bullets to move. also inactive if outofbounds
            //In the past I called this instead of having an extended class with custom update
            // enemyList.moveDeltaX(-3);


            //Update/remove enemies
            enemyList.Update(gameTime);
            enemyList.removeIfOutside(new Rectangle(-100, -100, SCREEN_WIDTH + 200, SCREEN_HEIGHT + 200));

            //update/remove bullets
            playerBulletList.moveDeltaX(5);
            playerBulletList.removeIfOutside(new Rectangle(-100, -100, SCREEN_WIDTH + 200, SCREEN_HEIGHT + 200));



            //If player collides with enemy
            int collisionIndex = enemyList.collisionAA(spritePlayer);

            if (collisionIndex != -1)
            {
                Sprite3 temp = enemyList.getSprite(collisionIndex);
                //player collides with boss
                if (temp.getName() == "boss")
                {
                }
                else
                {
                    //enemy dies?
                    temp.active  = false;
                    temp.visible = false;
                    AddExplosion((int)temp.getPosX(), (int)temp.getPosY());
                    score         -= 5;
                    textScore.text = "Score : " + score.ToString();
                }
            }

            //if a bullet collides with enemy
            for (int i = 0; i < playerBulletList.count(); i++)
            {
                Sprite3 currentBullet = playerBulletList.getSprite(i);
                if (currentBullet == null)
                {
                    continue;
                }
                if (currentBullet.active != true)
                {
                    continue;
                }
                if (currentBullet.visible != true)
                {
                    continue;
                }

                //if collision success
                int collision = enemyList.collisionAA(currentBullet);
                if (collision != -1)
                {
                    Sprite3 currentEnemy = enemyList.getSprite(collision);

                    //kill both enemy and bullet
                    currentEnemy.active   = false;
                    currentEnemy.visible  = false;
                    currentBullet.active  = false;
                    currentBullet.visible = false;

                    //add particle
                    AddExplosion((int)currentEnemy.getPosX(), (int)currentEnemy.getPosY());

                    //add score
                    score         += 10;
                    textScore.text = "Score : " + score.ToString();
                }
            }

            //update particles
            particleList.animationTick(gameTime);



            base.Update(gameTime);
        }
Exemple #2
0
        public override void Update(GameTime gameTime)
        {
            //deltaTime is in seconds, so calculations = pixels per second.
            deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

            //Press enter to start game/level
            switch (levelState)
            {
            case LEVELSTATE.LEVELSTART:


                if (InputManager.Instance.KeyPressed(Keys.Enter))
                {
                    levelState = LEVELSTATE.LEVELPLAY;
                }
                return;

            case LEVELSTATE.LEVELPLAY:
                timer += deltaTime;



                //update player
                player.Update(gameTime);


                //PAUSE
                if (InputManager.Instance.KeyPressed(Keys.P))
                {
                    gameStateManager.pushLevel(3);
                }


                //PLAYER SHOOT
                if (InputManager.Instance.KeyPressed(Keys.Space))
                {
                    AddBullet((int)player.getPosX(), (int)player.getPosY());
                    soundBulletLimit.playSoundIfOk();
                }


                break;

            case LEVELSTATE.LEVELFINISH:
                player.Update(gameTime);
                if (InputManager.Instance.KeyPressed(Keys.Enter))
                {
                    gameStateManager.pushLevel(1);
                }
                break;

            case LEVELSTATE.LEVELGAMEOVER:
                timer += deltaTime;

                if (InputManager.Instance.KeyPressed(Keys.Q))
                {
                    gameStateManager.setLevel(4);
                }
                break;

            default:
                throw new Exception("hahaha");
            }

            //Every 2 seconds, spawn an enemy randomly.
            if (timer > spawnInterval && enemyCounter < enemyToSpawn)
            {
                AddEnemy(enemyList);
                timer = 0;
                enemyCounter++;
            }

            //update backgrounds
            scrollBack.Update(gameTime);
            scrollFore.Update(gameTime);
            scrollBack.setScrollSpeed(player.playerBackgroundSpeed - 2);
            scrollFore.setScrollSpeed(player.playerBackgroundSpeed);


            //Update/remove enemies
            enemyList.Update(gameTime);
            enemyList.removeIfOutside(new Rectangle(-100, -100, Game1.SCREEN_WIDTH + 200, Game1.SCREEN_HEIGHT + 200));
            if (enemyList.countActive() == 0 && enemyCounter >= enemyToSpawn)
            {
                levelState = LEVELSTATE.LEVELFINISH;
            }

            //update/remove bullets
            playerBulletList.moveDeltaX(5);
            playerBulletList.removeIfOutside(new Rectangle(-100, -100, Game1.SCREEN_WIDTH + 200, Game1.SCREEN_HEIGHT + 200));

            //If player collides with enemy
            int collisionIndex = enemyList.collisionAA(player);

            if (collisionIndex != -1)
            {
                Sprite3 temp = enemyList.getSprite(collisionIndex);
                //player collides with boss
                if (temp.getName() == "boss")
                {
                }
                else
                {
                    //enemy dies?
                    temp.active  = false;
                    temp.visible = false;
                    AddExplosion((int)temp.getPosX(), (int)temp.getPosY());
                    score         -= 5;
                    textScore.text = "Score : " + score.ToString();
                    player.TakeDamage();

                    if (player.hitPoints <= 0)
                    {
                        levelState = LEVELSTATE.LEVELGAMEOVER;
                        AddExplosion((int)player.getPosX(), (int)player.getPosY());
                        player.setPos(0, -300); // offscreen
                        player.setActive(false);
                    }

                    //TODO: GET HITSOUND
                    soundEnemyHitLimit.playSoundIfOk();
                }
            }

            //if a bullet collides with enemy
            for (int i = 0; i < playerBulletList.count(); i++)
            {
                Sprite3 currentBullet = playerBulletList.getSprite(i);
                if (currentBullet == null)
                {
                    continue;
                }
                if (currentBullet.active != true)
                {
                    continue;
                }
                if (currentBullet.visible != true)
                {
                    continue;
                }

                //if collision success
                int collision = enemyList.collisionAA(currentBullet);
                if (collision != -1)
                {
                    Sprite3 currentEnemy = enemyList.getSprite(collision);

                    currentEnemy.hitPoints -= 1;
                    //DEAD ENEMY
                    if (currentEnemy.hitPoints <= 0)
                    {
                        currentEnemy.active  = false;
                        currentEnemy.visible = false;


                        //add particle
                        AddExplosion((int)currentEnemy.getPosX(), (int)currentEnemy.getPosY());

                        //add score
                        score         += 10;
                        textScore.text = "Score : " + score.ToString();
                    }
                    //DAMAGED ENEMY
                    else
                    {
                        currentEnemy.varInt0 = 100;
                        soundEnemyHitLimit.playSoundIfOk();
                    }

                    //REMOVE BULLET
                    currentBullet.active  = false;
                    currentBullet.visible = false;
                }
            }

            //update particles
            particleList.animationTick(gameTime);

            base.Update(gameTime);
        }