Hello. I’m working on a 2D tile based game. Right now I’m trying to get it so that walls draw so that they connect correctly, i.e. I have corner images, vertical and horizontal images, end piece images, and so on. The problem arises when I try to determine which image to draw. I have it working but I have the feeling that there is a better way to do it, and I just can’t seem to find it. So help, suggestions, tips, etc. are greatly welcome. This isn’t really a problem so I understand if nobody wants to answer, or if I posted in the wrong area, or something.
The level, is loaded from a text file into a char array containing chars that represent different map elements, like U for user, W for wall (The position of the walls don’t change during the level). When the game is drawn the type of wall is checked by this method. Would getting the types at start time and storing them be better so I don’t have to check every time the walls are drawn?
public int getWallType(int row,int col)
{
boolean r = getType(row,col + 1) == WALL, // Space to the right is a wall
l = getType(row,col - 1) == WALL, // Space to the left is a wall
a = getType(row - 1,col) == WALL, // Space above is a wall
b = getType(row + 1,col) == WALL; // Space below is a wall
if(r && l && !a && !b)
return WH; // Horizontal wall piece -
else if(a && b && !r && !l)
return WV; // Vertical wall piece |
else if(b && !a && !r && !l)
return WTE; // Top end of a wall ^
else if(l && !r && !a && !b)
return WRE; // Right end of a wall >
else if(a && !b && !r && !l)
return WBE; // Bottom end of a wall V
else if(r && !l && !a && !b)
return WLE; // Left end of a wall <
else if(r && b && !l && !a)
return WUL; // Upper left corner of a wall
else if(l && b && !r && !a)
return WUR; // Upper right corner of a wall
else if(r && a && !l && !b)
return WLL; // Lower left corner of a wall
else if(l && a && !r && !b)
return WLR; // Lower right corner of a wall
return WM; // Middle wall piece +
}