This post has been de-listed (Author was flagged for spam)
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
1
i need help on making a dungeon generator utilizing unity's tilemap feature
Post Body
im working on a dungeon crawler but i can't figure out how to make rooms connect i can't find anything helpful on google
current code:
public class Dungeon : MonoBehaviour
{
public Tile GroundTile;
public Tile WallTile;
public Tilemap tilemap;
List<Room> rooms = new List<Room>();
void Start ()
{
for(int i = 0; i < 10; i )
{
rooms.Add(new Room(10, 10, new Vector2Int(i * 20, 0)));
}
for(int i = 0; i < 10;i )
{
if(i > 0 && i < 10)
{
rooms[i].position = rooms[i - 1].Connect(rooms[i]);
}
}
AddRoomsToTilemap();
}
public void AddRoomsToTilemap()
{
foreach(Room room in rooms)
{
for (int x = 0; x < room.width; x )
{
for (int y = 0; y < room.height; y )
{
if (room.tiles[x, y] == 0)
{
tilemap.SetTile(new Vector3Int(x room.position.x, y room.position.y, Random.Range(0, 10)), GroundTile);
}
else
{
tilemap.SetTile(new Vector3Int(x room.position.x, y room.position.y, 0), WallTile);
}
}
}
}
}
}
public class Room
{
public Vector2Int position;
public int[,] tiles;
public int width = 16, height = 16;
public Room() { }
int exitCount = 0;
public List<Vector2Int> exits = new List<Vector2Int>();
public Room(int width,int height, Vector2Int position)
{
this.position = position;
this.width = width;
this.height = height;
tiles = new int[width, height];
for (int x = 0; x < width; x )
{
for (int y = 0; y < height; y )
{
if (x == 0 || x == width - 1 || y == 0 || y == height - 1)
{
tiles[x, y] = 1;
if(y == Random.Range(1, height - 1))
{
tiles[x, y] = 0;
exitCount ;
exits.Add(new Vector2Int(x, y));
}
if(exitCount == 0 && y >= 1 && y <= height - 1)
{
tiles[x, y] = 0;
exitCount ;
exits.Add(new Vector2Int(x, y));
}
}
else
{
tiles[x, y] = 0;
}
}
}
}
public Vector2Int Connect(Room room)
{
Vector2Int pos = room.exits[Random.Range(0, room.exits.Count - 1)];
Vector2Int postomatch = exits[Random.Range(0, exits.Count - 1)];
if (Vector2Int.Distance(postomatch, pos) > 0)
{
return postomatch * (pos * room.position);
}
else
{
return pos;
}
}
}
Author
Account Strength
0%
Account Age
7 years
Verified Email
Yes
Verified Flair
No
Total Karma
34,548
Link Karma
9,973
Comment Karma
23,263
Profile updated: 9 months ago
Posts updated: 11 months ago
Subreddit
Post Details
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 5 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/Unity2D/com...