Tile-Based Games

Started by
9 comments, last by egwenejs 17 years, 8 months ago
I've been thinking about this for a bit now and would like to know what others think. What is the best data type to use for storing tile information? I was thinking a 2D array. Also, should a tile know its surrounding tiles or should each tile be independent of each other?
Advertisement
The best, IMHO, is an std::vector, for a couple of reasons:

1) It is dynamic, which means you get more flexibility, and you can resize your maps if needed.
2) It is just about as fast as a normal array.

I wouldn't use a 2D array, as it makes some things unnecessarily complex; when drawing the tiles, you need to use two loops instead of one.

I would make tiles independent of each other, if I were you, as it greatly simplifies things.
a 2D array is my prefered, as yes I do like my tiles to know their emediate nieghbors. If your going to incorporate fringe you'll want to know what the tile next to it is.
-----------------------------------------------The ZoloProject
Hmm differing opinions...

I can see why tiles should be independant of each other. It causes alot of overhang trying to keep up with the quad-link list. And I would perfer to not have the overhang. However, how do you tell your characters what tile to move to if you do not keep the surrounding tile data?
You could also use a std::map<int, std::map< int, Tile> > for 2d, though of course it is somewhat complicated but I guess it would "feel" much more like an array.

edit: Doh xD
Quote:Original post by Anatol
You could also use a std::map<int, std::map< int, Tile> > for 2d, though of course it is somewhat complicated but I guess it would "feel" much more like an array.
If it's just the [][] syntax you're after, that's probably not the best way to get it; access will be slower and the data may not be stored contiguously.

If you want that syntax, use boost::multi_array instead.
I'm not after any syntax, just methodology. Infact, I couldn't even tell you what the syntax shown does exactly. I'm programming in C#, not C++.
In any language, I think any multidimensional array is overkill for a rectangular NxM array, which can be simulated with your 1D array of choice (whether it be a raw array or a dynamic array) -
// the NxM dimensions of our arrayint SOME_MAX_X_SIZE;int SOME_MAX_Y_SIZE;// an arbitrary array with enough room for our dimensions -// can be a raw C-style array or a std::vector or C# equivalent.ArrayValueType array[ SOME_MAX_X_SIZE * SOME_MAX_Y_SIZE ];// a function to get an individual value from an the array// using a 2D coordinate -ArrayValueType getIndex( int x, int y ) {     return array[ x + y * SOME_MAX_X_SIZE ];}

Using that little chunk, (x+y*MAX_X_SIZE) you can simulate an Nd array with a single dimensional array, given that its rectangular. If you want a non-rectangular array (ie, each row doesn't have the same number of elements), look into boost::multi_array as suggested, or the C# equivalent.
If you want to use shortest path algorithms later on, you will need to keep the relations between the neighbours stored somewhere.
Personally I think I would prefer to have them in a separate array/vector so that I could choose how many nodes to use for the path finding separately from how many tiles there is.
Quote:Original post by pulpfist
If you want to use shortest path algorithms later on, you will need to keep the relations between the neighbours stored somewhere.
Personally I think I would prefer to have them in a separate array/vector so that I could choose how many nodes to use for the path finding separately from how many tiles there is.

Umm, why a completely separate vector? Traversal information is part of each tile - why not actually make it part of each tile:

struct CTile {   /* whatever render information */   /* relations to neighbors; 1 for each cardinal direction */   CPathfindingInformation _terrain[4];   /* maybe a reference/pointer to master array, but this should be a       POD-ish class and have all the operations done on it by the       controlling (CMap?) class. */};

This topic is closed to new replies.

Advertisement