凝结如果
我希望能得到一些帮助。 我遇到的唯一真正的问题是这个代码并不像它可能的那样高效。 基本上我有一套瓷砖,需要根据它们周围的瓷砖组合来决定实例化哪种瓷砖。 在下面的代码中,我正在检查的瓦片的位置是我的地图中的关键字(例如,[右侧]是右侧的瓦片,topLeft是与左上角相邻的瓦片,top2等于瓦片在那个方向上有两个空间)。
有问题的代码:
if (map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.NOTHING && map[bottom].Type == TileType.NOTHING)
{
Instantiate(wallEdgeTopLeftInternalCornerTile, t.Position, Quaternion.identity);
}
else if (map[left].Type == TileType.NOTHING && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.NOTHING)
{
Instantiate(wallEdgeTopRightInternalCornerTile, t.Position, Quaternion.identity);
}
else if (map[left].Type == TileType.WALL && map[top].Type == TileType.NOTHING && map[right].Type == TileType.NOTHING && map[bottom].Type == TileType.WALL)
{
Instantiate(wallEdgeBottomLeftInternalCornerTile, t.Position, Quaternion.identity);
}
else if (map[left].Type == TileType.NOTHING && map[top].Type == TileType.NOTHING && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL)
{
Instantiate(wallEdgeBottomRightInternalCornerTile, t.Position, Quaternion.identity);
}
else if (map[left].Type == TileType.WALL && map[top].Type == TileType.FLOOR && map[right].Type == TileType.FLOOR && map[bottom].Type == TileType.WALL)
{
Instantiate(wallEdgeCornerTopRightTile, t.Position, Quaternion.identity);
}
else if (map[left].Type == TileType.FLOOR && map[top].Type == TileType.FLOOR && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL)
{
Instantiate(wallEdgeCornerTopLeftTile, t.Position, Quaternion.identity);
}
else if (map[left].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[topRight].Type == TileType.NOTHING)
{
Instantiate(wallEdgeCornerBottomLeftTile, t.Position, Quaternion.identity);
}
else if (map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.FLOOR && map[bottom].Type == TileType.WALL && map[topLeft].Type == TileType.NOTHING)
{
Instantiate(wallEdgeCornerBottomRightTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.NOTHING && map[left2].Type == TileType.NOTHING) || (map[left].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.WALL && map[left2].Type == TileType.FLOOR && map[bottom2].Type == TileType.WALL))
{
Instantiate(wallTopLeftTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.NOTHING && map[right2].Type == TileType.NOTHING) || (map[right].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[left].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.WALL && map[right2].Type == TileType.FLOOR && map[bottom2].Type == TileType.WALL))
{
Instantiate(wallTopRightTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.FLOOR && map[top2].Type == TileType.WALL && map[left2].Type == TileType.NOTHING) || (map[left].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.FLOOR && map[top2].Type == TileType.WALL && map[left2].Type == TileType.FLOOR && map[bottom2].Type == TileType.FLOOR))
{
Instantiate(wallBottomLeftTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.FLOOR && map[top2].Type == TileType.WALL && map[right2].Type == TileType.NOTHING) || (map[right].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[left].Type == TileType.WALL && map[bottom].Type == TileType.FLOOR && map[top2].Type == TileType.WALL && map[right2].Type == TileType.FLOOR && map[bottom2].Type == TileType.FLOOR))
{
Instantiate(wallBottomRightTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[left2].Type == TileType.NOTHING && map[bottom2].Type == TileType.FLOOR) || (map[left].Type == TileType.FLOOR && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[left2].Type == TileType.FLOOR && map[bottom2].Type == TileType.FLOOR))
{
Instantiate(wallMidLeftTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[right2].Type == TileType.NOTHING && map[bottom2].Type == TileType.FLOOR) || (map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.FLOOR && map[bottom].Type == TileType.WALL && map[right2].Type == TileType.FLOOR && map[bottom2].Type == TileType.FLOOR))
{
Instantiate(wallMidRightTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.NOTHING && map[bottom2].Type == TileType.WALL))
{
Instantiate(wallTopTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.FLOOR && map[top2].Type == TileType.WALL && map[bottom2].Type == TileType.FLOOR))
{
Instantiate(wallBottomTile, t.Position, Quaternion.identity);
}
else if (map[top].Type == TileType.NOTHING && map[bottom].Type == TileType.WALL)
{
Instantiate(wallEdgeBottomTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.NOTHING && map[right].Type == TileType.FLOOR) || (map[left].Type == TileType.NOTHING && map[right].Type == TileType.WALL && map[top].Type != TileType.NOTHING))
{
Instantiate(wallEdgeRightTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.FLOOR && map[right].Type == TileType.NOTHING) || (map[left].Type == TileType.WALL && map[right].Type == TileType.NOTHING && map[top].Type != TileType.NOTHING))
{
Instantiate(wallEdgeLeftTile, t.Position, Quaternion.identity);
}
else if (map[bottom].Type == TileType.NOTHING && map[top].Type == TileType.FLOOR)
{
Instantiate(wallEdgeTopTile, t.Position, Quaternion.identity);
}
else if ((map[left].Type == TileType.WALL && map[top].Type == TileType.WALL && map[right].Type == TileType.WALL && map[bottom].Type == TileType.WALL && map[top2].Type == TileType.WALL && map[bottom2].Type == TileType.FLOOR))
{
Instantiate(wallMiddleTile, t.Position, Quaternion.identity);
}
else
{
// Should never get here, so if a white Tile is seen, something went wrong!
Instantiate(whiteTile, t.Position, Quaternion.identity);
}
我创建了一个表格,因为我认为我应该检查每个位置,为其分配一个0-2的数字,将该数字从基数3转换为小数,然后在结果上使用switch语句来确定要实例化的瓦片。 数字代码在右边。 但有很多组合,我觉得必须有更好或更简单的方法。 任何想法都是值得赞赏的!
表:条件表
为了简单地减少优化操作的次数,我建议你安排你的AND条件为树型。 这里是一个简单的例子。
注意:我正在使用单个&可读性 - 我的意思是逻辑AND。
从...开始:
if(a&b&c) {functionW(a);return;} //1 comparison, 2 ANDops
else if(a&b&!c) {functionX(a);return;}//2 comparison, 4 ANDops
else if(a&!b&!c) {functionX(b);return;}//3 comparison, 6 ANDops
else if(a&!b&c) {functionX(c);return;}//4 comparison, 8 ANDops
else if(!&a&b&c) {functionY(b);return}//5 comparison, 10 ANDops
else if(!a&!b&c){functionZ(c);return}//6 comparison, 12 ANDops
else if(!b&!e){functionZ(c);return;}//7 comparison, 13 ANDops
树格式:为我们正在检查的每个元素分支,所以首先检查一下:
//out here,first, go all the statements, where it doesn't mater what value a has. We haveonly one condition in our example, but if we had many, we could create a separate "tree" out here.
if(!b&!e){functionZ(c);return;}//1 comparison, 1 ANDops
//now we start a branch based on a
if(a)
{//in here go all the if statements where a is true
//now we create onther tree branch based on b
if(b)
{
if(!c){ functionW(a); return;}// total 4 comparisons to get here. 1 ANDops
}
else
{//all option where b is false
if(c) {functionX(c); return;}// total 4 comparisons to get here, 1 ANDops
else {functionX(b); return;}// total 4 comparisons to get here, 1 ANDops
}
}
else
{//in here go all the if statements where a is false (there are only two, so no need for a branch)
if(b&c) {functionY(b);return}// total 3 comparisons to get here. 2 ANDops
else if(!b&c){functionZ(c);return}// total 3 comparisons to get here, 3 ANDops
}
显然,只有7个不同的条件,你不会获得巨大的优势,但随着你开始的条件数量的增加,这种改进会增加。
链接地址: http://www.djcxy.com/p/63659.html上一篇: Condensing an if