private void PlaceTrap() { for (int i = 0; i < trap - target; i++) { // Step.1 隨機設置陷阱位置 List <int> placeIndex = new List <int>(); // 可使用網格索引 List <int> trapNeighborIndex; // 陷阱鄰近網格索引 int placeRandom; // 隨機索引值 int placeNeighborTrap; // 鄰近陷阱數 bool placeAgain; // 不滿足條件 gridIndex.ForEach(index => placeIndex.Add(index)); // 複製一份當前剩餘的網格列表,僅用於下方迴圈; do { // 重置參數 placeRandom = placeIndex[Random.Range(0, placeIndex.Count)]; placeNeighborTrap = 0; placeAgain = false; // 檢查鄰近網格 lock (trapNeighborIndex = HexagonMesh.FindNeighborIndex(placeRandom)) { for (int j = 0; j < trapNeighborIndex.Count; j++) { int indexNeighbor = trapNeighborIndex[j]; // 鄰近網格是否為安全區 if (HexagonMesh.grids[indexNeighbor].gridInfo == -999) { placeAgain = true; placeIndex.Remove(placeRandom); // 移除不符合條件之隨機索引值 } // 鄰近網格是否為陷阱或寶藏 else if (HexagonMesh.grids[indexNeighbor].gridInfo == -1 || HexagonMesh.grids[indexNeighbor].gridInfo == -99) { placeNeighborTrap++; } // 鄰近陷阱與寶藏數量超過難度限制,重新生成索引值 if (placeNeighborTrap > maxTrapGroup) { placeAgain = true; placeIndex.Remove(placeRandom); // 移除不符合條件之隨機索引值 } } } }while (placeAgain); // 檢查完成 gridIndex.Remove(placeRandom); HexagonMesh.grids[placeRandom].gridInfo = -1; if (showAll) { HexagonMesh.grids[placeRandom].gridImage.sprite = themeManager.iconTitleTrap.sprite; } } }
private void SafeArea() { gridIndex.Remove(firstIndex); HexagonMesh.grids[firstIndex].gridInfo = -999; List <int> safeIndex = HexagonMesh.FindNeighborIndex(firstIndex); for (int i = 0; i < safeIndex.Count; i++) { gridIndex.Remove(safeIndex[i]); HexagonMesh.grids[safeIndex[i]].gridInfo = -777; } }
private void PlaceTreasure() { for (int i = 0; i < target; i++) { // Step.1 隨機設置寶藏位置,但是寶藏彼此不得相鄰 List <int> placeTreasureIndex = new List <int>(); // 寶藏可佈置網格索引 List <int> placeTrapIndex; // 寶藏鄰近陷阱可佈置網格索引 List <int> treasureNeighborIndex; // 寶藏鄰近網格索引 bool placeAgain; // 不滿足條件 int placeRandom; // 隨機索引值 int placeNeighborTrap; // 鄰近陷阱數 gridIndex.ForEach(index => placeTreasureIndex.Add(index)); // 複製一份當前剩餘的網格列表,僅用於下方迴圈; do { // 重置參數 placeTrapIndex = new List <int>(); placeAgain = false; placeRandom = placeTreasureIndex[Random.Range(0, placeTreasureIndex.Count)]; placeNeighborTrap = 0; // 檢查鄰近網格 HexagonMesh.grids[placeRandom].Neighbor = HexagonMesh.GetNeighbor(placeRandom); HexagonMesh.grids[placeRandom].NeighborIndex = HexagonMesh.FindNeighborIndex(placeRandom); treasureNeighborIndex = HexagonMesh.grids[placeRandom].NeighborIndex; treasureNeighborIndex.ForEach(index => placeTrapIndex.Add(index)); // 複製一份寶藏鄰近網格列表,新列表將用於佈置陷阱; lock (treasureNeighborIndex) { for (int j = 0; j < treasureNeighborIndex.Count; j++) { int indexNeighbor = treasureNeighborIndex[j]; // 鄰近網格是否為寶藏或安全區 if (HexagonMesh.grids[indexNeighbor].gridInfo == -99 || HexagonMesh.grids[indexNeighbor].gridInfo == -999) { placeAgain = true; placeTreasureIndex.Remove(placeRandom); // 移除不符合條件之隨機索引值 } // 鄰近網格是否為陷阱 else if (HexagonMesh.grids[indexNeighbor].gridInfo == -1) { placeNeighborTrap++; placeTrapIndex.Remove(indexNeighbor); } // 鄰近陷阱數量超過難度限制,重新生成索引值 if (placeNeighborTrap > maxTrapGroup) { placeAgain = true; placeTreasureIndex.Remove(placeRandom); // 移除不符合條件之隨機索引值 } } } }while (placeAgain); // 檢查完成 gridIndex.Remove(placeRandom); HexagonMesh.grids[placeRandom].gridInfo = -99; HexagonMesh.grids[placeRandom].isSuspected = true; if (showAll) { HexagonMesh.grids[placeRandom].gridImage.sprite = themeManager.iconTitleTreasure.sprite; } // Step.2 寶藏周圍隨機設置一處陷阱 if (placeTrapIndex.Count > 0) { do { if (placeTrapIndex.Count == 0) { break; } // 重置參數 placeRandom = placeTrapIndex[Random.Range(0, placeTrapIndex.Count)]; placeNeighborTrap = 0; placeAgain = false; // 檢查鄰近網格 List <int> trapNeighborIndex; // 陷阱鄰近網格索引 lock (trapNeighborIndex = HexagonMesh.FindNeighborIndex(placeRandom)) { for (int j = 0; j < trapNeighborIndex.Count; j++) { int indexNeighbor = trapNeighborIndex[j]; // 鄰近網格是否為安全區 if (HexagonMesh.grids[indexNeighbor].gridInfo == -999) { placeAgain = true; placeTrapIndex.Remove(placeRandom); // 移除不符合條件之隨機索引值 } // 鄰近網格是否為陷阱或寶藏 else if (HexagonMesh.grids[indexNeighbor].gridInfo == -1 || HexagonMesh.grids[indexNeighbor].gridInfo == -99) { placeNeighborTrap++; } // 鄰近陷阱與寶藏數量超過難度限制,重新生成索引值 if (placeNeighborTrap > maxTrapGroup) { placeAgain = true; placeTrapIndex.Remove(placeRandom); // 移除不符合條件之隨機索引值 } } } }while (placeAgain); // 檢查完成 gridIndex.Remove(placeRandom); HexagonMesh.grids[placeRandom].gridInfo = -1; HexagonMesh.grids[placeRandom].isSuspected = true; if (showAll) { HexagonMesh.grids[placeRandom].gridImage.sprite = themeManager.iconTitleTrap.sprite; } } else { Debug.LogWarning("陷阱危機"); } } }