/// <summary>Разместить префаб ключа от выхода</summary> /// <param name="chunk">Текущий блок лабиринта</param> private void PlaceExitKey(Chunk chunk) { if (!chunk.hasExitKey) return; MazePrefabID mazePrefabID = MazePrefabID.EXIT_KEY; int numnberRandomPrefab = mazePrefabContainer.GetRandomNumberPrefab(mazePrefabID); GameObject cloningPrefab = mazePrefabContainer.prefabs[mazePrefabID][numnberRandomPrefab]; GameObject gameObjectExitKey = Instantiate(cloningPrefab, transform) as GameObject; Vector3 newPosition = Vector3.zero; newPosition.x = chunk.x * chunkSize + chunkSize / 2f; newPosition.z = chunk.y * chunkSize; gameObjectExitKey.transform.position = newPosition; // Развернуть стенд с ключем в сторону отсутствующей стены Vector3 newRotation = Vector3.zero; newRotation.x = gameObjectExitKey.transform.eulerAngles.x; newRotation.y = gameObjectExitKey.transform.eulerAngles.y; newRotation.z = gameObjectExitKey.transform.eulerAngles.z; if (!chunk.hasRightWall) { newRotation.y -= 90f; } else if (!chunk.hasLeftWall) { newRotation.y += 90f; } else if (!chunk.hasTopWall) { newRotation.y += 180f; } // в остальных случаях newRotation.y остается прежним gameObjectExitKey.transform.eulerAngles = newRotation; gameObjectExitKey.SetActive(true); }
/// <summary>Получить случайный префаб по заданному id</summary> public GameObject GetRandomPrefab(MazePrefabID mazePrefabID) { if (prefabs[mazePrefabID].Count == 0) { return(null); } return(prefabs[mazePrefabID][GetRandomNumberPrefab(mazePrefabID)]); }
/// <summary>Вставить дверь в объект <paramref name="anyGameObject"/></summary> /// <param name="anyGameObject">Объект, в котоый будет помещена дверь</param> private void InsertExitDoor(GameObject anyGameObject) { MazePrefabID mazePrefabID = MazePrefabID.EXIT_DOOR; int numnberRandomPrefab = mazePrefabContainer.GetRandomNumberPrefab(mazePrefabID); GameObject cloningPrefab = mazePrefabContainer.prefabs[mazePrefabID][numnberRandomPrefab]; GameObject gameObjectExitDoor = Instantiate(cloningPrefab, anyGameObject.transform) as GameObject; gameObjectExitDoor.SetActive(true); }
/// <summary>Разместить префаб любой стены</summary> /// <param name="wallPosition">Место расположения на поле</param> /// <param name="wallRotation">Угол разворота</param> /// <returns>Возвращает размещенный на сцене объект стены</returns> private GameObject PlaceAnyWalls(Vector3 wallPosition, Quaternion wallRotation) { MazePrefabID mazePrefabID = MazePrefabID.WALL; int numnberRandomPrefab = mazePrefabContainer.GetRandomNumberPrefab(mazePrefabID); GameObject cloningPrefab = mazePrefabContainer.prefabs[mazePrefabID][numnberRandomPrefab]; GameObject gameObjectWall = Instantiate(cloningPrefab, transform) as GameObject; gameObjectWall.transform.position = wallPosition; gameObjectWall.transform.rotation = wallRotation; gameObjectWall.SetActive(true); return gameObjectWall; }
/// <summary>Разместить префабы пола</summary> /// <param name="chunk">Текущий блок лабиринта</param> private void PlaceFloors(Chunk chunk) { MazePrefabID mazePrefabID = MazePrefabID.FLOOR; int numnberRandomPrefab = mazePrefabContainer.GetRandomNumberPrefab(mazePrefabID); GameObject cloningPrefab = mazePrefabContainer.prefabs[mazePrefabID][numnberRandomPrefab]; GameObject gameObjectFloor = Instantiate(cloningPrefab, transform) as GameObject; Vector3 newPosition = Vector3.zero; newPosition.x = chunk.x * chunkSize + chunkSize / 2f; newPosition.z = chunk.y * chunkSize; gameObjectFloor.transform.position = newPosition; gameObjectFloor.SetActive(true); }
/// <summary>Разместить префабы сундуков</summary> /// <param name="chunk">Текущий блок лабиринта</param> private void PlaceChestsInDeadEnd(Chunk chunk) { if (!chunk.isDeadEnd || chunk.hasExitKey || chunk.hasExitDoor || chunk.hasChest || chunk.hasTrap) return; if (rnd.Next(0, 100) > 32) return; chunk.hasChest = true; MazePrefabID mazePrefabID = MazePrefabID.CHEST; int numnberRandomPrefab = mazePrefabContainer.GetRandomNumberPrefab(mazePrefabID); GameObject cloningPrefab = mazePrefabContainer.prefabs[mazePrefabID][numnberRandomPrefab]; GameObject gameObjectChest = Instantiate(cloningPrefab, transform) as GameObject; Vector3 newPosition = Vector3.zero; newPosition.x = chunk.x * chunkSize + chunkSize / 2f; newPosition.z = chunk.y * chunkSize; // Развернуть сундук в сторону отсутствующей стены Vector3 newRotation = Vector3.zero; newRotation.x = gameObjectChest.transform.eulerAngles.x; newRotation.y = gameObjectChest.transform.eulerAngles.y; newRotation.z = gameObjectChest.transform.eulerAngles.z; if (!chunk.hasRightWall) { newPosition.z -= 1f; newRotation.y -= 90f; } else if (!chunk.hasLeftWall) { newPosition.z += 1f; newRotation.y += 90f; } else if (!chunk.hasTopWall) { newPosition.x += 1f; newRotation.y += 180f; } else if (!chunk.hasBottomWall) { newPosition.x -= 1f; } gameObjectChest.transform.position = newPosition; gameObjectChest.transform.eulerAngles = newRotation; gameObjectChest.SetActive(true); }
/// <summary>Разместить префабы ловушек</summary> private void PlaceTraps() { const double percentOfTraps = 0.23; int countTraps = Convert.ToInt32(mazeStructure.sizeX * mazeStructure.sizeY * percentOfTraps); int countChunksForTraps = getCountAvailableChunksForTraps(); if (countTraps > countChunksForTraps) countTraps = countChunksForTraps; while (countTraps > 0) { Chunk chunk = mazeStructure.GetRandomChunk(); if (chunk.hasChest || chunk.isDeadEnd || chunk.hasExitKey || chunk.hasExitDoor) continue; if (chunk.hasTrap || chunk.countNeighbourTraps > 2) continue; chunk.hasTrap = true; MazePrefabID mazePrefabID = MazePrefabID.TRAP; if (rnd.Next(0, getCountTraps()) < mazePrefabContainer.prefabs[MazePrefabID.TRAP_WALL].Count) { if (chunk.hasAnyWalls) { mazePrefabID = MazePrefabID.TRAP_WALL; } else { mazePrefabID = MazePrefabID.TRAP; } } else { mazePrefabID = MazePrefabID.TRAP; } int numnberRandomPrefab = mazePrefabContainer.GetRandomNumberPrefab(mazePrefabID); GameObject cloningPrefab = mazePrefabContainer.prefabs[mazePrefabID][numnberRandomPrefab]; GameObject gameObjectTrap = Instantiate(cloningPrefab, transform) as GameObject; Vector3 newPosition = Vector3.zero; newPosition.x = chunk.x * chunkSize + chunkSize / 2f; newPosition.z = chunk.y * chunkSize; gameObjectTrap.transform.position = newPosition; // Развернуть ловушку в сторону, где есть стена if (mazePrefabID == MazePrefabID.TRAP_WALL) { Vector3 newRotation = Vector3.zero; newRotation.x = gameObjectTrap.transform.rotation.x; newRotation.z = gameObjectTrap.transform.rotation.z; while (true) { int side = rnd.Next(0, 4); if (side == 0) { if (chunk.hasRightWall) { newRotation.y += 180f; break; } } else if (side == 1) { if (chunk.hasLeftWall) { newRotation.y += 0f; break; } } else if (side == 2) { if (chunk.hasBottomWall) { newRotation.y += -90f; break; } } else if (side == 3) { if (chunk.hasTopWall) { newRotation.y += 90f; break; } } }//elihw gameObjectTrap.transform.rotation = Quaternion.Euler(newRotation); } gameObjectTrap.SetActive(true); countTraps--; } }
/// <summary>Получить случайный номер префаба в списке по заданному id</summary> public int GetRandomNumberPrefab(MazePrefabID mazePrefabID) { return(Random.Range(0, prefabs[mazePrefabID].Count)); }