Example #1
0
 public function renderVisibleSprites()
 {
     foreach ($this->sprites as $sprite) {
         if ($sprite->isVisible()) {
             $this->frameBuffer->drawBitmapAt($sprite->getBitmap(), $sprite->getX(), $sprite->getY());
         }
     }
 }
 public function onFrameUpdate()
 {
     $now = time();
     if ($now > $this->previousTime) {
         $this->currentFrameIndex = ($this->currentFrameIndex + 1) % count($this->frames);
         $this->frameBuffer->setBackgroundFrame($this->frames[$this->currentFrameIndex]);
     }
     $this->previousTime = $now;
 }
Example #3
0
 public function __construct(FrameBuffer $frameBuffer)
 {
     $this->width = $frameBuffer->getWidth();
     $this->height = $frameBuffer->getHeight();
     // need room for each pixel as a char, and newlines between them
     $encodedSize = $this->height * ($this->width + 1) - 1;
     $this->blankEncodedFrame = str_repeat(self::ENCODED_PIXEL_OFF, $encodedSize);
     for ($i = $this->width; $i < $encodedSize; $i += $this->width + 1) {
         $this->blankEncodedFrame[$i] = "\n";
     }
     $this->colorCharMap = array_flip(BitmapLoader::$colorMap);
 }
Example #4
0
 function onFrameUpdate(TimerInterface $timer)
 {
     $utime = microtime(true);
     $time = (int) $utime;
     $ms = (int) (($utime - $time) * 1000);
     //        printf("== frameupdate: %s.%03d\n", strftime('%H:%M:%S', $time), $ms);
     $this->gameLoop->onFrameUpdate();
     $frame = $this->frameBuffer->getAndSwitchFrame();
     $encodedFrameCache = [];
     foreach ($this->connections as $conn) {
         /** @var PlayerConnection $playerConnection */
         $playerConnection = $this->connections[$conn];
         if (!$playerConnection->isOutputEnabled()) {
             continue;
         }
         $encoder = $playerConnection->getFrameEncoder();
         $key = get_class($encoder);
         if (!isset($encodedFrameCache[$key])) {
             $encodedFrameCache[$key] = $playerConnection->getFrameEncoder()->encodeFrame($frame);
         }
         //            printf("message: %s\n", $encodedFrameCache[$key]);
         if ($encodedFrameCache[$key]) {
             // the encoded frame may be null if there are no changes
             $conn->send($encodedFrameCache[$key]);
         }
     }
 }
Example #5
0
 protected function initializeGame(FrameBuffer $frameBuffer)
 {
     $this->background = $this->bitmapLoader->loadBitmap('main_game');
     $this->displayHeight = $frameBuffer->getHeight();
     $this->displayWidth = $frameBuffer->getWidth();
     $this->paddles = [self::LEFT => $this->bitmapLoader->loadSprite('paddle'), self::RIGHT => $this->bitmapLoader->loadSprite('paddle')];
     $this->addSprite($this->paddles[self::LEFT]);
     $this->addSprite($this->paddles[self::RIGHT]);
     $this->paddleHeight = $this->paddles[self::LEFT]->getBitmap()->getHeight();
     $this->paddleWidth = $this->paddles[self::LEFT]->getBitmap()->getWidth();
     $this->paddleMinY = self::FRAME_EDGE_SIZE;
     $this->paddleMaxY = $this->displayHeight - $this->paddleHeight - self::FRAME_EDGE_SIZE;
     $this->ball = $this->bitmapLoader->loadSprite('ball');
     $this->addSprite($this->ball);
     $this->ballHeight = $this->ball->getBitmap()->getHeight();
     $this->ballWidth = $this->ball->getBitmap()->getWidth();
     self::$paddlePosX[self::RIGHT] = $this->displayWidth - 1.0 - $this->paddleWidth;
     $this->ballPaddleLimitX = [self::LEFT => (double) (self::$paddlePosX[self::LEFT] + $this->paddleWidth), self::RIGHT => (double) (self::$paddlePosX[self::RIGHT] - $this->paddleWidth)];
     $this->ballEdgeLimitY = [self::TOP => 1.0, self::BOTTOM => (double) ($this->displayHeight - $this->ballHeight)];
     $this->frameTimestamp = microtime(true);
     $this->gameState = self::GAMESTATE_INITIALIZING;
 }
Example #6
0
 /**
  * @param FrameBuffer $frameBuffer
  * @return string
  */
 public function encodeFrameInfo(FrameBuffer $frameBuffer)
 {
     return json_encode(['frameInfo' => ['width' => $frameBuffer->getWidth(), 'height' => $frameBuffer->getHeight(), 'palette' => Color::getPalette()]]);
 }