how to make tiles walkable or not

Started by
20 comments, last by raptorstrike 19 years, 4 months ago
How do I make my tiles so the can not be walked on?
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
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.
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 tilesvoid 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 playerPlayers.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?
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;
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.
is there any other ideas or know of any tutorials on this and not the ones on this site.
flash-based games use a LOT of tile-based strategies. if u want a good reference for all these 'walkable'/'collidable' tiles.. check this out:

Tile based games
- To learn, we share... Give some to take some -
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
My RPG Newbie guide could be useful for what you need ;)

Newbie guide
RPG game programming and tutorials - Playable demo in Progress!
http://www.rrc2soft.com

This topic is closed to new replies.

Advertisement