public static TileImage CreateTileImage(GameObject parentGameObject, MapReader.Map mapData, MapReader.Layer layer, int tileIndex, int fringeSize = 0) { int tileID = layer.Data[tileIndex]; TileImage image = CreateTileImageOfTileID(parentGameObject, mapData, tileID, fringeSize); MapReader.Tileset tileset = GetTilesetFromTileID(tileID, mapData.Tilesets); image.tileIndex = tileIndex; int yTilePos = (tileIndex / mapData.Width) * tileset.TileHeight; int xTilePos = (tileIndex % mapData.Width) * tileset.TileWidth; Vector3 position = new Vector3(xTilePos, -yTilePos, Game_bottomMapLayerDepth); if (layer.Name.Split(',').Length > 1) { float.TryParse(layer.Name.Split(',')[1], out position.z); //position.z += Game_bottomMapLayerDepth; } //print("position z: " + position.z); image.transform.localPosition = new Vector3(position.x, (position.y), (position.y * Game_depthDivision) + position.z); return(image); }
public static MapReader.Tileset GetTilesetFromTileID(int tileID, List <MapReader.Tileset> tilesets) { MapReader.Tileset tilesetResult = null; foreach (MapReader.Tileset tileset in tilesets) { if (tileID < tileset.FirstGID) { break; } tilesetResult = tileset; } return(tilesetResult); }
public static TileImage CreateTileImageOfTileID(GameObject parentGameObject, MapReader.Map mapData, int tileID, int fringeSize = 0) { MapReader.Tileset tileset = GetTilesetFromTileID(tileID, mapData.Tilesets); int imageTileIndex = tileID - tileset.FirstGID; int imageWidthInTiles = tileset.Images[0].Width / tileset.TileWidth; int imageHeightInTiles = tileset.Images[0].Height / tileset.TileHeight; int yTileIndex = imageTileIndex / imageWidthInTiles; int xTileIndex = imageTileIndex % imageWidthInTiles; //print("TileIndex: " + yTileIndex + " " + xTileIndex); Texture2D texture2D = LoadTilePNG(tileset.Images[0].Source); //calc tile offset float u0 = (xTileIndex * tileset.TileWidth) - fringeSize; float v0 = (((imageHeightInTiles - 1) - yTileIndex) * tileset.TileHeight) - fringeSize;// WHY int doubleFringe = fringeSize << 1; float u1 = (u0 + tileset.TileWidth) + doubleFringe; float v1 = (v0 + tileset.TileHeight) + doubleFringe; u0 /= tileset.Images[0].Width; v0 /= tileset.Images[0].Height; u1 /= tileset.Images[0].Width; v1 /= tileset.Images[0].Height; //print("tileset.Images[0]: " + tileset.Images[0].Width + " " + tileset.Images[0].Height); //print("tileset.TileWidth: " + tileset.TileWidth + " " + tileset.TileHeight); //print("UV: " + u0 + " " + v0 + " " + u1 + " " + v1); int fullWidth = tileset.TileWidth + doubleFringe; int fullHeight = tileset.TileHeight + doubleFringe; TileImage image = Image.CreateImage <TileImage>(parentGameObject.transform, texture2D, fullWidth, fullHeight, u0, v0, u1, v1); image.width = fullWidth; image.height = fullHeight; image.tileset = tileset; image.tileID = tileID; return(image); }