public int Distance(BattleMapBlock target) { //TODO 计算两点间的距离 return(0); }
public Vector3 GetCoordinate(BattleMapBlock mapBlock) { for (int i = columns - 1; i > 0; i--) { for (int j = 0; j < rows; j++) { if (_mapBlocks[i, j] == mapBlock) { return(new Vector3(i, j, 0f)); } } } return(new Vector3(-1, -1, 0f)); }
//判断该战区能不能召唤(所属权) public bool WarZoneBelong(Vector3 position) { int area; area = _mapBlocks[(int)position.x, (int)position.y].area; //units.Clear(); //emptyMapBlocks.Clear(); if (area == -1) { foreach (BattleMapBlock map in battleArea_1) { Debug.Log(map.x + ":" + map.y); if (_mapBlocks[map.x, map.y].transform.childCount != 0) { if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>() != null) { if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>().owner == "Enemy") { Debug.Log("This WarZone has Enemy,you can`t summon"); return(false); } else if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>().owner == "Self") { unit = _mapBlocks[map.x, map.y].GetComponentInChildren <Unit>(); units.Add(unit); } } } else { emptyMbpBlock = new BattleMapBlock(map.x, map.y); Debug.Log(emptyMbpBlock); emptyMapBlocks.Add(emptyMbpBlock); } } } if (area == 0) { foreach (BattleMapBlock map in battleArea0) { Debug.Log(map.x + ":" + map.y); if (_mapBlocks[map.x, map.y].transform.childCount != 0) { if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>() != null) { if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>().owner == "Enemy") { Debug.Log("This WarZone has Enemy,you can`t summon"); return(false); } else if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>().owner == "Self") { unit = _mapBlocks[map.x, map.y].GetComponentInChildren <Unit>(); units.Add(unit); } } } else { emptyMbpBlock = new BattleMapBlock(map.x, map.y); Debug.Log(emptyMbpBlock); emptyMapBlocks.Add(emptyMbpBlock); } } } if (area == 1) { foreach (BattleMapBlock map in battleArea1) { Debug.Log(map.x + ":" + map.y); if (_mapBlocks[map.x, map.y].transform.childCount != 0) { if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>() != null) { if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>().owner == "Enemy") { Debug.Log("This WarZone has Enemy,you can`t summon"); return(false); } else if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>().owner == "Self") { unit = _mapBlocks[map.x, map.y].GetComponentInChildren <Unit>(); units.Add(unit); } } } else { emptyMbpBlock = new BattleMapBlock(map.x, map.y); Debug.Log(emptyMbpBlock); emptyMapBlocks.Add(emptyMbpBlock); } } } if (area == 2) { foreach (BattleMapBlock map in battleArea2) { Debug.Log(map.x + ":" + map.y); if (_mapBlocks[map.x, map.y].transform.childCount != 0) { if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>() != null) { if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>().owner == "Enemy") { Debug.Log("This WarZone has Enemy,you can`t summon"); return(false); } else if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>().owner == "Self") { unit = _mapBlocks[map.x, map.y].GetComponentInChildren <Unit>(); units.Add(unit); } } } else { emptyMbpBlock = new BattleMapBlock(map.x, map.y); Debug.Log(emptyMbpBlock); emptyMapBlocks.Add(emptyMbpBlock); } } } if (area == 3) { foreach (BattleMapBlock map in battleArea3) { Debug.Log(map.x + ":" + map.y); if (_mapBlocks[map.x, map.y].transform.childCount != 0) { if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>() != null) { if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>().owner == "Enemy") { Debug.Log("This WarZone has Enemy,you can`t summon"); return(false); } else if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>().owner == "Self") { unit = _mapBlocks[map.x, map.y].GetComponentInChildren <Unit>(); units.Add(unit); } } } else { emptyMbpBlock = new BattleMapBlock(map.x, map.y); Debug.Log(emptyMbpBlock); emptyMapBlocks.Add(emptyMbpBlock); } } } if (area == 4) { foreach (BattleMapBlock map in battleArea4) { Debug.Log(map.x + ":" + map.y); if (_mapBlocks[map.x, map.y].transform.childCount != 0) { if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>() != null) { if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>().owner == "Enemy") { Debug.Log("This WarZone has Enemy,you can`t summon"); return(false); } else if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>().owner == "Self") { unit = _mapBlocks[map.x, map.y].GetComponentInChildren <Unit>(); units.Add(unit); } } } else { emptyMbpBlock = new BattleMapBlock(map.x, map.y); Debug.Log(emptyMbpBlock); emptyMapBlocks.Add(emptyMbpBlock); } } } if (area == 5) { foreach (BattleMapBlock map in battleArea5) { Debug.Log(map.x + ":" + map.y); if (_mapBlocks[map.x, map.y].transform.childCount != 0) { if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>() != null) { if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>().owner == "Enemy") { Debug.Log("This WarZone has Enemy,you can`t summon"); return(false); } else if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>().owner == "Self") { unit = _mapBlocks[map.x, map.y].GetComponentInChildren <Unit>(); units.Add(unit); } } } else { emptyMbpBlock = new BattleMapBlock(map.x, map.y); Debug.Log(emptyMbpBlock); emptyMapBlocks.Add(emptyMbpBlock); } } } if (area == 6) { foreach (BattleMapBlock map in battleArea6) { Debug.Log(map.x + ":" + map.y); if (_mapBlocks[map.x, map.y].transform.childCount != 0) { if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>() != null) { if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>().owner == "Enemy") { Debug.Log("This WarZone has Enemy,you can`t summon"); return(false); } else if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>().owner == "Self") { unit = _mapBlocks[map.x, map.y].GetComponentInChildren <Unit>(); units.Add(unit); } } } else { emptyMbpBlock = new BattleMapBlock(map.x, map.y); Debug.Log(emptyMbpBlock); emptyMapBlocks.Add(emptyMbpBlock); } } } if (area == 7) { foreach (BattleMapBlock map in battleArea7) { Debug.Log(map.x + ":" + map.y); if (_mapBlocks[map.x, map.y].transform.childCount != 0) { if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>() != null) { if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>().owner == "Enemy") { Debug.Log("This WarZone has Enemy,you can`t summon"); return(false); } else if (_mapBlocks[map.x, map.y].GetComponentInChildren <Unit>().owner == "Self") { unit = _mapBlocks[map.x, map.y].GetComponentInChildren <Unit>(); units.Add(unit); } } } else { emptyMbpBlock = new BattleMapBlock(map.x, map.y); Debug.Log(emptyMbpBlock); emptyMapBlocks.Add(emptyMbpBlock); } } } return(true); }
private void InitAndInstantiateMapBlocks() { // 更改地图数据位置则需修改此处路径 JsonData mapData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/Scripts/BattleMap/eg.json")); int mapDataCount = mapData.Count; this.columns = (int)mapData[mapDataCount - 1]["y"] + 1; this.rows = (int)mapData[mapDataCount - 1]["x"] + 1; _mapBlocks = new BattleMapBlock[rows, columns]; GameObject _instance = new GameObject(); battleArea_1 = new List <BattleMapBlock>(); battleArea0 = new List <BattleMapBlock>(); battleArea1 = new List <BattleMapBlock>(); battleArea2 = new List <BattleMapBlock>(); battleArea3 = new List <BattleMapBlock>(); battleArea4 = new List <BattleMapBlock>(); battleArea5 = new List <BattleMapBlock>(); battleArea6 = new List <BattleMapBlock>(); battleArea7 = new List <BattleMapBlock>(); int x = 0; int y = 0; int area = 0; for (int i = 0; i < mapDataCount; i++) { x = (int)mapData[i]["x"]; y = (int)mapData[i]["y"]; area = (int)mapData[i]["area"]; mapBlock = new BattleMapBlock(x, y); #region 存储同一战区的地图块 switch (area) { case -1: battleArea_1.Add(mapBlock); break; case 0: battleArea0.Add(mapBlock); break; case 1: battleArea1.Add(mapBlock); break; case 2: battleArea2.Add(mapBlock); break; case 3: battleArea3.Add(mapBlock); break; case 4: battleArea4.Add(mapBlock); break; case 5: battleArea5.Add(mapBlock); break; case 6: battleArea6.Add(mapBlock); break; case 7: battleArea7.Add(mapBlock); break; default: break; } #endregion //实例化地图块 _instance = GameObject.Instantiate(normalMapBlocks, new Vector3(x, y, 0f), Quaternion.identity); _instance.transform.SetParent(_tilesHolder); if (x == 0 && y == 0) { Debug.Log(string.Format("固定的灼烧块{0},{0}", x, y)); _instance.gameObject.AddComponent <BattleMapBlockBurning>(); _instance.gameObject.AddComponent <BattleMapBlock>(); _mapBlocks[x, y] = _instance.gameObject.GetComponent <BattleMapBlock>(); _mapBlocks[x, y].area = area; _mapBlocks[x, y].x = x; _mapBlocks[x, y].y = y; _mapBlocks[x, y].aStarState = AStarState.free; _mapBlocks[x, y].blockType = EMapBlockType.Normal; mapBlockDict.Add(new Vector2(x, y), _mapBlocks[x, y]);//寻路字典 } else { _instance.gameObject.AddComponent <BattleMapBlock>(); _mapBlocks[x, y] = _instance.gameObject.GetComponent <BattleMapBlock>(); _mapBlocks[x, y].area = area; _mapBlocks[x, y].x = x; _mapBlocks[x, y].y = y; _mapBlocks[x, y].aStarState = AStarState.free; _mapBlocks[x, y].blockType = EMapBlockType.Normal; mapBlockDict.Add(new Vector2(x, y), _mapBlocks[x, y]);//寻路字典 } int tokenCount = mapData[i]["token"].Count; if (tokenCount > 0) { if (tokenCount == 1) { Unit unit = InitAndInstantiateGameUnit(mapData[i]["token"][0], x, y); unit.mapBlockBelow = _mapBlocks[x, y]; _unitsList.Add(unit); _mapBlocks[x, y].AddUnit(unit); } //else //{ // Unit[] units = InitAndInstantiateGameUnits(mapData[i]["token"], tokenCount, x, y); // for (int j = 0; j < units.Length; j++) // { // //units[j].mapBlockBelow = _mapBlocks[x, y]; // _unitsList.Add(units[j]); // } // _mapBlocks[x, y].AddUnits(units); //} } } #region 得到每块地图块周围的地图块 BattleMapBlock[] neighbourBlock = new BattleMapBlock[4]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { Vector2 t = new Vector2(j, i - 1); Vector2 b = new Vector2(j, i + 1); Vector2 r = new Vector2(j + 1, i); Vector2 l = new Vector2(j - 1, i); if (t.x >= 0 && t.y >= 0 && t.x < columns && t.y < rows && _mapBlocks[(int)t.x, (int)t.y].transform.childCount == 0) { neighbourBlock[0] = mapBlockDict[t]; Debug.Log(neighbourBlock[0].x); } if (b.x >= 0 && b.y >= 0 && b.x < columns && b.y < rows && _mapBlocks[(int)b.x, (int)b.y].transform.childCount == 0) { neighbourBlock[1] = mapBlockDict[b]; } if (r.x >= 0 && r.y >= 0 && r.x < columns && r.y < rows && _mapBlocks[(int)r.x, (int)r.y].transform.childCount == 0) { neighbourBlock[2] = mapBlockDict[r]; } if (l.x >= 0 && l.y >= 0 && l.x < columns && l.y < rows && _mapBlocks[(int)l.x, (int)l.y].transform.childCount == 0) { neighbourBlock[3] = mapBlockDict[l]; } } } #endregion }