It is time that we replaced the nodes and lines connecting the nodes with an actual maze. We have all of the sprites we need to draw the maze in our spritesheet already. We just need to figure out a way to draw it. Looking at the spritesheet again we can see that the images that make up the mazes are on the right side of the sheet. You'll notice that these sprites are smaller than the other ones. All of the sprites on a sprite sheet don't have to be the same size. They can be whatever size you want them to be. On this sprite sheet I have two different sprite sizes: 32x32 pixels and 16x16 pixels. Everything on the sprite sheet is 32x32 pixels except for the maze sprites which are 16x16 pixels.
There are 10 sprites that define a maze layout. Each of those 10 sprites can also be rotated 90, 180 and 270 degrees. For example, the first sprite is a corner piece that defines the top left outer corner of the maze. However, we can rotate that same sprite 90, 180, and 270 degrees to define the other three corners. All of the other pieces can be rotated to define other parts of the maze as well. So basically the maze is made up of 10 unique pieces. All of those 10 pieces will be rotated four ways to make up a total of 40 pieces that define a maze.
Anyways, the goal here is to try and find a way to represent each piece and it's orientation in a text file so I can write a program that reads that text file and figures out which piece and how many times to rotate that piece at any particular location. I need to be able to define 40 unique characters. I can use the alphabet with upper and lower case symbols since there are 52 of those characters minus the ones I'm already using taking it down to 44. But that will just make the whole text file really hard to read for a human. Not that it has to be easy for a human to read, a human does have to create it.
Another method is to use two digits for each piece. Since there are 10 unique pieces I can label each piece from 0 to 9 with the first digit. The second digit can be how that piece is rotated. 0 means no rotation, 1 means rotated 90 degrees, 2 means rotated 180 degrees and 3 means rotated 270 degrees or -90 degrees. That can work but then my maze text files won't be neatly lined up when reading them unless I make all of the characters double digits. There's no perfect solution for this as far as I can tell, we'll just have to use the best solution we can think of. In order to solve this in the cleanest way I know how we'll just create another text file that has the rotations of each tile.
Yet another method is to use the original maze text file and infer the pieces and rotations of each piece from the nodes and paths. That's obviously going to be more difficult and a lot more code. Maybe I'll make that into a future optional section because it does sound fun. For now we'll just stick with the extra text files.
We'll need to open up our maze1.txt file and modify it to look like the following. We basically take our maze1.txt file and replace some of the 'X' values with a value 0-9 which in turn refers to which tile sprite to use.
Since the maze is composed of sprites, we'll create a new class called MazeSprites that inherits from Spritesheet as well.
When we create the MazeSprites object we pass in the maze1 text file so it knows which sprites to get and where to place them. We're actually going to just place the sprites onto the background object which makes things easier. For the level we're passing in level mod 5 because we only have 5 background tile sets. So for each level we'll simply loop through the tile sets.
Run the game now and you'll see something similar to the image on the right. How is that right? Did we do something wrong? No, believe it or not, all of those sprites are in the correct positions. For some of those sprites, though, we need to rotate them. Some of them have to be rotated 90 degrees and others need to be rotated 180 or 270 degrees. How do we know which sprite to rotate and how far it needs to be rotated? Go to the next section to find out!