#include "example4.h"

#define NUM_BOXES 8

using namespace std;

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

static void create_box_row_vary_friction(rat_world *world,vector2 boxsize,rat_real padx,rat_real y,rat_real xcenter,unsigned int numboxes)
{
	register unsigned int i;
	register rat_real halfwidth=((boxsize.x+padx)/2.0)*numboxes;
	vector2 halfsize=vector2_multf(boxsize,0.5);

	numboxes=numboxes>1?numboxes:1;
	for (i=0; i<numboxes; i++)
	{
		vector2 *boxverts;
		vector2 boxpos={xcenter-halfwidth+(boxsize.x+padx)*i+halfsize.x+padx/2.0,y};
		rat_hull *box=rat_hull_polygon_create_box(boxpos,halfsize);
		rat_hull_set_pos(box,boxpos);

		rat_hull_polygon_get_verts(box,&boxverts);

		rat_world_add_body(world,
			rat_body_create(box,0.7,0.15-((rat_real)i/(rat_real)(numboxes-1))*0.1,boxsize.x*boxsize.y,
			rat_moi_polygon(boxsize.x*boxsize.y,boxverts,4)));
	}
}

bool RatPhysicsExample4::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,NULL);

	rat_world_set_gravity(world,gravity);
	rat_world_set_damping(world,0.975);

	vector2 boxsize={30,30};
	create_box_row_vary_friction(world,boxsize,20,20,SCENE_WIDTH/2.0,5);

	vector2 *boxverts,extents,pos;
	rat_hull *box;

	vector2_set(&extents,SCENE_WIDTH*0.5,15);
	vector2_set(&pos,SCENE_WIDTH/2.0-100,80);
	box=rat_hull_polygon_create_box(pos,extents);
	rat_hull_set_angle(box,0.11);
	rat_hull_polygon_get_verts(box,&boxverts);
	rat_world_add_body(world,rat_body_create(box,0.0,0.8,0.0,0.0));

	vector2_set(&extents,SCENE_WIDTH*0.5,15);
	vector2_set(&pos,SCENE_WIDTH/2.0+100,250);
	box=rat_hull_polygon_create_box(pos,extents);
	rat_hull_set_angle(box,-0.08);
	rat_hull_polygon_get_verts(box,&boxverts);
	rat_world_add_body(world,rat_body_create(box,0.0,0.8,0.0,0.0));

	vector2_set(&extents,SCENE_WIDTH*0.5,15);
	vector2_set(&pos,SCENE_WIDTH/2.0-100,420);
	box=rat_hull_polygon_create_box(pos,extents);
	rat_hull_set_angle(box,0.06);
	rat_hull_polygon_get_verts(box,&boxverts);
	rat_world_add_body(world,rat_body_create(box,0.0,0.8,0.0,0.0));

	vector2_set(&extents,SCENE_WIDTH*0.5,15);
	vector2_set(&pos,SCENE_WIDTH/2.0+100,600);
	box=rat_hull_polygon_create_box(pos,extents);
	rat_hull_set_angle(box,-0.04);
	rat_hull_polygon_get_verts(box,&boxverts);
	rat_world_add_body(world,rat_body_create(box,0.0,0.8,0.0,0.0));

	rat_examples_create_walls(world);

	return (bool)world;
}

bool RatPhysicsExample4::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 RatPhysicsExample4::UpdateVisuals()
{
	rat_draw_world(world,show_quadtree,show_arbiters,show_aabbs);
	return true;
}

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

	return true;
}

