//instatiation of maze and indices public void instantiate_maze(maze dungeon) { Transform mazeObject = new GameObject("mazeObject").transform; GameObject generator = GameObject.FindGameObjectWithTag("Mg"); Vector3 startPoint = generator.transform.position; if (mazeLength % 2 == 0) { startPoint.x -= ((mazeLength / 2) - 0.5f) * 16; startPoint.y += ((mazeLength / 2) - 0.5f) * 16; } else { startPoint.x -= Mathf.Floor(mazeLength / 2) * 16; startPoint.y += Mathf.Floor(mazeLength / 2) * 16; } int startIndex_x, startIndex_y, endIndex_x, endIndex_y; //Select random start and end point or make them fixed to the most left-up and the most right-down if (select_random_start_and_end) { startIndex_x = Random.Range(0, mazeLength); startIndex_y = Random.Range(0, mazeLength); endIndex_x = Random.Range(0, mazeLength); endIndex_y = Random.Range(0, mazeLength); while (startIndex_x == endIndex_x && startIndex_y == endIndex_y) { endIndex_x = Random.Range(0, mazeLength); endIndex_y = Random.Range(0, mazeLength); } } else { startIndex_x = startIndex_y = 0; endIndex_x = endIndex_y = mazeLength - 1; } dungeon.start_i = startIndex_y; dungeon.start_j = startIndex_x; dungeon.end_i = endIndex_y; dungeon.end_j = endIndex_x; dungeon.start_x = startPoint.x + dungeon.start_j * 16; dungeon.start_y = startPoint.y - dungeon.start_i * 16; for (int i = 0; i < mazeLength; i++) { for (int j = 0; j < mazeLength; j++) { Vector3 point = startPoint; point.x += j * 16; point.y -= i * 16; cell room = dungeon.getCells()[i][j]; int index = 0; bool[] doors = room.getDoors(); if (doors[0]) { index += 8; } if (doors[1]) { index += 4; } if (doors[2]) { index += 2; } if (doors[3]) { index += 1; } GameObject newRoom; if (i == startIndex_y && j == startIndex_x) { newRoom = Instantiate(roomArray_start[index], point, Quaternion.identity) as GameObject; } else if (i == endIndex_y && j == endIndex_x) { newRoom = Instantiate(roomArray_end[index], point, Quaternion.identity) as GameObject; } else { newRoom = Instantiate(roomArray[index], point, Quaternion.identity) as GameObject; } newRoom.transform.SetParent(mazeObject); GameObject marker; if (doors[0]) { marker = Instantiate(markers[directions.north], newRoom.transform); } if (doors[1]) { marker = Instantiate(markers[directions.east], newRoom.transform); } if (doors[2]) { marker = Instantiate(markers[directions.south], newRoom.transform); } if (doors[3]) { marker = Instantiate(markers[directions.west], newRoom.transform); } dungeonObjects[i][j] = newRoom; } } }