// Update is called once per frame void Update() { Debug.Log(tilemap.GetTile(1, 1, 3)); tilemap.ClearTile(1, 1, 3); tilemap.Build(); }
//从一个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 bool enemy(Enemy enemy) { UM.infoDabuguo = "你打不过他\n\n"; UM.infoDabuguo += "怪物属性:\n"; UM.infoDabuguo += "生命:" + enemy.shengming + "\n"; UM.infoDabuguo += "攻击:" + enemy.gongji + "\n"; UM.infoDabuguo += "防御:" + enemy.fangyu + "\n"; UM.infoDabuguo += "金币:" + enemy.jinbi + "\n"; UM.infoDabuguo += "经验:" + enemy.jingyan + "\n"; if (HA._gongji <= enemy.fangyu) { UM.state = "dabuguo"; } else { int shanghai = HA._gongji - enemy.fangyu; float cishu = Mathf.Floor(enemy.shengming / shanghai); float zongshanghai = 0; if (enemy.gongji > HA._fangyu) { float shoushang = enemy.gongji - HA._fangyu; zongshanghai = shoushang * cishu; } if (zongshanghai >= HA._shengming) { UM.state = "dabuguo"; } else { AuM.playAudio("fight"); Instantiate(fightAnimation, enemy.gameObject.transform.position, enemy.gameObject.transform.rotation); HA._shengming -= (int)zongshanghai; HA._jingyan += enemy.jingyan; HA.plusJinbi(enemy.jinbi); UM.tipContent = "经验+" + enemy.jingyan + ",金币+" + enemy.jinbi; UM.tipTime = 3f; if (enemy.isDoorProtector) { List <GameObject> protectorGameObject = enemy.doorComponent.protectorGameObject; if (protectorGameObject.Remove(enemy.gameObject)) { if (protectorGameObject.Count == 0) { DoorAnim = enemy.doorComponent.gameObject.GetComponent <tk2dSpriteAnimator>(); DoorAnim.Play("DoorBig"); Destroy(enemy.doorComponent.gameObject, 0.5f); tk2dTileMap tm = GameObject.Find("TileMap").GetComponent <tk2dTileMap>(); int x, y; tm.GetTileAtPosition(enemy.doorComponent.gameObject.transform.position, out x, out y); tm.ClearTile(x, y, 1); } } } Destroy(enemy.gameObject); return(true); } } return(false); }
//对话完成后,移除精灵 public void clearJingLing() { tk2dTileMap tm = GameObject.Find("TileMap").GetComponent <tk2dTileMap>(); int x, y; tm.GetTileAtPosition(jingling.transform.position, out x, out y); tm.ClearTile(x, y, 1); Destroy(jingling); }
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); }
//false:不可通过 //true:可通过 public bool checkTile(int x, int y) { if (!(x < 11 && y < 11 && x > -1 && y > -1)) { return(false); } tm = GameObject.Find("TileMap").GetComponent <tk2dTileMap>(); AM = GetComponent <ActionManager>(); int tileID = tm.GetTile(x, y, 1); if (tileID == -1) { return(true); } tk2dRuntime.TileMap.TileInfo tileinfo = tm.GetTileInfoForTileId(tileID); if (tileinfo != null) { if (tileinfo.stringVal == "Wall") { return(false); } //拥有Prefab Vector2 startPosition = new Vector2(x - 0.2f, y); Vector2 Direction = new Vector2(x, y); RaycastHit2D hit = Physics2D.Raycast(startPosition, Direction, 0.4f); //Debug.DrawLine(new Vector3(x - 0.2f, y, 0), new Vector3(x + 0.2f, y, 0), Color.red); if (hit.collider != null) { switch (hit.collider.tag) { case "Talk": AM.talk(hit.collider.gameObject.GetComponent <Talk>()); return(false); case "Key": if (AM.key(hit.collider.gameObject.GetComponent <Key>())) { tm.ClearTile(x, y, 1); } break; case "Door": if (AM.door(hit.collider.gameObject.GetComponent <Door>())) { tm.ClearTile(x, y, 1); } return(false); case "DoorWall": if (AM.doorwall(hit.collider.gameObject.GetComponent <DoorWall>())) { tm.ClearTile(x, y, 1); } return(false); case "Baoshi": if (AM.baoshi(hit.collider.gameObject.GetComponent <Baoshi>())) { tm.ClearTile(x, y, 1); } break; case "Pingzi": if (AM.pingzi(hit.collider.gameObject.GetComponent <Pingzi>())) { tm.ClearTile(x, y, 1); } break; case "Yaoshui": if (AM.yaoshui(hit.collider.gameObject.GetComponent <Yaoshui>())) { tm.ClearTile(x, y, 1); } break; case "Zhuangbei": if (AM.zhuangbei(hit.collider.gameObject.GetComponent <Zhuangbei>())) { tm.ClearTile(x, y, 1); } break; case "Enemy": if (AM.enemy(hit.collider.gameObject.GetComponent <Enemy>())) { tm.ClearTile(x, y, 1); } return(false); case "StairUp": AM.stair(hit.collider.gameObject.GetComponent <Stair>()); break; case "StairDown": AM.stair(hit.collider.gameObject.GetComponent <Stair>()); break; case "Tool": if (AM.mttool(hit.collider.gameObject.GetComponent <mtTool>())) { tm.ClearTile(x, y, 1); } break; } return(true); } else { return(true); } } else { return(false); } }
public void ClearPrevLoc(int x, int y) { tileMap.ClearTile(x, y, 1); }