1
\$\begingroup\$

I'm looking to make a map editor for a 2D strategy tile-based game (I am using OpenGL with SDL and writing the game in C). My game levels are made up of a set amount of different tiles, like a river tile, a mountain tile, a sea tile, etc.

A level is a struct holding the tiles like this:

struct Level {
    Tile[LEVEL_ROWS_AMOUNT][LEVEL_COLUMNS_AMOUNT];
    ....

I initialize the level by filling it with a plain default tile. Then I just iterate every tile and render them one by one.

Every one of these tiles always has the same properties no matter what, BUT should look different depending on the tiles surrounding them. For example, if a river tile has a river tile to its left and to its bottom, it should take a top-right corner "L" shape appearance, while the river tile to the left should be horizontal and the one to the bottom should be vertical.

Here's an illustration of what I mean:

Tiles example

All of my tiles' visuals are loaded into a single sprite sheet image, and I have a JSON file pointing to the coordinates of every tile inside the image, where every entry looks like this:

{
"variations": {
    "horizontal": [
        {
            "x": 32,
            "y": 0
        }
    ],
    "top_right": [
        {
            "x": 320,
            "y": 96
        }
    ],
    ...
},

So every tile I place down in my game has multiple possible visual variations, and I'm able to manually set which one a given tile should have. But like stated above, I would like to make this process automatic upon placement of the tile.

What would be a good way to implement this? I have a good amount of unique tiles (around 20 or so) and most of them would require unique logic. I'll also probably be adding more tiles in the future, so I'm hoping I can find a way that doesn't involve long hard-coded if statements.

\$\endgroup\$
  • 2
    \$\begingroup\$ This looks like a technique called auto-tiling, which is commonly implemented by forming a bitmask from adjacent tiles that match/mismatch the center tile's type, and indexing a list of variants using the bitmask value. You can find tutorials about it online — how have you tried putting this into practice so far? \$\endgroup\$ – DMGregory Oct 24 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.