#include "example6.h"

using namespace std;

RatPhysicsExample6::RatPhysicsExample6(string name): RatPhysicsExample(name) {}
RatPhysicsExample6::RatPhysicsExample6(const char *name): RatPhysicsExample(name) {}
RatPhysicsExample6::RatPhysicsExample6(char *name): RatPhysicsExample(name) {}

bool RatPhysicsExample6::InitWorld()
{
	SDL_WM_SetCaption(title.c_str(),NULL);

	aabb space={{0,0},{bounds.x,bounds.y}};
	vector2 cell_size={150,150};
	vector2 gravity={0,12};

	world=rat_world_create(space,rat_integrator_euler,10,
		rat_body_manager_type_quad_tree,&cell_size);

	rat_world_set_body_left_world_callback(world,rat_examples_leave_world_callback,this);

	rat_world_set_gravity(world,gravity);

	/*rat_world_add_collision_filter(world,1,1,0);
	rat_world_add_collision_filter(world,2,2,0);
	rat_world_add_collision_filter(world,3,3,0);
	rat_world_add_collision_filter(world,4,4,0);
	rat_world_add_collision_filter(world,5,5,0);
	rat_world_add_collision_filter(world,6,6,0);
	rat_world_add_collision_filter(world,7,7,0);
	rat_world_add_collision_filter(world,8,8,0);
	rat_world_add_collision_filter(world,9,9,0);*/

	rat_examples_create_walls(world);

	vector2 size={28.0,10.0};
	rat_examples_create_chain(world,false,size,bounds.y*0.2,bounds.x/2.0,20,1);
	rat_examples_create_chain(world,false,size,bounds.y*0.25,bounds.x/2.0,20,2);
	rat_examples_create_chain(world,false,size,bounds.y*0.3,bounds.x/2.0,20,3);
	rat_examples_create_chain(world,false,size,bounds.y*0.35,bounds.x/2.0,20,4);
	rat_examples_create_chain(world,false,size,bounds.y*0.4,bounds.x/2.0,20,5);
	rat_examples_create_chain(world,false,size,bounds.y*0.45,bounds.x/2.0,20,6);
	rat_examples_create_chain(world,false,size,bounds.y*0.5,bounds.x/2.0,20,7);
	rat_examples_create_chain(world,false,size,bounds.y*0.55,bounds.x/2.0,20,8);
	rat_examples_create_chain(world,false,size,bounds.y*0.6,bounds.x/2.0,20,9);

	vector2 center={bounds.x*0.5,bounds.y*0.9};
	
	rat_world_add_body(world,
		rat_body_create(rat_hull_circle_create(center,200),
		0.8,1.0,0.0,0.0));

	return (bool)world;
}

bool RatPhysicsExample6::UpdateWorld(rat_real timestep)
{
	timestep=timestep>2.0?2.0:timestep;
	rat_world_update(world,timestep*0.05,timestep<=1.0?1:0);

	return true;
}

bool RatPhysicsExample6::UpdateVisuals()
{
	rat_draw_world(world,show_quadtree,show_arbiters,show_aabbs);
	return true;
}

bool RatPhysicsExample6::FreeWorld()
{
	rat_world_free_entities(world);
	rat_world_destroy(world);

	return true;
}
