Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void treeTick(int i) {
- int tree = trees[i];
- // Die
- if (tree >= 100) {
- trees[i] = -1;
- return;
- }
- // Grow
- int shade = 0;
- int[] neighbours = getNeighbours(i);
- for (int nidx : neighbours) {
- int nx = grid.getCellX(nidx),
- ny = grid.getCellY(nidx);
- if (!(grid.isValid(nx,ny) && (grid.isFree(nx,ny) || grid.get(nx,ny) == Tree.COLOR_RGB)))
- continue;
- int neighbour = trees[nidx];
- if (neighbour > tree)
- shade += (neighbour - tree);
- }
- shade = Math.max((int)(shade * (100-tree) * .1),1);
- double chanceToGrow = 1.0 / shade;
- if (TestGame.rand.nextDouble() < chanceToGrow)
- ++trees[i];
- // Multiply
- if (TestGame.rand.nextDouble() < tree * .01) {
- int dx = (TestGame.rand.nextBoolean() ? 1 : -1),
- dy = (TestGame.rand.nextBoolean() ? 1 : -1);
- dx *= Math.pow(TestGame.rand.nextDouble(),3) * 10;
- dy *= Math.pow(TestGame.rand.nextDouble(),3) * 10;
- int newTree = i + dy * grid.getWidth();
- if (!EasyMath.isBetweenOrEqual(grid.getCellX(newTree) + dx,0,grid.getWidth()-1))
- return;
- newTree += dx;
- if (!EasyMath.isBetweenOrEqual(newTree,0,trees.length-1))
- return;
- if (trees[newTree] == -1 && grid.isFree(grid.getCellX(newTree),grid.getCellY(newTree)))
- trees[newTree] = 0;
- }
- }
- private int[] getNeighbours(int i) {
- int i1 = i - grid.getWidth(),
- i2 = i,
- i3 = i + grid.getWidth();
- return new int[]{i1 - 1,i1,i1 + 1,i2 - 1,i2,i2 + 1,i3 - 1,i3,i3 + 1};
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement