Example #1
0
 private static ObjectLayer LoadObjectLayer(Map map, ObjectLayerContent ctx)
 {
     return(new ObjectLayer(ctx.Name,
                            ctx.OffsetX,
                            ctx.OffsetY,
                            ctx.Opacity,
                            ctx.Visible));
 }
Example #2
0
        /// <summary>
        /// Creates the proper map object layer based on the layer name such as collidables and path nodes.
        /// </summary>
        /// <param name="layer"></param>
        /// <param name="orientation"></param>
        /// <returns></returns>
        private MapObjectLayer CreateObjectLayer(LayerContent layer, Orientation orientation)
        {
            if (layer == null)
            {
                throw new ArgumentNullException("layer");
            }

            ObjectLayerContent objectLayerContent = layer as ObjectLayerContent;

            MapObjectLayerType mapObjectLayerType;
            MapObjectType      mapObjectType;

            if (objectLayerContent.Name == "DeadZones")
            {
                mapObjectLayerType = MapObjectLayerType.DeadZone;
                mapObjectType      = MapObjectType.DeadZone;
            }
            else if (objectLayerContent.Name == "PathNodes")
            {
                mapObjectLayerType = MapObjectLayerType.PathNode;
                mapObjectType      = MapObjectType.PathNode;
            }
            else if (objectLayerContent.Name == "Equipment")
            {
                mapObjectLayerType = MapObjectLayerType.Equipment;
                mapObjectType      = MapObjectType.Equipment;
            }
            else
            {
                throw new Exception("Unknown map object layer type. Did you name your layer correctly? \"DeadZones\", \"PathNodes\", and \"Equipment\" are valid layers.");
            }

            MapObjectLayer mapObjectLayer = new MapObjectLayer(objectLayerContent.Name, mapObjectLayerType);

            foreach (ObjectContent objectContent in objectLayerContent.MapObjects)
            {
                if (mapObjectLayer.Type == MapObjectLayerType.PathNode)
                {
                    PathNode pathNode = new PathNode(objectContent.Name, objectContent.Bounds, orientation, objectContent.Properties);
                    mapObjectLayer.AddMapObject(pathNode);
                }
                else if (mapObjectLayer.Type == MapObjectLayerType.Equipment)
                {
                    EquipmentObjectType equipmentObjectType = GetEquipmentObjectType(objectContent);
                    EquipmentObject     equipmentObject     = new EquipmentObject(objectContent.Name, objectContent.Bounds, orientation, objectContent.Properties, equipmentObjectType);
                    mapObjectLayer.AddMapObject(equipmentObject);
                }
                else
                {
                    MapObject mapObject = new MapObject(objectContent.Name, objectContent.Bounds, orientation, mapObjectType, objectContent.Properties);
                    mapObjectLayer.AddMapObject(mapObject);
                }
            }

            return(mapObjectLayer);
        }
Example #3
0
        /// <summary>Default constructor creates a map from a .tmx file and creates any associated tileset textures by using the passed renderer.
        /// </summary>
        /// <param name="filePath">Path to the .tmx file to load</param>
        /// <param name="renderer">Renderer object used to load tileset textures</param>
        public TiledMap(string filePath, Renderer renderer, string contentRoot = "")
        {
            MapContent mapContent = new MapContent(filePath, renderer, contentRoot);

            TileWidth  = mapContent.TileWidth;
            TileHeight = mapContent.TileHeight;

            Width  = mapContent.Width * TileWidth;
            Height = mapContent.Height * TileHeight;

            foreach (LayerContent layer in mapContent.Layers)
            {
                if (layer is TileLayerContent)
                {
                    TileLayerContent tileLayerContent = layer as TileLayerContent;
                    TileLayer        tileLayer        = new TileLayer(tileLayerContent.Width, tileLayerContent.Height);

                    for (int i = 0; i < tileLayerContent.Data.Length; i++)
                    {
                        // strip out the flipped flags from the map editor to get the real tile index
                        uint tileID = tileLayerContent.Data[i];
                        uint flippedHorizontallyFlag = 0x80000000;
                        uint flippedVerticallyFlag   = 0x40000000;
                        int  tileIndex = (int)(tileID & ~(flippedVerticallyFlag | flippedHorizontallyFlag));

                        Tile tile = CreateTile(tileIndex, mapContent.TileSets);

                        tileLayer.AddTile(tile);
                    }

                    tileLayers.Add(tileLayer);
                }
                else if (layer is ObjectLayerContent)
                {
                    ObjectLayerContent objectLayerContent = layer as ObjectLayerContent;

                    bool isCollidable = false;
                    if (objectLayerContent.Name == "Collidables")
                    {
                        isCollidable = true;
                    }

                    MapObjectLayer mapObjectLayer = new MapObjectLayer(objectLayerContent.Name);

                    foreach (ObjectContent objectContent in objectLayerContent.MapObjects)
                    {
                        MapObject mapObject = new MapObject(objectContent.Name, objectContent.Bounds, isCollidable, mapContent.Orientation);
                        mapObjectLayer.AddMapObject(mapObject);
                    }

                    mapObjectLayers.Add(mapObjectLayer);
                }
            }

            CalculateTilePositions(mapContent.Orientation);
        }
