コード例 #1
0
ファイル: Game.cs プロジェクト: isaacwycoff/GrimDorkness
        /// <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)
        {
            fader.Update(gameTime);

            if (gameState == GameStatus.startMenu)
            {
                ParseInput(gameTime);           // fixme
                return;
            }

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            foreach (Entity tmpEntity in newEntities)
            {
                listOfEntities.Add(tmpEntity);
            }

            newEntities.RemoveRange(0, newEntities.Count());

            // this code should be off in its own method
            // add more planes:
            if (numberOfEnemies < MAX_ENEMIES && randomizer.Next(10) < 1)
            {

                Vector2 tmpVector = new Vector2(randomizer.Next(10, 790), -50);     // FIXME: get rid of magic numbers

                ShipType tmpShipType = (ShipType)randomizer.Next(0, 3);

                AIType tmpAIType = (AIType)randomizer.Next(0, 4);

                // FIXME: this should be controlled differently
                if (enemiesPassed % ZEPPELIN_FREQUENCY == (ZEPPELIN_FREQUENCY - 1))
                {
                    ZeppelinBoss tmpZepp = new ZeppelinBoss(zeppelinTexture, new Vector2(400, -250), this);
                    listOfEntities.Add(tmpZepp);
                }
                else
                {

                    EnemyShip tmpEnemyShip = new EnemyShip(enemyTexture, tmpShipType, tmpAIType, tmpVector, this);
                    listOfEntities.Add(tmpEnemyShip);
                }
                ++numberOfEnemies;
                ++enemiesPassed;

            }

            // parse player's keypresses
            ParseInput(gameTime);

            List<Entity> entitiesToDelete = new List<Entity>();

            // go thru each entity and update its AI:
            foreach (Entity tmpEntity in listOfEntities)
            {
                tmpEntity.Update(gameTime);

                // if an entity is off the screen, mark it for death!
                if (!tmpEntity.isVisible()) entitiesToDelete.Add(tmpEntity);
            }

            // collision detection -- FIXME: this should be its own function at the very least.
            for (int compare_index1 = 0; compare_index1 < listOfEntities.Count; compare_index1++)
            {
                for (int compare_index2 = compare_index1 + 1; compare_index2 < listOfEntities.Count; compare_index2++)
                {
                    Entity compareEntity1 = listOfEntities[compare_index1];
                    Entity compareEntity2 = listOfEntities[compare_index2];

                    if(compareEntity1.Collision() &&
                       compareEntity2.Collision() &&
                       (compareEntity1.GetTeam() != compareEntity2.GetTeam())
                       )
                    {
                    Rectangle rect1 = compareEntity1.ScreenRect();
                    Rectangle rect2 = compareEntity2.ScreenRect();

                    if (rect1.Intersects(rect2))
                    {
                        // FIXME: this should take data from the individual ships
                        compareEntity1.TakeDamage(5);
                        compareEntity2.TakeDamage(5);

                        // FIXME
                        if (!compareEntity1.isVisible() || !compareEntity2.isVisible())
                        {
                            if (randomizer.Next(0, 10) < 1)
                            {
                                Entity tmpPowerUp = new PowerUp(powerUpsTexture, PowerUpType.repair, compareEntity1.CurrentPosition());
                                listOfPowerUps.Add(tmpPowerUp);

                            }

                        }

                        // randomize the pitch of the explosion
                        // TODO: put this in a separate class
                        float pitchShift = -0.1f * (float)randomizer.Next(1, 5) - 0.5f;

                        explosion1.Play(0.20f, pitchShift, 0.0f);

                        Vector2 tmpPosition = listOfEntities[compare_index2].CurrentPosition();

                        // create the physical explosion:
                        Explosion tmpExplosion = new Explosion(explosionsTexture, ExplosionType.Basic, tmpPosition);

                        listOfExplosions.Add(tmpExplosion);

                    }
                    }
                }

                // power-up collisions:

                foreach (Entity tmpPowerUp in listOfPowerUps)
                {
                    if (player.ScreenRect().Intersects(tmpPowerUp.ScreenRect()))
                    {
                        tmpPowerUp.Destroy();

                        // FIXME: this should be dealt with by the the powerup + player
                        player.Repair(1);

                    }
                }

                // FIXME: this is messy! should probably be split off
                if (player.GetHealth() <= 0 && gameState == GameStatus.game)
                {
                    gameState = GameStatus.dead;

                    MediaPlayer.IsRepeating = false;
                    MediaPlayer.Play(Content.Load<Song>("Music/taps"));
                    MediaPlayer.Volume = 1.0f;

                }
            }
            // end collision detection

            // remove any entities that have been marked for death:
            // have to do it this way, or the foreach code above will freak out
            foreach (Entity tmpEntity in entitiesToDelete)
            {
                listOfEntities.Remove(tmpEntity);
                if (tmpEntity is EnemyShip) --numberOfEnemies;
            }

            // power-ups next
            // update & delete ones that are finished       --- TODO: put this all in a separate function that gets called repeatedly
            List<Entity> powerUpsToDelete = new List<Entity>();

            foreach (Entity tmpPowerUp in listOfPowerUps)
            {
                tmpPowerUp.Update(gameTime);
                if (!tmpPowerUp.isVisible()) powerUpsToDelete.Add(tmpPowerUp);
            }

            foreach (Entity tmpPowerUp in powerUpsToDelete)
            {
                listOfPowerUps.Remove(tmpPowerUp);
            }

            // explosions now!
            // update and delete ones that are finished
            List<Entity> explosionsToDelete = new List<Entity>();

            foreach (Entity tmpExplosion in listOfExplosions)
            {
                tmpExplosion.Update(gameTime);
                if (!tmpExplosion.isVisible()) explosionsToDelete.Add(tmpExplosion);
            }

            foreach (Entity tmpExplosion in explosionsToDelete)
            {
                listOfExplosions.Remove(tmpExplosion);
            }

            base.Update(gameTime);
        }
