text: #include "Globals.h"
  #include "Application.h"
  #include "ModulePlayer.h"
  #include "Primitive.h"
  #include "PhysVehicle3D.h"
  #include "PhysBody3D.h"
  
  ModulePlayer::ModulePlayer(Application* app, bool start_enabled) : Module(app, start_enabled), vehicle(NULL)
  {
  	turn = acceleration = brake = 0.0f;
  	initPos.Set(0, 2, 0);
  }
  
  ModulePlayer::~ModulePlayer()
  {}
  
  // Load assets
  bool ModulePlayer::Start()
  {
  	LOG("Loading player");
  
  	VehicleInfo car;
  
  	// Car properties ----------------------------------------
  	car.chassis_size.Set(2.2f, 0.5f, 5.0f);
  	car.chassis_offset.Set(0.0f, 1.0f, 0.0f);
  	car.mass = 500.0f;
  	car.suspensionStiffness = 14.5f;
  	car.suspensionCompression = 0.83f;
  	car.suspensionDamping = 0.95f;
  	car.maxSuspensionTravelCm = 1000.0f;
  	car.frictionSlip = 50.5;
  	car.maxSuspensionForce = 6000.0f;
  	car.color = Yellow;
  
  	// Wheel properties ---------------------------------------
  	float connection_height = 1.2f;
  	float wheel_radius = 0.25f;
  	float wheel_width = 0.2f;
  	float suspensionRestLength = 0.7f;
  
  	// Don't change anything below this line ------------------
  
  	float half_width = car.chassis_size.x*0.5f;
  	float half_length = car.chassis_size.z*0.5f;
  	
  	vec3 direction(0,-1,0);
  	vec3 axis(-1,0,0);
  	
  	car.num_wheels = 4;
  	car.wheels = new Wheel[4];
  
  	// FRONT-LEFT ------------------------
  	car.wheels[0].connection.Set(half_width - 0.3f * wheel_width, connection_height, half_length - wheel_radius);
  	car.wheels[0].direction = direction;
  	car.wheels[0].axis = axis;
  	car.wheels[0].suspensionRestLength = suspensionRestLength;
  	car.wheels[0].radius = wheel_radius;
  	car.wheels[0].width = wheel_width;
  	car.wheels[0].front = true;
  	car.wheels[0].drive = true;
  	car.wheels[0].brake = false;
  	car.wheels[0].steering = true;
  
  	// FRONT-RIGHT ------------------------
  	car.wheels[1].connection.Set(-half_width + 0.3f * wheel_width, connection_height, half_length - wheel_radius);
  	car.wheels[1].direction = direction;
  	car.wheels[1].axis = axis;
  	car.wheels[1].suspensionRestLength = suspensionRestLength;
  	car.wheels[1].radius = wheel_radius;
  	car.wheels[1].width = wheel_width;
  	car.wheels[1].front = true;
  	car.wheels[1].drive = true;
  	car.wheels[1].brake = false;
  	car.wheels[1].steering = true;
  
  	// REAR-LEFT ------------------------
  	car.wheels[2].connection.Set(half_width - 0.3f * wheel_width, connection_height, -half_length + wheel_radius);
  	car.wheels[2].direction = direction;
  	car.wheels[2].axis = axis;
  	car.wheels[2].suspensionRestLength = suspensionRestLength;
  	car.wheels[2].radius = wheel_radius;
  	car.wheels[2].width = wheel_width;
  	car.wheels[2].front = false;
  	car.wheels[2].drive = false;
  	car.wheels[2].brake = true;
  	car.wheels[2].steering = false;
  
  	// REAR-RIGHT ------------------------
  	car.wheels[3].connection.Set(-half_width + 0.3f * wheel_width, connection_height, -half_length + wheel_radius);
  	car.wheels[3].direction = direction;
  	car.wheels[3].axis = axis;
  	car.wheels[3].suspensionRestLength = suspensionRestLength;
  	car.wheels[3].radius = wheel_radius;
  	car.wheels[3].width = wheel_width;
  	car.wheels[3].front = false;
  	car.wheels[3].drive = false;
  	car.wheels[3].brake = true;
  	car.wheels[3].steering = false;
  
  	vehicle = App->physics->AddVehicle(car);
  
  	vehicle->SetPos(initPos.x, initPos.y, initPos.z);
  	vehicle->Rotate(3.14f);
  	vehicle->GetTransform(initMat);
  	vehicle->GetTransform(checkpointMat);
  	st = state::Empty;
  	
  	return true;
  }
  
  // Unload assets
  bool ModulePlayer::CleanUp()
  {
  	LOG("Unloading player");
  
  	return true;
  }
  
  // Update: draw background
  update_status ModulePlayer::Update(float dt)
  {
  	turn = acceleration = brake = 0.0f;
  
  	if (App->input->GetKey(SDL_SCANCODE_F2) == KEY_DOWN)
  	{
  		App->scene_intro->Restart();
  		vehicle->info.color = Yellow;
  	}
  
  	if (App->input->GetKey(SDL_SCANCODE_F3) == KEY_DOWN)
  	{
  		App->player->vehicle->SetTransform(App->player->checkpointMat);
  	}
  
  	if(App->input->GetKey(SDL_SCANCODE_UP) == KEY_REPEAT && App->input->GetKey(SDL_SCANCODE_LEFT) != KEY_REPEAT && App->input->GetKey(SDL_SCANCODE_RIGHT) != KEY_REPEAT)
  	{
  		acceleration = MAX_ACCELERATION;
  	}
  
  	if (App->input->GetKey(SDL_SCANCODE_UP) == KEY_REPEAT && App->input->GetKey(SDL_SCANCODE_LEFT) == KEY_REPEAT)
  	{
  		acceleration = MAX_ACCELERATION;
  
  		if (turn < TURN_DEGREES)
  			turn += TURN_DEGREES;
  	}
  
  	if (App->input->GetKey(SDL_SCANCODE_UP) == KEY_REPEAT && App->input->GetKey(SDL_SCANCODE_RIGHT) == KEY_REPEAT)
  	{
  		acceleration = MAX_ACCELERATION;
  
  		if (turn > -TURN_DEGREES)
  			turn -= TURN_DEGREES;
  	}
  
  	if(App->input->GetKey(SDL_SCANCODE_LEFT) == KEY_REPEAT)
  	{
  		if(turn < TURN_DEGREES)
  			turn +=  TURN_DEGREES;
  	}
  
  	if(App->input->GetKey(SDL_SCANCODE_RIGHT) == KEY_REPEAT)
  	{
  		if(turn > -TURN_DEGREES)
  			turn -= TURN_DEGREES;
  	}
  
  	if(App->input->GetKey(SDL_SCANCODE_DOWN) == KEY_REPEAT && App->input->GetKey(SDL_SCANCODE_LEFT) != KEY_REPEAT && App->input->GetKey(SDL_SCANCODE_RIGHT) != KEY_REPEAT)
  	{
  		acceleration = -MAX_ACCELERATION;
  	}
  
  	if (App->input->GetKey(SDL_SCANCODE_DOWN) == KEY_REPEAT && App->input->GetKey(SDL_SCANCODE_LEFT) == KEY_REPEAT)
  	{
  		acceleration = -MAX_ACCELERATION;
  
  		if (turn < TURN_DEGREES)
  			turn += TURN_DEGREES;
  	}
  
  	if (App->input->GetKey(SDL_SCANCODE_DOWN) == KEY_REPEAT && App->input->GetKey(SDL_SCANCODE_RIGHT) == KEY_REPEAT)
  	{
  		acceleration = -MAX_ACCELERATION;
  
  		if (turn > -TURN_DEGREES)
  			turn -= TURN_DEGREES;
  	}
  
  	if (App->input->GetKey(SDL_SCANCODE_SPACE) == KEY_REPEAT)
  	{
  		brake = BRAKE_POWER;
  	}
  
  	vehicle->ApplyEngineForce(acceleration);
  	vehicle->Turn(turn);
  	vehicle->Brake(brake);
  
  	vehicle->Render();
  
  	char title[80];
  	sprintf_s(title, "%.1f Km/h, %ds left, %d/5 clients satisfied", vehicle->GetKmh(), 200 - App->scene_intro->game_timer.Read()/1000, App->scene_intro->satisfiedcl);
  	App->window->SetTitle(title);
  
  	btTransform t;
  	pos = vehicle->GetPos();
  
  	return UPDATE_CONTINUE;
  }
  
  
  
  