DEV Community

Anthuulos
Anthuulos

Posted on

The Big Refactoring - Chapter 1

Hello everyone!

Welcome back to The Big Refactoring, where I explain the significant changes we’ve made in Town Alight since we published its first version. Feel free to ask any questions regarding the development process in the comments! Today, I’m going to talk about one of the biggest reasons that led us to make the choice of refactoring the game: Godot’s tilemap system.

Chapter 1: Living in a tile world

Tiles… most of you might have an idea of what these little guys are, but for those of you who don’t: a tile is a small rectangle (or hexagon, or other funky shape, but let’s not get ahead of ourselves) which, when placed together with other tiles, creates a tileset. Using tilesets is how worlds are created in many 2D games, and when used wisely, they can make your life easier as a game developer. Guess who didn’t use them wisely initially? That’s right, yours truly.

And how could I mess up such a basic concept? Well… let me explain. In the original version of Town Alight that you can play in the browser, almost everything in the game is a tile. The trunk of that tree? A tile. That fire that is threatening our birb’s way of life? A tile. That piece of roof from where our beloved mayor bravely commands his troops? You guessed it, a tile.

Image description

This doesn’t seem that bad, right? Wrong. In Godot, and other game engines, tiles are not their own objects like the villagers or the birb. Instead, they are part of the tileset object, which is in charge of telling each tile where it goes and which layer it belongs to. And, hey, it’s great that it works this way because otherwise tile-based games would be very resource-intensive. However, the stuff you can do with tiles is very limited. Each tile only knows about itself and is completely unaware of its surroundings; it doesn’t even know if it belongs to a house or a tree! This leads to weird scenarios…

Image description

Now… I confess I’m not an expert on burning trees, but this doesn’t look like how a tree would burn in real life. The issue is that, not only does it look weird, but we also didn’t have control of what happened when a tree or a building was scorched, because the game didn’t even know that there was a building there in the first place! For this reason, we decided to change how the tileset interacted with the rest of the game, which, at the moment, was our core mechanic.

Changing this behavior wasn’t going to be easy, because the fires only wanted to spread throughout the tileset without acknowledging there were other objects in the scene. This meant that the first step was changing the fires from being tiles to being their own separate object, which allowed them to have individual colliders and to check their neighbors. So… here you go, our new, independent fire, with upgraded graphics thanks to Doops!

Image description

Now, we just needed to make our building and nature tiles understand that they were part of a group, and the obvious step was simply to take them out of the tileset and make them their own objects, similar to how the fire became its own object. This allowed us to give each structure its own identity, so no more half-scorched, half-healthy trees for us. Not only that, but this also allowed us to give each structure more complex features, such as defined fire resistance and a value, among other properties. The result (besides many features which I will not discuss yet) was structures that burned in a more natural way, like this guy:

Image description

As a bonus, now that structures aren’t part of the tileset system, we are able to place them wherever we want, without the placement constraints imposed by the tileset system.

Finally, with these two major changes, we now can improve the interaction between fires and structures. Since fires only knew how to spread throughout their grid and now have more complex behaviors, we can make them detect their neighbors and tell them that these neighbors could come in the form of other fires, bushes, trees, buildings, and more! The structures, on their end, now understand that having an active fire as a neighbor means trouble. In summary, the world is much more interconnected now than it once was, and you’ll be able to experience that yourself in the future.

There’s so much more I could discuss about the new changes to the tileset system, but I don’t want to overwhelm you with a mega-long entry. For now, we’ve covered the most significant changes. If you have any questions or want me to dive deeper into any of these topics, feel free to let me know—I'll be happy to explore them in future entries!

That’s all from me for now. Thank you for reading, and I look forward to sharing more exciting updates with you soon. Stay tuned!

– Anthuulos

Top comments (0)