コード例 #2
0
ファイル: Game.cs プロジェクト: isaacwycoff/GrimDorkness
        /// <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)
        {
            fader.Update(gameTime);

            if (gameState == GameStatus.startMenu)
            {
                ParseInput(gameTime);           // fixme
                return;
            }

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            // TODO: Add your update logic here

            foreach (Entity tmpEntity in newEntities)
            {
                listOfEntities.Add(tmpEntity);
            }

            newEntities.RemoveRange(0, newEntities.Count());

            // this code should be off in its own method
            // add more planes:
            if (numberOfEnemies < MAX_ENEMIES && randomizer.Next(10) < 1)
            {
                Vector2 tmpVector = new Vector2(randomizer.Next(10, 790), -50);     // FIXME: get rid of magic numbers

                ShipType tmpShipType = (ShipType)randomizer.Next(0, 3);

                AIType tmpAIType = (AIType)randomizer.Next(0, 4);

                // FIXME: this should be controlled differently
                if (enemiesPassed % ZEPPELIN_FREQUENCY == (ZEPPELIN_FREQUENCY - 1))
                {
                    ZeppelinBoss tmpZepp = new ZeppelinBoss(zeppelinTexture, new Vector2(400, -250), this);
                    listOfEntities.Add(tmpZepp);
                }
                else
                {
                    EnemyShip tmpEnemyShip = new EnemyShip(enemyTexture, tmpShipType, tmpAIType, tmpVector, this);
                    listOfEntities.Add(tmpEnemyShip);
                }
                ++numberOfEnemies;
                ++enemiesPassed;
            }

            // parse player's keypresses
            ParseInput(gameTime);

            List <Entity> entitiesToDelete = new List <Entity>();

            // go thru each entity and update its AI:
            foreach (Entity tmpEntity in listOfEntities)
            {
                tmpEntity.Update(gameTime);

                // if an entity is off the screen, mark it for death!
                if (!tmpEntity.isVisible())
                {
                    entitiesToDelete.Add(tmpEntity);
                }
            }

            // collision detection -- FIXME: this should be its own function at the very least.
            for (int compare_index1 = 0; compare_index1 < listOfEntities.Count; compare_index1++)
            {
                for (int compare_index2 = compare_index1 + 1; compare_index2 < listOfEntities.Count; compare_index2++)
                {
                    Entity compareEntity1 = listOfEntities[compare_index1];
                    Entity compareEntity2 = listOfEntities[compare_index2];

                    if (compareEntity1.Collision() &&
                        compareEntity2.Collision() &&
                        (compareEntity1.GetTeam() != compareEntity2.GetTeam())
                        )
                    {
                        Rectangle rect1 = compareEntity1.ScreenRect();
                        Rectangle rect2 = compareEntity2.ScreenRect();


                        if (rect1.Intersects(rect2))
                        {
                            // FIXME: this should take data from the individual ships
                            compareEntity1.TakeDamage(5);
                            compareEntity2.TakeDamage(5);

                            // FIXME
                            if (!compareEntity1.isVisible() || !compareEntity2.isVisible())
                            {
                                if (randomizer.Next(0, 10) < 1)
                                {
                                    Entity tmpPowerUp = new PowerUp(powerUpsTexture, PowerUpType.repair, compareEntity1.CurrentPosition());
                                    listOfPowerUps.Add(tmpPowerUp);
                                }
                            }

                            // randomize the pitch of the explosion
                            // TODO: put this in a separate class
                            float pitchShift = -0.1f * (float)randomizer.Next(1, 5) - 0.5f;

                            explosion1.Play(0.20f, pitchShift, 0.0f);

                            Vector2 tmpPosition = listOfEntities[compare_index2].CurrentPosition();

                            // create the physical explosion:
                            Explosion tmpExplosion = new Explosion(explosionsTexture, ExplosionType.Basic, tmpPosition);

                            listOfExplosions.Add(tmpExplosion);
                        }
                    }
                }



                // power-up collisions:



                foreach (Entity tmpPowerUp in listOfPowerUps)
                {
                    if (player.ScreenRect().Intersects(tmpPowerUp.ScreenRect()))
                    {
                        tmpPowerUp.Destroy();

                        // FIXME: this should be dealt with by the the powerup + player
                        player.Repair(1);
                    }
                }

                // FIXME: this is messy! should probably be split off
                if (player.GetHealth() <= 0 && gameState == GameStatus.game)
                {
                    gameState = GameStatus.dead;

                    MediaPlayer.IsRepeating = false;
                    MediaPlayer.Play(Content.Load <Song>("Music/taps"));
                    MediaPlayer.Volume = 1.0f;
                }
            }
            // end collision detection

            // remove any entities that have been marked for death:
            // have to do it this way, or the foreach code above will freak out
            foreach (Entity tmpEntity in entitiesToDelete)
            {
                listOfEntities.Remove(tmpEntity);
                if (tmpEntity is EnemyShip)
                {
                    --numberOfEnemies;
                }
            }

            // power-ups next
            // update & delete ones that are finished       --- TODO: put this all in a separate function that gets called repeatedly
            List <Entity> powerUpsToDelete = new List <Entity>();

            foreach (Entity tmpPowerUp in listOfPowerUps)
            {
                tmpPowerUp.Update(gameTime);
                if (!tmpPowerUp.isVisible())
                {
                    powerUpsToDelete.Add(tmpPowerUp);
                }
            }

            foreach (Entity tmpPowerUp in powerUpsToDelete)
            {
                listOfPowerUps.Remove(tmpPowerUp);
            }


            // explosions now!
            // update and delete ones that are finished
            List <Entity> explosionsToDelete = new List <Entity>();

            foreach (Entity tmpExplosion in listOfExplosions)
            {
                tmpExplosion.Update(gameTime);
                if (!tmpExplosion.isVisible())
                {
                    explosionsToDelete.Add(tmpExplosion);
                }
            }

            foreach (Entity tmpExplosion in explosionsToDelete)
            {
                listOfExplosions.Remove(tmpExplosion);
            }


            base.Update(gameTime);
        }