Level Generation API updated

This commit is contained in:
Shoghi Cervantes Pueyo 2013-05-18 15:22:07 +02:00
parent c408ee07b1
commit 7297f8d2c0
3 changed files with 26 additions and 18 deletions

View File

@ -27,12 +27,14 @@ the Free Software Foundation, either version 3 of the License, or
interface LevelGenerator{
public function __construct(array $options = array());
public function init(Level $level, Random $random);
public function generateChunk(Level $level, $chunkX, $chunkY, $chunkZ, Random $random);
public function generateChunk($chunkX, $chunkY, $chunkZ);
public function populateChunk(Level $level, $chunkX, $chunkY, $chunkZ, Random $random);
public function populateChunk($chunkX, $chunkY, $chunkZ);
public function populateLevel(Level $level, Random $random);
public function populateLevel();
public function getSpawn(Random $random);
public function getSpawn();
}

View File

@ -30,7 +30,7 @@ require_once("LevelGenerator.php");
/***REM_END***/
class SuperflatGenerator implements LevelGenerator{
private $config, $structure, $chunks, $options, $floorLevel;
private $level, $random, $structure, $chunks, $options, $floorLevel;
public function __construct(array $options = array()){
$this->preset = "2;7,2x3,2;1;spawn(radius=10 block=24)";
@ -101,16 +101,21 @@ class SuperflatGenerator implements LevelGenerator{
$this->options[$option] = $params;
}
}
public function init(Level $level, Random $random){
$this->level = $level;
$this->random = $random;
}
public function generateChunk(Level $level, $chunkX, $chunkY, $chunkZ, Random $random){
$level->setMiniChunk($chunkX, $chunkZ, $chunkY, $this->chunks[$chunkY]);
public function generateChunk($chunkX, $chunkY, $chunkZ){
$this->level->setMiniChunk($chunkX, $chunkZ, $chunkY, $this->chunks[$chunkY]);
}
public function populateChunk(Level $level, $chunkX, $chunkY, $chunkZ, Random $random){
public function populateChunk($chunkX, $chunkY, $chunkZ){
}
public function populateLevel(Level $level, Random $random){
public function populateLevel(){
if(isset($this->options["spawn"])){
$spawn = array(10, new SandstoneBlock());
if(isset($this->options["spawn"]["radius"])){
@ -128,7 +133,7 @@ class SuperflatGenerator implements LevelGenerator{
for($x = $start; $x <= $end; ++$x){
for($z = $start; $z <= $end; ++$z){
if(floor(sqrt(pow($x - 128, 2) + pow($z - 128, 2))) <= $spawn[0]){
$level->setBlockRaw(new Vector3($x, $this->floorLevel - 1, $z), $spawn[1]);
$this->level->setBlockRaw(new Vector3($x, $this->floorLevel - 1, $z), $spawn[1]);
}
}
}
@ -140,14 +145,14 @@ class SuperflatGenerator implements LevelGenerator{
$treecount = intval($this->options["spawn"]["treecount"]);
}
for($t = 0; $t < $treecount; ++$t){
$centerX = $random->nextRange(0, 256);
$centerZ = $random->nextRange(0, 256);
TreeObject::growTree($level, new Vector3($centerX, $this->floorLevel, $centerZ), $random->nextRange(0,3));
$centerX = $this->random->nextRange(0, 256);
$centerZ = $this->random->nextRange(0, 256);
TreeObject::growTree($this->level, new Vector3($centerX, $this->floorLevel, $centerZ), $this->random->nextRange(0,3));
}
}
}
public function getSpawn(Random $random){
public function getSpawn(){
return new Vector3(128, $this->floorLevel, 128);
}
}

View File

@ -51,17 +51,18 @@ class WorldGenerator{
}
public function generate(){
$this->generator->init($this->level, $this->random);
for($Z = 0; $Z < $this->width; ++$Z){
for($X = 0; $X < $this->width; ++$X){
for($Y = 0; $Y < $this->height; ++$Y){
$this->generator->generateChunk($this->level, $X, $Y, $Z, $this->random);
$this->generator->populateChunk($this->level, $X, $Y, $Z, $this->random);
$this->generator->generateChunk($X, $Y, $Z);
$this->generator->populateChunk($X, $Y, $Z);
}
}
console("[NOTICE] Generating level ".ceil((($Z + 1)/$this->width) * 100)."%");
}
$this->generator->populateLevel($this->level, $this->random);
$this->level->setSpawn($this->generator->getSpawn($this->random));
$this->generator->populateLevel();
$this->level->setSpawn($this->generator->getSpawn());
$this->level->save(true);
}