Game Programming

Layered TileMap Support

by Cory on March 5, 2019 1:08 AM (Edited March 5, 2019 1:10 AM)

About a month or two ago, I made the decision to simplify my TileMap class so that no tiles could overlap. I believe that's the way that almost everyone else does it. Previously I wanted to have it so that I only needed 1 TileMap object for everything and you could have tiles overlap each other, but this presents numerous problems. For instance, just looking at a screenshot of a 2D zelda makes it obvious that you're going to want multiple TileMap layers.

Take this screenshot from A Link To the Past, for instance. One layer is probably good enough to implement almost everything in this picture. You'd want your player character on a layer above everything in the TileMap. But then there's a lot of special cases you might run into. For instance, what if you were able to walk underneath the bridge in this picture? You can't specifically for this bridge... But I swear I remember ALttP has a few. And besides, if you have bridges in your game you might as well have this situation at some point.

What do you do if you have 1 TileMap layer? I dunno. I don't really care to figure it out. But let's say you didn't constrain everything to one layer. You could put the upper grassland tiles on a high z-index layer, say z-index 1. The water and lower tiles in the trench go in a lower zindex. The bridge stays with the grassland on z-index 1, except for the pillars of the bridge leading into the water, which can go on layer 0.

Now what you can do is have your player character normally on top of the layer 1 map, walking around everywhere. But then have the engine (using script trigger points or something) place the player character on top of the level 0 map whenever the character walks into stairs or jumps into the water. Then, the player will appear to go underneath the bridge (which is on layer 1, remember) without having to do anything special to the graphics rendering code.

You can do other things with layers too! Like having really large foreground objects that scroll into view and obscure things in the background. Or parallax scrolling in general. Like this:

Note the two dark trees that scroll off screen to the left and the dark bush that comes into view from the right.

Ok so now what does this have to do with what I'm working on? I've been refactoring some code and adding new UI elements to support layered TileMaps. It's not as pretty as the screenshots, but it's something:

In this gif, I load up a tilemap file. When it finishes, you can see a panel called "Map Layers" appears with TileMap0 highlighted in orange as the active layer. I add a second layer (TileMap1) and then paint some water tiles on it. Since TileMap1 is on top of TileMap0, the water tiles appear over the existing tiles. I then press the red minus button to delete Layer1.

It feels like I said I'd start working on playable stuff forever ago. I'm still working towards it. I need to finish up this layer editing tool and then add a stamp/clone tool.



This Thought is part of Game Programming

Game programming general topic. I eventually hope to split this into separate ideas exclusively about specific games that I make.

back to the

top