NaughtScape[05] - A small function that will save a lot of time

in programming •  7 years ago 

Now that I had a couple models to start working with I couldn't put off writing the world generation. It was time to take off my artist's smock, and put on my super programmer power pants !

As I got started I realized that I would probably want to use a full map for testing, and might have to empty the map for a multitude of reasons. I didn't want to be forced to repaint all of those tiles again the next time I needed to fill the map, so I added a Fill Map button after the Clear Tiles button.

Filling the Map

In order to build this function I took most of it directly from the existing Draw and OnDrawGizmosSelected functions. The latter is located in the TileMap class and is called when the object the TileMap class is attached to is selected. It draws the grid squares when the TileMap is selected. The code I brought over from this function was the section that loops through the grid.

FillMap.PNG

The only change I had to make to the code was to add the NewBrush call to ensure that there was a brush instantiated.

This used a single loop and a modulo to navigate the grid. A modulo (%) is the remainder amount after a division, so in float column = i % maxColumns: column started counting up from 0 because as i counts up in the for loop it gets divided by the _maxColumn_s (32 currently). It starts with 0/32 which returns 0, then goes on to 1/32. 1 goes into 32 0 times, with a remainder of 1. Therefore the modulo is 1.

The Draw function here is responsible for drawing the tile at a specific location, I'll go into this more later.

After drawing the tile it checks to see if the column is equal to the width of the grid minus one, which will actually be at the final column because it started counting on 0 instead of 1. If this check returns true, it increments the row counter. This is necessary because without it once the column variable reaches 32, the modulo expression will be 32/32 which works out to: 32 goes into 32 once, with 0 remainder. So the column variable gets set back to 0, overwriting the first tile I set down and then each one that was placed after it.

Drawing a Tile

Before creating the FillMap function the Draw function was only called from a single location, painting with the brush. The FillMap function doesn't use a brush that moves over the tiles I wanted it to draw in so I had to refactor the drawing code a little bit.

draw_functions.PNG

Using the same function name with different parameters is called overloading and can be a very useful tool.

The refactoring of the Draw function was actually pretty easy. The entire contents of the Draw(x, z) were originally located where Draw has a call to Draw(x, z). I needed to call the draw functionality, but without using the position of the brush to determine the coordinates to use. By creating the Draw(x, z) I gave myself the freedom to draw a tile to the tile map at any position I choose.

Then all I had to do to maintain the old functionality was to make a call to Draw(x, z) inside Draw, passing the coordinates pulled from the brush to the new function.

Voila ! A convenient button in the editor to set the entire map to grass tiles.

fillmap.gif

Until Next Time !

If you have any suggestions for improvements, questions, or links to resources that would help me out feel free to leave a comment. I am using Unity as a game engine and Blender for any 3D modeling I may have to do during this project. Upvote if you liked the post or found it helpful, and follow me for future updates on NaughtScape, sometimes other programming, and occasionally other stuff. All of the source code can be found on my Github.

You could also add me in game on OSRS if you play !
My ign is MapleThunder.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!