package controllers 
{
	import flash.display.Sprite;
	import interfaces.IGameObject;
	/**
	 * ...
	 * @author Fiodar Paulavets
	 * @version 1.1
	 * @since 10.05.2012
	 */
	public class ObjectController
	{
		public var objects: Vector.<IGameObject> = new Vector.<IGameObject>();
		public var sprite: Sprite;
		private var _sprites: Boolean;
		
		/**
		 * 
		 * @param	objectsAsSprites	Обрабатывать объекты как спрайты. Если false, то переменная sprite не будет инициализирована.
		 */
		public function ObjectController(objectsAsSprites: Boolean = true)
		{
			_sprites = objectsAsSprites;
			if (_sprites)
			{
				sprite = new Sprite();
				sprite.mouseEnabled = false;
			}
		}
		
		public function add(object: IGameObject): void
		{
			objects.push(object);
			if (_sprites)
				sprite.addChild(object as Sprite);
		}
		
		public function remove(object: IGameObject): void
		{
			//if (sprite.contains(object as Sprite)
			if (_sprites)
				sprite.removeChild(object as Sprite);
			objects.splice(objects.indexOf(object), 1);
		}
		
		public function update(dt: Number): void
		{
			for each (var object: IGameObject in objects)
				object.Update(dt);
		}
		
	}

}