Infinite Roller Tutorial Twelve – Blocks Part Two

Welcome to Edge of Code’s series of Infinite Roller tutorials. If you’d prefer a video tutorial, you can find one here.

In the previous tutorial, we starting looking at using blocks rather than tiles for the terrain. Today we’ll build on that.

Open the Infinite Roller project and the scene, then create a new script called AbstractTerrainGenerator. This will be the superclass of ChunkTerrainGenerator and BlockTerrainGenerator, so we want to have any code common to both types of terrain generator. It is an abstract class, so add the word abstract to the class declaration so that it becomes

public abstract class AbstractTerrainGenerator : MonoBehaviour 
{}

An abstract class cannot be used in the same way as a normal class. You can’t create an instance of one, which also means you can’t add the AbstractTerrainGenerator script to a gameobject. If you try, Unity will display an error message.

Unity tutorial abstract

They are used as base classes. In this case, we’re using an abstract class because ChunkTerrainGenerator and BlockTerrainGenerator have some common functionality and we’ll need to make use of something called polymorphism which I’ll explain later. AbstractTerrainGenerator is a subclass of MonoBehaviour, so it can use the functionality in MonoBehaviour, as can any subclass of AbstractTerrainGenerator.

Save the class. Go to ChunkTerrainGenerator and change the class declaration so that AbstractTerrainGenerator is its superclass:

public class ChunkTerrainGenerator : AbstractTerrainGenerator 

Do the same for BlockTerrainGenerator:

public class BlockTerrainGenerator : AbstractTerrainGenerator

Now that both classes inherit from our abstract class, we can think about what should go in there. We’ll need to restart the terrain generation of both, but due to the differences in the way the terrain is created, each one will need its own method. We can allow this by creating an abstract method in AbstractTerrainGenerator:

public abstract void Restart();

This method must be implemented by the subclasses. Save the script, go to ChunkTerrainGenerator, and find its Restart method. Since this is now implementing the abstract class’s method, we need to add the override keyword to make it work. Change the method declaration to

public override void Restart()

and save the script.

Unity tutorial override abstract

Next go to BlockTerrainGenerator and create an empty Restart method.

public override void Restart()
{}

Save it. We’ll add code to it later. There will be other methods to add to AbstractTerrainGenerator, but we’ll do that as we go.

Open the GameController script. Replace the existing ChunkTerrainGenerator variable with

//reference to the terrain generators
public ChunkTerrainGenerator chunkTerrainGenerator;
public BlockTerrainGenerator blockTerrainGenerator;
private AbstractTerrainGenerator currentTerrainGenerator;

Unity tutorial blocks generator

Find the terrainGen.Restart() line in RestartGame(), and replace it with

currentTerrainGenerator.Restart();

The currentTerrainGenerator variable is an AbstractTerrainGenerator which means we can assign either chunkTerrainGenerator or blockTerrainGenerator to it, since they are both subclasses of AbstractTerrainGenerator. This is polymorphism – the subclasses of AbstractTerrainGenerator can be treated as AbstractTerrainGenerator objects. This and the fact that Restart is an abstract method means currentTerrainGenerator.Restart() calls the overridden Restart method in the subclass that is assigned to currentTerrainGenerator.

We’ll assign the first two in the inspector, and the third will help us switch between which type of terrain we want to play the game with. Add the following line to the end of Awake() to initialise currentTerrainGenerator:

//start off as chunks
currentTerrainGenerator = chunkTerrainGenerator;

Save the script, go back to Unity and select the GameController in the hierarchy. Drag the BlockTerrainGenerator and ChunkTerrainGenerator gameobjects onto the appropriate slots.

Next we’ll add a new menu button to allow the player to pick which game to play. Find the button child objects in the hierarchy under the MainMenu gameobject and duplicate the Play gameobject by pressing Ctrl+D.

Unity tutorial UI button

Rename the top one “Play Infinite Roller – Chunks”, and change the text in the child Text gameobject to the same. Use “Play Infinite Roller – Blocks” for the other button. Make sure the buttons are in the order you want them to be in by dragging and dropping them in the hierarchy. The order of the Layout’s child gameobjects mirrors the order they appear on the screen.

Unity tutorial UI button text

Open the MenuManager script and change the method name of PlayGame to PlayChunksGame, then copy and paste the method, and rename it to PlayBlocksGame. You should have the following:

public void PlayChunksGame()
{
	ToggleMenu();
}

public void PlayBlocksGame()
{
	ToggleMenu();
}

We’ll need to make the methods set the currentTerrainGenerator in GameController, but first save the script and go back to Unity. Select the chunk button gameobject and under OnClick(), select MenuManager → PlayChunksGame. For the block button, select MenuManager → PlayBlocksGame.

Unity tutorial hook up buttons

Go back to the GameController script and add an enum,

public enum TerrainType
{
	Chunks, Blocks
}

enum is short for enumerator and is a set of named constants. It’s used when something should only have a limited number of possible values. For us, the terrain can only be made of chunks or blocks, and TerrainType will let us show which.

Save the scene and the project.

Next time we’ll finish making the buttons play the appropriate game and write code to generate random terrain.

Remember you can download the files for this tutorial on the Downloads page. See you next time!

PreviousTutorialButtonNextTutorialButton

Comments are closed.