예제 #1
0
 /**
  * @param CannonBall|Point $object
  * @return int
  */
 public function isHit($object)
 {
     if (!is_null($this->childTarget)) {
         $points = $this->childTarget->isHit($object);
         if ($points !== false) {
             return $points;
         }
     }
     $half = $this->width / 2;
     if ($object instanceof CannonBall) {
         foreach ($object->pixels as $pixel) {
             $pixelPosition = new Point($object->position->x + $pixel->x, $object->position->y + $pixel->y);
             if ($pixelPosition->x >= $this->center->x - $half && $pixelPosition->x <= $this->center->x + $half) {
                 return $this->points;
             }
         }
         return false;
     }
     if ($object->x >= $this->center->x - $half && $object->x <= $this->center->x + $half) {
         return $this->points;
     }
     return false;
 }
예제 #2
0
 /**
  * @param CannonBall|Point $object
  * @return int
  */
 public function isHit($object)
 {
     $points = false;
     if (!is_null($this->childTarget)) {
         $points = $this->childTarget->isHit($object);
     }
     $half = $this->width / 2;
     if ($object instanceof CannonBall) {
         foreach ($object->pixels as $pixel) {
             if (!$pixel->destroyedLocation) {
                 $currentPosition = Math::pointSum($object->position, $pixel);
                 $previousPosition = Math::pointSum($object->previousPosition, $pixel);
                 if ($currentPosition->y >= $this->center->y) {
                     $diff = Math::pointDiff($currentPosition, $previousPosition);
                     $slope = 0;
                     if ($diff->x != 0) {
                         $slope = -1 * ($diff->y / $diff->x);
                     }
                     $intercept = -1 * ($this->center->y - $previousPosition->y);
                     $intersectPoint = new Point($previousPosition->x + ($slope != 0 ? $intercept / $slope : 0), $this->center->y);
                     if ($intersectPoint->x >= $this->center->x - $half && $intersectPoint->x <= $this->center->x + $half) {
                         $pixel->destroyedLocation = $intersectPoint;
                         $pixel->destroyedColor = $this->color;
                         //echo '<pre>scored ' . $this->points . PHP_EOL;
                         $points = $points === false ? $this->points : $points + $this->points;
                     }
                 }
             }
         }
         return $points;
     }
     if ($object->x >= $this->center->x - $half && $object->x <= $this->center->x + $half) {
         return $this->points;
     }
     return false;
 }