home *** CD-ROM | disk | FTP | other *** search
- class LRG.Utils.LRGGameTimer extends MovieClip
- {
- var m_fOffsetTime;
- var m_fStartedTime;
- var m_fStoppedTime;
- var m_fCurrentTime;
- var m_pClockListeners;
- var m_fIntervalId;
- var m_fTimeToRun;
- function LRGGameTimer()
- {
- super();
- this.init();
- }
- function init()
- {
- this.m_fOffsetTime = 0;
- this.m_fStartedTime = undefined;
- this.m_fStoppedTime = undefined;
- this.m_fCurrentTime = undefined;
- this.m_pClockListeners = new Array();
- this.m_fIntervalId = undefined;
- this.m_fTimeToRun = Infinity;
- }
- function getTime()
- {
- return this.m_fCurrentTime;
- }
- function isRunning()
- {
- return this.m_fIntervalId != undefined;
- }
- function stopClock()
- {
- if(this.isRunning())
- {
- clearInterval(this.m_fIntervalId);
- this.m_fIntervalId = undefined;
- this.m_fStoppedTime = getTimer() / 1000;
- var _loc2_ = 0;
- while(_loc2_ < this.m_pClockListeners.length)
- {
- this.m_pClockListeners[_loc2_].doOnClockStopped(this);
- _loc2_ = _loc2_ + 1;
- }
- }
- }
- function startClock(bReset)
- {
- if(bReset || this.m_fCurrentTime == undefined)
- {
- this.resetClock();
- }
- else
- {
- this.m_fOffsetTime += getTimer() / 1000 - this.m_fStoppedTime;
- this.m_fCurrentTime = getTimer() / 1000 - this.m_fOffsetTime;
- }
- var _loc2_ = 0;
- while(_loc2_ < this.m_pClockListeners.length)
- {
- this.m_pClockListeners[_loc2_].doOnClockStarted(this);
- _loc2_ = _loc2_ + 1;
- }
- this.m_fIntervalId = setInterval(this,"updateClock",8.333333333333334);
- }
- function resetClock()
- {
- this.m_fCurrentTime = 0;
- this.m_fOffsetTime = getTimer() / 1000;
- }
- function setTimeToRun(fTime)
- {
- this.m_fTimeToRun = fTime;
- }
- function addListener(kListener)
- {
- this.m_pClockListeners.push(kListener);
- }
- function removeListener(kListener)
- {
- var _loc2_ = this.m_pClockListeners;
- this.m_pClockListeners = _loc2_.removeData(kListener);
- false;
- }
- function updateClock()
- {
- this.m_fCurrentTime = getTimer() / 1000 - this.m_fOffsetTime;
- if(this.m_fCurrentTime >= this.m_fTimeToRun)
- {
- this.stopClock();
- }
- }
- }
-