/// <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];

                    foreach (XmlElement e in xmlEntities) {
                        CreateEntity(e, scene);
                    }
                }

            }

            if (UseCameraBounds) {
                scene.CameraBounds = new Rectangle(0, 0, scene.Width, scene.Height);
                scene.UseCameraBounds = true;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Load level data from a string into a Scene.
        /// </summary>
        /// <param name="data">The level data to load.</param>
        /// <param name="scene">The Scene to load into.</param>
        public void LoadLevel(string data, Scene scene)
        {
            Entities.Clear();

            CurrentLevel = data;

            var xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(data);

            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(ref 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 tilepath = ImagePath + TileMaps[tileset];

                    foreach (var kv in assetMappings)
                    {
                        var find    = kv.Key;
                        var replace = kv.Value;

                        if (tilepath.EndsWith(find))
                        {
                            tilepath = replace;
                            break;
                        }
                    }

                    var tilemap = new Tilemap(tilepath, scene.Width, scene.Height, layer.GridWidth, layer.GridHeight);

                    var exportMode = xmlTiles.Attributes["exportMode"].Value;
                    switch (exportMode)
                    {
                    case "CSV":
                        tilemap.LoadCSV(xmlTiles.InnerText);
                        break;

                    case "XMLCoords":
                        foreach (XmlElement t in xmlTiles)
                        {
                            tilemap.SetTile(t);
                        }
                        break;
                    }

                    tilemap.Update();

                    Entity.AddGraphic(tilemap);

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

                    scene.Add(ref 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;
            }
        }
Beispiel #3
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;
            }
        }