Sie sind auf Seite 1von 2

using UnityEngine;

using System.Collections;

public class LevelGenerator : MonoBehaviour {

//This script was written from class


public int rows;
public int cols;
public RoomData[,] grid;
public float roomWidth = 50.0f;
public float roomLength = 50.0f;

public GameObject[] roomPrefabs;

private Transform tf;

// Use this for initialization


void Start () {
// Load our variables
tf = gameObject.GetComponent<Transform>();
// Generate the level
GenerateLevel ();
}

// Update is called once per frame


void Update () {

public void GenerateLevel()


{
// Initialize our 2D array
grid = new RoomData[cols,rows];

// Create one row at time


for (int currentRow = 0; currentRow < rows; currentRow++) {
// Create one column at a time
for (int currentCol = 0; currentCol < cols; currentCol++) {

// Instantiate (and hold on to) our Room Object


GameObject newRoom = Instantiate (GetRandomRoom (),
Vector3.zero,
Quaternion.identity) as GameObject;
// Move it to the correct location
Vector3 offset;
offset = new Vector3 (currentCol * roomWidth, 0, currentRow
* roomLength);
newRoom.GetComponent<Transform> ().position = tf.position +
offset;
// Make it a child of the level
newRoom.GetComponent<Transform>().parent = tf;
// Save its RoomData component in the array
grid[currentCol,currentRow] =
newRoom.GetComponent<RoomData>();
// Open the appropriate doors
if (currentCol == 0) {
grid [currentCol, currentRow].eastWall.SetActive
(false);
} else if (currentCol == cols - 1) {
grid [currentCol, currentRow].westWall.SetActive
(false);
} else {
grid [currentCol, currentRow].eastWall.SetActive
(false);
grid [currentCol, currentRow].westWall.SetActive
(false);
}

if (currentRow == 0) {
grid [currentCol, currentRow].northWall.SetActive
(false);
} else if (currentRow == rows - 1) {
grid [currentCol, currentRow].southWall.SetActive
(false);
} else {
grid [currentCol, currentRow].northWall.SetActive
(false);
grid [currentCol, currentRow].southWall.SetActive
(false);
}
}
}
}

public GameObject GetRandomRoom () {


GameObject roomPrefab;
roomPrefab = roomPrefabs [Random.Range (0, roomPrefabs.Length)];
return roomPrefab;
}
}

Das könnte Ihnen auch gefallen