Example #4
0
        /// <summary>
        /// Creates the proper map object layer based on the layer name such as collidables and path nodes.
        /// </summary>
        /// <param name="layer"></param>
        /// <param name="orientation"></param>
        /// <returns></returns>
        private MapObjectLayer CreateObjectLayer(LayerContent layer, Orientation orientation)
        {
            if (layer == null)
            {
                throw new ArgumentNullException("layer");
            }

            ObjectLayerContent objectLayerContent = layer as ObjectLayerContent;

            MapObjectLayer mapObjectLayer = new MapObjectLayer(objectLayerContent.Name);

            foreach (ObjectContent objectContent in objectLayerContent.MapObjects)
            {
                MapObject mapObject = new MapObject(objectContent.Name, objectContent.Bounds, orientation, objectContent.Properties);
                mapObjectLayer.AddMapObject(mapObject);
            }

            return(mapObjectLayer);
        }
Example #5
0
        public LevelContent(ProjectContent project, XmlDocument document)
        {
            this.Project = project;
            XmlNode levelNode = document["level"];

            // Level values/attributes
            foreach (ValueTemplateContent value in project.Values)
            {
                XmlNode attribute = null;
                if ((attribute = levelNode.Attributes[value.Name]) != null)
                {
                    if (value is BooleanValueTemplateContent)
                    {
                        this.Values.Add(new BooleanValueContent(value.Name, bool.Parse(attribute.Value)));
                    }
                    else if (value is IntegerValueTemplateContent)
                    {
                        this.Values.Add(new IntegerValueContent(value.Name,
                                                                int.Parse(attribute.Value, CultureInfo.InvariantCulture)));
                    }
                    else if (value is NumberValueTemplateContent)
                    {
                        this.Values.Add(new NumberValueContent(value.Name,
                                                               float.Parse(attribute.Value, CultureInfo.InvariantCulture)));
                    }
                    else if (value is StringValueTemplateContent)
                    {
                        this.Values.Add(new StringValueContent(value.Name, attribute.Value));
                    }
                }
            }
            // Height
            this.Height = int.Parse(levelNode.SelectSingleNode("height").InnerText, CultureInfo.InvariantCulture);
            // Width
            this.Width = int.Parse(levelNode.SelectSingleNode("width").InnerText, CultureInfo.InvariantCulture);
            // Layers
            // Here we'll construct an XPath query of all possible layer names so we can just extract the nodes all
            // at once.
            string[] layerNames = (from x in project.LayerSettings select x.Name).ToArray <string>();
            string   layerXPath = string.Join("|", layerNames);

            foreach (XmlNode layerNode in levelNode.SelectNodes(layerXPath))
            {
                // Attempt to extract the settings for this layer.
                LayerSettingsContent[] s = (from x in project.LayerSettings
                                            where x.Name.Equals(layerNode.Name)
                                            select x).ToArray <LayerSettingsContent>();
                if (!(s.Length > 0))
                {
                    continue;
                }
                LayerSettingsContent layerSettings = s[0];
                // We have a grid layer.
                if (layerSettings is GridLayerSettingsContent)
                {
                    GridLayerSettingsContent settings  = layerSettings as GridLayerSettingsContent;
                    GridLayerContent         gridLayer = new GridLayerContent(layerNode, this, settings);
                    if (gridLayer != null)
                    {
                        this.Layers.Add(gridLayer);
                    }
                }
                else if (layerSettings is TileLayerSettingsContent)
                {
                    TileLayerSettingsContent settings  = layerSettings as TileLayerSettingsContent;
                    TileLayerContent         tileLayer = new TileLayerContent(layerNode, this, settings);
                    if (tileLayer != null)
                    {
                        this.Layers.Add(tileLayer);
                    }
                }
                else if (layerSettings is ObjectLayerSettingsContent)
                {
                    ObjectLayerContent objectLayer = new ObjectLayerContent(layerNode, this);
                    if (objectLayer != null)
                    {
                        this.Layers.Add(objectLayer);
                    }
                }
            }
        }