コード例 #1
0
ファイル: InvadersGame.cs プロジェクト: saviit/mono-invaders
        /// <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)
        {
            // PLAYER CONTROLS
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                if (player.Position.X > 4)
                {
                    player.Position.X -= 4;
                    player.UpdateBoundingBox();
                }
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                if (player.Position.X + playerTex.Width < ResolutionX)
                {
                    player.Position.X += 4;
                    player.UpdateBoundingBox();
                }
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                if ((gameTime.TotalGameTime.TotalMilliseconds - playerFireCooldown) > lastFired)
                {
                    FireBullet(player.Position, gameTime);
                    lastFired = (int)gameTime.TotalGameTime.TotalMilliseconds;
                }
            }
            //----------------------------------------


            // ENEMY PROCEDURES
            foreach (Enemy enemy in enemies)
            {
                // Move enemy
                if (enemy.Position.X - enemy.MovePattern[enemy.CurrentGoal].X < 0)
                {
                    enemy.Position.X += enemy.MoveSpeed;
                    enemy.UpdateBoundingBox();
                }
                else if (enemy.Position.X - enemy.MovePattern[enemy.CurrentGoal].X > 0)
                {
                    enemy.Position.X -= enemy.MoveSpeed;
                    enemy.UpdateBoundingBox();
                }
                else //current move goal reached, change move goal
                {
                    if (enemy.CurrentGoal + 1 == enemy.MovePattern.Length)
                    {
                        enemy.CurrentGoal = 0;
                    }
                    else
                    {
                        enemy.CurrentGoal++;
                    }
                }
                // Detertime bomb dropping
                if (gameTime.TotalGameTime.TotalMilliseconds - enemy.LastDropped > bombDropCooldown)
                {
                    if (random.Next(1, bombDropChance + 1) == bombDropChance)
                    {
                        DropBomb(enemy.Position, gameTime);
                    }
                    enemy.LastDropped = (int)gameTime.TotalGameTime.TotalMilliseconds;
                }
            }

            //----------------------------------------



            // ADVANCE PROJECTILES AND CHECK COLLISIONS


            for (int i = 0; i < bullets.Count; i++)
            {
                Projectile p = bullets[i];
                p.Position.Y -= 10;
                p.UpdateBoundingBox();
                if (p.Position.Y < -(p.Texture.Height))
                {
                    bullets.Remove(p);
                    continue;
                }
                // Collision detection
                for (int j = 0; j < bunkers.Count; j++)
                {
                    Bunker b = bunkers[j];
                    if (p.BoundingBox.Intersects(b.BoundingBox))
                    {
                        bullets.Remove(p);
                        b.Health--;
                        if (b.Health < 0)
                        {
                            bunkers.Remove(b);
                        }
                        else
                        {
                            b.Texture = bunker_hit[b.Health];
                        }
                        break;
                    }
                }
                for (int k = 0; k < enemies.Count; k++)
                {
                    Enemy enemy = enemies[k];
                    if (p.BoundingBox.Intersects(enemy.BoundingBox))
                    {
                        enemies.Remove(enemy);
                        bullets.Remove(p);
                        break;
                    }
                }
            }


            for (int i = 0; i < bombs.Count; i++)
            {
                Bomb b = bombs[i];
                b.Position.Y += 10;
                b.UpdateBoundingBox();

                // Collision detection
                for (int j = 0; j < bunkers.Count; j++)
                {
                    Bunker bunker = bunkers[j];
                    if (b.BoundingBox.Intersects(bunker.BoundingBox))
                    {
                        bombs.Remove(b);
                        bunker.Health--;
                        if (bunker.Health < 0)
                        {
                            bunkers.Remove(bunker);
                        }
                        else
                        {
                            bunker.Texture = bunker_hit[bunker.Health];
                        }
                        break;
                    }
                }

                if (b.BoundingBox.Intersects(ground.BoundingBox) ||
                    b.Position.Y > ResolutionY)
                {
                    bombs.Remove(b);
                }
            }


            //----------------------------------------



            base.Update(gameTime);
        }