public TiledTile(int tileID, Vector2 positionOnImage, int tileWidth, int tileHeight, TiledSet tileSet, string type = "", bool isAnimated = false, bool loopAnimation = true, List <FrameData> animationFrames = null) { TileID = tileID; SourceImage = tileSet.Texture; PositionOnImage = positionOnImage; TileWidth = tileWidth; TileHeight = tileHeight; TiledSet = tileSet; Type = type; IsAnimated = isAnimated; LoopAnimation = loopAnimation; AnimationFrames = animationFrames; if (GetTileList == null) { GetTileList = new List <TiledTile>(); } GetTileList.Add(this); }
public static (TiledSet, uint) GetTileSetAndFirstGIDFromID(int id) { foreach (var set in CurrentMap.TiledSets) { if (id >= set.Key) { TiledSet tileSet = (TiledSet)TileSets[set.Value]; if (id < set.Key + tileSet.TileCount) { return(tileSet, set.Key); } } } return(null, 0); }
public TiledTile(int tileId, int xPositionOnImage, int yPositionOnImage, int tileWidth, int tileHeight, TiledSet tileSet, string type = "", bool isAnimated = false, bool loopAnimation = true, List <FrameData> animationFrames = null) : this(tileId, new Vector2((float)xPositionOnImage, (float)yPositionOnImage), tileWidth, tileHeight, tileSet, type, isAnimated, loopAnimation, animationFrames) { }
} = 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 } } }
private static void DrawTileLayer(SpriteBatch spriteBatch, float z, Layer layer) { List <uint> firstGlobalIDs = CurrentMap.TiledSets.Keys.ToList <uint>(); TileLayer tileLayer = layer as TileLayer; foreach (var chunk in tileLayer.MapChunks) { for (int tileY = 0; tileY < chunk.Height; tileY++) { for (int tileX = 0; tileX < chunk.Width; tileX++) { uint GlobalID = chunk.Data[tileY, tileX]; if (GlobalID != 0) { int worldX = (tileX * CurrentMap.TileWidth) + (int)tileLayer.HorizontalOffset + (int)(chunk.Position.X * CurrentMap.TileWidth); int worldY = (tileY * CurrentMap.TileHeight) + (int)tileLayer.VerticalOffset + (int)(chunk.Position.Y * CurrentMap.TileHeight); Rectangle worldRect = new Rectangle(worldX, worldY, CurrentMap.TileWidth, CurrentMap.TileHeight); Rectangle screenRect = Camera.WorldToScreen(worldRect); //Wrap the screen. // the before and after changes to the position are done for smoothing purposes. screenRect.X += CurrentMap.TileWidth; screenRect.Y += CurrentMap.TileHeight; screenRect.X = MyMath.Mod(screenRect.X, Camera.WorldRectangle.Width); screenRect.Y = MyMath.Mod(screenRect.Y, Camera.WorldRectangle.Height); screenRect.X -= CurrentMap.TileWidth; screenRect.Y -= CurrentMap.TileHeight; //Check bounds if (screenRect.X + screenRect.Width > 0 && screenRect.X < Camera.ViewPortWidth && screenRect.Y + screenRect.Height > 0 && screenRect.Y < Camera.ViewPortHeight) { int tilesetIndex = TiledHelperMethods.GetTileSetIndex(GlobalID, firstGlobalIDs); int localID = TiledHelperMethods.ConvertGIDToID(GlobalID, firstGlobalIDs); bool hFlip = TiledHelperMethods.isGIDHorizontallyFlipped(GlobalID); bool vFlip = TiledHelperMethods.isGIDVerticallyFlipped(GlobalID); bool dFlip = TiledHelperMethods.isGIDDiagonallyFlipped(GlobalID); float rotation = 0.0f; TiledSet tiledSet = (TiledSet)MapManager.TileSets[tilesetIndex]; Vector2 origin = new Vector2(tiledSet.TileWidth / 2, tiledSet.TileHeight / 2); SpriteEffects effects = SpriteEffects.None; Rectangle sourceRect = new Rectangle(); // Do we draw the default tile or it's animation tiles if (tiledSet.Tiles[localID].IsAnimated) { int currentAnimationTileIndex = tiledSet.Tiles[localID].CurrentFrameID; sourceRect = tiledSet.Tiles[currentAnimationTileIndex].Rectangle(); } else { sourceRect = tiledSet.Tiles[localID].Rectangle(); } //Account for the new sprite origins screenRect.X += (int)(origin.X); screenRect.Y += (int)(origin.Y); //Rotate and flip our sprites effects = CalculateEffects(hFlip, vFlip, dFlip); rotation = CalculateRotation(hFlip, vFlip, dFlip); spriteBatch.Draw(tiledSet.Texture, screenRect, sourceRect, Color.White, rotation, origin, effects, z); } } } } } }