
package {
	import com.netgrind.core.UtilsString;
	import flash.desktop.Clipboard;
	import flash.desktop.ClipboardFormats;
	import flash.display.Sprite;
	import nape.geom.Vec2;
	import nape.phys.Body;
	/**
	 * ...
	 * @author netgrind
	 */
	public class LoadManager extends Sprite {
		
		[Embed(source="../lib/Map.json", mimeType="application/octet-stream")]public var map:Class
		
		public var menu:Array = [];
		public var body:Body;		
		
		public function LoadManager(body:Body) {
			super();
			this.body = body;
			load();
		}
		
		public function load():void {
			var o:Object = JSON.parse(new map());
			var wp:Array;
			var race:Array ;
			var poly:Array;
			for (var i:int = 0; i < o.layers.length; i++) {
				if (o.layers[i].name == "Waypoints") wp = o.layers[i].objects;
				else if (o.layers[i].name == "Colliders") poly = o.layers[i].objects;
				else if (o.layers[i].name == "Waypoints Track 1") race = o.layers[i].objects;
			}
			var p:Vector.<Vec2>;
			trace(poly.length);
			for (var j:int = 0; j < poly.length; j++) {
				p = new Vector.<Vec2>();
				if (poly[j].polygon == null) continue;
				for (var k:int = 0; k < poly[j].polygon.length; k++) {
					p.push(new Vec2(poly[j].x+poly[j].polygon[k].x,poly[j].y+poly[j].polygon[k].y));
				}
				addChild(new Poly(body, p, 1));
			}
			optimize();
			
			for (var l:int = 0; l < wp.length; l++) {
				addChild(new Waypoint(wp[l].x, wp[l].y, wp[l].name));
			}
			for (var m:int = 0; m < race.length; m++) {
				addChild(new Waypoint(race[m].x, race[m].y, race[m].name));
			}
		}
		
		public function optimize():void {
			for (var i:int = Poly.all.length-1; i >-1 ; i--) {
				Poly.all[i].subdivide();
			}
		}
		
		public function addPoly(verts:Vector.<Vec2>, type:int = 1 ):void {
			var p:Poly = new Poly(body, verts);
			addChild(p);
		}		
	}	
}