//从一个CVS格式内容,读取数据,并根据数据生成当前tilemap public bool LoadTileMap(tk2dTileMap tk2dTM, int layerID, string result) { try { //清空layerID此层的所有数据,若不清空,之前拥有Prefab的tile会继续保留在场景中,所以要销毁此层所有数据 tk2dTM.Layers[layerID].DestroyGameData(tk2dTM); width = tk2dTM.width; height = tk2dTM.height; int tileID; //根据换行符 "\r\n" 将数据分割为数组,每行对应一个元素 ★重点应用★ string[] resultArray = result.Split(new string[] { "\r\n" }, StringSplitOptions.None); //从第一行开始,读取数据,tiled中的第一行,对应的是tk2dTileMap中的 height-1-y 行 for (int y = height - 1; y > -1; y--) { //将每行的数据分割为数组,对应为每个tile的tileID string[] row = resultArray[height - 1 - y].Split(new Char[] { ',' }); //循环读取行数组的数据,并将每个tile设置为对应内容 for (int x = 0; x < width; x++) { tileID = Convert.ToInt16(row[x]); //设置前,清除当前位置的tile tk2dTM.ClearTile(x, y, layerID); tk2dTM.SetTile(x, y, layerID, tileID); } } //对tk2dTileMap进行修改后,需要build才能生效 tk2dTM.Build(); return(true); } catch { //throw; return(false); } }
public void BuildLevel(int level) { this.level = level; MainGame.Level = level; for (int i = 0; i < MainGame.fullMap.Width; i++) { for (int j = 0; j < MainGame.fullMap.Height; j++) { if (MainGame.fullMap.GetTile(i, j, level).Underground) { int tile = tileMap.SpriteCollectionInst.GetSpriteIdByName("Underground-square"); tileMap.SetTile(i, j, 0, tile); } else { int tile = tileMap.SpriteCollectionInst.GetSpriteIdByName(MainGame.fullMap.GetTile(i, j, level).Biome + "-square"); tileMap.SetTile(i, j, 0, tile); } } } tileMap.Build(); }
public void Refresh() { for (int i = 0; i < MAX_WIDTH; i++) { for (int j = 0; j < MAX_HEIGHT; j++) { if (Layout[i, j] > 0) { tileMap.SetTile(i, j, 0, Layout[i, j] - 1 + Variant * Block.MAX_VALUE); } else { tileMap.ClearTile(i, j, 0); } } } tileMap.Build(); }
public void Refresh() { for (int i = 0; i < PreviewPiece.MAX_WIDTH; i++) { for (int j = 0; j < PreviewPiece.MAX_HEIGHT; j++) { normalPiece.ClearTile(i, j, 0); } } for (int i = GameControl.Instance.LastShift.x; i < PreviewPiece.MAX_WIDTH; i++) { for (int j = GameControl.Instance.LastShift.y; j < PreviewPiece.MAX_HEIGHT; j++) { var ii = i - GameControl.Instance.LastShift.x; var jj = j - GameControl.Instance.LastShift.y; if (normalLayout.Layout[ii, jj] > 0) { normalPiece.SetTile(i, j, 0, normalLayout.Layout[ii, jj] - 1 + normalLayout.Variant * Block.MAX_VALUE); } } } normalPiece.Build(); for (int i = 0; i < PreviewPiece.MAX_WIDTH; i++) { for (int j = 0; j < PreviewPiece.MAX_HEIGHT; j++) { resultingPiece.ClearTile(i, j, 0); if (resultingLayout.Layout[i, j] > 0) { resultingPiece.SetTile(i, j, 0, resultingLayout.Layout[i, j] - 1 + resultingLayout.Variant * Block.MAX_VALUE); } } } resultingPiece.Build(); normalAlpha = 1; gameObject.SetLayerRecursively(5); }
private void cloneOverride(int tileToChange, int x, int y, int layer) { _tileMap.SetTile(x, y, layer, tileToChange); }
void FillWithWalls() { for (int x = 0; x < map.width; x++) { for (int y = 0; y < map.width; y++) { map.SetTile(x, y, wallLayer, 33); } } }