Advertisement
Rafpast

lineOnGrid2

Aug 13th, 2021
1,169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. void update()
  2. {
  3.   for (int i = verticalNumberOfCells - 1; i > 0; i--)
  4.   {
  5.     for (int j = 0; j < horizontalNumberOfCells; j++)
  6.     {
  7.       world[j][i].update(false);
  8.     }
  9.   }
  10.  
  11.   for (int y = verticalNumberOfCells - 1; y > 0; y--)
  12.   {
  13.     for (int x = 0; x < horizontalNumberOfCells; x++)
  14.     {
  15.       if (world[x][y].hasMoved) continue;
  16.       if (world[x][y].state == 0 && world[x][y].state == 1) continue;
  17.  
  18.       if (canMove(world[x][y].state, x, y + 1))
  19.       {
  20.         move(x, y, x, y + 1);
  21.       }
  22.     }
  23.   }
  24. }
  25.  
  26. void move(int fromX, int fromY, int toX, int toY)
  27. {
  28.   Cells otherSubstance = world[toX][toY];
  29.  
  30.   world[toX][toY] = world[fromX][fromY];
  31.   world[fromX][fromY] = otherSubstance;
  32.   world[fromX][fromY].hasMoved = true;
  33.   world[toX][toY].hasMoved = true;
  34.  
  35.   world[fromX][fromY].velocityX = 0;
  36.   world[fromX][fromY].velocityY = 0;
  37.  
  38.   if (toX > fromX)
  39.   {
  40.     world[toX][toY].velocityX = 1;
  41.   } else if (toX < fromX)
  42.   {
  43.     world[toX][toY].velocityX = -1;
  44.   } else
  45.   {
  46.     world[toX][toY].velocityX = 0;
  47.   }
  48.  
  49.   if (toY > fromY)
  50.   {
  51.     world[toX][toY].velocityY = 1;
  52.   } else if (toY < fromY)
  53.   {
  54.     world[toX][toY].velocityY = -1;
  55.   } else
  56.   {
  57.     world[toX][toY].velocityY = 0;
  58.   }
  59. }
  60.  
  61.  
  62. boolean canMove(int state, int positionX, int positionY)
  63. {
  64.   if (positionX < 0 || positionX >= horizontalNumberOfCells || positionY < 0 || positionY >= verticalNumberOfCells) return false;
  65.  
  66.   int otherSubstance = world[positionX][positionY].state;
  67.  
  68.   if (state == 5) return (otherSubstance == 4);
  69.  
  70.   if (otherSubstance == 0) return true;
  71.  
  72.   if (state == 2 && otherSubstance == 3 && random(1f) < 0.5f) return true;
  73.  
  74.   return false;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement