Exemple #1
0
        public AutoTiler(string xmlDescription)
        {
            XmlDocument document = new XmlDocument();

            document.LoadXml(xmlDescription);
            Dictionary <char, XmlElement> dictionary = new Dictionary <char, XmlElement>();

            foreach (object obj in document.GetElementsByTagName("Tileset"))
            {
                XmlElement xmlElement = (XmlElement)obj;
                char       c          = xmlElement.AttrChar("id");
                Bitmap     tileImages = Gameplay.GetImage("tilesets/" + xmlElement.Attr("path"));
                TileGrid   tileset    = new TileGrid(8, 8, tileImages.Width / 8, tileImages.Height / 8);
                tileset.Load(tileImages);
                TerrainType terrainType = new TerrainType(c);
                ReadInto(terrainType, tileset, xmlElement);

                if (xmlElement.HasAttr("copy"))
                {
                    char key = xmlElement.AttrChar("copy");
                    if (!dictionary.ContainsKey(key))
                    {
                        throw new Exception("Copied tilesets must be defined before the tilesets that copy them!");
                    }
                    ReadInto(terrainType, tileset, dictionary[key]);
                }

                if (xmlElement.HasAttr("ignores"))
                {
                    foreach (string text in xmlElement.Attr("ignores").Split(','))
                    {
                        if (text.Length > 0)
                        {
                            terrainType.Ignores.Add(text[0]);
                        }
                    }
                }

                dictionary.Add(c, xmlElement);
                lookup.Add(c, terrainType);
            }
        }
Exemple #2
0
        public void Overlay(MapElement level, string objects, int width, int height, TileGrid tileset)
        {
            MapElement       tileData = level.SelectFirst(objects);
            VirtualMap <int> map      = ReadMapInt(tileData == null ? string.Empty : tileData.Attr("InnerText"), width, height);

            if (map.Columns == 0 || map.Rows == 0)
            {
                return;
            }

            for (int i = 0; i < map.Columns; i++)
            {
                for (int j = 0; j < map.Rows; j++)
                {
                    Bitmap tile = tileset[map[i, j]];
                    if (tile != null)
                    {
                        this[i, j] = tile;
                    }
                }
            }
        }
Exemple #3
0
 private void ReadInto(TerrainType data, TileGrid tileset, XmlElement xml)
 {
     foreach (object obj in xml)
     {
         if (!(obj is XmlComment))
         {
             XmlElement xml2 = obj as XmlElement;
             string     text = xml2.Attr("mask");
             Tiles      tiles;
             if (text == "center")
             {
                 tiles = data.Center;
             }
             else if (text == "padding")
             {
                 tiles = data.Padded;
             }
             else
             {
                 Masked masked = new Masked();
                 tiles = masked.Tiles;
                 int i   = 0;
                 int num = 0;
                 while (i < text.Length)
                 {
                     if (text[i] == '0')
                     {
                         masked.Mask[num++] = 0;
                     }
                     else if (text[i] == '1')
                     {
                         masked.Mask[num++] = 1;
                     }
                     else if (text[i] == 'x' || text[i] == 'X')
                     {
                         masked.Mask[num++] = 2;
                     }
                     i++;
                 }
                 data.Masked.Add(masked);
             }
             string[] array = xml2.Attr("tiles").Split(';');
             for (int j = 0; j < array.Length; j++)
             {
                 string[] array2 = array[j].Split(',');
                 int      x      = int.Parse(array2[0]);
                 int      y      = int.Parse(array2[1]);
                 Bitmap   item   = tileset.Tiles[x, y];
                 tiles.Textures.Add(item);
             }
             if (xml2.HasAttr("sprites"))
             {
                 foreach (string item2 in xml2.Attr("sprites").Split(','))
                 {
                     tiles.OverlapSprites.Add(item2);
                 }
                 tiles.HasOverlays = true;
             }
         }
     }
     data.Masked.Sort(delegate(Masked a, Masked b) {
         int num2 = 0;
         int num3 = 0;
         for (int k = 0; k < 9; k++)
         {
             if (a.Mask[k] == 2)
             {
                 num2++;
             }
             if (b.Mask[k] == 2)
             {
                 num3++;
             }
         }
         return(num2 - num3);
     });
 }
