public void CreateNavMeshObstacles(MazeGeneration.Maze maze) { // Create New walls for (int x = 0; x < 51; x++) { for (int y = 0; y < 51; y++) { _walls[x, y].gameObject.SetActive(maze.squaredCells[x, y]); } } NavMeshSurface.BuildNavMesh(); }
static void Main(string[] args) { var maze = new Maze(20, 20); maze.Display(); }
void Start() { // Crear la seed int iSeed = UnityEngine.Random.Range(0, 99999); UnityEngine.Random.InitState(iSeed); // Me aseguro que la Y sea la mayor, y la X la menor if (mazeSizeX > mazeSizeY) { int minSize = mazeSizeY; mazeSizeY = mazeSizeX; mazeSizeX = minSize; } // Usaremos 4 maze para crear la ilusion de que se hacen alrededor de la Safe Zone List <Vector3> mazePos = new List <Vector3>(); mazePos.Add(new Vector3(0, 0, 0)); mazePos.Add(new Vector3(mazeSizeY * 12.0f, 0, 0)); mazePos.Add(new Vector3(mazeSizeX * 12.0f, 0, mazeSizeY * 12.0f)); mazePos.Add(new Vector3(0, 0, mazeSizeX * 12.0f)); // Instanciar los 4 maze ConnectMaze(); for (int i = 0; i < 4; i++) { int sizeX = (i % 2 == 0) ? mazeSizeX : mazeSizeY; int sizeY = (i % 2 == 0) ? mazeSizeY : mazeSizeX; // Paredes MazeGeneration.Maze maze = MazeGeneration.Maze.CreateInstance <MazeGeneration.Maze>(); maze.Init(sizeX, sizeY, mazePos[i], iSeed, mazeWall); maze.Display(); // Piso mazeFloor.transform.localScale = new Vector3(sizeY * 12.0f, 1.0f, sizeX * 12.0f); mazeFloor.GetComponent <Renderer>().sharedMaterial.mainTextureScale = new Vector2(mazeFloor.transform.localScale.x / 8.0f, mazeFloor.transform.localScale.z / 16.0f); UnityEngine.Object.Instantiate(mazeFloor, new Vector3(mazePos[i].x + sizeY * 6.0f, 0.0f, mazePos[i].z + sizeX * 6.0f), Quaternion.identity); } // Obtener el centro del maze int sizeXCenter = (mazeSizeX >= mazeSizeY) ? mazeSizeX : mazeSizeY; int sizeYCenter = (mazeSizeX < mazeSizeY) ? mazeSizeX : mazeSizeY; Vector3 mazeCenterPos = new Vector3(sizeXCenter * 12.0f - Math.Abs(sizeXCenter - sizeYCenter) * 6.0f, 0, sizeYCenter * 12.0f + Math.Abs(sizeXCenter - sizeYCenter) * 6.0f); // Instanciar la Safe Zone mazeCenter.transform.localScale = new Vector3(Math.Abs(sizeXCenter - sizeYCenter) * 12.0f, 1.0f, Math.Abs(sizeXCenter - sizeYCenter) * 12.0f); mazeCenter.GetComponent <Renderer>().sharedMaterial.mainTextureScale = new Vector2(mazeCenter.transform.localScale.x / 3.0f, mazeCenter.transform.localScale.z / 3.0f); UnityEngine.Object.Instantiate(mazeCenter, mazeCenterPos, Quaternion.identity); // Instanciar al player en Safe Zone GameManager.Instance.goPlayer = UnityEngine.Object.Instantiate(player, mazeCenterPos, Quaternion.identity) as GameObject; GameManager.Instance.SetReferences(); // Instanciar Gates Vector3 gateScale = mazeGates.transform.localScale; UnityEngine.Object.Instantiate(mazeGates, new Vector3(162.19f, -24.0f, 203.54f), Quaternion.Euler(0, 180, 0)); UnityEngine.Object.Instantiate(mazeGates, new Vector3(162.19f, -24.0f, 120.06f), Quaternion.Euler(0, 0, 0)); UnityEngine.Object.Instantiate(mazeGates, new Vector3(120.16f, -24.0f, 162.33f), Quaternion.Euler(0, 90, 0)); UnityEngine.Object.Instantiate(mazeGates, new Vector3(204.00f, -24.0f, 162.33f), Quaternion.Euler(0, 270, 0)); /// Cambiar a evento Wait(8.0f, () => { InstanciateStuff(); }); }
private void CreateMaze(Vector3 playerPosition) { // Clear old Walls Entities.ForEach((Entity wall, ref TerrainComponent terrainComponent) => { PostUpdateCommands.DestroyEntity(wall); }); bool isPassed = false; MazeGeneration.Maze mazeData = null; while (!isPassed) { // Create Maze mazeData = new MazeGeneration.Maze(25, 25); mazeData.WallToSquare(); int playerPositionX = Mathf.RoundToInt(playerPosition.x); int playerPositionY = Mathf.RoundToInt(playerPosition.z); isPassed = !mazeData.squaredCells[playerPositionX, playerPositionY]; playerPosition = new Vector3(playerPositionX, playerPosition.y, playerPositionY); } // Create Entity EntityManager entityManager = World.Active.EntityManager; EntityArchetype obsticleType = entityManager.CreateArchetype( typeof(TerrainComponent), typeof(Translation), typeof(RenderMesh), typeof(LocalToWorld) ); List <Vector2> moveablePosition = new List <Vector2>(); for (int x = 0; x < 51; x++) { for (int y = 0; y < 51; y++) { Enum.TerrainType type = Enum.TerrainType.None; if (!mazeData.squaredCells[x, y]) { if (x % 2 == 1 && y % 2 == 1) { moveablePosition.Add(new Vector2(x, y)); } continue; } Entity entity = entityManager.CreateEntity(obsticleType); entityManager.SetComponentData(entity, new Translation { Value = new float3(x, 0, y) }); Material material = GameManager.GetInstance().GroundMaterials[1]; entityManager.SetSharedComponentData(entity, new RenderMesh { mesh = GameManager.GetInstance().TerrainMesh, material = material }); } } // Create new treasure Entity treasure = entityManager.CreateEntity(entityManager.CreateArchetype( typeof(TreasureComponent), typeof(Translation), typeof(RenderMesh), typeof(LocalToWorld) )); var treasurePosition = moveablePosition[Random.Range(0, moveablePosition.Count)]; var treasurePositionV3 = new float3(treasurePosition.x, 0, treasurePosition.y); entityManager.SetComponentData(treasure, new Translation { Value = treasurePositionV3 }); Material material2 = GameManager.GetInstance().GroundMaterials[2]; entityManager.SetSharedComponentData(treasure, new RenderMesh { mesh = GameManager.GetInstance().PlayerMesh, material = material2 }); GameManager.GetInstance().CreateNavMeshObstacles(mazeData); GameManager.GetInstance().NavMeshAgent.transform.position = playerPosition; GameManager.GetInstance().NavMeshAgent.SetDestination(treasurePositionV3); }