Exemple #1
0
        /// <summary>
        /// Constructor creating the tile from an existing tile set image.
        /// </summary>
        /// <param name="tileSetImage">Tile set image.</param>
        public Tile(TileSetImage tileSetImage, int tileIndex, int tileSetIndex, TileCollisionValue collisionValue)
        {
            this.tileIndex    = tileIndex;
            this.tileSetIndex = tileSetIndex;
            this.tileSetImage = tileSetImage;
            dimensions        = tileSetImage.TileDimensions;
            sourceRect        = tileSetImage.GetSourceRectangle(tileIndex);

            this.collisionValue = collisionValue;

            boundingBox = new BoundingBoxAA(Vector2.Zero, new Vector2(dimensions.X, dimensions.Y));
        }
Exemple #2
0
        public TileSet(int firstId, string name, int tileWidth, int tileHeight, TileSetImage image)
        {
            FirstId = firstId;

            Name = name;

            TileWidth = tileWidth;

            TileHeight = tileHeight;

            Image = image;

            LastId = FirstId + ((Image.Width / TileWidth) * (Image.Height / TileHeight)) - 1;
        }
Exemple #3
0
        public TileSet(string TilesPath, ContentManager content, int StartingId)
        {
            XmlDocument TilesDoc = new XmlDocument();

            TilesDoc.Load(TilesPath);

            this.StartingId = StartingId;
            XmlNode mainNode = TilesDoc.ChildNodes[1];

            TileWidth    = int.Parse(mainNode.Attributes[1].Value);
            TileHeight   = int.Parse(mainNode.Attributes[2].Value);
            TileCount    = int.Parse(mainNode.Attributes[3].Value);
            TileColumns  = int.Parse(mainNode.Attributes[4].Value);
            TileSetImage = mainNode.ChildNodes[0].Attributes[0].Value;
            Tiles        = content.Load <Texture2D>(TileSetImage.Substring(0, TileSetImage.Length - 4));

            Width  = int.Parse(mainNode.ChildNodes[0].Attributes[1].Value);
            Height = int.Parse(mainNode.ChildNodes[0].Attributes[2].Value);

            CollisionShapes = new Dictionary <int, PolygonCollider>();

            //Now parse stuff involving the collision shapes as well as the tile animations.
            //Console.WriteLine(mainNode.ChildNodes[2].ChildNodes[0].Name);
            for (int i = 2; i < mainNode.ChildNodes.Count; i++)
            {
                int id = int.Parse(mainNode.ChildNodes[i].Attributes[0].Value);

                if (mainNode.ChildNodes[i].ChildNodes[0].Name == "objectgroup")
                {
                    //x -> 1
                    //y -> 2

                    float   x             = float.Parse(mainNode.ChildNodes[i].ChildNodes[0].ChildNodes[0].Attributes[1].Value);
                    float   y             = float.Parse(mainNode.ChildNodes[i].ChildNodes[0].ChildNodes[0].Attributes[2].Value);
                    Polygon ColliderShape = new Polygon();

                    if (mainNode.ChildNodes[i].ChildNodes[0].ChildNodes[0].ChildNodes.Count > 0)
                    {
                        //Polyline
                        //Just load all the points and add them to the offset vector, so that way it's where it should be
                        string[] pairs = mainNode.ChildNodes[i].ChildNodes[0].ChildNodes[0].ChildNodes[0].Attributes[0].Value.Split(' ');

                        for (int j = 0; j < pairs.Length; j++)
                        {
                            string[] Coords = pairs[j].Split(',');
                            float    xC     = x + float.Parse(Coords[0]);
                            float    yC     = y + float.Parse(Coords[1]);

                            ColliderShape.addPoint(new Vector2(xC, yC));
                        }

                        CollisionShapes.Add(id, new PolygonCollider(ColliderShape));
                    }
                    else
                    {
                        //Rectangular
                        float Width  = float.Parse(mainNode.ChildNodes[i].ChildNodes[0].ChildNodes[0].Attributes[3].Value);
                        float Height = float.Parse(mainNode.ChildNodes[i].ChildNodes[0].ChildNodes[0].Attributes[4].Value);

                        ColliderShape.addPoint(new Vector2(x, y));
                        ColliderShape.addPoint(new Vector2(x + Width, y));
                        ColliderShape.addPoint(new Vector2(x + Width, y + Height));
                        ColliderShape.addPoint(new Vector2(x, y + Height));

                        CollisionShapes.Add(id, new PolygonCollider(ColliderShape));
                    }
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// Loads a tile map from an Xml file.
        /// </summary>
        /// <param name="tileMapFilePath">Path to the tile map Xml file.</param>
        /// <returns>Tile map created from the specified Xml file.</returns>
        public static TileMap LoadTileMap(string tileMapFilePath)
        {
            // Import Xml file as XmlDocument
            XmlDocument tileSetFile = new XmlDocument();

            tileSetFile.Load(MAPS_BASE_PATH + tileMapFilePath);

            XmlNode tileMapParentNode = tileSetFile.DocumentElement;

            // Array of tile set images
            TileSetImage[] tileSetArray = new TileSetImage[int.Parse(tileMapParentNode["TileSetCount"].InnerText)];

            // Parse tile dimensions
            string[]    tileDimensionsSplitString = tileMapParentNode["TileDimensions"].InnerText.Split(',');
            Dimensions2 tileDimensions;

            tileDimensions.X = int.Parse(tileDimensionsSplitString[0]);
            tileDimensions.Y = int.Parse(tileDimensionsSplitString[1]);

            // Create individual tile set images
            foreach (XmlNode currentTileSetNote in tileMapParentNode.SelectNodes("TileSet"))
            {
                int    tileSetIndex = int.Parse(currentTileSetNote["Index"].InnerText);
                string fileName     = currentTileSetNote["FileName"].InnerText;

                // Parse tile set dimensions
                string[]    tileSetDimensionsSplitString = currentTileSetNote["Dimensions"].InnerText.Split(',');
                Dimensions2 dimensions;
                dimensions.X = int.Parse(tileSetDimensionsSplitString[0]);
                dimensions.Y = int.Parse(tileSetDimensionsSplitString[1]);

                tileSetArray[tileSetIndex] = new TileSetImage(fileName, dimensions, tileDimensions);
            }

            // Parse tile map data
            XmlNodeList tileMapRowNodeList = tileMapParentNode["Data"].SelectNodes("Row");

            // Dimensions of the tile map
            Dimensions2 tileMapDimensions;

            tileMapDimensions.Y = tileMapRowNodeList.Count;
            tileMapDimensions.X = 1;
            for (int i = 0; i < tileMapRowNodeList[0].InnerText.Length; i++)
            {
                if (tileMapRowNodeList[0].InnerText[i] == ';')
                {
                    tileMapDimensions.X++;
                }
            }

            // Array of tiles for the tile map
            Tile[,] tileArray = new Tile[tileMapDimensions.X, tileMapDimensions.Y];

            for (int y = 0; y < tileMapDimensions.Y; y++)
            {
                string[] tileRowSplitString = tileMapRowNodeList[y].InnerText.Split(';');

                for (int x = 0; x < tileMapDimensions.X; x++)
                {
                    string tileDataString = tileRowSplitString[x];
                    tileDataString = tileDataString.Replace("[", String.Empty);
                    tileDataString = tileDataString.Replace("]", String.Empty);

                    string[] tileDataSplitString = tileDataString.Split(':');

                    int tileIndex    = int.Parse(tileDataSplitString[0]);
                    int tileSetIndex = int.Parse(tileDataSplitString[1]);

                    TileCollisionValue tileCollision = TileCollisionValue.None;
                    if (tileDataSplitString[2] == "1")
                    {
                        tileCollision = TileCollisionValue.Solid;
                    }

                    // Create tile object
                    tileArray[x, y]          = new Tile(tileSetArray[tileSetIndex], tileIndex, tileSetIndex, tileCollision);
                    tileArray[x, y].Position = new Vector2(x * tileSetArray[tileSetIndex].TileDimensions.X, y * tileSetArray[tileSetIndex].TileDimensions.Y);
                }
            }

            // Create tile map object
            TileMap tileMap = new TileMap(tileMapDimensions, tileDimensions, tileArray, tileSetArray);

            return(tileMap);
        }