Пример #1
0
  /**
   * Base tick method for the level. Updates all entities, tiles, and player. Has slow repopulation
   * algorithm
   */
  public void tick() {
    player.tick();
    List<Entity> toTick = new ArrayList<Entity>();
    int lr = 7;
    for (int x = -lr; x < lr; x++) {
      for (int y = -lr; y < lr; y++) {
        Tile tile = getTile(player.x + x, player.z + y);
        tile.addToTick(toTick);
        tile.tick();
      }
    }

    for (int i = 0; i < toTick.size(); i++) {
      Entity e = toTick.get(i);
      e.tick(this);
    }
    tickcount++;
    if (tickcount % 1800 == 0) {
      System.out.println("Adding entity to world");
      if (r != null) {
        int rx = r.nextInt(w);
        int ry = r.nextInt(h);

        if (skillNoise.noise(rx / 80d, ry / 80d) > 0.0) {
          SmallMob mob = new SmallMob();
          if (!tiles[rx + ry * w].blocks(mob)) {
            tiles[rx + ry * w].addEntity(r.nextInt(4), mob);
          }
        }
      }
    }
  }
Пример #2
0
  /**
   * Creates a new level instance with a randomly generated overworld
   *
   * @param player The player to be spawned into the world
   * @param layer The layer of the level
   * @param loading Indicates whether or not the map is being loaded from disk.
   * @param log A gui where output from the gen can be displayed
   */
  public Level(Player player, int layer, boolean loading, GuiLog log) {
    r = new Random();

    OctavesNoiseGen noise = new OctavesNoiseGen(r, 8);
    SimplexNoise flowerDensity = new SimplexNoise(r);
    SimplexNoise treeDensity = new SimplexNoise(r);
    skillNoise = new SimplexNoise(r);

    w = 512;
    h = 512;

    tiles = new Tile[w * h];

    this.layer = layer;

    int xSpawn = 0;
    int ySpawn = 0;

    int tilecount = 0;
    double average = 0.0;

    log.addMessage("Building world");
    for (int x = 0; x < w; x++) {
      for (int y = 0; y < h; y++) {
        Tile tile;
        double noiseval = noise.noise(x / 140d, y / 140d);
        if (noiseval >= 0.0) {
          int flowerchance = (int) Math.abs(flowerDensity.noise(x / 2D, y / 2D) * 16) + 1;
          int treechance = (int) (treeDensity.noise(x / 8D, y / 8D) * 32d);
          if (treechance < 1) {
            treechance = 1;
          }
          if (r.nextInt(treechance) == 1) {
            tile = new BlockingTile(Walls.tree);
          } else if (r.nextInt(flowerchance) == 0) {
            tile = new Tile(Walls.grassFlowers);
          } else {
            tile = new Tile(Walls.grass);
          }
        } else if (noiseval < 0.0 && noiseval >= -0.2) {
          tile = new Tile(Walls.sand);
        } else if (noiseval < -0.2 && noiseval >= -0.4) {
          tile = new BlockingTile(Walls.water);
        } else {
          tile = new BlockingTile(Walls.ocean);
        }

        average += noiseval;

        tile.init(this, x, y);
        tiles[x + y * w] = tile;
        tilecount++;
      }
    }
    log.addMessage("Spawning entities");
    /*for (int x = 0; x < w; x++)
    {
    	for (int y = 0; y < h; y++)
    	{
    		double noiseval = skillNoise.noise(x / 80d, y / 80d);

    		if (noiseval > 0.0)
    		{
    			double spawnChance = 16 - (noiseval * 16);
    			spawnChance += 16;
    			if (spawnChance >= 1)
    			{
    				if (r.nextInt((int) spawnChance) == 0)
    				{
    					SmallMob mob = new SmallMob();
    					if (!tiles[x + y * w].blocks(mob))
    					{
    						tiles[x + y * w].addEntity(r.nextInt(4), mob);
    					}
    				}
    			}
    		}
    	}
    }*/

    log.addMessage("Finding spawn");
    if (player == null) player = new Player(this, 0, 0);

    int rx = r.nextInt(512);
    int ry = r.nextInt(512);

    while (getTile(rx, ry).blocks(player) && skillNoise.noise(rx / 80d, ry / 80d) < 0.0) {
      rx = r.nextInt(512);
      ry = r.nextInt(512);
    }

    player.x = player.l_x = rx;
    player.z = player.l_z = ry;

    this.player = player;
  }