Comment by fredrikholm
10 days ago
Tiling sprites is a wonderful example of something that starts off relatively simple and then explodes into all kinds of interesting headaches.
In the beginning, you're looking up a set of UVs in a table indexed by some mask value.
In the end, you're debugging errors where one off by one error cascades into strange realms of indexing the wrong (unloaded) chunk with coordinates rolling over to positions that make no sense, looking up neighbours that look one way yet you end up drawing something completely different.
The suggest solution is an elegant and relatively common approach to what would otherwise be a naive series of nested 40+ if statements consisting of much swearing and sprite editing.
Don't be afraid of if-then chains. Smile and put the word REFACTOR in a comment nearby and move on to the next problem. Ship the thing! Perfectionism is overrated.
If you only had two tilesets, sure. This scales with square of n, so in a real game you're looking at a 4000-8000 long if-else chain.
You're going to spend 100x more time maintaining that disaster than you'd have learning a (to this problem) fundamental algorithm. Shoulders of giants.