Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void update()
- {
- for (int i = verticalNumberOfCells - 1; i > 0; i--)
- {
- for (int j = 0; j < horizontalNumberOfCells; j++)
- {
- world[j][i].update(false);
- }
- }
- for (int y = verticalNumberOfCells - 1; y > 0; y--)
- {
- for (int x = 0; x < horizontalNumberOfCells; x++)
- {
- if (world[x][y].hasMoved) continue;
- if (world[x][y].state == 0 && world[x][y].state == 1) continue;
- if (canMove(world[x][y].state, x, y + 1))
- {
- move(x, y, x, y + 1);
- }
- }
- }
- }
- void move(int fromX, int fromY, int toX, int toY)
- {
- Cells otherSubstance = world[toX][toY];
- world[toX][toY] = world[fromX][fromY];
- world[fromX][fromY] = otherSubstance;
- world[fromX][fromY].hasMoved = true;
- world[toX][toY].hasMoved = true;
- world[fromX][fromY].velocityX = 0;
- world[fromX][fromY].velocityY = 0;
- if (toX > fromX)
- {
- world[toX][toY].velocityX = 1;
- } else if (toX < fromX)
- {
- world[toX][toY].velocityX = -1;
- } else
- {
- world[toX][toY].velocityX = 0;
- }
- if (toY > fromY)
- {
- world[toX][toY].velocityY = 1;
- } else if (toY < fromY)
- {
- world[toX][toY].velocityY = -1;
- } else
- {
- world[toX][toY].velocityY = 0;
- }
- }
- boolean canMove(int state, int positionX, int positionY)
- {
- if (positionX < 0 || positionX >= horizontalNumberOfCells || positionY < 0 || positionY >= verticalNumberOfCells) return false;
- int otherSubstance = world[positionX][positionY].state;
- if (state == 5) return (otherSubstance == 4);
- if (otherSubstance == 0) return true;
- if (state == 2 && otherSubstance == 3 && random(1f) < 0.5f) return true;
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement