예제 #1
0
        public void Update(GameTime gameTime, Camera camera)
        {
            Clock.Update(gameTime);
            //list to separate the two types of sprites that need to be updated
            //need to do this in two steps to make sure they don't call update on an entity that was moving but then stop due to a collision
            ISet <IEntity> moving = entities.MovingEntities(camera.DrawRectangle);

            moving.UnionWith(enemies);//add any enemies that are out of the screen
            ISet <IEntity> animated = entities.EntitiesNeedingUpdated(camera.DrawRectangle);

            foreach (IEntity entity in moving)
            {
                Point     location    = new Point(entity.CollisionBox.X - entity.TrueWidth(), entity.CollisionBox.Y - entity.TrueHeight());
                Point     size        = new Point(3 * entity.TrueWidth(), 3 * entity.TrueHeight());
                Rectangle checkRegion = new Rectangle(location, size);//incase it somehow moved out of a square before removing itself.
                entities.Remove(entity, checkRegion);
                entity.Update(gameTime);
                //check for thecollision

                if (entity is Fireball || entity is AbstractItem || entity is BulletBill)
                {
                    if (entity.Position.X > camera.DrawRectangle.Right || entity.Position.X + entity.TrueWidth() < camera.DrawRectangle.Left)
                    {
                        entity.Collect = true;
                    }
                }
                else
                {
                    CollisionDetection.CheckAndFixEntityOutOfBounds(entity, entity.CollisionBox.Width, entity.CollisionBox.Height, (Rectangle)camera.Limits);
                }

                if (!entity.Collect)//check if the entity should be put back into the world
                {
                    entities.Insert(entity);
                    if (entity is AbstractEnemy)//try to add it to enemies, set will takes care of duplicates
                    {
                        enemies.Add((AbstractEnemy)entity);
                    }
                }
                if (entity.Collidable)
                {
                    CollisionDetection.SetOnGround(entity, entities);
                    if (entity is Goomba && !entity.OnGround)
                    {
                    }
                }
            }
            if (moving.Count > 0)
            {
                CollisionDetection.CollisionsCheckAndFix(moving, entities);
            }
            foreach (IEntity entity in animated)
            {
                entity.Update(gameTime);

                if (entity.Collect)//check if the entity should be removed from the world
                {
                    entities.Remove(entity);
                    if (entity is AbstractEnemy)
                    {
                        enemies.Remove((AbstractEnemy)entity);
                    }
                }
            }
            camera.LookAt(Stage.mario.Position);
            // Overlay.Update(gameTime);
            foreach (Layer layer in layers)
            {
                foreach (ISprite sprite in layer.Sprites)
                {
                    sprite.Update(gameTime);
                }
            }
            if (Clock.Time == 0)
            {
                OnRaiseLevelEvent(null);
            }
        }