私はこれについていくつかの助けを得ることを望んでいました。私が抱えている唯一の問題は、このコードが効率的ではないことです。基本的に私はタイルのセットを持っており、それらの周りのタイルの組み合わせに基づいてどのようなタイルをインスタンス化するかを決定する必要があります。下のコードでは、チェックしているタイルの位置がマップ内のキーです(exマップ[右]は右のタイルです、topLeftは左上の対角線に隣接するタイル、top2などはタイルです)その方向に2つのスペース)。当該if-elseif-elseステートメントを縮約して比較を保存する
コード:私は代わりに、それぞれの位置を確認0-2からそれに番号を割り当て、小数にベース3からその番号を変換するだろうと思ったので、私はテーブルを作った
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);
}
、その結果に対してswitch文を使用して、どのタイルをインスタンス化するかを決定します。数字コードは右端にあります。 しかし、それほど多くの組み合わせがありますが、私はより良い、またはより簡単な方法でなければならないように感じています。すべてのアイデアは高く評価されています!
Switchステートメントを使用するhttp://stackoverflow.com/questions/767821/is-else-if-faster-than-switch-case – LumbusterTick
私はチェックする必要があるため、switch文が私の問題を解決するとは思わないただ1つの特定の値ではなく、値の組み合わせの場合。 – user3754585
"インスタンス化([値]、t.Position、Quaternion.identity);" [value]の値を変数に設定すると、このように "Instantiate()"という行が1つしかありません。また、オペランドを繰り返しています(例:map [left] .Type == TileType.WALL)。各等価比較子が1回だけ出現する方法を見つけようとする。 –