function pixelCannons($force, $angle, &$data, $userName = '') { //INPUT //$force = 7.5; // 0-10 //$angle = 45; // degree 1-90 //CONFIG TODO: MAKE IT CONFIGURABLE THROUGH HIPCHAT $maxWidth = 800; $maxHeight = 400; $baseSpeed = 10; //pixels $timeIncrement = 0.05; $gravity = 9.81; $colors = ['red' => '13369344', 'yellow' => '13421568', 'cyan' => '52428', 'green' => '52224', 'blue' => '204', 'purple' => '6684876', 'pink' => '13369446', 'back' => '657930']; $cannon = new Cannon(new Point(10, 399), Cannon::BODY_TYPE_SQUARE); $cannon->setAngle($angle); if (empty($data['current.target'])) { $data['current.target'] = $data['defaults']['current.target']; } $center = new Point($data['current.target']['x'], $data['current.target']['y']); $target = new Target($center, 100, 10, $colors['yellow'], new Target($center, 80, 20, $colors['green'], new Target($center, 60, 40, $colors['blue'], new Target($center, 40, 80, $colors['purple'], new Target($center, 10, 160, $colors['back'], new Target($center, 4, 320, $colors['red'])))))); /*$target1 = new Cannon( new Point(50, 399), Cannon::BODY_TYPE_SQUARE ); $target1->setAngle(-90); $target2 = new Cannon( new Point(150, 399), Cannon::BODY_TYPE_RECTANGLE ); $target2->setAngle(-90); $targets = [ $target1, $target2 ];*/ //SETUP $angleRadians = $angle * M_PI / 180; $speed = $baseSpeed * abs($force); $time = 0; //$previousCannonBallPosition = $cannon->cannonBall->position; $animation = new Animation($maxWidth, $maxHeight); do { $x = Math::calculateWidth($speed, $angleRadians, $time); $y = Math::calculateHeight($speed, $angleRadians, $gravity, $time); $cannon->cannonBallPosition($x, $y); $canvas = $animation->createCanvas(); $cannon->draw($canvas); $target->draw($canvas); //echo $cannon->cannonBall->position->x . PHP_EOL; /*$hit = false; foreach ($targets as $target) { if ($target->isHit($cannon->cannonBall, $previousCannonBallPosition, $hits)) { $hit = true; } $target->draw($canvas); } if ($hit) { $animation->addFrame($canvas); break; }*/ $cannon->cannonBallDraw($canvas); $animation->addFrame($canvas); //$previousCannonBallPosition = $cannon->cannonBall->position; $time += $timeIncrement; } while ($cannon->cannonBall->position->x >= 0 && $cannon->cannonBall->position->x <= $maxWidth && $cannon->cannonBall->position->y <= $maxHeight); //Last Frame $canvas = $animation->createCanvas(); $cannon->draw($canvas); $target->draw($canvas); if (($points = $target->isHit($cannon->cannonBall)) !== false) { if (isset($data['user.score'][$userName])) { $data['user.score'][$userName] += $points; } else { $data['user.score'][$userName] = $points; } $notification = new Notification(new Point(50, 50), 'HIT! ' . $userName . ' Scored ' . $points . ' points!', 'Total: ' . $data['user.score'][$userName]); $notification->draw($canvas); } /*foreach ($targets as $target) { $target->draw($canvas); }*/ /*$blue = imagecolorallocate($canvas, 255, 255, 255); foreach ($hits as $hit) { imagesetpixel($canvas, $hit[0], $hit[1], $blue); }*/ $animation->addFinishFrame($canvas); return $animation->generateGif(); }
function pixelCannons($force, $angle, &$data, $userName = '') { //CONFIG $maxWidth = 800; $maxHeight = 400; $baseSpeed = 10; //pixels $timeIncrement = 0.05; $gravity = 9.81; if (!isset($data['user'][$userName]['cannon'])) { if (empty($data['available.colors'])) { $data['available.colors'] = array_keys(Cannon::$cannonColors); } $data['user'][$userName]['cannon'] = ['color' => Color::extractRandomColor($data['available.colors']), 'body' => Cannon::BODY_TYPE_SQUARE, 'cannonball' => CannonBall::TYPE_BASIC]; } $cannon = new Cannon(new Point(10, 399), $data['user'][$userName]['cannon']['body'], $data['user'][$userName]['cannon']['color'], $data['user'][$userName]['cannon']['cannonball']); $cannon->setAngle($angle); if (isset($data['user'][$userName]['cannon']['power-up']) && $data['user'][$userName]['cannon']['power-up']) { CannonBallPowerUp::modifyCannonBall($cannon->cannonBall, $data['user'][$userName]['cannon']['power-up']); $data['user'][$userName]['cannon']['power-up'] = false; } if (empty($data['current.target'])) { $data['current.target'] = $data['defaults']['current.target']; } $center = new Point($data['current.target']['x'], $data['current.target']['y']); $target = new Target($center, 100, 3, Color::YELLOW, new Target($center, 80, 5, Color::GREEN, new Target($center, 60, 10, Color::BLUE, new Target($center, 40, 20, Color::PURPLE, new Target($center, 10, 40, Color::BLACK, new Target($center, 4, 80, Color::RED)))))); //SETUP $force = $force * $data['drag']; $angleRadians = $angle * M_PI / 180; $speed = $baseSpeed * abs($force); $time = 0; $animation = new Animation($maxWidth, $maxHeight); $info = new Info(new Point(10, 10), $data); $powerUp = false; if (isset($data['power-up']) && $data['power-up'] !== false) { //Calculate if next shot will have a powerUp $powerUp = new CannonBallPowerUp(new Point($data['power-up']['x'], $data['power-up']['y']), $data['power-up']['type']); $powerUp->draw($canvas); } $totalPoints = false; do { $x = Math::calculateWidth($speed, $angleRadians, $time); $y = Math::calculateHeight($speed, $angleRadians, $gravity, $time); $cannon->cannonBallPosition($x, $y); $canvas = $animation->createCanvas(); $cannon->draw($canvas); $target->draw($canvas); $info->draw($canvas); if ($powerUp !== false) { if ($powerUp->isHit($cannon->cannonBall)) { $data['user'][$userName]['cannon']['power-up'] = $powerUp->type; $data['power-up'] = false; } $powerUp->draw($canvas); } $cannon->cannonBallDraw($canvas); $animation->addFrame($canvas); $time += $timeIncrement; if (($hitPoints = $target->isHit($cannon->cannonBall)) !== false) { $totalPoints = $totalPoints === false ? $hitPoints : $totalPoints + $hitPoints; } } while (!$cannon->cannonBall->isDestroyed() && ($cannon->cannonBall->position->x >= 0 && $cannon->cannonBall->position->x <= $maxWidth) && $cannon->cannonBall->position->y <= $maxHeight); //LAST FRAME $canvas = $animation->createCanvas(); $cannon->draw($canvas); $cannon->cannonBall->draw($canvas); $target->draw($canvas); if ($powerUp !== false) { $powerUp->draw($canvas); } if ($totalPoints !== false) { if (isset($data['scores'][$userName])) { $data['scores'][$userName] += $totalPoints; } else { $data['scores'][$userName] = $totalPoints; } if ($data['scores'][$userName] >= 1000) { $notification = new Notification(new Point(100, 100), ['ALL HAIL THE KING ' . $userName . '!', 'FOR HE HAS WON THIS ROUND WITH AN OVERKILL OF ' . ($data['scores'][$userName] - 1000) . ' POINTS!']); resetPixelCannons($data); } else { $notification = new Notification(new Point(50, 50), ['HIT! ' . $userName . ' Scored ' . $totalPoints . ' points!', 'Total: ' . $data['scores'][$userName]]); $data['drag'] = Math::calculateDrag(); } $notification->draw($canvas); } if (!isset($data['power-up']) || $data['power-up'] === false) { //Calculate if next shot will have a powerUp if (CannonBallPowerUp::getPowerUp($data)) { $powerUp = new CannonBallPowerUp(new Point($data['power-up']['x'], $data['power-up']['y']), CannonBallPowerUp::TYPE_SUPER_SIZED); $powerUp->draw($canvas); } } //Extra show updated info after the possible hits $info = new Info(new Point(10, 10), $data); $info->draw($canvas); $animation->addFinishFrame($canvas); return $animation->generateGif(); }