- Home /
BSP Tree Dungeon Generation via Tilemap
i am currently working on a dungeon generator and i know there have been answered a lot of questions but i didn't find the one to solve my problem.. i am not the best programmer but have set up a great way of building a bsp tree and spawn cubes at the center width a smaller width/height that it's area replacing my rooms.. the point is that i wanted to spawn rooms via tiles and the help of an int[,] array but my areas all don't fit in this curtain tilemap.. i tried to fix it and using int instead of float that i used before but there where a lot of errors caused by the divisions i use to split areas into 2 new ones and this results in a big mess.. is there a way to prober split those areas or round them to an int or something.. or what would be the best way to setup my idea :/ greets..
my code: (codeboy doesn't work)
using UnityEngine; using System.Collections.Generic;
public class dungeonV2 : MonoBehaviour { public int dunSizeX; public int dunSizeY; public int maxRoomSize;
public Transform areas;
public Transform rooms;
public Transform paths;
public List<leaf> tree = new List<leaf>();
public List<leaf> toCheck = new List<leaf>();
public class leaf{
public Vector2 leafcenter;
public float left;
public float bot;
public float width;
public float height;
public leaf parent;
public leaf leftLeaf;
public leaf rightLeaf;
public Vector2 housecenter;
public int house_x;
public int house_y;
public int house_z = 6;// Random.Range(4,20);
public bool isSplit;
public void Draw(Transform target){
GameObject leaf = GameObject.CreatePrimitive(PrimitiveType.Cube);
leaf.name = "Leaf: (" + this.left + "," + this.bot+") / (" + this.left+this.width+","+this.bot+this.height+")";
leaf.transform.parent = target;
leaf.renderer.material.color = new Color(Random.Range(0.01f,2f), Random.Range(0.01f,2f), Random.Range(0.01f,2f));
leaf.tag = "Tile";
leaf.transform.localScale = new Vector3(width, 0.1f, height);
leafcenter = new Vector2 (this.left + this.width / 2, this.bot + this.height / 2);
leaf.transform.localPosition = new Vector3(leafcenter.x, 0, leafcenter.y);
}
public void MakeHouse(int number,Transform target){
GameObject house = GameObject.CreatePrimitive(PrimitiveType.Cube);
house.name = "House";
house.transform.parent = target;
if (number == 1||number == 2)
house.renderer.material.color = Color.red;
else {
house.renderer.material.color = Color.white;
}
house.tag = "Tile";
house_x=(int)width-7;
house_y=(int)height-7;
house.transform.localScale = new Vector3(house_x, house_z, house_y);
housecenter = new Vector2 (this.left + this.width / 2, this.bot + this.height / 2);
house.transform.localPosition = new Vector3(housecenter.x, house_z/2, housecenter.y);
}
}
void OnGUI() {
if (GUILayout.Button ("Generate",GUILayout.Height(50),GUILayout.Width(100)))
{
tree.Clear();
GameObject[] Tiles=GameObject.FindGameObjectsWithTag("Tile");
foreach(GameObject t in Tiles){
Destroy(t);
}
Camera.main.transform.position=new Vector3(dunSizeX/2,20,dunSizeY/2);
Generate ();
}
}
void Generate () {
leaf root=CreateLeaf (0, 0, dunSizeX, dunSizeY);
toCheck.Add (root);
leaf splitTarget=root;
while(toCheck.Count>0){
Split (toCheck[0]);
}
int loop = 1;
foreach (leaf a in tree) {
a.Draw(areas);
a.MakeHouse(loop,rooms);
/* Debug.Log ("***************************************");
Debug.Log (loop+": Width:" +a.width+" Height: "+a.height);
Debug.Log ("***************************************");
*/
loop++;
}
tree[0].MakePaths(paths,tree[1]);
}
public void Split(leaf target){
if (target.isSplit==false){
toCheck.Remove(target);
if(target.width>maxRoomSize||target.height>maxRoomSize){
bool splitHor =(Random.Range(0f,1f)>0.5);
leaf leftLeaf;
leaf rightLeaf;
float splitter=Random.Range(1.7f,2.3f);
if(target.width>target.height){
splitHor=true;
}
if(target.width<target.height){
splitHor=false;
}
if(splitHor){
leftLeaf = CreateLeaf(target.left,target.bot,target.width/splitter,target.height);
leftLeaf.parent=target;
rightLeaf = CreateLeaf(target.left+target.width/splitter,target.bot,target.width-target.width/splitter,target.height);
rightLeaf.parent=target;
}
else{
leftLeaf = CreateLeaf(target.left,target.bot,target.width,target.height/splitter);
leftLeaf.parent=target;
rightLeaf = CreateLeaf(target.left,target.bot+target.height/splitter,target.width,target.height-target.height/splitter);
rightLeaf.parent=target;
}
target.rightLeaf=rightLeaf;
target.leftLeaf=leftLeaf;
tree.Remove(target);
toCheck.Remove(target);
tree.Add(leftLeaf);
toCheck.Add(leftLeaf);
tree.Add(rightLeaf);
toCheck.Add(rightLeaf);
target.isSplit=true;
}
else{
toCheck.Remove(target);
}
}
}
public leaf CreateLeaf(float left,float bot,float width,float height){
leaf Leaf = new leaf ();
Leaf.left = left;
Leaf.bot = bot;
Leaf.width = width;
Leaf.height = height;
return Leaf;
}
}
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
method for grid movement 1 Answer
repeated nullreferenceexception errors 0 Answers
BSP Tree for dungeons generation, HELP! 1 Answer
First Person RPG Logic... 0 Answers