Hey guys this is fade X and today we’re gonna be starting a series and this series is gonna be about creating a kind of a 2d Minecraft slash terraria clone so yeah let’s get right into it in the first episode we’re gonna be looking at changi generation so first off I’m gonna Set the default clear clear color to some kind of blue so it would resemble the sky something like this and now we’re gonna create a world node world environment I’m gonna call it world and now we’re gonna create a new scene well let’s first save this into scenes and World sen now the new scene is gonna be note 2d and it’s going to be the cheung scene okay let’s save it into i’m gonna put it into a new folder called instances and just gonna put it here and then we need one more scene cold well That’s this is gonna be a static body 2d and it’s gonna be the block scene that’s gonna hold all our blocks I’m gonna save it in two instances block okay so first off let’s create the block scene I’m gonna set the grid I’m gonna show grid and configure snap so because ours all Our sprites are 32 by 32 and the grid is going to be 32 by 32 I need to be on that grid I’m gonna set this to like 16 by 16 so I would have more I could edit it more perfectly or precisely so now let’s let’s add a new node and It’s gonna be the sprite node and I created some surprise here so I’m gonna use them let’s put stone in there as a placeholder okay and the third thing that the block is gonna have is the gold lesion shape obviously so let’s set it to rectangle let’s then drag it out so Stretch it out so it would fit the block and that’s denotes for the block I’m gonna then attach a script to it and create a basic behavior so let’s set the past two scripts and block the Gd okay and here so the block is gonna have a Few variables the first one is the block type I guess which is going to be set from the chunk parent we’re gonna do that later and then we need to get all this price from the sprite float folder so I’m gonna save our blocks block sprites and I’m gonna make an array and The array is gonna contain all the preload its price I’m gonna say preload and put the ID of grass first for example okay now the order of these sprites is really important so I’m gonna say grass first then I’m gonna say dirt then I’m gonna put stone in there and finally the Bedrock right okay and then what I’m gonna do in the ready function is set the sprite to the according to the block type so I’m gonna say get node the sprite dot texture equals to what sprite block type and what this is gonna do is set the texture of the sprite through The block according to the block type so if the block type is 0 it’s gonna set the texture to grass if the block type is 1 it’s gonna set it to dirt okay now this is this is the basic block that behavior done and that’s we’re gonna Leave the block node for today now let’s get right into the do into the generation so I even gonna attach a script to the chunk o node let’s put it into scripts and call it chunk the Gd okay now first off we need a few variety rables so even before I started this Tutorial i added a script it’s called soft noise and this script contains a few soft noise soft noise algorithm like from a few dimensions so i’m gonna use one from there so lets preloaded bar i’m gonna save our noise equals preload and then the soft noise script then we need Then we need the block seem like the block prefab you just call it so i’m gonna pull out the preload block scene let’s see instances block so I could instantiate it then what I’m gonna do here is create an enum called block types which is gonna hold all the block Types now remember how I told you that the the order of this is really important so what I’ll do here is say grass dirt stone bedrock and well enum does is it assigns a number to each of these words each of these keywords so grass is gonna be zero dirt is gonna be One stone is gonna be replaced by two and bedrock is gonna be replaced by three so that is the same order as these so it’s gonna be easier for us so next up I’m gonna create a few more variables first one is gonna be the world depth Which is gonna hold the amount of blocks that resembles the world depth basically so I’m gonna say it’s gonna be 64 blocks deep another one is surface height which is gonna say how high the surface can be I’m gonna say 64 two and then I’m gonna Set the chunk weight which is gonna hold the amount of blocks that the chunk the basically the size of the chunk okay so now we can get into the generating so first off we’re gonna create an instance of the noise noise script or basically a soft noise Instance so I’m gonna say versa noise equals to noise soft noise door new which is going to create a new instance of the soft noise now here we can set a few parameters parameters one of which is the world seed so we can just put Some number in here for now and then we can randomize it so now I’m gonna create a for loop which is gonna iterate over the x coordinate so I’m gonna say for X in range from zero to chunk with okay and then I’m gonna calculate a Y for Each X so I’m gonna say what VAR y now I want number to behold so I’m gonna floor it because the Y is gonna be like uh offset of a block so zero is gonna be on the zero coordinate one is gonna be on the like might want a great coordinate You will see so and what I’m gonna do narrate the noise here so I’m gonna say some noise dot open simplex 2d open simplex is a noise algorithm similar to the Perlin noise but you know open source and now for the parameters we have in open simplex 2d as You can see we have two dimensions so the we have we are only going to use one dimension because we’re generating the noise only according to the X X variable and what I’m gonna put here is get global transform now what this does is it it gets the global position in the World and this is gonna be important because you will see I will explain it when we come to it I’m gonna say get global transform the origin dot X this is gonna get the x-coordinate in the world I’m gonna divide it by two now by 32 now I’m Dividing it because we’re working in 32 grid size but we but in the loops we’re working in like block units basically so one block is 32 which has you know F width of 32 so I need to divide this by 32 so I would get the transform for the Block not for the like grid coordinate now I’m gonna say plus X and this is what makes the difference in the chunk so I’m basically saying get the global origin and from there for every X generate Y for every X in this for loop okay now I’m gonna even multiply this Number by some like small really small number so basically divided and this is really important because lots of noise does is it makes you know like a fluent round noise I would say so it doesn’t give like really random numbers but so we say give me the choice for X and Gives it gives you like one even for x0 and it gives you one you made the noise for x1 and it gives you a number that’s that is quite random from the previous one so I need to get a noise for a closer number so I will say you for a Noise for zero and get gives you one give me a noise for 0.1 and give it gives you a close number to one okay so that’s why I’m multiplying here by a small number and now I’m the Y the Y parameter is gonna be zero because I’m Not using that now this o this noise generates a number between zero and one but I want a number between like zero and the surface height that I have here so I’m gonna say x sir this height okay now in my experience the surface height is too much so I’m gonna even more Divide it by us some fraction so it would give me like a normal height okay so now we have generated y y for the X so now we can try creating the block so I’m gonna create a new function for that fun fun new walk which is gonna have two Parameters first of which is the block position and the second one is the block type and I’m gonna say just type here now what we do here is say part new block and we are gonna create an instance of the block preload here prefab from of the scene now we’re gonna Say new block the friend’s light so we’re gonna set the position of the new block to the position parameter now we’re gonna say new block the tie and we’re I mean walk hi I named it block type right the a block time and we are Gonna set it to the type and then simply add it to the chunk new block okay so now we can create the new block here so you’re gonna say new block and the position is gonna be a vector two and let’s see so if we would put just an X NY here we’re still talking in the you know in the block units or how to say it so we need to multiply it by 32 so we would work in the actual light pixel units okay and now we’re gonna set the block type which is gonna be the grass Block okay so you are saying that the surface block is gonna be grass now we can test this out so I’m gonna I’m gonna instantiate the chunk instance in the world and let’s run this oh we didn’t choose the default team well the Sen now you can see that there is some Terran generation going on what I’m now quickly gonna do is create camera 2d so I could move around the world and quickly gonna create a script for it let’s call it let’s put it in the script folder and call it camera what’s safe great and let’s just quickly say said Process tree so this the process function is gonna be called every every frame fun process and let’s say if input is action pressed UI up so if the up button is pressed I’m gonna say translate back to zero by I don’t know I’m gonna say I’m gonna create a Variable called VAR count speed and set it to like 10 so I’m gonna say move by the cam speed okay can i press up I’m gonna move on the Y up by the chemistry now I’m gonna repeat this for every every key for every Derek direction I’m Gonna say down left and right then I need to set the camera to cure and and no drag Martin okay now we can see that there’s a terrain here okay I’m gonna say that this is gonna be a fixed of left okay let’s back let’s get back to The Jiang Chang generation so what we need to do now is to create the whole like Chang and to fill it with you know dirt stone and then bedrock so let’s say let’s create a new for loop and it’s gonna hold y Y variable and it’s gonna Be forward while I in range now the range is gonna be from from the cure and y plus one because I don’t want to create a block on the here and why I want to create a blog starting from a below the Kieran brass book and from why +12 world death okay So now I can create a new blog in there and it’s gonna be on X x 32 on the same X and on the YY x 32 now and I’m gonna say it’s gonna be dirt okay now it should create what did I do wrong here Okay I didn’t put it a parent season here okay now it should work now we get a dirt block up to the lowest point in the in the world but we don’t want it to be only dirt so now what we are gonna do is create a new noise or a new generate A new Y for where the stone for the current X start so I’m gonna say we’re a stone y equals to again for so noise not open simplex 2d and the X is the current X I mean the X is obviously again the same X and I’m gonna multiply it by the Same number but I don’t know now if I would put Y to 0 it would give me the same noise value as the surface Y which we don’t want because it will do really dumb so I’m gonna set the Y value to something like 32 so it would give me a Completely different Y value now I’m now gonna multiply the noise by some number which is gonna determine the height of the like the variation in the stone height so I’m gonna say for for example and close it off okay so now we have the stone Y and now we want to create we Want to create dirt until it reaches the stone why if Y is bigger then now bigger than what I want to say I want to make it relative to the world that’s so I’m gonna say world depth times point 25 now this says that it’s gonna create it’s gonna start Creating the stone at the corner of the world world the depth so there’s that and then I’m gonna say plus the stone Y which is gonna you know take in into account the stone Y variation so now if that’s the case I’m gonna create a new Block similar to here and it’s gonna be stone and if it’s not below the world or below this value I’m gonna create dirt okay let’s see what we have here you can see that we now generate stone okay now what we are gonna do next is generate the bedrock bedrock bedrock Layer what I could simply say here is create the the bedrock @ LX e vector 2x x 32 at the same X but I’m gonna say the world I’m going to say the world depth here x 32 and what this would do is bedrock of course and what this would do Is it would claim create a like a flat bed bedrock layer in the bottom but we want it to be a little varied so to take that into account I’m gonna create a new Y called bet walk fly which is gonna again be of soft noise open simplex and here again I’m Gonna say I’m gonna say the get global you know so it would be real it relative to the world size the world posit the Chang position in the world and again I’m gonna multiply it by some number let’s do something different now like point five and now The why a parameter is again gonna be different from these two because I wanted to be different and yeah I know I’m not gonna multiply it by any variable now here because I don’t want it to be that like varied the height of the bedrug that vary and this is it So now instead of going to the world depth I’m gonna do go to the world depth plus bedrock Y which is gonna go to the world depth plus the variation in the bedrock Y and now I need to create the bedrock with that variation and I’m Gonna put it in the parent Jesus because it so it would multiply by 32 the whole sum okay now if you look at this we can see that we get a very varied very bedrock player now we can play with these variables here I can for example say the world I Mean the surface height to be 128 so we would get like mountains or something you can see that now it’s much higher and so yeah you can like generate chunk like this like mountains grasslands and stuff so this is basically what I wanted to do for this tutorial what we can do Now is create a few more chunks so I’m gonna create a world scrip call it world’s clear script and put it into screen shows script folder let’s get the chunk seen load chunk okay and let’s also generate a seat in the world script because I want the seat to be same for Each chunk so the noise would be flute like the noise would connect the from chunk to chunk so I’m gonna say one seed oh this is already taken so I’m going to say like see now in the red here I’m gonna say randomize which is gonna make Sure which is gonna make sure for us that the random number is randomized and I’m gonna say world C equals to random R and E Rand int and I’m gonna say x I don’t know a lot so the world T would be really random and now let’s create the Chunks so I’m gonna say mark new chunk because China dog instance and add child the new chunk now I’m gonna make a few more chunks the left and to the right I’m gonna say trying don’t translate and I’m gonna say 3 2 – so the – here I need to if I want To create a chunk on the Left I need to do – the chunk width times 32 so chunk with a 32 and 32 squared is 124 if I’m not mistaken and on the Y zero now let’s also create a new chunk to the right of the center channel now if we OH Also I forgot to get the seat from the world I’m still using the hard-coded seat here so I’m gonna say get node the parent dog world seat okay so now it gets the world team so I seem to have found a weird but now it works what I Now then is I deleted the chunk in here so it wouldn’t intersect with the script added chunk now if we press f5 we can see that we have a few chunks there are smoothly connected to each other and nicely generated so this is gonna help us when we made the world Infinite but this is what I wanted to talk about today and yeah see you next time I guess You Video Information
This video, titled ‘2D Minecraft in Godot Engine #1 – Chunk Generation’, was uploaded by Fadex 2 on 2017-11-10 14:54:56. It has garnered 22403 views and 453 likes. The duration of the video is 00:24:24 or 1464 seconds.
Starting a 2D minecraft series. Write any suggestions in the comments
softnoise script: https://github.com/PerduGames/SoftNoise-GDScript-