var background;var button_clear;var button_fill;var button_on;var button_off;var total_display;var total_show=false;var ground;var wall1;var wall2;var obstacles=new Array;var obstacle_counter;var active_list;var world;var world_scale=30;var move_timer;var add_list=new Array;var menu_x=735;var b2Vec2;var b2AABB;var b2BodyDef;var b2Body;var b2FixtureDef;var b2Fixture;var b2World;var b2PolygonShape;var b2CircleShape;var b2RevoluteJointDef;var b2DebugDraw;var b2Listener;var render_run=false;function init(){document.getElementById('game_container').style.display="inline";document.getElementById('loading_div').style.display="none";setInteractiveParameters();set_data();world=new b2World(new b2Vec2(0,20),true);background=new MovieClip(document.getElementById('background').cloneNode(true));background.x=57;background.y=135;background.scale=1.7;background.transform();stage.appendChild(background.instance);button_off=new MovieClip(document.getElementById('button_off').cloneNode(true));button_off.x=740;button_off.y=500;button_off.transform();stage.appendChild(button_off.instance);button_on=new MovieClip(document.getElementById('button_on').cloneNode(true));button_on.x=740;button_on.y=500;button_on.transform();stage.appendChild(button_on.instance);button_fill=new MovieClip(document.getElementById('button_fill').cloneNode(true));button_fill.x=740;button_fill.y=600;button_fill.transform();stage.appendChild(button_fill.instance);button_clear=new MovieClip(document.getElementById('button_clear').cloneNode(true));button_clear.x=740;button_clear.y=700;button_clear.transform();stage.appendChild(button_clear.instance);total_display=new MovieClip(document.getElementById('total_display').cloneNode(true));total_display.x=740;total_display.y=400;total_display.transform();total_display.text=total_display.instance.querySelector(".total_display");stage.appendChild(total_display.instance);ground=new MovieClip(document.getElementById('box').cloneNode(true));ground.area=ground.instance.querySelector("#area");ground.x=365;ground.y=727;ground.width=610;ground.height=17;ground.rotation=0;ground.physics_type=b2Body.b2_staticBody;ground.area.setAttribute("x",-1*ground.width/2);ground.area.setAttribute("y",-1*ground.height/2);ground.area.setAttribute("width",ground.width);ground.area.setAttribute("height",ground.height);ground.area.setAttribute("fill","#253666");ground.area.setAttribute("stroke","#FFFFFF");ground.area.setAttribute("stroke-width",3);ground.transform();ground.physics_data=create_box(ground.width,ground.height,ground.x,ground.y,ground.rotation,ground.physics_type,ground);wall1=new MovieClip(document.getElementById('box').cloneNode(true));wall1.area=wall1.instance.querySelector("#area");wall1.x=63;wall1.y=430;wall1.width=17;wall1.height=600;wall1.rotation=0;wall1.physics_type=b2Body.b2_staticBody;wall1.area.setAttribute("x",-1*wall1.width/2);wall1.area.setAttribute("y",-1*wall1.height/2);wall1.area.setAttribute("width",wall1.width);wall1.area.setAttribute("height",wall1.height);wall1.area.setAttribute("fill","#253666");wall1.area.setAttribute("stroke","#FFFFFF");wall1.area.setAttribute("stroke-width",3);wall1.transform();wall1.physics_data=create_box(wall1.width,wall1.height,wall1.x,wall1.y,wall1.rotation,wall1.physics_type,wall1);wall2=new MovieClip(document.getElementById('box').cloneNode(true));wall2.area=wall2.instance.querySelector("#area");wall2.x=681;wall2.y=wall1.y;wall2.width=wall1.width;wall2.height=wall1.height;wall2.rotation=0;wall2.physics_type=b2Body.b2_staticBody;wall2.area.setAttribute("x",-1*wall2.width/2);wall2.area.setAttribute("y",-1*wall2.height/2);wall2.area.setAttribute("width",wall2.width);wall2.area.setAttribute("height",wall2.height);wall2.area.setAttribute("fill","#253666");wall2.area.setAttribute("stroke","#FFFFFF");wall2.area.setAttribute("stroke-width",3);wall2.transform();wall2.physics_data=create_box(wall2.width,wall2.height,wall2.x,wall2.y,wall2.rotation,wall2.physics_type,wall2);addPointerListeners();requestAnimationFrame(move_timer_handler);start_game();}
function start_game(){obstacle_counter=0;active_list=new Array;for(var i=1;i<=4;i++){new_obstacle(i,1,menu_x,60+(i-1)*70,false);}
render_run=true;}
function new_obstacle(type,origin,x,y,physics){var radius=30;obstacle_counter++;obstacles[obstacle_counter]=new MovieClip(document.getElementById('marble'+type).cloneNode(true));obstacles[obstacle_counter].type=type;obstacles[obstacle_counter].instance.setAttribute("data-id",obstacle_counter);obstacles[obstacle_counter].id=obstacle_counter;obstacles[obstacle_counter].x=x;obstacles[obstacle_counter].y=y;obstacles[obstacle_counter].rotation=0;obstacles[obstacle_counter].dragFront=true;obstacles[obstacle_counter].selected=false;obstacles[obstacle_counter].remove=false;obstacles[obstacle_counter].origin=origin;obstacles[obstacle_counter].start_x=obstacles[obstacle_counter].x;obstacles[obstacle_counter].start_y=obstacles[obstacle_counter].y;obstacles[obstacle_counter].radius=radius;obstacles[obstacle_counter].physics_type=b2Body.b2_dynamicBody;obstacles[obstacle_counter].transform();obstacles[obstacle_counter].physics_name='obstacle';obstacles[obstacle_counter].dragMoveHandler=function(){}
obstacles[obstacle_counter].dragStopHandler=function(){this.selected=false;this.remove=false;this.physics_data=create_circle(this.radius,this.x,this.y,b2Body.b2_dynamicBody,this);add_list.push(this);if(this.origin==1){new_obstacle(this.type,this.origin,menu_x,this.start_y,false);}
this.origin=2;}
if(physics){obstacles[obstacle_counter].physics_data=create_circle(radius,x,y,b2Body.b2_dynamicBody,obstacles[obstacle_counter]);add_list.push(obstacles[obstacle_counter]);}
obstacles[obstacle_counter].instance.addEventListener("pointerdown",obstacle_drag_handler,false);stage.appendChild(obstacles[obstacle_counter].instance);}
function move_timer_handler(timestamp){if(render_run){world.Step(1/30,10,10);for(var i in add_list){active_list.push(add_list[i]);}
for(var i in active_list){active_list[i].x=active_list[i].physics_data.GetPosition().x*world_scale;active_list[i].y=active_list[i].physics_data.GetPosition().y*world_scale;active_list[i].rotation=active_list[i].physics_data.GetAngle()*(180/Math.PI);active_list[i].transform();}
for(var i in active_list){if(active_list[i].remove||active_list[i].y>800){world.DestroyBody(active_list[i].physics_data);if(!active_list[i].selected){active_list[i].instance.setAttribute("display","none");}
active_list.splice(i,1);i++;}}
if(total_show){total_display.text.textContent=active_list.length;}
add_list=new Array;world.ClearForces();}
requestAnimationFrame(move_timer_handler);};function clear_all(){for(var i in active_list){active_list[i].remove=true;}}
function obstacle_drag_handler(event){event.preventDefault();if(event.isPrimary){var local=obstacles[this.getAttribute("data-id")];if(local.origin==2){local.selected=true;local.remove=true;}
dragElement=local;dragPointerStart(event);}}
function total_handler(event){event.preventDefault();if(event.isPrimary){if(total_show){total_show=false;total_display.text.textContent="";button_on.instance.setAttribute("display","inline");button_off.instance.setAttribute("display","none");}else{total_show=true;button_on.instance.setAttribute("display","none");button_off.instance.setAttribute("display","inline");}}}
function fill_handler(event){event.preventDefault();if(event.isPrimary){clear_all();let total=generateRandom(20,100)
for(var i=1;i<=total;i++){new_obstacle(generateRandom(1,4),2,generateRandom(100,640),generateRandom(100,600),true);}}}
function clear_handler(event){event.preventDefault();if(event.isPrimary){clear_all();}}
function set_data(){b2Vec2=Box2D.Common.Math.b2Vec2;b2AABB=Box2D.Collision.b2AABB;b2BodyDef=Box2D.Dynamics.b2BodyDef;b2Body=Box2D.Dynamics.b2Body;b2FixtureDef=Box2D.Dynamics.b2FixtureDef;b2Fixture=Box2D.Dynamics.b2Fixture;b2World=Box2D.Dynamics.b2World;b2PolygonShape=Box2D.Collision.Shapes.b2PolygonShape;b2CircleShape=Box2D.Collision.Shapes.b2CircleShape;b2RevoluteJointDef=Box2D.Dynamics.Joints.b2RevoluteJointDef;b2DebugDraw=Box2D.Dynamics.b2DebugDraw;b2Listener=Box2D.Dynamics.b2ContactListener;}
function addPointerListeners(){button_on.instance.addEventListener("pointerdown",total_handler,false);button_off.instance.addEventListener("pointerdown",total_handler,false);button_fill.instance.addEventListener("pointerdown",fill_handler,false);button_clear.instance.addEventListener("pointerdown",clear_handler,false);}
function create_box(width,height,px,py,angle,type,movieclip){var bodyDef=new b2BodyDef;bodyDef.type=type;bodyDef.position.Set(px/world_scale,py/world_scale);bodyDef.userData=movieclip;var polygonShape=new b2PolygonShape;polygonShape.SetAsOrientedBox(width/2/world_scale,height/2/world_scale,new b2Vec2(0,0),angle*Math.PI/180);var fixtureDef=new b2FixtureDef;fixtureDef.density=0.05;fixtureDef.friction=1.0;fixtureDef.restitution=0.;fixtureDef.shape=polygonShape;var world_body=world.CreateBody(bodyDef);world_body.CreateFixture(fixtureDef);return world_body;}
function create_circle(radius,pX,pY,type,movieclip){var bodyDef=new b2BodyDef;bodyDef.type=type;bodyDef.position.Set(pX/world_scale,pY/world_scale);bodyDef.userData=movieclip;var fixtureDef=new b2FixtureDef;fixtureDef.density=1.0;fixtureDef.friction=0.5;fixtureDef.restitution=0.5;fixtureDef.shape=new b2CircleShape(radius/world_scale);var world_body=world.CreateBody(bodyDef);world_body.CreateFixture(fixtureDef);return world_body;}