Jump to content
  • Advertisement
Sign in to follow this  
Thanamos

Organizing a map structure for a top-down tile game

This topic is 1561 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

As the topic states I am trying to write a  top-down tile-based game, something like RimWorld or Prison Architect. Where the player can manipulate the world in different ways.

But before I just go ahead and start writing code I am trying to decide on the structure of the world. There are multiple ways of dealing with this I know, but maybe someone can put me on the right track.

 

For one option I was thinking of having a two-dimensional array of Tile objects

Each tile has a position as well as having a collection of Entities.

Each entity having a position and rotation relative to the Tile as well as a texture to draw. This way I can find a tile instance from the Map, add a table, and put a vase on top if it very easily.

 

To draw I would just call Tile.Draw() and it would draw all it's entities.

 

I see alot of advantages with this approach, like I only have to check if the Tile is on screen before I draw it. Plus it's really easy to detect what items the user wants to manipulate.

 

On the downside it makes it harder to place bigger entities, like a table that spans 2x4 tiles, Especially if the player can move/place/remove items in the world.

 

What other ways are good? I could keep a big list of entities, maybe grouped by position, so for each 25x25 tiles I will have one list containing everything in that area. But again I would have to keep in mind some items may span multiple areas.

 

It all ends with me being confused as to what is the best/a good/an acceptable way to store and manipulate a world where everything can be moved, rotated, removed and added smile.png And when I am confused I get unproductive.

 

I have been trying to google but I guess I keep searching for the wrong keywords, because I can't find much usefull information. So any hints and pointers are very welcome.

Share this post


Link to post
Share on other sites
Advertisement

You're throwing away a lot of advantages of a tile map.

 

IMHO: I always cringe when I see someone defining a tile class for a simple tile map.

 

For one, the indices of your tile array are all you need for positions. The tiles themselves don't need any, since you can deduce the position from the indices already.

Determining the tiles to draw is a simple calculation. With camera offset and screen size you can calculate the starting end ending indices of tiles on the screen.

 

Adding entities to tiles sounds good on a glance, but you already saw some disadvantages yourself. Also, when entities move you'll be constantly updating tile pointers. When an entity is on the move between tiles, which tile do you add it onto? Which tiles entities do you check for collision when an entity moves?

I'd put entities in a separate list, optionally a spatial container. This lets you reduce overdraw easily.

 

What's left for the tiles? Indices to the image. These could be stored directly, so you end up with a much simpler two dimensional array.

Share this post


Link to post
Share on other sites

What's left for the tiles? Indices to the image. These could be stored directly, so you end up with a much simpler two dimensional array.

Indices to animations, which contain indices to the image? You'll want to animate your tiles - especially for things like water.

Also, for things like walls, you might want collision data baked into the tile data - maybe (it depends on your game's requirements).

 

So what you probably end up with is that the map is an array of indices to some kind of tile data struct, which is shared between all "tiles" in maps in the game.

struct TileDetails
{
    std::vector<ImageSubrect> animationFrames;
    Type type; //For whatever logic your game needs - 'Water', 'Lava', 'Wall', 'Trap', etc...
};

You do run into some cases where you need data directly at a specific location in a map - teleporting (where does it warp the player to?), and script triggers (which script does it trigger?) - but those kinds cases could be handled separately than the grid-based regular tiles.

Edited by Servant of the Lord

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!