I have a tilemap and I want to add walls to it. I painted 16 images, one for each possible combination. For example:
- A lone wall tile would get a image that has walls around.
- A corner would get either four of the corner images.
- A T intersection would get one of the 4 T intersections.
- etc...
I could make 16 statements but I was wondering if there would be a better way to determine what tile needs to be placed. I have seen some code a long time ago where they used bit comparison for the walls but I don't know if that could be used for this specific problem.
Another option would be to use transparent walls and paint them where necessary then compare these and draw a connection image in the corner since they would overlap.
For clarification I paste my current method here. Which looks amateurish to me.
private void setCeilingTexture(Map map, int x, int y) {
if (map.isWall(x - 1, y) && map.isWall(x, y + 1) && map.isWall(x + 1, y) && map.isWall(x, y - 1))
{
((Wall)map.getTileMap()[x][y]).setCeilingTexture(randomTexture("ceiling_open"));
return;
}
//T intersections
if (!map.isWall(x - 1, y) && map.isWall(x, y + 1) && map.isWall(x + 1, y) && map.isWall(x, y - 1))
{
((Wall)map.getTileMap()[x][y]).setCeilingTexture(randomTexture("ceiling_leftclosed"));
return;
}
if (map.isWall(x - 1, y) && !map.isWall(x, y + 1) && map.isWall(x + 1, y) && map.isWall(x, y - 1))
{
((Wall)map.getTileMap()[x][y]).setCeilingTexture(randomTexture("ceiling_topclosed"));
return;
}
if (map.isWall(x - 1, y) && map.isWall(x, y + 1) && !map.isWall(x + 1, y) && map.isWall(x, y - 1))
{
((Wall)map.getTileMap()[x][y]).setCeilingTexture(randomTexture("ceiling_rightclosed"));
return;
}
if (map.isWall(x - 1, y) && map.isWall(x, y + 1) && map.isWall(x + 1, y) && !map.isWall(x, y - 1))
{
((Wall)map.getTileMap()[x][y]).setCeilingTexture(randomTexture("ceiling_bottomclosed"));
return;
}
//Single openings
if (map.isWall(x - 1, y) && !map.isWall(x, y + 1) && !map.isWall(x + 1, y) && !map.isWall(x, y - 1))
{
((Wall)map.getTileMap()[x][y]).setCeilingTexture(randomTexture("ceiling_leftopen"));
return;
}
if (!map.isWall(x - 1, y) && map.isWall(x, y + 1) && !map.isWall(x + 1, y) && !map.isWall(x, y - 1))
{
((Wall)map.getTileMap()[x][y]).setCeilingTexture(randomTexture("ceiling_topopen"));
return;
}
if (!map.isWall(x - 1, y) && !map.isWall(x, y + 1) && map.isWall(x + 1, y) && !map.isWall(x, y - 1))
{
((Wall)map.getTileMap()[x][y]).setCeilingTexture(randomTexture("ceiling_rightopen"));
return;
}
if (!map.isWall(x - 1, y) && !map.isWall(x, y + 1) && !map.isWall(x + 1, y) && map.isWall(x, y - 1))
{
((Wall)map.getTileMap()[x][y]).setCeilingTexture(randomTexture("ceiling_bottomopen"));
return;
}
//straight
if (map.isWall(x - 1, y) && !map.isWall(x, y + 1) && map.isWall(x + 1, y) && !map.isWall(x, y - 1))
{
((Wall)map.getTileMap()[x][y]).setCeilingTexture(randomTexture("ceiling_leftright"));
return;
}
if (!map.isWall(x - 1, y) && map.isWall(x, y + 1) && !map.isWall(x + 1, y) && map.isWall(x, y - 1))
{
((Wall)map.getTileMap()[x][y]).setCeilingTexture(randomTexture("ceiling_topbottom"));
return;
}
((Wall)map.getTileMap()[x][y]).setCeilingTexture(randomTexture("ceiling_single"));
//corners
if (map.isWall(x - 1, y) && map.isWall(x, y + 1) && !map.isWall(x + 1, y) && !map.isWall(x, y - 1))
{
((Wall)map.getTileMap()[x][y]).setCeilingTexture(randomTexture("ceiling_topleftopen"));
return;
}
if (!map.isWall(x - 1, y) && map.isWall(x, y + 1) && map.isWall(x + 1, y) && !map.isWall(x, y - 1))
{
((Wall)map.getTileMap()[x][y]).setCeilingTexture(randomTexture("ceiling_toprightopen"));
return;
}
if (!map.isWall(x - 1, y) && !map.isWall(x, y + 1) && map.isWall(x + 1, y) && map.isWall(x, y - 1))
{
((Wall)map.getTileMap()[x][y]).setCeilingTexture(randomTexture("ceiling_bottomrightopen"));
return;
}
if (map.isWall(x - 1, y) && !map.isWall(x, y + 1) && !map.isWall(x + 1, y) && map.isWall(x, y - 1))
{
((Wall)map.getTileMap()[x][y]).setCeilingTexture(randomTexture("ceiling_bottomleftopen"));
return;
}
}
Works perfectly, yet there must be a better way then this wall of statements.