I’m still rather new to unity but I have a beginners level amount of knowledge with c#.
Im trying to build a script that will, when it is run, generate a 8x8(as in rooms) dungeon. I have already made models for it and they are 15x15. My two questions are where would I put this script so that it will run when I start the game and are there any problems that would cause this to not work
I’m not asking for someone to clean up/tidy the script because I’m going to do that later as I just threw this together as a start.
Script
public class GenerateDungeon : MonoBehaviour {
// Use this for initialization
//1 Tjunc 2 Crossroads 3 CorridorVert 4 CorridorHorz
//5 Left 8 Left 90
//6 Right 7 Right90
//0 is empty 30 is barrier
bool[] TJun = { false, true, true, true }; //True means open False means walled off
bool[] Corridor = { false, true, false, true }; //defining accesability for diff rooms
bool[] OppCorridor = { true, false, true, false };
bool[] CrossRoads = { true, true, true, true };
bool[] TurnRight = { true, false, false, true };
bool[] TurnLeft = { true, true, false, false };
bool[] TurnRight90 = { false, false, true, true };
bool[] TurnLeft90 = { false, true, true, false };
bool[] Empty = { true, true, true, true };
void Start () {
MapGenerate();
}
bool CheckNeighborbility(int[,]MapData, bool[]PieceParamaters, int PieceX, int PieceY)
{ //Checking to see if room can fit
if (MapData[PieceY,PieceX] == 30) //Doesnt check walls as to preven out of bounds error
{ //Don't need to change barriers anyway
return false;
}
int[] NeighborPieces = { MapData[PieceY, PieceY - 1], MapData[PieceY - 1, PieceY],
MapData[PieceY, PieceY + 1], MapData[PieceY + 1, PieceY] };
int i = 0;
bool[] SideOps = new bool[4];
while (i < 4)
{
int PLCHNum = NeighborPieces[i];
if (PLCHNum == 0)
{
SideOps[i] = Empty[i];
}
if (PLCHNum == 1)
{
SideOps[i] = TJun[i];
}
if (PLCHNum == 2)
{
SideOps[i] = CrossRoads[i];
}
if (PLCHNum == 3)
{
SideOps[1] = OppCorridor[i];
}
if (PLCHNum == 4)
{
SideOps[1] = Corridor[i];
}
if (PLCHNum == 5)
{
SideOps[i] = TurnLeft[i];
}
if (PLCHNum == 6)
{
SideOps[i] = TurnRight[i];
}
if (PLCHNum == 7)
{
SideOps[i] = TurnRight90[i];
}
if (PLCHNum == 8)
{
SideOps[i] = TurnLeft90[i];
}
i++;
}
if (SideOps == PieceParamaters)
{
return true;
}
else
{
return false;
}
}
bool[] FlipPiece90(bool[] OpNeeded)
{
bool[] Holder = OpNeeded;
OpNeeded[0] = Holder[3];
OpNeeded[1] = Holder[0];
OpNeeded[2] = Holder[1];
OpNeeded[3] = Holder[2];
return OpNeeded;
}
void MapGenerate()
{
int[,] MapGrid = new int[8,8];
for (int w = 0; w < 8; w++) //setting all parts in the mapdata to empty
{
for (int y = 0; y < 8; y++)
{
MapGrid[w, y] = 0;
}
}
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (i == 7 || i == 0)
{
MapGrid[i, j] = 30;
}
if (j == 7 || j == 0)
{
MapGrid[i, j] = 30;
}
bool works = false;
bool[] PieceParams = new bool[4];
while (works && MapGrid[i, j] != 30)
{
int PlaceHolderNum = Random.Range(0, 8);
if (PlaceHolderNum == 0)
{
PieceParams = Empty;
}
if (PlaceHolderNum == 1 )
{
PieceParams = TJun;
}
if (PlaceHolderNum == 2)
{
PieceParams = CrossRoads;
}
if (PlaceHolderNum == 3)
{
PieceParams = Corridor;
}
if (PlaceHolderNum == 4)
{
PieceParams = OppCorridor;
}
if (PlaceHolderNum == 5)
{
PieceParams = TurnLeft;
}
if (PlaceHolderNum == 6)
{
PieceParams = TurnRight;
}
if (PlaceHolderNum == 7)
{
PieceParams = TurnRight90;
}
if (PlaceHolderNum == 8)
{
PieceParams = TurnLeft90;
}
if (CheckNeighborbility(MapGrid, PieceParams, j, i))
{
works = true;
Quaternion DngRoomRot = new Quaternion(0f, 0f, 0f, 0f);
Vector3 DngRoomLoc = new Vector3(i * 15, j * 15, 0f);
GameObject DngRoom = GameObject.Find("Crossroads");
Instantiate(DngRoom, DngRoomLoc, DngRoomRot );
}
}
}
}
Debug.Log(MapGrid.ToString());
}
// Update is called once per frame
void Update () {
}
}