Exemple #4
0
        public Bitmap GenerateMap(MapElement element)
        {
            string package = element.Attr("_package", "map");

            TileGrid.DefaultBackground = Util.HexToColor("000000");             //Util.HexToColor("48106e");//

            List <MapElement> levels        = element.Select("levels", "level");
            Rectangle         chapterBounds = GetChapterBounds(levels);

            Rectangle viewport = new Rectangle(0, 0, chapterBounds.Width, chapterBounds.Height);
            //Rectangle viewport = new Rectangle(250, 3000, 300, 200);
            //Rectangle viewport = GetLevelBounds(levels, chapterBounds, "lvl_08-c");
            Bitmap     chapter = new Bitmap(viewport.Width, viewport.Height, PixelFormat.Format32bppArgb);
            MapElement bgs     = element.SelectFirst("Style", "Backgrounds");
            MapElement fgs     = element.SelectFirst("Style", "Foregrounds");

            background.SetLevelBounds(levels);
            foreground.SetLevelBounds(levels);
            string   sceneryTileset = "scenery";
            TileGrid scenery        = GetTileset(sceneryTileset);

            for (int i = 0; i < levels.Count; i++)
            {
                MapElement level = levels[i];

                int x           = level.AttrInt("x", 0);
                int y           = level.AttrInt("y", 0);
                int width       = level.AttrInt("width", 0);
                int height      = level.AttrInt("height", 0);
                int widthTiles  = width / 8;
                int heightTiles = height / 8;

                Point     pos         = new Point(x - chapterBounds.X, y - chapterBounds.Y);
                Point     offset      = new Point(pos.X - viewport.X, pos.Y - viewport.Y);
                Rectangle levelBounds = new Rectangle(pos.X, pos.Y, width, height);
                if (!levelBounds.IntersectsWith(viewport))
                {
                    continue;
                }

                TileGrid tiles   = GenerateLevelTiles(level, "bg", widthTiles, heightTiles, background, out VirtualMap <char> solids);
                string   tileset = level.SelectFirst("bgtiles").Attr("tileset", "Scenery");
                if (tileset.Equals("terrain", StringComparison.OrdinalIgnoreCase))
                {
                    tileset = "scenery";
                }
                if (tileset != sceneryTileset)
                {
                    scenery        = GetTileset(tileset);
                    sceneryTileset = tileset;
                }
                tiles.Overlay(level, "bgtiles", widthTiles, heightTiles, scenery);

                using (Bitmap map = tiles.DisplayMap(level, Backdrop.CreateBackdrops(bgs, levels), new Rectangle(pos, chapterBounds.Size), true)) {
                    OverlayDecals(level.Select("bgdecals", "decal"), map);
                    tiles = GenerateLevelTiles(level, "solids", widthTiles, heightTiles, foreground, out solids);
                    OverlayEntities(level.SelectFirst("entities"), map, solids, true);
                    Util.CopyTo(chapter, map, offset);
                }

                tileset = level.SelectFirst("fgtiles").Attr("tileset", "Scenery");
                if (tileset.Equals("terrain", StringComparison.OrdinalIgnoreCase))
                {
                    tileset = "scenery";
                }
                if (tileset != sceneryTileset)
                {
                    scenery        = GetTileset(tileset);
                    sceneryTileset = tileset;
                }

                tiles.Overlay(level, "fgtiles", widthTiles, heightTiles, scenery);
                using (Bitmap map = tiles.DisplayMap(level, Backdrop.CreateBackdrops(fgs, levels), new Rectangle(pos, chapterBounds.Size), false)) {
                    OverlayDecals(level.Select("fgdecals", "decal"), map);
                    OverlayEntities(level.SelectFirst("entities"), map, solids, false);
                    Util.CopyTo(chapter, map, offset);
                }

                //XmlNode objtiles = level.SelectSingleNode("objtiles");
            }

            levels = element.Select("Filler", "rect");
            for (int i = 0; i < levels.Count; i++)
            {
                MapElement level = levels[i];

                int x      = level.AttrInt("x", 0);
                int y      = level.AttrInt("y", 0);
                int width  = level.AttrInt("w", 0);
                int height = level.AttrInt("h", 0);

                Point     pos         = new Point(x * 8 - chapterBounds.X, y * 8 - chapterBounds.Y);
                Point     offset      = new Point(pos.X - viewport.X, pos.Y - viewport.Y);
                Rectangle levelBounds = new Rectangle(pos.X, pos.Y, width, height);
                if (!levelBounds.IntersectsWith(viewport))
                {
                    continue;
                }

                TileGrid tiles = foreground.GenerateOverlay(DefaultTile, 0, 0, width, height, null);
                using (Bitmap map = tiles.DisplayMap(null, null, chapterBounds, false)) {
                    Util.CopyTo(chapter, map, pos);
                }
            }
            return(chapter);
        }