Beispiel #1
0
 protected function init()
 {
     // This sleep should be removed ASAP. It's here because of a bug. Comment
     // out this line, and run your game a couple of times, and you should
     // notice a problem.
     sleep(1);
     $this->running = true;
     // Will be used later for getting the $dt (delta-time)
     $oldTime = microtime(true);
     $drawTime = 0;
     $frameLimit = 1 / 60;
     // Send Config Details
     Game::send_config();
     // Game Loop
     while ($this->running) {
         // Deltatime
         $newTime = microtime(true);
         $dt = $newTime - $oldTime;
         $oldTime = $newTime;
         // Handle Events sent from the client implementation to here
         $data = "";
         $from = "";
         $port = 0;
         socket_recvfrom(self::$sock, $data, 50, 0, $from, $port);
         if ($data) {
             $data = json_decode($data);
             if ($data->e == self::$protocol["keyPressed"]) {
                 $this->key_pressed($data->k);
             }
             if ($data->e == self::$protocol["keyReleased"]) {
                 $this->key_released($data->k);
             }
         }
         // Update
         $this->update($dt);
         // Draw
         $drawTime += $dt;
         if ($drawTime > $frameLimit) {
             // echo "$drawTime\n";
             $this->draw();
             Game::send();
             $drawTime = 0;
         }
         sleep(0.001);
     }
 }