Пример #1
0
        public TileMap(World world, string fileName)
        {
            this.fileName = fileName;
            XDocument fileContents = ScreenManager.Instance.Content.Load<XDocument>(fileName);
            int width, height;

            width = Int32.Parse(fileContents.Root.Attribute("Width").Value);
            height = Int32.Parse(fileContents.Root.Attribute("Height").Value);

            tileWidth = Int32.Parse(fileContents.Root.Attribute("TileWidth").Value);
            tileHeight = Int32.Parse(fileContents.Root.Attribute("TileHeight").Value);

            layerCount = Int32.Parse(fileContents.Root.Attribute("LayerCount").Value);
            subLayerCount = Int32.Parse(fileContents.Root.Attribute("SubLayerCount").Value);

            tiles = new Tile[width, height];

            textureService = (ITextureService)ScreenManager.Instance.Services.GetService(typeof(ITextureService));

            IEnumerable<XElement> allSheets = fileContents.Descendants("sh");

            SubTextureSheet[] sheets = new SubTextureSheet[allSheets.Count<XElement>()];
            Dictionary<string, int> sheetIndex = new Dictionary<string, int>();

            int i;
            foreach (XElement sheet in allSheets)
            {
                i = Int32.Parse(sheet.Attribute("i").Value);
                string sheetFileName = sheet.Attribute("fn").Value;
                sheets[i] = textureService.GetSheet(sheetFileName);
                sheetIndex.Add(sheetFileName, i);
            }

            IEnumerable<XElement> allTileElements = fileContents.Descendants("T");

            // This is kind of dangerous, here. We assume that the XML is properly formatted.
            //  If it is not, our program will crash. This is not a huge deal, but if the debugger
            //  is highlighting a line below, you can blame Lou, unless you tried to hack a TileMap XML file by hand.
            int j = 0;
            i = 0;
            foreach (XElement tileElement in allTileElements)
            {
                Tile tile = TileFromXElement(tileElement, sheets, i, j, world);
                this.SetTile(tile, i, j);

                if (j < height - 1)
                {
                    ++j;
                }
                else
                {
                    ++i;
                    j = 0;
                }
            }
        }
        private void SetSelectingSubTexture(bool value)
        {
            isSelectingSubTexture = value;
            tileSelector.Enabled = !value;
            tileSelector.Visible = !value;
            this.DrawOrder = value ? 500 : 25;
            foreach (SubTextureSelector sts in ((EditorScreen)ParentScreen).SubTextureSelectors)
            {
                if (sts != this)
                {
                    sts.Enabled = !value;
                    sts.DrawOrder = value ? 25 : 500;
                }
            }

            if (isSelectingSubTexture)
            {
                // We're now selecting a texture.
                if (subTexture != null)
                    this.selectingSheet = subTexture.Sheet;
                this.Bounds = new RectangleF(Position.X, Position.Y, selectingSheet.Rectangle.Width, selectingSheet.Rectangle.Height);
            }
            else
            {
                this.Bounds = new RectangleF(Position.X, Position.Y, TileMap.TileWidth, TileMap.TileWidth);
            }
        }
Пример #3
0
        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;
        }
Пример #4
0
        public System.Xml.Linq.XElement ToXElement()
        {
            XElement returnValue = new XElement("TileMap",
                                                new XAttribute("Width", Width),
                                                new XAttribute("Height", Height),
                                                new XAttribute("TileWidth", tileWidth),
                                                new XAttribute("TileHeight", tileHeight),
                                                new XAttribute("LayerCount", layerCount),
                                                new XAttribute("SubLayerCount", subLayerCount)
                                                );

            XElement sheetIndexElement = new XElement("SheetIndex");
            returnValue.Add(sheetIndexElement);

            Dictionary<string, int> sheetIndex = new Dictionary<string, int>();
            foreach (Tile tile in tiles)
            {
                if (tile != null)
                {
                    foreach (SubTexture subTexture in tile.Textures)
                    {
                        if (subTexture != null && !sheetIndex.Keys.Contains<string>(subTexture.Sheet.Name))
                        {
                            sheetIndex.Add(subTexture.Sheet.Name, sheetIndex.Count);
                        }
                    }
                }
                returnValue.Add(TileToXElement(tile, sheetIndex));
            }

            SubTextureSheet[] sheets = new SubTextureSheet[sheetIndex.Count];
            foreach (string sheetName in sheetIndex.Keys)
            {
                sheets[sheetIndex[sheetName]] = textureService.GetSheet(sheetName);
            }

            foreach (SubTextureSheet sheet in sheets)
            {
                sheetIndexElement.Add(new XElement("sh", new XAttribute("fn", sheet.Name), new XAttribute("i", sheetIndex[sheet.Name])));
            }

            return returnValue;
        }