/** * Renvoie les hexagones dans l'ordre d'un balayage * @return HexaCollection */ public function buildHexas() { $this->hexas = new HexaCollection(); $debutLigne = $this->coinSupGauche(); $rivieres = array(); for ($y = 1; $y <= $this->hauteur; $y++) { $current = $debutLigne; for ($x = 0; $x < $this->largeur; $x++) { $this->hexas->ajout($current); // Rivères foreach ($current->getRivieres() as $riviere) { /** @var Riviere $riviere */ if (!isset($rivieres[$riviere->getId()])) { $this->rivieres->ajout($riviere); $rivieres[$riviere->getId()] = true; } } // Visibles $this->ajoutVisiblesHexa($current); $current = $current->getVoisin(0); } if (fmod($debutLigne->getY(), 2) == 1) { // Y impair $debutLigne = $debutLigne->getVoisin(5); } else { // Y pair $debutLigne = $debutLigne->getVoisin(4); } } }
/** * Crée les rivieres de la carte */ public function creerRivieres() { mt_srand($this->getRandRivieres()); // Création d'une grille de frontières avec voisinages de proche en proche $frontieres = $this->getHexas()->getFrontieres(); $frontieres->uasort('fr\\gilman\\nj\\common\\bb\\collection\\FrontiereCollection::triParHauteur'); // Création des sources de rivières $sources = new RiviereCollection(); $rivieres = new RiviereCollection(); $index = 0; foreach ($frontieres as $frontiere) { /** @var Frontiere $frontiere */ if ($frontiere->bordDeMer()) { break; } $proba = 1 / 40 * ($frontiere->getHauteur() / 15); if (mt_rand() / mt_getrandmax() <= $proba) { $riviere = new Riviere(); $riviere->setSource($frontiere->getIndex()); $riviere->setFrontiere($frontiere); $sources->ajout($riviere); $rivieres->ajout($riviere); $index++; } } // Faire couler les rivieres foreach ($sources as $index => $source) { /** @var Riviere $source */ /** @var Frontiere $prochain */ $frontiereCourante = $source->getFrontiere(); $coule = true; $precedent = null; while ($coule) { $prochain = $frontiereCourante->prochainCoulage(); if (!is_null($prochain)) { // La rivière se jette dans un fleuve if (!is_null($prochain->getRiviere()) && $prochain->getRiviere()->getSource() != $frontiereCourante->getRiviere()->getSource()) { break; } $riviere = new Riviere(); $riviere->setSource($source->getFrontiere()->getIndex()); $riviere->setFrontiere($prochain); if (rand(1, 10) == 1) { $riviere->setGue(1); } $rivieres->ajout($riviere); $frontiereCourante = $prochain; // La rivière se jette dans la mer if ($prochain->bordDeMer()) { $coule = false; } } else { $coule = false; } } } $rivieres->save(); }
/** * Ajoute une rivière au cache sans passer par une requête * @param Riviere $riviere */ public function addRiviereToCache(Riviere $riviere) { if (is_null($this->cacheRivieres)) { $this->cacheRivieres = new RiviereCollection(); } $this->cacheRivieres->ajout($riviere); }