Plant Jumper – Procedural Level Generation

Building a game it is not just create some mechanics and a beauty graphic, it is overall and experience and for that the player expects to be amazed with the universe you created and therefore there must have a good amount of content.

In a lot of games, levels, and players and everything else are handcrafted but this approach it is not scalable if you want for example to create a endless level game, in this cases we use to create procedural levels.

Understanding the level

The first thing you need to know when creating procedural levels it is the pieces you will tie together, how will they behave and how will the player interact with it. For example, if you are creating platforms, as i am, you need to be sure the player can reach the platforms.

The creation of a level i considered two ways.

  • Create a Maker and a Validator: This two steps approach it is a more robust but in the other hand harder to implement. In the Maker phase, you have to generate a level and in the Validator use the possible player actions to validate it
  • Create a Maker and Validate yourself. In this approach you have to create the level as in the previous approach, but there will be no valiration phase, which means that the level have to be well formed or you will have to look to it and validate. In this approach you have to have a way to make sure the moviment will be feasible.

In my case, i will use the second one at the beginning

PseudoCode of generator

In the plant jumper, the levels will formed of different kind of blocks and enemies, probabily i will add differente types while developing the game but there is a core behavior i want then to follow.

At this moment i want to be able to control:

  • how many items will be in each height of the level;
  • The height space between a new group of items;
  • The amount of items each block will contain. For example a group of 2 normal blocks together or 3 normal blocks together;
  • The minX and maxX
  • The minY and maxY
public List<float3> buildBlocks()
    {
        List<float3> blocks = new List<float3>();

        int currentY = minY;

        while (currentY < maxY)
        {
            int distance = UnityEngine.Random.Range(2, this.maxDistanceBetweenRows);
            currentY += (distance * size);

            int currentX = minX;

            int blocksPlaced = 0;

            while (currentX < maxX && blocksPlaced < this.maxItemsByColumn)
            {
                bool canInsertBlock = (UnityEngine.Random.Range(0, 10.0f) > 5) && (currentX + blockSize * size < maxX);
                if (canInsertBlock)
                {
                    for (int i = 0; i < blockSize; i++)
                    {
                        float3 blockPosition = new float3(currentX, currentY, 0);
                        blocks.Add(blockPosition);
                        blocksPlaced++;
                        currentX += size;
                    }
                }
                currentX += size;
            }

        }
        return blocks;
    }

Next steps

In this level editor i want to be able to create a group of items and set the dificulty. For that in another post i will discuss about how i decided what it is a easy and a hard level