public void Create() { //Create a quad and give the position and w/h if (IsLeaf()) { m_quad = m_assetFabric.GetRandomRoom(m_width, m_height, m_center); m_quad.transform.SetParent(transform); BSPDungeon dungeon = GameObject.FindObjectOfType <BSPDungeon>(); dungeon.AddLeaf(this); foreach (Door d in m_doors) { //Create a door GameObject door; if (d.position == 'u' || d.position == 'd') { door = m_assetFabric.GetDoor(d.center, d.size, false); } else { door = m_assetFabric.GetDoor(d.center, d.size, true); } if (door != null) { door.transform.SetParent(transform); } } } else { m_lChild.GetComponent <Room>().Create(); m_rChild.GetComponent <Room>().Create(); } }
/// <summary> /// Divide the room in a random Horizontal positions (random X value) /// </summary> void DivideVertically() { //We are able to create a room BSPDungeon dungeon = GameObject.FindObjectOfType <BSPDungeon>(); float minSize = dungeon.GetMinSize(); if (m_width > minSize) { //We are able to create a room float randomX = Random.Range(minSize, (int)m_width - minSize); m_divideH = false; m_divisionPos = randomX; //Divide and create two new rooms float lWidth = randomX; float rWidth = m_width - randomX; Vector3 leftMost = m_center - new Vector3(m_width * 0.5f, 0.0f, 0.0f); Vector3 lCenter = leftMost + new Vector3(lWidth * 0.5f, 0.0f, 0.0f); Vector3 rCenter = leftMost + new Vector3(lWidth + rWidth * 0.5f, 0.0f, 0.0f); CreateChild(lCenter, lWidth, m_height, true); CreateChild(rCenter, rWidth, m_height, false); m_lChild.GetComponent <Room>().SetBrother(m_rChild); m_rChild.GetComponent <Room>().SetBrother(m_lChild); } }
/// <summary> /// Divide the room in a random Horizontal positions (random Y value) /// </summary> void DivideHorizontally() { //Calculate offset //We are able to create a room BSPDungeon dungeon = GameObject.FindObjectOfType <BSPDungeon>(); float minSize = dungeon.GetMinSize(); if (m_height > minSize) { float randomY = Random.Range(minSize, (int)m_height - minSize); m_divideH = true; m_divisionPos = m_height - randomY; //Divide and create two new rooms Vector3 bottom = m_center - new Vector3(0.0f, m_height * 0.5f, 0.0f); float lHeight = randomY; float rHeight = m_height - randomY; Vector3 upCenter = bottom + new Vector3(0.0f, rHeight + lHeight * 0.5f, 0.0f); Vector3 downCenter = bottom + new Vector3(0.0f, rHeight * 0.5f, 0.0f); CreateChild(upCenter, m_width, lHeight, true); CreateChild(downCenter, m_width, rHeight, false); m_lChild.GetComponent <Room>().SetBrother(m_rChild); m_rChild.GetComponent <Room>().SetBrother(m_lChild); } }
void CreateChild(Vector3 center, float w, float h, bool left) { Room component = null; if (left) { if (m_lChild == null) { m_lChild = new GameObject("LChild"); m_lChild.transform.SetParent(transform); component = m_lChild.AddComponent <Room>(); } } else { if (m_rChild == null) { m_rChild = new GameObject("RChild"); m_rChild.transform.SetParent(transform); component = m_rChild.AddComponent <Room>(); } } if (component) { component.Init(); component.SetParent(gameObject); component.SetCenter(center); component.SetWidth(w); component.SetHeight(h); } BSPDungeon dungeon = GameObject.FindObjectOfType <BSPDungeon>(); dungeon.AddLeaf(component); }
public BSPDungeonNode(BSPDungeon _dungeon, BSPDungeonNode _parent, int _x, int _y, int _x2, int _y2) { x = _x; y = _y; x2 = _x2; y2 = _y2; parent = _parent; dungeon = _dungeon; left = null; right = null; depth = (parent == null) ? 1 : parent.depth + 1;//if parent is null then this is the top level if (depth == dungeon.depth) { shrink(); } else { split(); dungeon.letsjoinlist[depth].Add(this); } if (parent == null) { //we are at the top level and the split has been done and recursed all the way down //join and complete! //if it failed somewhere, exit before joining if (dungeon.hasfailed) { return; } for (int f = dungeon.depth - 1; f > 0; f--) { foreach (var g in dungeon.letsjoinlist[f]) { g.join(); } } } }
public void initBSPDungeon() { Transform[] tf = instance.GetComponentsInChildren<Transform>(); tf[0] = null; //костыль невероятной мощи foreach (Transform t in tf) { if (t != null) Destroy(t.gameObject); } playerCreated = false; dun = new BSPDungeon(mapWidth, mapHeight, minLeafSize, maxLeafSize); root = dun.createLeaf(0, mapWidth, 0, mapHeight); buildDungeonMap(root); }
void CreateRoom() { if (level != null) { Destroy(level); } level = new GameObject("Level"); level.transform.parent = gameObject.transform; BSPDungeon bspDungeon = new BSPDungeon(binarySpacePartitionSettings, roomSettings, connectionSettings); bspDungeon.GenerateDungeon(); List <Partition> finalPartitions = bspDungeon.GenerateFinalPartitionList(); List <Partition> allPartitions = bspDungeon.GeneratePartitionList(); Vector3 position; Vector3 size; csgModel = level.AddComponent <CSGModelBase>(); //Debug.Log("drawing rooms and partitions"); foreach (Partition partition in finalPartitions) { if (drawPartitions) { position = (Vector2)(partition.start + partition.end) / 2; size = (Vector2)(partition.end - partition.start); position.z += 5; size.x -= 0.5f; size.y -= 0.5f; size.z = 1; //Debug.Log("Position: " + position + ", Size: " + size); csgModel.CreateBrush(PrimitiveBrushType.Cube, position, size, Quaternion.identity, partitionMaterial); } if (drawRooms) { if (partition.room != null) { foreach (Shape shape in partition.room) { if (shape.GetType() == typeof(Rectangle)) { Rectangle rectangle = (Rectangle)shape; position = (Vector2)(rectangle.start + rectangle.end) / 2; position.z -= 0; size = (Vector2)(rectangle.end - rectangle.start); size.z = 1; //Debug.Log("Position: " + position + ", Size: " + size); csgModel.CreateBrush(PrimitiveBrushType.Cube, position, size, Quaternion.identity, roomMaterial); } } } } } foreach (Partition partition in allPartitions) { if (drawConnections) { foreach (Shape shape in partition.connection) { if (shape.GetType() == typeof(Rectangle)) { Rectangle rectangle = (Rectangle)shape; position = (Vector2)(rectangle.start + rectangle.end) / 2; position.z -= 5; size = (Vector2)(rectangle.end - rectangle.start); size.z = 1; //Debug.Log("Position: " + position + ", Size: " + size); csgModel.CreateBrush(PrimitiveBrushType.Cube, position, size, Quaternion.identity, connectionMaterial); } } } } Debug.Log(finalPartitions.Count); csgModel.Build(true, false); }