The map class is what puts all the tiles together. It’s responsible for creating each tile class as well as storing them internally and assigning them a unique id. Later on the map class will handle decorating the tiles as well.
- [Voiceover] Now we're ready to create the map class. The map class will store all of the references to the tiles and will also be responsible for manipulating the values to make sure that we set them up correctly, as well as navigate between them when we need to. Let's go ahead and create a new C Sharp script called Map. Then let's open it in MonoDevelop. Just like our tile class, the map class doesn't need to extend mono behavior. So let's go ahead and delete that, as well as the start and update methods.
We're gonna want to create a few public fields. The first is going to be an array of tiles. Next, we're gonna wanna keep track of the columns and rows that are inside of our map. Here you'll see that I'm typing them to int. This way we make sure that we always have whole numbers. The reason we're storing it this way is that columns will represent the width of our map, and rows will represent the height. Let's go ahead and set that up by creating a new public method in order to create our map.
We'll name this method NewMap. And our new map will need a width and a height. Let's set the columns field value to the width, and we'll set the rows field value to the height. Next, we're gonna need to know how many tiles will exist inside of our map. In order to do this, we're gonna set the value of the tiles field to a new array, and inside of the array we're gonna set the value equal to columns multiplied by rows.
Now, it's important to note that we're actually gonna use a 1D array to represent all the tiles in our map. A lot of the times people wanna use a 2D array because it's a little bit easier to visualize. But when you get to more complicated maps or very large map sizes, 2D arrays can actually slow you down. So throughout the course we'll be using some basic math in order to pretend as if our 1D array is actually a 2D array. Now the next thing we need to do is create all the instances of the tiles that'll go inside of our map.
Let's go ahead and create a private method called CreateTiles. Inside of this method we're gonna need to get the total number of tiles we'll wanna create. In order to do this, we'll just create a new variable called total, and we'll make it equal to tiles array length. Next, we'll wanna create a new tile in each index of the array. Here we'll use a for loop, we'll set i to equal zero, we'll make sure that i is less than the total, and we'll increment i over one.
Inside of our loop, we're gonna create a new tile class. We'll simply make a variable called tile, and we'll set it to a new instance of a tile class. Next, we're gonna wanna give the tile an ID. Here, we'll set the value of id to equal i in the loop. And finally we're gonna wanna save this tile inside of the tiles array. Here we'll use i, for the index, and we'll pass it in the reference to the tile we just created. At this point we now have the ability to create all the tiles we need for our map.
The last thing we need to do is at the bottom of NewMap, let's go ahead and call CreateTiles. Now whenever we wanna create a new map, we simply call the NewMap method and we'll save out the columns and rows, we'll create a new array to store the tiles, and we'll pass in new tile instances to each of the indexes of the tiles array.
- Setting up the project
- Creating the tile, map, and map tester classes
- Visualizing the map
- Creating a moving camera
- Autotiling and bit shifting
- Decorating the map