Dungeon Crawler Using Procedural Generation in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] hey welcome back to broken Knights games my name is Sam and for those of you don't know I make game dev related videos and I post them every other Friday on this channel and I'm also working on a game called sumo showdown and that's a local multiplayer fighting game already control sumo wrestlers but the thing about that is I'm kind of at a point where I need to do a lot of play testing and in order to do that I need to get people to come over and play my game which is kind of hard to do with the given situation that's going on so I decided to take a quick little break and work on a side project of mine that I've been itching to do for a really long time and that's creating a dungeon crawler specifically I want to make a dungeon crawler where the dungeons are procedurally generated so that's something that you hear about a lot a lot of games they have a lot of procedural generated content where the levels are built you know by an algorithm so I thought that was really kind of fascinating and kind of cool and it would be fun to play with so in this video I'm going to show you my random dungeon generator and how I went about building it specifically around the algorithm side like kind of the thought process of how to go about building something like this I'm not gonna get too into the weeds with the code because that's really specific to what I'm doing here but if we kind of take a step back and look at the algorithm as a whole I think if we get interesting or helpful for those of you kind of wondering how would you break a problem like this down so how this algorithm works is kind of follow the component driven design so it's a top-level component we have a dungeon and then a dungeon has rooms and then the rooms have openings and then those openings are in charge of creating other rooms and all these pieces kind of communicate to each other at different through different layers and steps so starting at the top level of the dungeon so step one we spawn a room and then step two we look at that room and ask does it have any available openings or unconnected openings if so then we tell that room to spawn it's adjacent chambers and at this point the dungeon just kind of waits for the rooms openings to be created so then after the all the openings have spawned a room then that the dungeon is going to add those rooms to a variable that is tracking and then step six every Pete's step two three six so it'll start find the next available room that has openings that are unconnected and go through that until all the rooms that are returned are closed off so then what would this look like at the room level so what would the room do the room step one would get all the room openings so find all the spots where you can spawn a room and then iterate each of those openings and spawn an adjacent chamber and then it kind of similar to the dungeon level the rooms gonna wait until all the adjacent chambers are spawned and are in a valid state and I'll explain what that means in a little bit and then lastly it'll notify the dungeon hey I have these new rooms and they're ready to go so then what would this look like from the room opening so step on we're gonna get available pieces from the dungeon to spawn so I have a list of prefabs in the dungeon for all the different rooms and hallways that if that a given opening can spawn and then step two we're gonna spawn a connected piece so it's gonna spawn a new chamber or a hallway or whatever and then step three the opening is gonna wait for what I'm calling a validation period to complete so we're gonna spawn the room and it's gonna wait until the room is confirmed in a valid state and I'll talk about that in a second here and then lastly it's gonna mark the opening is connected I have a chamber connected to this opening and it's going to notify the room that hey we have an adjacent chamber ready so what does how does the validation work how do we make sure that we're getting a valid dungeon at the so when every room is created it starts off in a valid state but there's a couple things that can happen that'll mark it invalid so I'm doing some collision detection and a room will become invalid if it ever intersects another room and if a room becomes invalid the room will destroy itself and notify the connection point that hey this prefab that you tried to spawn doesn't work try a different one and then so the the connection point will try its next prefab so what will happen if all the prefabs that the connection point has available to use are attempted and nothing fits in the spot available so that at that point the connection point will just put a wall there instead of the room all right so I'm here in unity and you can see here this is an example of a dungeon that was generated so you can see it's got some rooms and hallways chambers so I'm just gonna hide that so basically every dungeon will start with a entry chamber and then it'll kind of sprawl out from there so I have if you prefabs created under dungeon so I have it kind of broken out between chambers doors halls and rooms and then connection points so I have different sized chambers so if I open up one of these for example this medium chamber everything's kind of structured the same so but if I expand my prefab here it's basically I have the walls the floors the connection points and validators then I have my lights groups as well so the walls and the floor I have separated out with the geometry and then I have all the colliders also similarly with the floors and then under connection points I have my connection point prefab so I have these connection point objects that have their own chamber connection points script and basically I assign what valid adjacent chambers can spawn so I have an enum created for all the different kinds so I have hallway I have a corner room a small chamber medium chamber large chamber etc a room like this you can't spawn a room without having a hallway first so that's basically why I'm valid connection types and then the validator is that's kind of interesting so I have a collection of objects that they're just called dungeon validators and they have a script on there that basically just listens for collisions within all these colliders so if if a new dungeon spawns it'll and it over intersects with this then we'll know that it's an invalid piece so if i click play and go to the scene view i'll just drag this out of the way here so if I click this generate button you can see how the dungeon kind of expands I'll try that again and I set it up so I can keep on generating new dungeons and then another cool thing I have on my dungeon is I have I call it generation bias so I can say how many target medium and small chambers so if I change I change this to like three or two and then regenerate [Music] I'll say that will get a larger dungeon and also you can see it as it's generating if the rooms don't fit it kind of corrects itself as it's going and that's the the room validators [Music] so sounds kind of like a labyrinth it's interesting and another cool thing about this is if it spits out a dungeon that I like I can just take that generated dungeon and then drag it and create a prefab out of that so I don't necessarily always have to create these at runtime for the player I can always create some some dungeons and then modify them later if I want to make some handmade levels so there we have that that's my simple dungeon generator I'll probably add to add more to it like I need to add doors and loot and enemies and things like that I might build it up more and maybe make it a game and maybe release it I don't know it's pretty early so far just kind of playing around the idea and also just kind of learning some techniques and methods so if you like this video subscribe to my channel give me a thumbs up and also if there's a game dev topic specifically around unity or c-sharp feel free to leave a comment to let me know what you'd like to see and as always thanks for watching catch you later [Music]
Info
Channel: Broken Knights Games
Views: 50,563
Rating: undefined out of 5
Keywords:
Id: rfKcpp8UDQ0
Channel Id: undefined
Length: 10min 13sec (613 seconds)
Published: Fri May 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.