/** * @param MovableInterface $fish * @param Position $current * * @return Position */ protected function moveFish(MovableInterface $fish, Position $current) { $mov = $fish->getMovement(); $next = $current->calculateNewPoint($mov); $destination = $this->worldMap->get($next); if ($destination === null) { // Destinazione vuota: mi sposto $this->worldMap->put($fish, $next); $this->worldMap->put(null, $current); $current = $next; } else { // Destinazione occupata: se posso mangio if ($fish instanceof PredatorInterface and $destination instanceof FoodInterface) { $fish->eat($destination); $this->worldMap->put($fish, $next); $this->worldMap->put(null, $current); $current = $next; unset($destination); } } return $current; }
/** * @param Position $pos * Il metodo fixPosition si occupa di far * si che il nostro mondo sia Toroidale. */ public function fixPosition(Position $pos) { if ($pos->getX() < 0) { $pos->setX($this->w + $pos->getX()); } elseif ($pos->getX() >= $this->w) { $pos->setX($pos->getX() - $this->w); } if ($pos->getY() < 0) { $pos->setY($this->h + $pos->getY()); } elseif ($pos->getY() >= $this->h) { $pos->setY($pos->getY() - $this->h); } }
/** * Restituisce una posizione libera (se disponibile) nei dintorni * * @param Position $position * @return null|Position Null * * @internal param FishInterface $fish */ public function getNearFreePosition(Position $position) { $pos = new Position($position->getX(), $position->getY()); $x = $pos->getX() - 1; $y = $pos->getY() - 1; for ($i = 0; $i < 3; $i++) { $pos->setX($x + $i); for ($j = 0; $j < 3; $j++) { $pos->setY($y + $j); if ($this->isFreePosition($pos) == true) { return new Position($pos->getX(), $pos->getY()); } } } return null; }