Hey guys! I’ve been working on a game idea of mine - but for that idea I need dungeon generation.
I’m NOT asking for help - just showcasing my algorithm/approach to see what you guys think.
I am aware that there is plenty of dungeon generation resources out there - I have been using them. But if you happen to know of a really good one you want to make sure I know about or have any good tips/resources - I will gladly check them out and appreciate the help !
Anyway, I thought I’d attach my script to see what you guys think.
using UnityEngine;
using System.Collections;
public class RandomRoomBuilder : MonoBehaviour
{
public GameObject Handle;
public GameObject Placer;
public GameObject Wall;
GameObject placedTile;
GameObject placedWall;
// Use this for initialization
void Start ()
{
float roomLength = Random.Range(2, 20);
float roomWidth = Random.Range(2, 20);
roomLength = Mathf.Round(roomLength);
roomWidth = Mathf.Round(roomWidth);
float placerCurrentWidthPos = Handle.transform.position.x;
float placerCurrentLengthPos = Handle.transform.position.z;
for(int y = 0; y < roomLength; y++)
{
for(int i = 0; i < roomWidth; i++)
{
placerCurrentWidthPos = Mathf.Round (placerCurrentWidthPos);
placerCurrentLengthPos = Mathf.Round (placerCurrentLengthPos);
Vector3 tilePos = Handle.transform.position;
placedTile = Object.Instantiate(Placer, tilePos, Placer.transform.rotation) as GameObject;
float placedTileWidthPos = placedTile.transform.position.x;
float placedTileLengthPos = placedTile.transform.position.z;
float placedTileHeight = placedTile.transform.position.y;
placedTileWidthPos = Mathf.Round(placedTileWidthPos);
placedTileLengthPos = Mathf.Round(placedTileLengthPos);
placedTile.transform.name = "Tile (" + placedTileWidthPos + "),(" + placedTileLengthPos + ")";
/////////// code below assigns corners and edges
if(placedTileWidthPos == 0)
{
placedTile.tag = "edge";
}
if(placedTileLengthPos == 0)
{
placedTile.tag = "edge";
}
if(placedTileWidthPos == roomWidth - 1)
{
placedTile.tag = "edge";
}
if(placedTileLengthPos == roomLength - 1)
{
placedTile.tag = "edge";
}
if(placedTile.transform.position == new Vector3(0, placedTileHeight, 0))
{
placedTile.tag = "corner";
}
if(placedTile.transform.position == new Vector3(0, placedTileHeight, roomLength-1))
{
placedTile.tag = "corner";
}
if(placedTile.transform.position == new Vector3(roomWidth-1, placedTileHeight, 0))
{
placedTile.tag = "corner";
}
if(placedTile.transform.position == new Vector3(roomWidth-1, placedTileHeight, roomLength-1))
{
placedTile.tag = "corner";
}
///////// code above assigns corners and edges
Handle.transform.Translate (1, 0, 0, Space.World);
}
Handle.transform.Translate(-roomWidth, -1, 0);
}
/////////// code below places walls
Vector3 wallStart = new Vector3(-1, 0, -1);
Handle.transform.position = wallStart;
for(int i = -2; i < roomLength; i++)
{
Vector3 wallPos = Handle.transform.position;
placedWall = Object.Instantiate(Wall, wallPos, Wall.transform.rotation) as GameObject;
float placedWallWidthPos = placedWall.transform.position.x;
float placedWallLengthPos = placedWall.transform.position.z;
float placedWallHeightPos = placedWall.transform.position.y;
placedWallWidthPos = Mathf.Round(placedWallWidthPos);
placedWallLengthPos = Mathf.Round(placedWallLengthPos);
placedWall.transform.name = "Wall (" + placedWallWidthPos + "),(" + placedWallLengthPos + ")";
Handle.transform.Translate (0, 0, 1, Space.World);
}
Handle.transform.Translate (1, 0, -1, Space.World);
for(int i = 0; i < roomWidth; i++)
{
Vector3 wallPos = Handle.transform.position;
placedWall = Object.Instantiate(Wall, wallPos, Wall.transform.rotation) as GameObject;
float placedWallWidthPos = placedWall.transform.position.x;
float placedWallLengthPos = placedWall.transform.position.z;
float placedWallHeightPos = placedWall.transform.position.y;
placedWallWidthPos = Mathf.Round(placedWallWidthPos);
placedWallLengthPos = Mathf.Round(placedWallLengthPos);
placedWall.transform.name = "Wall (" + placedWallWidthPos + "),(" + placedWallLengthPos + ")";
Handle.transform.Translate (1, 0, 0, Space.World);
}
for(int i = -1; i < roomLength; i++)
{
Vector3 wallPos = Handle.transform.position;
placedWall = Object.Instantiate(Wall, wallPos, Wall.transform.rotation) as GameObject;
float placedWallWidthPos = placedWall.transform.position.x;
float placedWallLengthPos = placedWall.transform.position.z;
float placedWallHeightPos = placedWall.transform.position.y;
placedWallWidthPos = Mathf.Round(placedWallWidthPos);
placedWallLengthPos = Mathf.Round(placedWallLengthPos);
placedWall.transform.name = "Wall (" + placedWallWidthPos + "),(" + placedWallLengthPos + ")";
Handle.transform.Translate (0, 0, -1, Space.World);
}
for(int i = -1; i < roomWidth; i++)
{
Vector3 wallPos = Handle.transform.position;
placedWall = Object.Instantiate(Wall, wallPos, Wall.transform.rotation) as GameObject;
float placedWallWidthPos = placedWall.transform.position.x;
float placedWallLengthPos = placedWall.transform.position.z;
float placedWallHeightPos = placedWall.transform.position.y;
placedWallWidthPos = Mathf.Round(placedWallWidthPos);
placedWallLengthPos = Mathf.Round(placedWallLengthPos);
placedWall.transform.name = "Wall (" + placedWallWidthPos + "),(" + placedWallLengthPos + ")";
Handle.transform.Translate (-1, 0, 0, Space.World);
}
/////code above places walls
}
// Update is called once per frame
void Update ()
{
}
}
I’m not sure how to upload a demo of it here yet - I’ll have to look around for how to do that. But it does indeed work the way it’s supposed to using my custom wall and tile prefabs in the “wall” public gameobject and “placer” public gameobject.
(I should probably revise the “placer” gameobject so it’s called “tile” so that I don’t confuse the instantiator and tile gameobjects)