public void OnDestruct(DestructableTile destructable)
 {
     Explosion expl = new Explosion(destructable.World, destructable.World.SpriteBatch,
         new Vector2(destructable.X * destructable.Map.TileWidth + destructable.Map.TileWidth/2,
                     destructable.Y * destructable.Map.TileHeight + destructable.Map.TileHeight / 2));
     destructable.World.AddWorldObject(expl);
 }
        public void OnDestruct(DestructableTile destructable)
        {
            if (rand == null)
                rand = new Random();
            SubTexture texture = destructable.Textures[destructable.Map.LayerCount-1,destructable.Map.SubLayerCount-1];
            if (texture != null)
            {
                int xslice, yslice, xinc, yinc;
                xslice = yslice = 0;

                List<Fragment> fragments = new List<Fragment>();

                while (xslice < destructable.Map.TileWidth)
                {
                    yslice = 0;
                    xinc = Math.Min(rand.Next(4, 8), destructable.Map.TileWidth - xslice);
                    while (yslice < destructable.Map.TileHeight)
                    {
                        yinc = Math.Min(rand.Next(4, 8), destructable.Map.TileHeight - yslice);
                        Vector2 pos = new Vector2(destructable.X * destructable.Map.TileWidth + xslice, destructable.Y * destructable.Map.TileHeight + yslice);
                        Rectangle rect = new Rectangle(xslice, yslice, xinc, yinc);
                        destructable.World.AddWorldObject(new Fragment(destructable.World, destructable.World.SpriteBatch, pos, texture, rect));
                        yslice += yinc;
                    }
                    xslice += xinc;
                }
                //destructable.World.AddWorldObject(f1);
            }

            /*
            Vector2 f2Pos = new Vector2(f1Rect.X, destructable.Y * destructable.Map.TileHeight);
            Rectangle f2Rect = new Rectangle(f1Rect.Width, 0, texture.Rectangle.Width / 2, texture.Rectangle.Height / 2);
            Fragment f2 = new Fragment(destructable.World, destructable.World.SpriteBatch, f2Pos, texture, f2Rect);

            Vector2 f3Pos = new Vector2(destructable.X * destructable.Map.TileWidth, destructable.Y * destructable.Map.TileHeight + f1Rect.Height);
            Rectangle f3Rect = new Rectangle(f1Rect.X, f1Rect.Y, texture.Rectangle.Width / 2, texture.Rectangle.Height / 2);
            Fragment f3 = new Fragment(destructable.World, destructable.World.SpriteBatch, f1Pos, texture, f1Rect);
             * */
        }
        private Tile TileFromXElement(XElement xelement, SubTextureSheet[] sheets, int x, int y, World world)
        {
            if (xelement.Attribute("e").Value == "-1")
            {
                return null;
            }

            Tile returnVal;

            SubTexture[,] subTexturesArray = new SubTexture[layerCount, subLayerCount];

            IEnumerable<XElement> subTextureElements = xelement.Descendants("x");

            int i = 0, j = 0;
            //int index = 0; // Was needed when changing format.
            foreach( XElement subTextureElement in subTextureElements)
            {
                i = Int32.Parse(subTextureElement.Attribute("z").Value) / SubLayerCount;
                j = Int32.Parse(subTextureElement.Attribute("z").Value) % SubLayerCount;
                SubTexture subTex = new SubTexture(sheets[Int32.Parse(subTextureElement.Attribute("s").Value)], Int32.Parse(subTextureElement.Attribute("i").Value));
                subTexturesArray[i, j] = subTex;
            }

            int edgeInt;
            edgeInt = Int32.Parse(xelement.Attribute("e").Value);

            bool[] edges = new bool[4];
            for (int k = 0; k < 4; ++k)
            {
                edges[k] = (edgeInt & (int)Math.Pow(2, k)) != 0;
            }

            returnVal = new Tile(subTexturesArray, edges);

            // Check for special tile types:
            bool isDestructable = (from att in xelement.Attributes() where att.Name == "d" select att).Count() > 0;

            if (isDestructable)
            {
                List<IDestructionEffect> effects = new List<IDestructionEffect>();
                effects.Add(new ExplosionEffect());
                effects.Add(new ShatterEffect());
                returnVal = new DestructableTile(returnVal, world, x, y, float.Parse(xelement.Attribute("d").Value), effects);
            }

            return returnVal;
        }
        /// <summary>
        /// Set all selected tiles to being either destructable or non-destructable
        /// based on the bool passed in.
        /// </summary>
        /// <param name="destructable">
        /// Whether or not the tile is destructable; true if so, false if not. Duh.
        /// </param>
        public void SetDestructable(bool destructable)
        {
            foreach (int[] tileCoord in selectedTileCoordinates)
            {
                int x, y;

                x = tileCoord[0];
                y = tileCoord[1];

                Tile tile = TileMap[x, y];

                if (tile == null)
                {
                    if (destructable)
                    {
                        tile = new Tile(new SubTexture[TileMap.LayerCount, TileMap.SubLayerCount],
                                        new bool[] { false, false, false, false });

                        tile = new DestructableTile(tile, EditorScreen.World, tileCoord[0], tileCoord[1], 10.0f);
                    }
                }
                else if (tile is DestructableTile)
                {
                    if (!destructable)
                    {
                        tile = new Tile(tile); // A plain-vanilla tile, we're only extracting the texture and edge data.
                    }
                }
                else
                {
                    if (destructable)
                    {
                        tile = new DestructableTile(tile, EditorScreen.World, tileCoord[0], tileCoord[1], 10.0f);
                    }
                }

                TileMap.SetTile(tile, x, y);
            }
            notifyTilePropertyComponents();
        }