Reputation: 204
I am currently working on making a node graph to store tile/object data for a 2D game that I am working on. Essentially it's a map of hexagon tiles with objects on each tile.
My current setup looks basically like this:
class HexCell {
public:
HexCell* GetNeighbor(int dir) { return neighbors[dir]; };
private:
HexCell* neighbors[6];
// (x, y) position
// Contents of cell
};
class HexGraph {
public:
HexCell* GetCellByIndex(int index) { return cells[index]; };
vector<HexCell*> GetCellsByGridIndex(int x, int y) { return grid[x][y]; };
private:
vector<HexCell*> cells;
vector< vector< vector <HexCell*> > > grid;
};
So currently there are 3 ways to access HexCells:
cells[]
(useful for iterating the whole list)grid[x][y]
(useful for rendering)My question then is: Is it a terrible waste of memory to store 8 pointers for each object (one in cells
one in grid
and up to six as neighbors of other cells)?
Is there a better/more common way to do this, or is it an acceptable system in order to have different access methods for different uses?
Upvotes: 1
Views: 92
Reputation: 3723
Given a hex map is just a 2D grid with straight columns and wavy rows, I'd recommend storing your map data as a 2D array of HexCells. You'll be able to determine based on the X index whether the column of hex cells is even or odd to determine if it is one of the shifted down hex tiles
Here is a rough representation of the hex cells and their corresponding x,y indexes in a 10x4 array where X%2==1 are the columns that are shifted down by 1/2 of a hex cell height
0,0 2,0 4,0 6,0 8,0
1,0 3,0 5,0 7,0 9,0
0,1 2,1 4,1 6,1 8,1
1,1 3,1 5,1 7,1 9,1
0,2 2,2 4,2 6,2 8,2
1,2 3,2 5,2 7,2 9,2
0,3 2,3 4,3 6,3 8,3
1,3 3,3 5,3 7,3 9,3
Using this method you can write logic to determine the 6 neighbors of X,Y pretty trivially without storing extra info in each hex cell
Upvotes: 1