Ejemplo n.º 1
0
 /**
  * @param CannonBall $cannonBall
  * @param Point $previousPoint
  * @return bool
  */
 public function isHit($cannonBall, $previousPoint)
 {
     //$cannonBall->pixels = array_values($cannonBall->pixels);
     $total = count($cannonBall->pixels);
     for ($index = 0; $index < $total; $index++) {
         //if(!isset($cannonBall->pixels[]))
         $pixel = $cannonBall->pixels[$index];
         $pixelPosition = new Point($cannonBall->position->x + $pixel->x, $cannonBall->position->y + $pixel->y);
         $hit = $this->body->isHit($pixelPosition);
         if ($hit !== false) {
             $pixelLevels = $this->bodyHeight <= $this->bodyWidth ? $this->bodyHeight : $this->bodyWidth;
             $currentPixelLevel = 0;
             $validHit = false;
             while (!$validHit && $currentPixelLevel <= $pixelLevels) {
                 $body_a_x = $this->body->a->x + $currentPixelLevel;
                 $body_b_y = $this->body->b->y + $currentPixelLevel;
                 $body_c_x = $this->body->c->x - $currentPixelLevel;
                 if ($hit->x >= $previousPoint->x) {
                     $x = $body_a_x <= $previousPoint->x ? $previousPoint->x : $body_a_x;
                     $y = $body_b_y <= $previousPoint->y ? $previousPoint->y : $body_b_y;
                     $x = $hit->x - ($hit->x - $x);
                     $y = $hit->y - ($hit->y - $y);
                 } else {
                     $x = $body_c_x >= $previousPoint->x ? $previousPoint->x : $body_c_x;
                     $y = $body_b_y <= $previousPoint->y ? $previousPoint->y : $body_b_y;
                     $x = $hit->x - ($hit->x - $x);
                     $y = $hit->y - ($hit->y - $y);
                 }
                 $x = (int) round($x);
                 $y = (int) round($y);
                 $validHit = true;
                 if (isset($this->destroyedParts[$x])) {
                     foreach ($this->destroyedParts[$x] as $destroyedY) {
                         if ($destroyedY == $y) {
                             $currentPixelLevel++;
                             $validHit = false;
                             break;
                         }
                     }
                 }
                 if ($validHit) {
                     $this->destroyedParts[$x][] = $y;
                     array_splice($cannonBall->pixels, $index, 1);
                     $index--;
                     $total--;
                 }
             }
             if (count($cannonBall->pixels) <= 0) {
                 return true;
             }
         }
     }
     return false;
 }