Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by champagne2008 · Sep 10, 2014 at 04:05 AM · generatedungeon

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;
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges