Advertisement
Guest User

Clean code suggestions requested

a guest
Jul 7th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. /*
  2. * This code is far from done and has tons of issues. All I currently want is suggestions on how to clean up the repetitive code for
  3. * each direction. This is just a snippet from a larger script that will generate a maze.
  4. *
  5. * Variable descriptions:
  6. * dungeon = 2D array of Tiles (either Hallway or Wall)
  7. * currentPos = Vector2Int (properties x and y) declaring the cursors current position in the dungeon
  8. */
  9. var currentTile = dungeon[currentPos.x, currentPos.y] as HallwayTile;
  10. var leftTile = currentPos.x > 0 ? dungeon[currentPos.x - 1, currentPos.y] : null;
  11. var upTile = currentPos.y > 0 ? dungeon[currentPos.x, currentPos.y + 1] : null;
  12. var rightTile = currentPos.x < dungeonArea.x - 1
  13.     ? dungeon[currentPos.x + 1, currentPos.y]
  14.     : null;
  15. var bottomTile = currentPos.y < dungeonArea.y - 1
  16.     ? dungeon[currentPos.x, currentPos.y - 1]
  17.     : null;
  18.  
  19. // Calculate backtracking
  20. var numberOfWalls = 0;
  21. if (leftTile is WallTile lwt1 && lwt1.IsBorderingMultiple())
  22. {
  23.     numberOfWalls++;
  24. }
  25. else if (leftTile is WallTile)
  26. {
  27.     canStartDigging = true;
  28.     currentPos.x--;
  29.     dungeon[currentPos.x - 1, currentPos.y] =
  30.         new WallTile(new Vector2Int(currentPos.x - 1, currentPos.y));
  31. }
  32. else if (leftTile is HallwayTile leftHall1 && leftHall1.Backtracked)
  33. {
  34.     numberOfWalls++;
  35. }
  36. else if (leftTile is HallwayTile leftHall2)
  37. {
  38.     currentPos.x--;
  39. }
  40.  
  41. if (upTile is WallTile uwt1 && uwt1.IsBorderingMultiple())
  42. {
  43.     numberOfWalls++;
  44. }
  45. else if (upTile is WallTile uwt2)
  46. {
  47.     canStartDigging = true;
  48.     currentPos.y++;
  49.     dungeon[currentPos.x, currentPos.y + 1] =
  50.         new WallTile(new Vector2Int(currentPos.x, currentPos.y + 1));
  51. }
  52. else if (upTile is HallwayTile upHall1 && upHall1.Backtracked)
  53. {
  54.     numberOfWalls++;
  55. }
  56. else if (upTile is HallwayTile upHall2)
  57. {
  58.     currentPos.y++;
  59. }
  60.  
  61. if (rightTile is WallTile rwt1 && rwt1.IsBorderingMultiple())
  62. {
  63.     numberOfWalls++;
  64. }
  65. else if (rightTile is WallTile rwt2)
  66. {
  67.     canStartDigging = true;
  68.     currentPos.x++;
  69.     dungeon[currentPos.x + 1, currentPos.y] =
  70.         new WallTile(new Vector2Int(currentPos.x + 1, currentPos.y));
  71. }
  72. else if (rightTile is HallwayTile rightHall1 && rightHall1.Backtracked)
  73. {
  74.     numberOfWalls++;
  75. }
  76. else if (rightTile is HallwayTile rightHall2)
  77. {
  78.     currentPos.x++;
  79. }
  80.  
  81. if (bottomTile is WallTile bwt1 && bwt1.IsBorderingMultiple())
  82. {
  83.     numberOfWalls++;
  84. }
  85. else if (bottomTile is WallTile bwt2)
  86. {
  87.     canStartDigging = true;
  88.     currentPos.y--;
  89.     dungeon[currentPos.x, currentPos.y - 1] =
  90.         new WallTile(new Vector2Int(currentPos.x, currentPos.y - 1));
  91. }
  92. else if (bottomTile is HallwayTile bottomHall1 && bottomHall1.Backtracked)
  93. {
  94.     numberOfWalls++;
  95. }
  96. else if (bottomTile is HallwayTile bottomHall2)
  97. {
  98.     currentPos.y--;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement