2016-04-09 2 views
3

Ich hatte gehofft, etwas Hilfe dabei zu bekommen. Das einzige wirkliche Problem, das ich habe, ist, dass dieser Code nicht so effizient ist, wie er sein könnte. Grundsätzlich habe ich eine Reihe von Kacheln und muss entscheiden, welche Art von Kacheln basierend auf der Kombination von Kacheln um sie herum zu instanziieren. Im folgenden Code ist die Position der Kachel, die ich überprüfe, der Schlüssel in meiner Karte (ex map [rechts] ist die Kachel rechts, topLeft ist die Kachel neben diagonal links oben und top2 usw. ist die Kachel) zwei Räume in dieser Richtung).Eine if-elsif-else-Anweisung kondensieren, um Vergleiche zu speichern

Der Code in Frage:

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); 
        } 

ich einen Tisch gemacht hatte, weil ich dachte, dass ich stattdessen jede Position überprüfen würde, weisen Sie eine Zahl von 0-2 ist, konvertieren diese Zahl von der Basis 3 bis dezimal, Verwenden Sie dann eine switch-Anweisung für das Ergebnis, um zu bestimmen, welche Kachel instanziert werden soll. Die Nummerncodes sind ganz rechts. Aber es gibt so viele Kombinationen, ich habe das Gefühl, dass es einen besseren oder einfacheren Weg geben muss. Irgendwelche Ideen werden geschätzt!

Tabelle: Table of conditions

+2

Verwenden Sie eine Switch-Anweisung http://stackoverflow.com/questions/767821/is-else-if-faster-than-switch-case – LumbusterTick

+0

Ich glaube nicht, eine Switch-Anweisung würde mein Problem lösen, weil ich überprüfen muss für Kombinationen von Werten und nicht nur für einen bestimmten Wert. – user3754585

+1

Verschieben "Instantiate ([Wert], t.Position, Quaternion.identity);" bis zum Ende der Kontrollstruktur. Setzen Sie den Wert von [Wert] auf eine Variable, so haben Sie nur eine Zeile mit "Instantiate()". Außerdem wiederholen Sie Operanden (z. B. map [left] .Type == TileType.WALL). Versuchen Sie einen Weg zu finden, wo jeder Gleichheitsvergleicher nur einmal vorkommt. –

Antwort

0

einfach Um die Anzahl der caparison Operationen zu reduzieren, würde ich Ihre UND conditionals in einer baumartigen Weise vorschlagen arrangieren. Hier ist ein vereinfachtes Beispiel.

Hinweis: Ich verwende einzelne & für Lesbarkeit - ich meine logische UND.

beginnend mit:

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 

Baum Format: Nehmen Sie einen Zweig für jedes Element prüfen wir, so erste Prüfung für eine:

//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 
} 

Offensichtlich bekommt man nicht einen großen Vorteil mit nur 7 verschiedenen Bedingungen, aber diese Verbesserung wächst mit der Anzahl der Bedingungen, mit denen Sie beginnen.

Verwandte Themen