public bool DeleteOffWorldSprites(Layer layer)
        {
            bool wereSpritesDeletedFlag = false;
            float layerWidth = 1280.0f + (layer.LayerParallax.X * ((float)Camera.WorldRectangle.Width - 1280.0f));
            float layerHeight = 720.0f + (layer.LayerParallax.Y * ((float)Camera.WorldRectangle.Height - 720.0f));

            for (int i=layer.layerSprites.Count-1; i > -1; i--)
                {

                    if
                   (layer.layerSprites[i].SpriteRectangle.Bottom <  -100 ||
                    layer.layerSprites[i].SpriteRectangle.Right <  -100 ||
                    layer.layerSprites[i].SpriteRectangle.Left > (layerWidth + 100) ||
                    layer.layerSprites[i].SpriteRectangle.Top > (layerHeight + 100))
                         {
                                 layer.DeleteSpriteFromLayer(layer.layerSprites[i]);
                                 wereSpritesDeletedFlag = true;
                         }
                 }
            return wereSpritesDeletedFlag;
        }
        public static void LoadLayerData(StreamReader read)
        {
            //variables to initialize new Layer when read in
            string layerName = "";
            Vector2 layerParallax = new Vector2(1f, 0f);
            bool isAwake = true;
            bool isVisible = true;
            bool isLayerMotion = false;
            float layerVelocity = 0.0f;
            Vector2 layerVDirection = Vector2.Zero;
            bool isLayerGravity = false;
            float layerAcceleration = 0.0f;
            Vector2 layerADirection = Vector2.UnitY;

            string line = read.ReadLine();
            string elementName = null;

            do
            {
                string[] stringSeparators = new string[] { ":" };
                string[] result = line.Split(stringSeparators,
                StringSplitOptions.RemoveEmptyEntries);
                elementName = result[0];

                switch (elementName)
                {
                    case "LayerName":
                        {
                            layerName = result[1];
                            break;
                        }
                    case "LayerParallax":
                        {
                            float x = 1.0f;
                            float y = 1.0f;
                            if (result[1] == "{X")
                            {
                                float value;
                                string temp;
                                temp = result[2].Replace(" Y", null);
                                if (float.TryParse(temp, out value)) x = value;
                                temp = result[3].Replace("}", null);
                                if (float.TryParse(temp, out value)) y = value;
                            }
                            layerParallax = new Vector2(x, y);
                            break;
                        }
                    //case "IsAwake":
                    //    {
                    //        bool value;
                    //        if (bool.TryParse(result[1], out value)) isAwake = value;
                    //        break;
                    //    }
                    //case "IsVisible":
                    //    {
                    //        bool value;
                    //        if (bool.TryParse(result[1], out value)) isVisible = value;
                    //        break;
                    //    }
                    //case "IsLayerMotion":
                    //    {
                    //        bool value;
                    //        if (bool.TryParse(result[1], out value)) isLayerMotion = value;
                    //        break;
                    //    }
                    //case "LayerVelocity":
                    //    {
                    //        float value;
                    //        if (float.TryParse(result[1], out value)) layerVelocity = value;
                    //        break;
                    //    }
                    //case "LayerVDirection":
                    //    {
                    //        float x = 1.0f;
                    //        float y = 1.0f;
                    //        if (result[1] == "{X")
                    //        {
                    //            float value;
                    //            string temp;
                    //            temp = result[2].Replace(" Y", null);
                    //            if (float.TryParse(temp, out value)) x = value;
                    //            temp = result[3].Replace("}", null);
                    //            if (float.TryParse(temp, out value)) y = value;
                    //        }
                    //        layerVDirection = new Vector2(x, y);
                    //        break;
                    //    }
                    //case "IsLayerGravity":
                    //    {
                    //        bool value;
                    //        if (bool.TryParse(result[1], out value)) isLayerGravity = value;
                    //        break;
                    //    }
                    //case "LayerAcceleration":
                    //    {
                    //        float value;
                    //        if (float.TryParse(result[1], out value)) layerAcceleration = value;
                    //        break;
                    //    }
                    //case "LayerADirection":
                    //    {
                    //        float x = 1.0f;
                    //        float y = 1.0f;
                    //        if (result[1] == "{X")
                    //        {
                    //            float value;
                    //            string temp;
                    //            temp = result[2].Replace(" Y", null);
                    //            if (float.TryParse(temp, out value)) x = value;
                    //            temp = result[3].Replace("}", null);
                    //            if (float.TryParse(temp, out value)) y = value;
                    //        }
                    //        layerADirection = new Vector2(x, y);
                    //        break;
                    //    }
                    default:
                        break;
                }
                line = read.ReadLine();
            } while (line != "<ENDLAYER>");

            Layer newLayer = new Layer(layerName, layerParallax, isAwake, isVisible, isLayerMotion, layerVelocity, layerVDirection, isLayerGravity, layerAcceleration, layerADirection);
            parallaxEngine.AddLayerToWorld(newLayer);

            return;
        }
        public Layer Clone()
        {
            Layer data = new Layer ();

            data.layerName = this.layerName;
            data.layerParallax = this.layerParallax;
            data.isAwake = this.isAwake;
            data.isVisible = this.isVisible;
            data.isExpired = this.isExpired;

            data.isLayerMotion = this.isLayerMotion;
            data.layerVelocity = this.layerVelocity;
            data.layerVDirection = this.layerVDirection;
            data.isLayerGravity = this.isLayerGravity;
            data.layerAcceleration = this.layerAcceleration;
            data.LayerADirection = this.LayerADirection;
            foreach (Sprite sprite in this.layerSprites)
            {
                data.layerSprites.Add(sprite.Clone());
            }
            return data;
        }
        public void NewMap(string _loadFilePath, int _tileWidth, int _tileHeight, int _tilesWide, int _tilesHigh)
        {
            worldGridTileWidth = _tileWidth;
            worldGridTileHeight = _tileHeight;
            worldGridMapWidth = _tilesWide;
            worldGridMapHeight = _tilesHigh;
            worldWidth = worldGridTileWidth * worldGridMapWidth;
            worldHeight = worldGridTileHeight * worldGridMapHeight;
            Camera.WorldRectangle = new Rectangle(0, 0, worldWidth, worldHeight);
            Camera.LookAt(new Vector2(0, (worldHeight - Camera.ViewportHeight)));
            world = 0;
            level = 0;

            //in the New Map function, a texture pack file was opened in the editor and that path passed here.
            //the texture pack file is just like a level file except it only contains instructions to load a list of textures
            LoadMap(_loadFilePath);
            Layer newLayer = new Layer("layer 0");
            parallaxEngine.AddLayerToWorld(newLayer);
            editingLayer = 0;
        }
        public void CreateSplitShot(World physics, Layer playLayer)
        {
            for (int i = 0; i < 3; i++)
            {
                splitShot[i].IsExpired = false;
                splitShot[i].IsVisible = true;
                splitShot[i].IsHit = false;
                splitShot[i].IsAwake = true;
                splitShot[i].IsCollidable = true;
                splitShot[i].Scale = shot.Scale;
                splitShot[i].SpriteType = Sprite.Type.FruitShot;
                splitShot[i].TintColor = new Color(255f, 255f, 255f, 255f);
                Vector2 position = ConvertUnits.ToSimUnits(shot.Location + shot.SpriteOrigin);
                if (i == 0) position = new Vector2(position.X - ConvertUnits.ToSimUnits(24f), position.Y);
                if (i == 2) position = new Vector2(position.X + ConvertUnits.ToSimUnits(24f), position.Y);
                splitShot[i].spriteBody = BodyFactory.CreateEllipse(physicsWorld, ConvertUnits.ToSimUnits(16f), ConvertUnits.ToSimUnits(16f), 6, 1.0f, position, splitShot[i]);
                splitShot[i].spriteBody.BodyType = BodyType.Dynamic;
                splitShot[i].spriteBody.Mass = GameSettings.FruitMass/3f;
                splitShot[i].Location = ConvertUnits.ToDisplayUnits( splitShot[i].spriteBody.Position);
                splitShot[i].spriteBody.Rotation = shot.spriteBody.Rotation;
                splitShot[i].spriteBody.LinearVelocity = shot.spriteBody.LinearVelocity;
                if (i == 0) splitShot[i].spriteBody.LinearVelocity = new Vector2(splitShot[i].spriteBody.LinearVelocity.X - ConvertUnits.ToSimUnits(100f), splitShot[i].spriteBody.LinearVelocity.Y);
                if (i == 2) splitShot[i].spriteBody.LinearVelocity = new Vector2(splitShot[i].spriteBody.LinearVelocity.X + ConvertUnits.ToSimUnits(100f), splitShot[i].spriteBody.LinearVelocity.Y);
                splitShot[i].spriteBody.AngularVelocity = shot.spriteBody.AngularVelocity+LevelDataManager.rand.Next(-10,11);
                splitShot[i].spriteBody.IgnoreGravity = false;
                splitShot[i].spriteBody.IsBullet = true;
                splitShot[i].spriteBody.Restitution = GameSettings.FruitBouncy;
                splitShot[i].spriteBody.Friction = GameSettings.FruitFriction;
                splitShot[i].HitPoints = (GameSettings.FruitHP/3)+1;
                playLayer.AddSpriteToLayer(splitShot[i]);
            }

            shot.spriteBody.ResetDynamics();
            shot.spriteBody.Enabled = false;
            shot.IsVisible = false;
            shot.spriteBody.Position = ConvertUnits.ToSimUnits(new Vector2 (-100f,-100f));
            shot.spriteBody.IgnoreGravity = true;
            shot.IsCollidable = false;
            isSplit = true;
            return;
        }
 public void DeleteLayerFromWorld(Layer layer)
 {
     this.worldLayers.Remove(layer);
     layer = null;
     return;
 }
 public void CopyLayerToWorld(Layer copyThisLayer)
 {
     this.worldLayers.Add(copyThisLayer.Clone());
     return;
 }
 public void AddLayerToWorld(Layer layer)
 {
     this.worldLayers.Add(layer);
 }
        public DecoManager(ParallaxManager _parallaxManager, bool _isSound)
        {
            IsSound = _isSound;

            DecoSprites = new List<Sprite>();
            StarSprites = new List<Sprite>();
            CloudSprites = new List<Sprite>();
            WeatherSprites = new List<Sprite>();
            parallaxEngine = _parallaxManager;
            SoundManager.ClearSounds();

            int decoworld = LevelDataManager.world;

            if (decoworld == 6) //bonus world
            {
                switch (LevelDataManager.level)
                {
                    case 1:
                    case 2:
                    case 4:
                        {
                            decoworld = 1; //play grass
                            break;
                        }
                    case 5:
                    case 13:
                        {
                            decoworld = 2; //forest
                            break;
                        }
                    case 6:
                    case 7:
                    case 8:
                        {
                            decoworld = 3; //desert
                            break;
                        }
                    case 3:
                    case 14:
                        {
                            decoworld = 4; //snow
                            break;
                        }
                    case 9:
                    case 10:
                    case 11:
                    case 12:
                    case 15:
                        {
                            decoworld = 5; //factory
                            break;
                        }
                    default:
                        break;
                }
            }

            if (decoworld != 5)
            {
                #region ROLL RANDOM TIME DAY, SUNSET, NIGHT
                int timeRoll = LevelDataManager.rand.Next(1, 11);
                if (timeRoll < 5) //day 40%
                {
                    for (int i = 0; i < parallaxEngine.worldLayers[0].layerSprites.Count; i++)
                    {
                        parallaxEngine.worldLayers[0].layerSprites[i].TextureID = 34;
                    }
                    IsSunset = false;
                    IsNight = false;
                }
                if (timeRoll == 5 || timeRoll == 6) //sunset 20%
                {
                    for (int i = 0; i < parallaxEngine.worldLayers[0].layerSprites.Count; i++)
                    {
                        parallaxEngine.worldLayers[0].layerSprites[i].TextureID = 35;
                    }
                    IsSunset = true;
                    IsNight = false;
                }
                if (timeRoll > 6) //night 40%
                {
                    for (int i = 0; i < parallaxEngine.worldLayers[0].layerSprites.Count; i++)
                    {
                        parallaxEngine.worldLayers[0].layerSprites[i].TextureID = 36;
                    }
                    IsNight = true;
                    IsSunset = false;
                    if (IsSound) SoundManager.Play(SoundManager.Sound.EffectCricketsLoop, true, false);
                }
                #endregion
            }

            if (LevelDataManager.world == 5 && LevelDataManager.level == 5)
            {
                for (int i = 0; i < parallaxEngine.worldLayers[0].layerSprites.Count; i++)
                {
                    parallaxEngine.worldLayers[0].layerSprites[i].TextureID = 36;
                }
                IsNight = true;
            }

            #region REMOVE PLACED MOON OR ADD MOON
            if (!IsNight && parallaxEngine.worldLayers[1].layerSprites[0].TextureID == 33) parallaxEngine.worldLayers.RemoveAt(1);
            if (IsNight && parallaxEngine.worldLayers[1].layerSprites[0].TextureID != 33)
            {
                int maxMoonHeight = (int)Math.Sqrt(Camera.ViewportHeight) * 70;
                Layer moonLayer = new Layer("moon", Vector2.Zero, false, true, false, 0f, Vector2.Zero, false, 0f, Vector2.Zero);
                Vector2 moonLocation = new Vector2( LevelDataManager.rand.Next(-400,Camera.ViewportWidth+150), (float)Math.Pow((float)LevelDataManager.rand.Next(0, maxMoonHeight) / 100f, (2.0)));
                Sprite moon = new Sprite(33, 0, moonLocation, false);
                moonLayer.AddSpriteToLayer(moon);
                parallaxEngine.worldLayers.Insert(1, moonLayer);
            }
            #endregion

            #region CREATE STARS
            if (IsNight)
            {
                starPerCycle = numberOfStars / 10;
                int maxStarHeight = (int)Math.Sqrt(Camera.ViewportHeight) * 70;
                Layer starLayer = new Layer("stars", Vector2.Zero, false, true, false, 0f, Vector2.Zero, false, 0f, Vector2.Zero);
                for (int i = 0; i < numberOfStars; i++)
                {
                    int starID = 21 + LevelDataManager.rand.Next(0, 2);
                    Vector2 starLocation = new Vector2(LevelDataManager.rand.Next(0, Camera.ViewportWidth), (float)Math.Pow((float)LevelDataManager.rand.Next(0, maxStarHeight) / 100f, (2.0)));
                    Sprite star = new Sprite(starID, 0, starLocation, true);
                    star.IsAwake = false;
                    star.Scale = (float)LevelDataManager.rand.Next(50, 101) / 100f;
                    star.TotalRotation = MathHelper.ToRadians(LevelDataManager.rand.Next(0, 361));
                    star.TintColor *= (float)LevelDataManager.rand.Next(0, 101) / 100f;
                    StarSprites.Add(star);
                    starLayer.AddSpriteToLayer(star);
                }
                parallaxEngine.worldLayers.Insert(1, starLayer);
            }

            #endregion

            if (parallaxEngine.worldLayers[0].layerSprites[0].TextureID == 36 || parallaxEngine.worldLayers[0].layerSprites[0].TextureID == 35 || parallaxEngine.worldLayers[0].layerSprites[0].TextureID == 34)
            {
                #region CREATE CLOUD LAYER
                cloudsPerCycle = numberOfClouds / 10;
                int maxCloudHeight = 400;
                Layer cloudLayer = new Layer("clouds", Vector2.Zero, false, true, false, 0f, Vector2.Zero, false, 0f, Vector2.Zero);
                for (int i = 0; i < numberOfClouds; i++)
                {
                    Vector2 cloudLocation = new Vector2(LevelDataManager.rand.Next(-200, Camera.ViewportWidth + 200), LevelDataManager.rand.Next(-100, maxCloudHeight));
                    Sprite cloud = new Sprite(23, LevelDataManager.rand.Next(0, 3), cloudLocation, true);
                    cloud.IsAwake = false;
                    if (IsSunset) cloud.TextureIndex += 3;
                    if (IsNight) cloud.TextureIndex += 6;
                    cloud.Scale = (float)LevelDataManager.rand.Next(100, 401) / 100f;
                    if (LevelDataManager.rand.Next(0, 2) == 0) cloud.IsFlippedHorizontally = true;
                    if (IsSunset) cloud.TintColor = duskTint;
                    if (IsNight) cloud.TintColor = nightTint;
                    cloud.TintColor *= (float)LevelDataManager.rand.Next(0, 51) / 100f;
                    cloud.Velocity = LevelDataManager.rand.Next(20, 60);
                    if (LevelDataManager.rand.Next(0, 2) == 0) cloud.Velocity *= -1;
                    CloudSprites.Add(cloud);
                    cloudLayer.AddSpriteToLayer(cloud);
                }
                if (IsNight) parallaxEngine.worldLayers.Insert(3, cloudLayer);
                if (IsSunset) parallaxEngine.worldLayers.Insert(1, cloudLayer);
                if (!IsSunset && !IsNight) parallaxEngine.worldLayers.Insert(1, cloudLayer);
                #endregion
            }

            #region CREATE WEATHER
            if (decoworld == 0) weather = Weather.Pollen;
            if (decoworld == 1) weather = Weather.Pollen;
            if (decoworld == 2) weather = Weather.Leaf;
            if (numberOfClouds > 70 && LevelDataManager.rand.Next(0, 2) == 0) weather = Weather.Rain; //50% chance of rain if cloud cover in top 20% (10% chance total)
            if (decoworld == 4) weather = Weather.Snow;
            if (decoworld == 3) weather = Weather.None;
            if (decoworld == 5) weather = Weather.None;

            int partID = 0;
            int tiles = 1;
            weatherLayer = new Layer("weather",weatherParallax,false,true,false,0f,Vector2.Zero,false,0f,Vector2.Zero);
            if (weather == Weather.Rain)
            {
                partID = 26;
                numberOfWeatherParticles = Math.Max(0, numberOfClouds - 60) * LevelDataManager.rand.Next(6,13);
                if (IsSound) SoundManager.Play(SoundManager.Sound.EffectRainLoop, true, false);
            }
            if (weather == Weather.Snow)
            {
                numberOfWeatherParticles = Math.Max(0, numberOfClouds - 30) * LevelDataManager.rand.Next(2,5);
                partID = 27;
                tiles = 5;
            }
            if (weather == Weather.Pollen)
            {
                numberOfWeatherParticles = LevelDataManager.rand.Next(10, 21);
                partID = 25;
                tiles = 3;
            }
            if (weather == Weather.Leaf)
            {
                numberOfWeatherParticles = LevelDataManager.rand.Next(8, 16);
                partID = 24;
                tiles = 9;
            }
            //weatherPartPerCycle = numberOfWeatherParticles / 10;
            weatherPartPerCycle = numberOfWeatherParticles;
            for (int i = 0; i < numberOfWeatherParticles; i++)
            {
                Vector2 weatherParticleLocation = Camera.ScreenToWorld(new Vector2(LevelDataManager.rand.Next(0, Camera.Viewport.Width),
                                                              LevelDataManager.rand.Next(0, Camera.Viewport.Height)),weatherLayer.LayerParallax);
                Sprite weatherP = new Sprite(partID, LevelDataManager.rand.Next(0, tiles), Vector2.Zero, true);
                weatherP.IsAwake = false;
                if (weather == Weather.Rain) NewRain(weatherP, weatherParticleLocation);
                if (weather == Weather.Snow) NewSnow(weatherP,weatherParticleLocation);
                if (weather == Weather.Pollen) NewPollen(weatherP,weatherParticleLocation);
                if (weather == Weather.Leaf) NewLeaf(weatherP,weatherParticleLocation);
                WeatherSprites.Add(weatherP);
                weatherLayer.AddSpriteToLayer(weatherP);
            }

            #endregion

            #region TINT
            for (int i = 0; i < parallaxEngine.worldLayers.Count; i++)
            {
                for (int j = 0; j < parallaxEngine.worldLayers[i].SpriteCount; j++)
                {
                    if (parallaxEngine.worldLayers[i].layerSprites[j].TextureID < 20 ||
                        (parallaxEngine.worldLayers[i].layerSprites[j].IsEffect && parallaxEngine.worldLayers[i].layerSprites[j].TextureID >= 24))
                    {
                        TintSprite(parallaxEngine.worldLayers[i].layerSprites[j]);
                    }

                    //tint for rain
                    if (weather == Weather.Rain)
                    {
                        if (parallaxEngine.worldLayers[i].layerSprites[j].TextureID < 20 || parallaxEngine.worldLayers[i].layerSprites[j].SpriteType == Sprite.Type.Deco ||
                            parallaxEngine.worldLayers[i].layerSprites[j].TextureID == 34 || parallaxEngine.worldLayers[i].layerSprites[j].TextureID == 35 || parallaxEngine.worldLayers[i].layerSprites[j].TextureID == 36 ||
                            parallaxEngine.worldLayers[i].layerSprites[j].IsEffect)
                        {
                            parallaxEngine.worldLayers[i].layerSprites[j].TintColor = Color.Lerp(parallaxEngine.worldLayers[i].layerSprites[j].TintColor, Color.Gray,0.5f);
                        }
                        Tint = Color.Lerp(Tint, Color.Gray, 0.5f);
                    }
                }
            }
            #endregion
        }