Vector3[] GetVertices(OTTileSet ts, OTTileMapLayer layer, Vector2 pos) { Vector2 _meshsize_ = new Vector2(1.0f/mapSize.x, 1.0f/mapSize.y); Vector2 _pivotPoint = new Vector2((pos.x-1) * _meshsize_.x * -1 - _meshsize_.x / 2, (pos.y-1) * _meshsize_.y + _meshsize_.y / 2); // Vector2 _pivotPoint = new Vector2((pos.x-1) * _meshsize_.x * -1, (pos.y-1) * _meshsize_.y); _pivotPoint.x += .5f; _pivotPoint.y -= .5f; float dx = (ts.tileSize.x / mapTileSize.x) - 1; float dy = (ts.tileSize.y / mapTileSize.y) - 1; return new Vector3[] { new Vector3(((_meshsize_.x/2) * -1) - _pivotPoint.x, (_meshsize_.y/2) - _pivotPoint.y + (dy * _meshsize_.y), layer.depth), new Vector3((_meshsize_.x/2) - _pivotPoint.x + (dx * _meshsize_.x), (_meshsize_.y/2) - _pivotPoint.y + (dy * _meshsize_.y), layer.depth), new Vector3((_meshsize_.x/2) - _pivotPoint.x + (dx * _meshsize_.x), ((_meshsize_.y/2) * -1) - _pivotPoint.y, layer.depth), new Vector3(((_meshsize_.x/2) * -1) - _pivotPoint.x, ((_meshsize_.y/2) * -1) - _pivotPoint.y, layer.depth) }; }
void LoadTileMap() { RemoveColliders(); List <OTTile>allTiles = new List<OTTile>(); Dictionary<int , OTTile>lookupTile = new Dictionary<int, OTTile>(); if (tileMapXML == null) { if (tileSets == null) tileSets = new OTTileSet[] { }; return; } XmlDocument xd = new XmlDocument(); try { xd.LoadXml(tileMapXML.text); } catch(System.Exception) { Debug.LogError("TileMap XML - invalid XML!"); return; } if (xd.DocumentElement == null) { Debug.LogError("TileMap XML - invalid XML!"); return; } if (xd.DocumentElement.Name != "map") { Debug.LogError("TileMap XML - No Tiled Tilemap found!"); return; } mapSize = new Vector2(AtI(xd.DocumentElement, "width"), AtI(xd.DocumentElement, "height")); mapTileSize = new Vector2(AtI(xd.DocumentElement, "tilewidth"), AtI(xd.DocumentElement, "tileheight")); XmlNodeList xmlTileSets = xd.DocumentElement.SelectNodes("tileset"); if (xmlTileSets.Count == 0) { Debug.LogError("TileMap XML - No Tilesets found!"); return; } XmlNodeList xmlLayers = xd.DocumentElement.SelectNodes("layer"); if (xmlLayers.Count == 0) { Debug.LogError("TileMap XML - No Layers found!"); return; } if (tileSets == null) tileSets = new OTTileSet[] { }; tileSetLookup.Clear(); System.Array.Resize<OTTileSet>(ref tileSets, xmlTileSets.Count); for (int i = 0; i < xmlTileSets.Count; i++) { if (tileSets[i] == null) tileSets[i] = new OTTileSet(); OTTileSet ts = tileSets[i]; XmlNode n = xmlTileSets[i]; ts.name = AtS(n, "name"); ts.firstGid = AtI(n, "firstgid"); ts.margin = AtI(n, "margin"); ts.spacing = AtI(n, "spacing"); ts.tileSize = new Vector2(AtI(n, "tilewidth"), AtI(n, "tileheight")); XmlNode im = n.SelectSingleNode("image"); string source = AtS(im, "source"); if (tileSetImages.Length > 0) { for (int ii = 0; ii < tileSetImages.Length; ii++) if (source.IndexOf(tileSetImages[ii].name) >= 0) ts.image = tileSetImages[ii]; } ts.imageSize = new Vector2(AtI(im, "width"), AtI(im, "height")); ts.tilesXY = new Vector2(Mathf.Round((ts.imageSize.x-(ts.margin*2)) / (ts.tileSize.x+ts.spacing)), Mathf.Round((ts.imageSize.y-(ts.margin*2)) / (ts.tileSize.y+ts.spacing))); int tileCount = (int)(ts.tilesXY.x * ts.tilesXY.y); int idx = 0; for (int ii = ts.firstGid; ii < ts.firstGid + tileCount; ii++) { if (!tileSetLookup.ContainsKey(ii)) tileSetLookup.Add(ii, ts); OTTile tile = new OTTile(); XmlNode tileNode = n.SelectSingleNode("tile[@id='"+idx+"']"); tile.index = idx++; if (tileNode!=null) { tile.collider = TileProp(tileNode,"collider"); tile.name = TileProp(tileNode,"name"); tile.material = TileProp(tileNode,"material"); tile.display = true; string sDisplay = TileProp(tileNode,"display").ToLower(); if (sDisplay=="none" || sDisplay=="false" || sDisplay=="hidden" || sDisplay=="0") tile.display = false; try { tile.height = System.Convert.ToInt16(TileProp(tileNode,"height")); } catch(System.Exception) { tile.height = 0; } if (tile.height == 0) tile.height = (int)mapTileSize.x; if (tile.height == 0) tile.height = 10; tile.gid = ii; if (tile.name=="") tile.name = "tileGUID"+ii; allTiles.Add(tile); lookupTile.Add(ii,tile); } } } if (layers == null) layers = new OTTileMapLayer[] { }; System.Array.Resize<OTTileMapLayer>(ref layers, xmlLayers.Count); for (int i = 0; i < xmlLayers.Count; i++) { if (layers[i] == null) layers[i] = new OTTileMapLayer(); OTTileMapLayer l = layers[i]; XmlNode n = xmlLayers[i]; l.name = AtS(n, "name"); l.depth = 0 - i; l.layerSize = new Vector2(AtI(n, "width"), AtI(n, "height")); int tileCount = (int)(l.layerSize.x * l.layerSize.y); if (l.tiles.Length!=tileCount) System.Array.Resize<int>(ref l.tiles, tileCount); try { try { XmlNodeList props = n.SelectSingleNode("properties").SelectNodes("property"); if (HasProp(props, "depth")) l.depth = GetPropI(props, "depth"); } catch (System.Exception) { } XmlNodeList tiles = n.SelectSingleNode("data").SelectNodes("tile"); if (tiles.Count != tileCount) Debug.LogWarning("TileMap XML - Invalid number of tiles " + tiles.Count+" on layer "+l.name+", size "+l.layerSize.x+" x "+l.layerSize.y+ " , so expected "+tileCount+" tiles."); for (int li = 0; li < tileCount; li++) l.tiles[li] = 0; for (int li = 0; li < tiles.Count; li++) { int gid = AtI(tiles[li],"gid"); if (lookupTile.ContainsKey(gid)) { if (lookupTile[gid].display) l.tiles[li] = AtI(tiles[li],"gid"); else l.tiles[li] = 0; } else l.tiles[li] = AtI(tiles[li],"gid"); } } catch (System.Exception) { Debug.LogError("TileMap XML - Could not load tiles from layer " + l.name); } } meshDirty = true; isDirty = true; size = new Vector2(mapSize.x * mapTileSize.x, mapSize.y * mapTileSize.y); if (generateColliders) CreateColliders(allTiles); }
public int[] GetTiles(Rect rect, OTTileSet tileset, OTTileMapLayer layer, bool checkTileSet) { int[] tiles = new int[(int)rect.width * (int)rect.height]; int idx = 0; if (layer == null) { for (int l=0; l<layers.Length; l++) { idx = 0; int[] ltiles = GetTiles(rect, tileset, layers[l], checkTileSet); for (int x = (int)rect.xMin; x<(int)rect.xMax; x++) for (int y = (int)rect.yMin; y<(int)rect.yMax; y++) { if (ltiles[idx]>=0) tiles[idx] = ltiles[idx]; idx++; } } return tiles; } if (layer!=null && checkTileSet) { for (int x = (int)rect.xMin; x<(int)rect.xMax; x++) for (int y = (int)rect.yMin; y<(int)rect.yMax; y++) { if (x>=0 && x<layer.layerSize.x && y>=0 && y<layer.layerSize.y) { int tile = layer.tiles[(y * (int)layer.layerSize.x) + x]; if (tileSetLookup.ContainsKey(tile) && tileSetLookup[tile] == tileset) tiles[idx++] = layer.tiles[(y * (int)layer.layerSize.x) + x] - tileset.firstGid; else tiles[idx++] = -1; } } } else { for (int x = (int)rect.xMin; x<(int)rect.xMax; x++) for (int y = (int)rect.yMin; y<(int)rect.yMax; y++) { if (x>=0 && x<layer.layerSize.x && y>=0 && y<layer.layerSize.y) tiles[idx++]= layer.tiles[(y * (int)layer.layerSize.x) + x] - tileset.firstGid; else tiles[idx++] = -1; } } return tiles; }