Connected roads in a tilemap

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By nicfer
:warning: Old Version Published before Godot 3 was released.

Hello.

I’m making a city builder game, and I’m trying to figure the best way to implement roads.
What’s the easiest way to implement them to connect when adjacent? That is, they change graphics depending on other roads 1 tile up/down/left/right. My idea is to flip/transpose them in order to not make 2 or 4 images of the same road in different directions.
Remember that not only the placed tile needs to be set to it’s correct sprite and position, but the adjacent ones also need to be changed.
I’m leaving this for a latter status of the game, but meanwhile, I’ll leave the question: Is this possible to do with Tilemaps? Is it any easier with overlaid sprites?

Goodbye.

It’s a bit ad, but you may check my GitHub, cause I’m doing city-builider too :wink:
Hope, that 'll be usefull. If you’ll have any question about my code, ask me here, on Github or Tumblr.
PS: #pasta_style_allert - that’s no the best solution…

Kamil Lewan | 2017-05-22 19:52

Ok, nice. It looks promising, although with some glitches in the UI (namely, the initial city placement is activated while scrolling with the right mouse button, and the add new houses/road window randomly places objects on the map).
I also uploaded my first revision of my project, GitHub - nicfer/godotcity , which is even more crude, but it’s something.

nicfer | 2017-05-25 12:56

Thanks for feedback. About your GH, I have 404 error, you added point to url :slight_smile: About that random placement, can’t confirm this (have you last commit?), btw, I suggest move discusion about my project to GH/issues, casue that’s no about your question. Thanks.

Kamil Lewan | 2017-05-25 23:19

Ok, I’ll continue the chat about your project there. And I fixed the URL, it was using the comma in front as part of it, now it should work.

nicfer | 2017-05-26 12:15

:bust_in_silhouette: Reply From: Zylann

You can easily script this if you know the list of tiles that make up roads. For a given road tile, look at the 4 neighbors. If they are roads or not, you have different cases. This way you have 4x4 cases: one of them being no road, it’s 15 cases in which you choose which tile to put and how to flip/transpose it.

That’s the easiest way, but if you like binary there is a clever way using bit masks of 4 bits, where each bit is a direction where the road tile can go. You then obtain a number between 0 and 15, and you map it to a tile ID and flip/transpose.

This question comes up a lot in 2D game dev, I’m sure you can find more examples and variations on the web, it’s not specific to Godot :wink:

:bust_in_silhouette: Reply From: eons

Even if Tilemap offer a structure capable of doing that, I suggest to create your own matrix with values to handle logic (could be a class/dictionary with cell data), it will be easier to manage, then just use the canvasitems to draw your data on screen mapping the data to tiles and other objects (tilemaps, sprites or a big changing texture).

You can look around for general approaches for this kind of games and see which one suits better your design.

:bust_in_silhouette: Reply From: CHROM

Use these bitmask for your Tilemap:

Road bitmask

How to use this?

sairam123 | 2020-10-29 16:57