1
0

Use Util instead Random

This commit is contained in:
Victor 2014-03-16 15:57:21 +02:00
parent 121912a0b0
commit de82812ee8
2 changed files with 8 additions and 5 deletions

View File

@ -102,8 +102,12 @@ public class GameCanvas extends DoubleBufferedCanvas implements Runnable, Networ
} }
private void startGame(long seed) { private void startGame(long seed) {
Util.setRandomSeed(seed);
// Reinit background with same seed on server and client.
initBackground();
terrain = new Terrain(Constants.WIDTH); terrain = new Terrain(Constants.WIDTH);
terrain.generate(seed); terrain.generate();
serverTurret = new Turret(Turret.SERVER, terrain.getFirstBlockHeight(), terrain); serverTurret = new Turret(Turret.SERVER, terrain.getFirstBlockHeight(), terrain);
serverTurret.setTurretListener(serverTurretListener); serverTurret.setTurretListener(serverTurretListener);

View File

@ -65,15 +65,14 @@ public class Terrain implements Constants {
return blockHeights[blocksCount - 1]; return blockHeights[blocksCount - 1];
} }
public void generate(long seed) { public void generate() {
final Random rnd = new Random(seed);
final int maxHeight = HEIGHT / 2; final int maxHeight = HEIGHT / 2;
final int stepHeight = maxHeight / 75 * blockSize; final int stepHeight = maxHeight / 75 * blockSize;
int maxBlockHeight = 0; int maxBlockHeight = 0;
blockHeights[0] = rnd.nextInt(maxHeight); blockHeights[0] = Util.rand(maxHeight);
for (int i = 1; i < blocksCount; i++) { for (int i = 1; i < blocksCount; i++) {
int value = blockHeights[i - 1] + rnd.nextInt(2 * stepHeight + 1) - stepHeight; int value = blockHeights[i - 1] + Util.rand(-stepHeight, stepHeight);
if (value > maxHeight) value = maxHeight; if (value > maxHeight) value = maxHeight;
else if (value < 0) value = 0; else if (value < 0) value = 0;
blockHeights[i] = value; blockHeights[i] = value;