Пример #1
0
        /// <summary>
        /// Adds collision types to the places on the map where it is needed.
        /// </summary>
        /// <param name="mapLayers"> The map layers the map is created from. </param>
        /// <param name="tilesets"> The tilesets the map is created from. </param>
        public void AddCollision(List<MapLayer> mapLayers, List<Tileset> tilesets)
        {
            // Finds the layer with the name "collision" in the list of layers.
            var collisionLayer = new MapLayer("collision", this.width, this.height);
            foreach (var mapLayer in mapLayers.Where(mapLayer => mapLayer.LayerName.Equals("collision")))
            {
                collisionLayer = mapLayer;
            }

            this.mapData = new MapCell[collisionLayer.Height, collisionLayer.Width];

            // Iterates over all the cells in the collisionlayer and stores their type of
            // collision in the respective cell in the mapData.
            for (int i = 0; i < this.height; i++)
            {
                for (int j = 0; j < this.width; j++)
                {
                    var cell = new MapCell
                    {
                        Collision =
                            (CollisionType)
                            (collisionLayer.GetTile(j, i).TileID - tilesets.ToArray()[collisionLayer.GetTile(j, i).TileSet].StartID)
                    };

                    this.mapData[i, j] = cell;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Map"/> class.
        /// </summary>
        /// <param name="tileset"> The tileset to create the map from. </param>
        /// <param name="layer"> The layer to create the map from. </param>
        public Map(Tileset tileset, MapLayer layer)
        {
            this.tilesets = new List<Tileset>();
            this.tilesets.Add(tileset);

            this.mapLayers = new List<MapLayer>();
            this.mapLayers.Add(layer);

            mapWidth = this.mapLayers[0].Width;
            mapHeight = this.mapLayers[0].Height;

            this.dataMap = new MapData(this.MapHeight, this.MapWidth);
            this.dataMap.AddCollision(this.mapLayers, this.tilesets);
        }
Пример #3
0
        /// <summary>
        /// Adds a layer to the tilemap
        /// </summary>
        /// <param name="layer"> The layer. </param>
        /// <exception cref="Exception"> An exceptiono </exception>
        public void AddLayer(MapLayer layer)
        {
            if (layer.Width != mapWidth && layer.Height != mapHeight)
            {
                throw new Exception("MapData layer size exception");
            }

            this.mapLayers.Add(layer);
        }
Пример #4
0
        /// <summary>
        /// Makes a layer based on the given XmlNode.
        /// </summary>
        /// <param name="node"> The node to make a layer from. </param>
        /// <exception cref="Exception"> An exception that tells that the encoding is invalid. </exception>
        private void MakeLayer(XmlNode node)
        {
            // Asserts that the node contains attributes.
            Debug.Assert(node.Attributes != null, "node.Attributes != null");

            XmlNode dataNode = node["data"];

            // Creates a layer from the name attribute in the dataNode.
            var layer = new MapLayer(node.Attributes["name"].Value, this.mapWidth, this.mapHeight);
            var layerData = new int[this.mapWidth * this.mapHeight];

            // Asserts that certain nodes needed to make tha layer exists.
            Debug.Assert(dataNode != null, "dataNode != null");
            Debug.Assert(dataNode.Attributes != null, "dataNode.Attributes != null");

            // Figure out what encoding is being used, if any, and process
            // the data appropriately
            if (dataNode.Attributes["encoding"] != null)
            {
                string encoding = dataNode.Attributes["encoding"].Value;

                if (encoding == "base64")
                {
                    this.ReadAsBase64(layerData, node, dataNode);
                }
                else if (encoding == "csv")
                {
                    this.ReadAsCsv(layerData, node);
                }
                else
                {
                    throw new Exception("Unknown encoding: " + encoding);
                }
            }
            else
            {
                // Load all tiles with value 0.
                for (int i = 0; i < layerData.Length; i++)
                {
                    layerData[i] = 0;
                }
            }

            this.LoadIntoLayer(layerData, layer);

            this.layerList.Add(layer);
        }
Пример #5
0
        /// <summary>
        /// Converts all the tileIDs into Tiles and plots
        /// them into the layer.
        /// </summary>
        /// <param name="tileIDs"> The tileIDs to convert. </param>
        /// <param name="layer"> The final layer. </param>
        private void LoadIntoLayer(int[] tileIDs, MapLayer layer)
        {
            int currentID = 0;

            // Loops through all tiles in tileIDs, converts them to object of the type
            // Tile and loads them into the layer.
            for (int i = 0; i < layer.Height; i++)
            {
                for (int j = 0; j < layer.Width; j++)
                {
                    int id = tileIDs[currentID] - 1 < 0 ? 0 : tileIDs[currentID] - 1;
                    layer.SetTile(j, i, id, this.FindTileset(id));
                    currentID++;
                }
            }
        }
Пример #6
0
        /// <summary>
        /// Makes a layer for the map from the given input.
        /// </summary>
        /// <param name="lines"> The lines that holds the data for the map. </param>
        /// <param name="start"> The line to start reading from. </param>
        /// <param name="height"> The height of the map. </param>
        /// <param name="width"> The width of the map. </param>
        /// <param name="gameRef"> A snapshot of the game. </param>
        private void MakeLayer(string[] lines, int start, int height, int width, Game1 gameRef)
        {
            // Finds the name of the tileset used to make this specific layer and loads it.
            var tilesetName = lines[start + 2].Substring(lines[start + 2].IndexOf('=') + 1);
            var tilesetTexture = gameRef.Content.Load<Texture2D>(@"Textures\TileSets\" + tilesetName);
            var backgroundTileset = new Tileset(
                tilesetTexture, tilesetTexture.Width / 32, tilesetTexture.Height / 32, 32, 32);
            this.tilesets.Add(backgroundTileset);

            var index = lines[start + 1].IndexOf('=') + 1;

            var layerName = lines[start + 1].Substring(index, lines[start + 1].Length - index);

            var background = new MapLayer(layerName, width, height);

            // Goes through all the lines that hold tileIDs
            for (int i = 0; i < height; i++)
            {
                // Loads the current line and splits it into a string array.
                string[] stringTileIDs = lines[i + start + 4].Split(',');
                var tileIDs = new int[width];

                // Converts each ID from a string to an int.
                for (int j = 0; j < width; j++)
                {
                    tileIDs[j] = Convert.ToInt32(stringTileIDs[j]);
                }

                // Loops through the list of TileIDs and places new tiles in the layer, corrosponding
                // to the ID and position.
                for (int j = 0; j < width; j++)
                {
                    background.SetTile(i, j, new Tile(tileIDs[j] - 1 < 0 ? 0 : tileIDs[j] - 1, this.tilesets.Count - 1));
                }
            }

            this.layers.Add(background);
        }