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
|
// the Tile structure:
struct Tile
{
bool IsWall;
int GraphicsX;
int GraphicsY;
int AnyOtherInfoAboutThisTileYouNeed;
};
// The "tileset"
Tile tileset[] = {
{ false, 0, 0, whatever } // the floor
{ false, 1, 0, whatever } // another floor that looks different
{ true, 2, 0, whatever } // a wall
{ true, 0, 1, whatever } // another wall that looks different
};
// The map (note I use a 2D array here because you are probably using them.
// it should be noted that you should not use them and instead use a
// container class which simulates a multidimentional array. See this thread
// for an example class:
// http://cplusplus.com/forum/articles/17108/#msg85595
int map[5][5] = {
{ 2,2,2,2,2 },
{ 2,0,0,0,2 },
{ 2,1,3,1,2 },
{ 2,0,0,0,2 },
{ 2,2,2,2,2 } };
// now to check to see if the player is walking into a wall, you just check the tile data:
newplayerpos = playerpos;
if( player_wants_to_move_left ) newplayerpos.x -= 1;
// ... do same for right, up, down
int tileid = map[ newplayerpos.y ][ newplayerpos.x ];
const Tile& tile = tileset[ tileid ];
if(tile.IsWall)
{
// player trying to walk onto a wall, don't let them
}
else
{
// player walking onto OK ground.
playerpos = newplayerpos; // allow the move
}
|