Jump to content
  • Advertisement
Sign in to follow this  
kingpinzs

how to make tiles walkable or not

This topic is 4963 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Probably you have your tilemap stored in a bidimensional array. Just make a second array of bytes and in each byte set an 1 or a 0. 1 meaning you can pass, 0 means you cant.

Also, you can use the values in the byte to give the terrain a difficulty or a type. So you may set the byte to 4 and that means water, 8 means void, 13 means a door, and so on.

Luck!
Guimo

Share this post


Link to post
Share on other sites
The basic idea would be via your tile map you should have some sort of tile IDs that make up your world.

Tilemap
1,1,1,1,1,1,1,
1,0,0,1,0,0,1,
1,0,0,1,0,0,1,
1,0,0,0,0,0,1,
1,1,1,1,1,1,1

Check the player current x,y position in the world and find out which tile ID on the map the character is on top of. Above 0 would be walkable and 1 is a wall. If the character moves in the direction off of a walkable tile towards a wall, dont allow the player to move that direction.

Share this post


Link to post
Share on other sites
so here is my map

char map[world_sizey][world_sizex] = {
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},

ans 2 is the walls.


this draws my tiles
void draw_tiles(void)
{
int tile;
int x;
int y;
RECT tile_src;

for(y=0; y<Screen_sizey; y++)
{for(x=0; x<Screen_sizex; x++)
{ tile = map[y][x];

scroll_x =(world_camerax/Tile_size);
scroll_y =(world_cameray/Tile_size);

offset_x = scroll_x & (Tile_size - 1);
offset_y = scroll_y & (Tile_size - 1);

tile_src.left =(tile - 1) *Tile_size;
tile_src.top = 0;
tile_src.right= tile * Tile_size;
tile_src.bottom=Tile_size;

dstr.left= (x*Tile_size) - offset_x;
dstr.top= (y*Tile_size) - offset_y ;
dstr.right = (x*Tile_size) - offset_x + tile_src.right ;
dstr.bottom = (y*Tile_size) - offset_y + tile_src.bottom ;

lpBackBuffer->Blt(&dstr,g_pddTile,&tile_src,DDBLT_WAIT,NULL);

} }}




this draws the player
Players

.width = 64;
Players

.hight = 64;
//the bug
r.left = Players

.cl;
r.top = Players

.ct;
r.right = Players

.cr;
r.bottom = Players

.cb;

dstr.left= Players

.x;
dstr.top= Players

.y;
dstr.right=Players

.x + (r.right - r.left);
dstr.bottom = Players

.y + (r.bottom - r.top);

lpBackBuffer->Blt(&dstr,g_pddbug,&r,DDBLT_WAIT|DDBLT_KEYSRC ,NULL);



so how do I find out what tile they are on or going onto?

Share this post


Link to post
Share on other sites
i did domething like this

struct Tile
{
int x;
int y;
int w;
int h;
};

std:vector<Tile> SolidTiles;

//Use a text file to say if the tile is walkable or not if it is //not add it to SolidTiles. . .

bool WalkAble(int x, int y)
{
for(int i(0); i < SolidTiles.size; i++)
{
if(x >= SolidTiles.x && x <= SolidTiles.x + SolidTiles.w && y >= SolidTiles.y && y <= SolidTiles.y + SolidTiles.h)
return false;
}
return true;
}

//Then if you want a player to move go like this. . .
cPlayer = new cPlayer();
if(WalkAble(Player->X + 3, Player->Y)) Player->X += 3;

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Lets say the player wants to move up in your world, check if he can move to the up-most square before you move him, simple as that really. Just remember to check before you move him. Capture the key press, check then move.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I was thinking about that not so long ago, so I'll just throw in my little and regrettably unexperienced advice...

I thought about doing a tile class that had an array
char canCome[4] = { u, l, d, r};
each representing a direction from which you can try to come into the tile.

So, quickly written, if the player press DOWN, you check if canCome[0] of the tile under the current position is 1, and you walk if it's true.

Now, my apprehension is that it uses a lot of memory to use a class like this... But I also thought about heritage, so it's easy to make a Poison Tile or an Action Tile with this concept. I guess you have to decide what's gonna be the most useful for you.

Did any of this make sense? Hope so! :D

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!