# Expandable 2d environment by spawning and controlling individual tiles

what is the best way to implement an environment that is "expandable" in every direction so that when the player moves in any direction a new row or column of tiles that can be manipulated individually is created,however as the player moves into the new created area whenever he decides to comeback, the tiles that disappeared by going off the screen are there when he comes back.

What I'm trying to do is make a new row if the player moves upwards or downwards, or make a new column if the player moves sideways. But I can control the individual tiles to make them change colors or place items. Then whenever the player advances and new tiles are created and others are moved off the screen if the player decides to come back to the area the item that was placed and color change is still there. Sorry if I'm not explaining my best, if you need a better explanation I'll be happy to give you one. Thank you.

• What is your main issue here, design or implementation? Are the tiles always the same but only differ by items generated on them? Are the tiles/items generated completely randomly, are they pre-defined? Do you need your grid be "infinite"? – Vaillancourt Feb 17 '14 at 21:18
• The issue is implementation,I know how I want it to work and look but clueless as to how to code it. The tiles are going to be rendered with the same texture, when the player steps into a new tile it turns a different texture and he can place an item. If the player advances to another tile,new set of tiles are created and the tiles that were in the opposite direction are removed from the screen. This process can go on forever thus the "infinitly expandable environment". – AllenZ41 Feb 17 '14 at 21:31
• however whenver the player returns to the area where he left the item the new tiles that are drawn contain the item and tiles where the texture was changed. – AllenZ41 Feb 17 '14 at 21:32

I don't know if it'll fit in your design, but here's how I'd do something like that.

Let's assume:

• that the default colour for your not-yet-visited tiles are pink and your visited tiles are white (as per in your question).
• that the radius the player can see is 3 tiles, excluding the one on which he is standing, so it results in a 7 by 7 square.

I would keep track only of the things that the player has modified.

For example, if your player starts at position [0, 0], you know how to generate the radius around him, so, for instance, tiles [-3, -3] through [3, 3] are generated as pink tiles and displayed. Now, since the player only went on tile [0, 0], you make it as a white tile and keep only this information in a data container (for instance a map or a set). If he places an item, you also keep that information in another container. (Or you could have your data able to keep track of both.)

Then, as your player travels, you add your data about the tiles he travelled. At the same time, when you "create" a new row or a new column, in fact, you just show temporary information that is easy to generate "on the fly" (the pink tiles), and then you query your data containers to check if he's already been into the "generated" tiles, and modify them accordingly.

For instance, your player goes from tile [0, 0] to tile [4, 0]. This creates 4 white tiles in your data container. When he steps back on tile [3, 0], the column 0 has to be re-generated. Which you do: all the generated tiles will be white tiles, but since you know you'll display tiles [0, 3] through [0, -3] (the whole column the player can now see), you'll query your data container and see that you have to put the tile at [0, 0] as a white tile. You'll also query you items container to see if he placed an item on one of these tiles.

Here is why I'd do it like that:

• You don't need to keep track of stuff you don't need (if you had to keep your infinite map in memory, it would end up taking a lot of memory).
• If your map becomes very large, you can easily partition your data into smaller containers.
• Thank you for your response.. I will try to implement this but the only thing that is a bit unclear to me is since you suggest that I only keep track of the stuff I need(white tiles and items placed) then how should I go about splitting the screen into a grid and placing a texture within a cell of the grid(pink tiles).. For example should I place the first 7*7 tiles into a list then assign them with 4 coordinates that represents the boundaries of the tile, then if the tile is stepped on or an item is placed then I move that data into a map or set? – AllenZ41 Feb 18 '14 at 2:42
• Hmm, well it all depends on your technology. If you have not done anything code-wise yet and don't have a clue about how to display a grid of images on the screen, you might want to look for tutorials into this matter first. – Vaillancourt Feb 18 '14 at 20:40

The technique I use is to create a method of being able to (re)generate the map at any point.
I generally use simplex noise to create 2D height maps and the like (you could, for example, layer multiple noise maps with varying frequencies to emulate height, flora, terrain type/biomes, danger/savagery etc etc (obviously I have an rpg/fantasy game bias but they're just examples and the technique can be modified and used for just about anything)).
The advantage of this is that you can generate terrain on demand at any point in the 'world' and know that it will 'connect' with any previously generated sections of the world.
Track only the changes to the environment and apply them on regeneration.

• I have researched and found that a technique called Perlin noise works for generating an "infinite" terrain. Is Perlin noise and simplex noise the same thing? and if not since in developing a top-down 2d game which one should I use? – AllenZ41 Feb 18 '14 at 19:40
• Also how would I go about implementing the noise? is it a formula or an specific algorithm? – AllenZ41 Feb 18 '14 at 20:13
• Yes, for the most part Perlin and Simplex are the same (and are often both called Perlin noise), actually developed by the same guy, though Ken Perlin himself recommend Simplex noise. As for implementation, it's too much to go into in this comment. Best resource I can find is a SO Question here: stackoverflow.com/questions/18279456/… that links to the 'origional' resources here: webstaff.itn.liu.se/~stegu/simplexnoise and even includes a java implementation that should be easy to convert to your language of choice. – DFreeman Feb 19 '14 at 9:49