Beispiel #1
0
        public void Load(string file, GraphicsDevice graphicsDevice)
        {
            int width    = -1;
            int height   = -1;
            int tilesize = -1;
            List <TiledObjectGroup> objectGroups = new List <TiledObjectGroup>();
            List <MapLayer>         layers       = new List <MapLayer>();
            TileSetManager          ts           = new TileSetManager();

            hope = new LightRenderer(graphicsDevice, this);

            using (XmlReader xr = XmlReader.Create(file))
            {
                while (xr.Read())
                {
                    if (xr.NodeType == XmlNodeType.EndElement)
                    {
                        continue;
                    }
                    switch (xr.Name)
                    {
                    case "tileset":
                        using (StringReader sr = new StringReader(xr.ReadOuterXml()))
                            using (XmlReader r = XmlReader.Create(sr))
                                ts.LoadTileSet(file, r, graphicsDevice);
                        break;

                    case "layer":
                        MapLayer layer = null;
                        string   name  = xr["name"];
                        using (StringReader sr = new StringReader(xr.ReadOuterXml()))
                            using (XmlReader r = XmlReader.Create(sr))
                                layer = MapLayer.Load(r, graphicsDevice, ts, System.IO.Path.GetDirectoryName(file));
                        if (!name.StartsWith("Light"))
                        {
                            layers.Add(layer);
                        }
                        else
                        {
                            hope.AddLayer(layer);
                        }
                        break;

                    case "map":
                        width    = int.Parse(xr["width"]);
                        height   = int.Parse(xr["height"]);
                        tilesize = int.Parse(xr["tilewidth"]);                                 //should be same as tileheight
                        break;

                    case "objectgroup":
                        TiledObjectGroup og = new TiledObjectGroup(xr["name"]);
                        og.Load(xr);
                        objectGroups.Add(og);
                        break;

                    default:
                        break;
                    }
                }
            }
            BasicTile.SetTileSize(tilesize);

            this.width        = width;
            this.height       = height;
            this.ObjectGroups = objectGroups.ToArray();
            this.Layers       = layers.ToArray();
            hope.LoadTileSets(ts);

            nodes = new Node[width, height];

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    nodes[x, y] = new Node(new Index2(x, y));
                }
            }
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    nodes[x, y].SetNeighbours(GetNeighbours(x, y));
                }
            }

            var lightbulbs = GetGroupByName("Lights");

            foreach (var lightbulb in lightbulbs.Objects)
            {
                hope.AddLight(new Light((Index2)lightbulb.Position, (short)lightbulb.Properties.GetPropertyAsInt("Strength"), 1f, lightbulb.Properties.GetPropertyAsColor("Color")));
            }
        }