In my game project I create the terrain programmatically by creating tiles on a 2D grid as the player moves around the game map. I have it working where I select the current tile and check all 8 positions around the tile, then I create a new tile at those positions unless one already exists there. This works in a "hard-coded" way, where I can take any given grid position like (x: 0, y: 0)
and then I check all the positions around it in this fashion:
(x: thisPosition.x - 1, y: thisPosition.y + 1) // POSITION UP-LEFT
(x: thisPosition.x, y: thisPosition.y + 1) // POSITION UP
(x: thisPosition.x + 1, y: thisPosition.y + 1) // POSITION UP-RIGHT
(x: thisPosition.x - 1, y: thisPosition.y) // POSITION LEFT
(x: thisPosition.x + 1, y: thisPosition.y) // POSITION RIGHT
(x: thisPosition.x - 1, y: thisPosition.y - 1) // POSITION DOWN-LEFT
(x: thisPosition.x, y: thisPosition.y - 1) // POSITION DOWN
(x: thisPosition.x + 1, y: thisPosition.y - 1) // POSITION DOWN-RIGHT
This works, but I can only check one "ring" around the selected tile at a time (as shown below in the image on the left). I want to replace this function with a new, improved version that works in a more programmatic way, something using nested loops. I want to be able to give the function a number, and then it will check that many "rings" around the selected tile and add terrain tiles to those positions. For example, if I ask it to check 3 rings, it will check all of the tiles shown in the image on the right:
I have some ideas but I'm not sure yet how to put them into code. I'm sure that someone online has solved this exact problem before, only I can't find it. I thought I'd put this question out here and see if this rings any bells for someone.
If anyone has any code suggestions, it would be helpful if you could write pseudocode, since the only language I know right now is Swift. I'd like to learn C and C++ someday, but I'm just a greenhorn hobbyist and starting with Swift was easiest for me.