Hey guys! I’ve decided to change the coding method of my roguelike because of a nudge from a fellow user to use 2d arrays. Using many(and i mean many) iterations and for loops, I figured out how to navigate through the array fairly easily in order to change values.
I decided to make rooms first, then worry about the paths.
I’ve managed to get rooms of various sizes to populate the grid.
It usually generates with some overlap
as you can see, the rooms are yet to be connected by paths, which is what I’m trying to figure out.
this is what I have so far:
using UnityEngine;
using System.Collections;
public class roomspreadS : MonoBehaviour
{
public GameObject tile;
public GameObject path;
public GameObject wall;
const int worldSize = 50;
int maxTiles = 0;
static int[,] worldMap = new int[worldSize, worldSize];
static bool[,] worldCheck = new bool [worldSize, worldSize];
void Start ()
{
clear ();
room ();
//road ();
make ();
}
//sets the positions for the initial room size
void clear ()
{
for (int y=0; y<worldSize; y++) {
for (int x=0; x<worldSize; x++) {
var shuf = Random.Range (1, 100);
if (shuf == 2 maxTiles < 20 y > 3 y < worldSize - 3 x > 3 x < worldSize - 3) {
worldMap [x, y] = 1;
maxTiles++;
} else {
worldMap [x, y] = 0;
}
}
}
maxTiles = 0;
}
void room ()
{
for (int a=0; a<3; a++) {
for (int y=0; y<worldSize; y++) {
for (int x=0; x<worldSize; x++) {
if (worldCheck [x, y]) {
continue;
}
if (x > 5 x < worldSize - 5 y > 5 y < worldSize - 5) {
if (worldMap [x, y] == 1) {
int shuffle = Random.Range (1, 4);
for (int b=-shuffle; b<=shuffle; b++) {
for (int c=-shuffle; c<=shuffle; c++) {
worldMap [x + b, y + c] = 1;
worldCheck [x + b, y + c] = true;
}
}
}
}
}
}
}
for (int y=0; y<worldSize; y++) {
for (int x=0; x<worldSize; x++) {
if (x > 0 x < worldSize - 1 y > 0 y < worldSize - 1) {
if (worldMap [x, y] == 1 worldMap [x - 1, y] == 0 worldMap [x + 1, y] == 0 worldMap [x, y - 1] == 0 worldMap [x, y + 1] == 0) {
worldMap [x, y] = 0;
}
}
}
}
for (int y=0; y<worldSize; y++) {
for (int x=0; x<worldSize; x++) {
worldCheck [x, y] = false;
}
}
}
void make ()
{
//place game objects
for (int y=0; y<worldSize; y++) {
for (int x=0; x<worldSize; x++) {
if (worldMap [x, y] == 1) {
Instantiate (tile, new Vector3 (x, 0, y), Quaternion.identity);
}
if (worldMap [x, y] == 2) {
Instantiate (path, new Vector3 (x, 0, y), Quaternion.identity);
}
}
}
}
void Update ()
{
}
}
For paths, I’m having a hard time (1) figuring out where to start the paths and (2) how the paths finally stop at certain rooms.
I’ve considered using iterations to find out the distance between rooms and filling in the paths, but I haven’t had much luck.
maybe I’m looking at this the wrong way. Do you guys have any suggestions?