TerrainGenerator

The brain.

Home

Classes

• TileMap
• ShapeGen
• TerrainGenerator

Extra

• Tutorials
• Demo

Description

Here's what you came here for - the terrain generator itself. Our focus on simplicity means that this generator uses no advanced noise algorithms or calculus, just a random number generator and a lot of circles. You may notice that there are no setters for X positions outside of the constructor. This is an intentional design choice, as we didn't want to overcomplicate our function signatures. If you want to make different terrain features on the X axis, make a second terrain generator and set its x range accordingly.

The first set of functions to call are those that create a foundation for your world. generateFlat(), generateBumps(), and generateHills() create features at the Y coordinates you specify. These features will appear as simple green renderables - this is another intentional design choice. Because a variety of textures may be applied to the ground (bedrock, stone, etc), we felt that texturing should be applied after the basic shapes are defined. This is completed with setTexture(), which can apply a texture to a range of Y coordinates.

But what if I don't want rectangular boundaries between my stone and dirt layers? This is where addTopTiles() comes into play. You can imagine this function as raining a layer of blocks down on your terrain. Placed into a for loop this will layer as much dirt on top of stone as you'd like. You can use the yMin and yMax to only a texture on a certain Y coordinate. This is how we apply snowy grass to the top of mountains and regular grass to the valleys in Scene1.

Finally, you can apply ground scatter with generateTrees(). The height range and texture allow for a wide range of trees to be created. The woodTexture and leafTexture can also be set to null, allowing for cactuses, cobblestone walls, floating orbs, or anything else you can imagine. Flowers and grass textures can be applied by setting the tree height to 1 and setting the leaf texture to null.

While we attempted to make our tree generation as versatile as possible, we recognize that it can't create everything. That's where you come in! The addTree and generateTrees functions can easily be copied and modified to generate any shape desired. Try making a pyramid or house and scattering them all over the map!

Finally, the getSurfaceCollision function can be used to efficiently retrieve an array of collision boundaries from the surface of your world. This is much more efficient than checking every block, but if your game implements block addition or removal, this method will need to be modified.

Constructor

new TerrainGenerator (tileMap, startX, endX)

Constructor that creates a TerrainGenerator object

tileMap | TileMap | The TileMap that TerrainGenerator will operate on
startX | int | The tile x to start at
endX | int | The tile x to end at

Methods

generateBumps (yLevel)

Generates bumpy terrain at given y level

yLevel | int | The tile y to generate bumps at


generateFlat (startY, endY)

Generates flat terrain between start and end y tile

startY | int | The y tile to start at
endY | int | The y tile to end at


generateHills (yLevel, frequency, scale, steepness)

Generates hills at given yLevel

yLevel | int | The tile y to generate hills at
frequency | float | The chance of generating a hill
scale | float | Adjusts the overall size of the hills
steepness | float | Adjusts how steep the hills will be


setTexture (startY, endY, texture, UVArray)

Sets the texture of all tile between given y tiles

startY | int | The y tile to start at
endY | int | The y tile to end at
texture | string | The texture file to use
UVArray | [float] | The UV coordinates the specify the texture


addTopTiles (texture, UVArray, flip)

Covers the surface of the TileMap with tiles

texture | string | The texture file to use
UVArray | [float] | The UV coordinates the specify the texture
flip | boolean | If true the tiles will go on the ceiling


addTree (x, y, height, woodTexture, woodUV, leafTexture, leafUV)

Creates a tree at given x and y tile

x | int | The tile x to put the tree at
y | int | The tile y to put the tree at
height | int | The height of the tree
woodTexture | string | The texture file to use for the wood
woodUV | [float] | The UV coordinates the specify the texture for the wood
leafTexture | string | The texture file to use for the leaf
leafUV | [float] | The UV coordinates the specify the texture for the leaf


generateTrees (minHeight, maxHeight, frequency, woodTexture, woodUV, leafTexture, leafUV)

Generates trees across the whole TerrainGenerator

minHeight | int | The minimum height of a tree
maxHeight | int | The maximum height of a tree
frequency | float | The chance of generating a tree
woodTexture | string | The texture file to use for the wood
woodUV | [float] | The UV coordinates the specify the texture for the wood
leafTexture | string | The texture file to use for the leaf
leafUV | [float] | The UV coordinates the specify the texture for the leaf


_generateBumps (startX, endX, yLevel)

Private helper function that generates bumpy terrain

startX | int | The x tile to start at
endX | int | The x tile to stop at
yLevel | int | The tile y to generate bumps at