ticksPerSecond = (int) $ticksPerSecond; $this->sleepTime = (int) (1000000 / $this->ticksPerSecond); $this->tickMeasure = $this->sleepTime; $this->start(PTHREADS_INHERIT_ALL & ~PTHREADS_INHERIT_CLASSES); } /** * Returns true if clear to run tick * * @return bool */ public function hasTick(){ return $this->synchronized(function (){ $hasTick = $this->hasTick; $this->hasTick = false; return $hasTick === true; }); } public function doTick(){ $this->notify(); } /** * @return float */ public function getTPS(){ return $this->synchronized(function (){ return round(($this->sleepTime / $this->tickMeasure) * $this->ticksPerSecond, 2); }); } public function run(){ $tickTime = microtime(true); $this->tickMeasure = $this->sleepTime; while(true){ $this->synchronized(function (){ $this->hasTick = true; $this->wait(); $this->hasTick = false; }); $this->tickMeasure = (int) ((($time = microtime(true)) - $tickTime) * 1000000); $tickTime = $time; $sleepTime = $this->sleepTime * ($this->sleepTime / max($this->sleepTime, $this->tickMeasure)); usleep((int) $sleepTime); } } }