Beispiel #1
0
        /// <summary>
        /// Load data into a scene from a source .oel file.
        /// </summary>
        /// <param name="source">The oel to load.</param>
        /// <param name="scene">The scene to load into.</param>
        public void LoadLevel(string source, Scene scene)
        {
            Entities.Clear();

            var xmlDoc = new XmlDocument();

            xmlDoc.Load(source);

            var xmlLevel = xmlDoc["level"];

            scene.Width  = int.Parse(xmlDoc["level"].Attributes["width"].Value);
            scene.Height = int.Parse(xmlDoc["level"].Attributes["height"].Value);

            int i = 0;

            foreach (var layer in Layers.Values)
            {
                if (layer.Type == "GridLayerDefinition")
                {
                    var Entity = new Entity();

                    var grid = new GridCollider(scene.Width, scene.Height, layer.GridWidth, layer.GridHeight);

                    grid.LoadString(xmlLevel[layer.Name].InnerText);
                    if (ColliderTags.ContainsKey(layer.Name))
                    {
                        grid.AddTag(ColliderTags[layer.Name]);
                    }

                    if (DisplayGrids)
                    {
                        var tilemap = new Tilemap(scene.Width, scene.Height, layer.GridWidth, layer.GridHeight);
                        tilemap.LoadString(xmlLevel[layer.Name].InnerText, layer.Color);
                        Entity.AddGraphic(tilemap);
                    }

                    Entity.AddCollider(grid);

                    scene.Add(Entity);
                    Entities.Add(layer.Name, Entity);
                }
                if (layer.Type == "TileLayerDefinition")
                {
                    var Entity = new Entity();

                    var xmlTiles = xmlLevel[layer.Name];

                    var tileset = xmlTiles.Attributes["tileset"].Value;

                    var tilemap = new Tilemap(ImagePath + TileMaps[tileset], scene.Width, scene.Height, layer.GridWidth, layer.GridHeight);

                    foreach (XmlElement t in xmlTiles)
                    {
                        tilemap.SetTile(t);
                    }

                    tilemap.Update();

                    Entity.AddGraphic(tilemap);

                    Entity.Layer = BaseTileDepth - i * TileDepthIncrement;
                    i++;

                    scene.Add(Entity);
                    Entities.Add(layer.Name, Entity);
                }
                if (layer.Type == "EntityLayerDefinition")
                {
                    var xmlEntities = xmlLevel[layer.Name];

                    if (xmlEntities != null)
                    {
                        foreach (XmlElement e in xmlEntities)
                        {
                            CreateEntity(e, scene);
                        }
                    }
                }
            }

            if (UseCameraBounds)
            {
                scene.CameraBounds    = new Rectangle(0, 0, scene.Width, scene.Height);
                scene.UseCameraBounds = true;
            }
        }