begin timin
This commit is contained in:
committed by
Steffen Schröder
parent
b751629dc5
commit
140c77441e
53
application/core/Callbacks/TimerManager.php
Normal file
53
application/core/Callbacks/TimerManager.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Lukas
|
||||
* Date: 30.01.14
|
||||
* Time: 21:11
|
||||
*/
|
||||
|
||||
namespace ManiaControl\Callbacks;
|
||||
|
||||
|
||||
use ManiaControl\ManiaControl;
|
||||
|
||||
class TimerManager {
|
||||
private $maniaControl = null;
|
||||
private $timerListenings = array();
|
||||
|
||||
public function __construct(ManiaControl $maniaControl) {
|
||||
$this->maniaControl = $maniaControl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a Timing Listening, note < 10ms it can get inaccurate
|
||||
*
|
||||
* @param TimerListener $listener
|
||||
* @param $method
|
||||
* @param $time
|
||||
* @return bool
|
||||
*/
|
||||
public function registerTimerListening(TimerListener $listener, $method, $time) {
|
||||
if (!method_exists($listener, $method)) {
|
||||
trigger_error("Given listener (" . get_class($listener) . ") can't handle timer (no method '{$method}')!");
|
||||
return false;
|
||||
}
|
||||
array_push($this->timerListenings, array("Listener" => $listener, "Method" => $method, "DeltaTime" => ($time / 1000), "LastTrigger" => microtime(true)));
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Manage the Timings on every ms
|
||||
*/
|
||||
public function manageTimings() {
|
||||
$time = microtime(true);
|
||||
foreach($this->timerListenings as $key => $listening) {
|
||||
if ($listening["LastTrigger"] + $listening["DeltaTime"] < $time) {
|
||||
call_user_func(array($listening["Listener"], $listening["Method"]), $time);
|
||||
$this->timerListenings[$key]["LastTrigger"] += ($listening["DeltaTime"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user