1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
struct TileProperties
{
bool bWall; // true if the tile is a solid wall
int nGfxX; // X coord of the graphic
int nGfxY; // Y coord of the graphic
};
TileProperties myTileset[2] = {
{ true, 0, 0 }, // tile 0 = solid wall; graphics at 0,0
{ false, 32, 0 } // tile 1 = open space, graphics at 32,0
};
const int MAP_WD = 5; // 5x5 map for simplicity
const int MAP_HT = 5;
int map[MAP_WD * MAP_HT] = {
1,1,1,1,1,
1,1,0,0,1,
1,0,0,0,1,
1,0,1,0,1,
1,1,1,1,1
};
//================================
// now if you want to get the properties of a tile at a specific coords:
const TileProperties& GetTile(int x, int y)
{
return myTileset[ map[ (y*MAP_HT) + x ] ];
}
// now you can do things like:
if( GetTile(player.x, player.y).bWall )
{
// disallow the player to move here
}
//================================
// as for drawing, it's the same idea:
int y, x;
for(y = 0; y < MAP_HT; ++y)
{
for(x = 0; x < MAP_WD; ++x)
{
const TileProperties& t = GetTile(x,y);
DrawImage( /* draw 32x32 pixels from whatever tileset image you have. Use t.nGfxX
and t.nGfxY as the source coords for the image */ );
}
}
|