Beispiel #1
0
 public void AddTile(TiledTile newTile)
 {
     if (newTile.TiledSet == this)
     {
         Tiles.Add(newTile);
     }
 }
Beispiel #2
0
        } = new Point(0, 0);                                                            // In Tiles
        #endregion


        #region Methods
        private static void LoadTileSets(GraphicsDevice graphicsDevice)
        {
            // Find all tile set file names.
            string searchPattern = "*.tsx";
            var    fileNames     = Directory.EnumerateFiles(MapDirectory, searchPattern, SearchOption.TopDirectoryOnly).Select(Path.GetFileName);

            tileSetFiles = new List <string>(fileNames);
            TileSets     = new OrderedDictionary();


            // Load the tilesets
            foreach (var fileName in tileSetFiles)
            {
                if (File.Exists(MapDirectory + fileName))
                {
                    // Read in the data
                    XElement xElement = ReadFileIntoXElement(fileName);

                    string name        = XMLHelperFuncs.GetStringFromAttribute(xElement, "name");
                    int    tileWidth   = XMLHelperFuncs.GetIntFromAttribute(xElement, "tilewidth");
                    int    tileHeight  = XMLHelperFuncs.GetIntFromAttribute(xElement, "tileheight");
                    int    tileCount   = XMLHelperFuncs.GetIntFromAttribute(xElement, "tilecount");
                    int    columns     = XMLHelperFuncs.GetIntFromAttribute(xElement, "columns");
                    string sourceImage = XMLHelperFuncs.GetStringFromAttribute(xElement.Element("image"), "source");

                    // Create the Tile Set
                    TiledSet tileSet = new TiledSet(name, fileName, sourceImage, MapDirectory, graphicsDevice, tileWidth, tileHeight, tileCount, columns);
                    TileSets.Add(fileName, tileSet);

                    // Add tiles
                    for (int currentID = 0; currentID < tileCount; currentID++)
                    {
                        // Calc the position based on id
                        Vector2 position = new Vector2();
                        position.X = (currentID % columns) * tileWidth;
                        position.Y = (currentID / columns) * tileHeight;


                        string           type       = "";
                        bool             isAnimated = false;
                        List <FrameData> frameData  = new List <FrameData>();

                        foreach (var element in xElement.Elements())
                        {
                            // Search for data specific to the tile
                            if (element.Name == "tile")
                            {
                                int attributeID = XMLHelperFuncs.GetIntFromAttribute(element, "id");
                                if (attributeID == currentID)
                                {
                                    // See if it has a type element
                                    if (XMLHelperFuncs.DoesAttributeExist(element, "type"))
                                    {
                                        type = element.Attribute("type").Value;
                                    }

                                    // See if it has animation data
                                    if (XMLHelperFuncs.DoesElementExist(element, "animation"))
                                    {
                                        isAnimated = true;
                                        foreach (var frameElement in element.Element("animation").Elements())
                                        {
                                            // Check to be sure each element is a frame
                                            if (frameElement.Name == "frame")
                                            {
                                                int   frameId        = XMLHelperFuncs.GetIntFromAttribute(frameElement, "tileid");
                                                float frameDurration = XMLHelperFuncs.GetFloatFromAttribute(frameElement, "duration");

                                                frameData.Add(new FrameData(frameId, frameDurration));
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        TiledTile tempTile = new TiledTile(currentID, position, tileWidth, tileHeight, tileSet, type, isAnimated, true, frameData);

                        tileSet.AddTile(tempTile);
                    }
                }
                else
                {
                    // Todo: log to file an error
                }
            }
        }