Jump to content

  • Log In with Google      Sign In   
  • Create Account


Organizing a map structure for a top-down tile game

  • You cannot reply to this topic
2 replies to this topic

#1 Thanamos   Members   -  Reputation: 150

Like
0Likes
Like

Posted 06 July 2014 - 11:23 AM

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.



Sponsor:

#2 Endurion   Crossbones+   -  Reputation: 3338

Like
3Likes
Like

Posted 07 July 2014 - 11:56 AM

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.


Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

#3 Servant of the Lord   Crossbones+   -  Reputation: 17322

Like
2Likes
Like

Posted 07 July 2014 - 12:42 PM

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, 07 July 2014 - 12:50 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal






PARTNERS