Hello everyone,
so, I'm not a really good programmer and I do programming only because I need theese skills for game creation. Today I wanted to post some of my new code I made to randomly generate a sort of a long hallway which is meant to sort of bifurcate ( it can't right now ) and add some rooms too. I'm not gonna lie, it was pretty challenging at first and I needed to come through quite a lot of tutorials, but there are still some things missing. So I wanted to ask some questions about the improvement of the current state, so that at the end I wont do everything from the beginnig againg. That would be very horrible and deteriorating ?. I really hope that it's not too horrible and can be understood at least a little bit. It is attached to an empty object. (Using Unity btw)
using System.Collections.Generic;
using UnityEngine;
public class RoomManager : MonoBehaviour
{
public GameObject[] rooms; // so, here all the Lists with rooms (only hallways right now) divided into several sections
public GameObject[] roomsLeft;
public GameObject[] roomsRight;
public GameObject[] ladders;
List<object> Left = new List<object>();
List<object> Right = new List<object>();
List<object> Ladders = new List<object>();
GameObject hub; // the first room to begin with
GameObject thisRoom; // the room which will be instantiated
Transform hubSpawnPoint;
Transform connectionPoint; // spawn point for the next rooms, which is a Child of every thisRoom obj
public string[] RoomsNamesDatabase; // database for all the names
int i = 0;
int rotationsCount = 0;
int tillTheEnd = 4;
private bool theend = false; // countdown till the end of the "dungeon"
private void Awake() // adding one lists category to another for the sake of using .Contains method (if there is another way to do this, please show me), and setting the spawn and connection point for the first room
{
Left.AddRange(roomsLeft);
Right.AddRange(roomsRight);
Ladders.AddRange(ladders);
hub = rooms[0];
hubSpawnPoint = hub.transform.GetChild(0);
connectionPoint = hub.transform.GetChild(1);
}
private void Start() // instantiating the first room and starting the RoomGeneration
{
Instantiate(hub, hubSpawnPoint.position, Quaternion.Euler(0, 0, 0));
while (theend == false) {
RoomGeneration();
}
}
private void RoomGeneration()
{
int r = Random.Range(1, rooms.Length); // random number for the rooms list
int rl = Random.Range(0, ladders.Length); // random number for the ladders list
thisRoom = rooms[r]; // randomly chosen room out of the rooms list
thisRoom.name = RoomsNamesDatabase[r]; // name reset (I hope you'll understand that abit later)
thisRoom.name = $"{i}" + thisRoom.name.Substring(1); // changing the index (and that too)
// so, what I practically doing down below : I've set a countdown for rotations to avoid clipping and collisions of hallways
// every time when a hallway takes two turns left it spawns a ladder and updates the tillTheEnd int
if (rotationsCount == 2)
{
thisRoom = ladders[rl];
thisRoom.name = RoomsNamesDatabase[rl + rooms.Length]; // name reset
thisRoom.name = $"{i}" + thisRoom.name.Substring(1);
Instantiate(thisRoom, connectionPoint.position, Quaternion.Euler(0, -90, 0));
rotationsCount = 0;
tillTheEnd -= 1;
}
else if (rotationsCount == 1)
{ Instantiate(thisRoom, connectionPoint.position, Quaternion.Euler(0, 0, 0)); }
else if (rotationsCount == 0)
{ Instantiate(thisRoom, connectionPoint.position, Quaternion.Euler(0, 90, 0)); }
if (rotationsCount == -1)
{ Instantiate(thisRoom, connectionPoint.position, Quaternion.Euler(0, 180, 0)); }
else if (rotationsCount == -2)
{
thisRoom = ladders[rl];
thisRoom.name = RoomsNamesDatabase[rl + rooms.Length]; // name reset
thisRoom.name = $"{i}" + thisRoom.name.Substring(1);
Instantiate(thisRoom, connectionPoint.position, Quaternion.Euler(0, 270, 0));
rotationsCount = 0;
tillTheEnd -= 1;
}
if (Left.Contains(thisRoom))
{
rotationsCount += 1;
}
else if (Right.Contains(thisRoom))
{
rotationsCount -= 1;
}
//so, the thing it all works like that is because of theese lines of codes here
thisRoom = GameObject.Find(thisRoom.name + "(Clone)"); // in order to find the room spawned right now as a GameObject is too find in the game field, the previous thisRoom obj was the prefab, so the spawned one will be with "(Clone)" printed on the end
i += 1; // this index is created for Unity to recognite and differentiate each new room from one another spawned in the past
connectionPoint = thisRoom.transform.GetChild(1); // setting a new connecttion point
if (tillTheEnd == 0)
{
theend = true;
}
}
}
So, the next decisions I have to make are:
1) how am I going to spawn actual rooms which will create new hallways?
It turned out to be not so easy, because you can't attach any script to a child of a prefab and that would help a lot. I think I could set a planned number of rooms that I want to generate and create another countdown, but there sill wouldn't be any bifurcations whatsoever. It is getting quite comlex here for me and I can't really descide what I need to do here. I thought, that maybe I could drag all the possible rooms before the start and than attach a script for every room category, that will spawn it if it is randomly chosen and in the end every other room, that wasn't chosen would delete itself automatically, but I don't know yet, wether it's really practical.
2) how to make an actual collision detecter?
Although it is not that comlicated, I think I'll may have some problems with doing it right. I will obviously need that later, because I want some hallways to go up and down and maybe rotate differently.
I would be very glad to see your solutions to my problems here!