package interf
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.utils.getTimer;
	import flash.display.Shape;
	
	public class FpsGame extends Object
	{
		
		public function FpsGame(steper:ISteper, ctrl:ICtrl)
		{
			super();
			this.steper = steper;
			this.ctrl = ctrl;
		}
		
		private var _startTime:int;
		
		private var _pauseTotal:int;
		
		private var _pauseTime:int;
		
		private var _playing:Boolean;
		
		private var _isPause:Boolean;
		
		private var _overTime:int;
		
		private var _current:int;
		
		private var _delay:int;
		
		public function get currTime():int
		{
			return this._current;
		}
		
		public function get delay():int
		{
			return this._delay;
		}
		
		public function get pausing():Boolean
		{
			return this._isPause;
		}
		
		private var steper:ISteper;
		
		private var ctrl:ICtrl;
		
		private const _e:DisplayObject = new Shape();
		
		private function step(e:Event = null):void
		{
			var time:int = getTimer() - this._startTime - this._pauseTotal;
			this._delay = time - this._current;
			this._current = time;
			this.steper.step();
		}
		
		public function start():void
		{
			this._e.addEventListener(Event.ENTER_FRAME, this.step);
			this._current = this._pauseTime = this._pauseTotal = 0;
			this._startTime = getTimer();
			this._isPause = false;
			this._playing = true;
			this.ctrl.ctrlStart();
		}
		
		public function pause():void
		{
			if ((!this._playing) || (this._isPause))
			{
				return;
			}
			this._isPause = true;
			this._e.removeEventListener(Event.ENTER_FRAME, this.step);
			this._pauseTime = getTimer();
			this.ctrl.ctrlPause();
		}
		
		public function play():void
		{
			if ((this._playing) && (this._isPause))
			{
				this._isPause = false;
				this._e.addEventListener(Event.ENTER_FRAME, this.step);
				this._pauseTotal = this._pauseTotal + (getTimer() - this._pauseTime);
				this.ctrl.ctrlStart();
				return;
			}
		}
		
		public function stop():void
		{
			this._e.removeEventListener(Event.ENTER_FRAME, this.step);
			this._overTime = getTimer();
			this.ctrl.ctrlPause();
			this._playing = false;
		}
	}
}
