text: #include "App.h"
  #include "Input.h"
  #include "Textures.h"
  #include "Audio.h"
  #include "Render.h"
  #include "Window.h"
  #include "Scene.h"
  #include "Map.h"
  #include "ModulePhysics.h"
  #include "ModulePlayer.h"
  #include "ModuleFadeToBlack.h"
  #include "Defs.h"
  #include "Log.h"
  
  Scene::Scene() : Module()
  {
  	name.Create("scene");
  }
  
  // Destructor
  Scene::~Scene()
  {}
  
  // Called before render is available
  bool Scene::Awake(pugi::xml_node& config)
  {
  	LOG("Loading Scene");
  	bool ret = true;
  	
  	return ret;
  }
  
  // Called before the first frame
  bool Scene::Start()
  {
  	/*app->render->camera.w = 200;
  	app->render->camera.h = 200;
  	app->render->camera.x = -600;
  	app->render->camera.y = -1200;*/
  
  	app->audio->PlayMusic("Assets/audio/music/GamePlaySEGASonic.ogg");
  	background = app->tex->Load("Assets/maps/backgroundPlatformerGameFinal.png");
  
  	app->map->Enable();
  	// L03: DONE: Load map
  	app->map->Load("tileMap1.tmx");
  
  	app->player->Enable();
  	app->map->Colliders();
  
  	app->render->camera.x = app->map->MapToWorld(0, 0).x;
  	app->render->camera.y = app->map->MapToWorld(0, -130).y;
  	
  	
  	return true;
  }
  
  // Called each loop iteration
  bool Scene::PreUpdate()
  {
  	return true;
  }
  
  // Called each loop iteration
  bool Scene::Update(float dt)
  {
  	if (app->player->horizonalCamera == false)
  	{
  		app->render->camera.x = -(app->player->pbody->body->GetPosition().x * 100) + 630;
  	}
  
  	if (app->player->verticalCamera == false)
  	{
  		app->render->camera.y = -(app->player->pbody->body->GetPosition().y * 100) + 450;
  	}
  
      // L02: DONE 3: Request Load / Save when pressing L/S
  	if(app->input->GetKey(SDL_SCANCODE_F6) == KEY_DOWN)
  		LoadGameRequest();
  
  	if(app->input->GetKey(SDL_SCANCODE_F5) == KEY_DOWN)
  		SaveGameRequest();
  
  	if(app->input->GetKey(SDL_SCANCODE_UP) == KEY_REPEAT)
  		app->render->camera.y -= 4;
  
  	if(app->input->GetKey(SDL_SCANCODE_DOWN) == KEY_REPEAT)
  		app->render->camera.y += 4;
  
  	if(app->input->GetKey(SDL_SCANCODE_LEFT) == KEY_REPEAT)
  		app->render->camera.x -= 4;
  
  	if(app->input->GetKey(SDL_SCANCODE_RIGHT) == KEY_REPEAT)
  		app->render->camera.x += 4;
  
  	if (app->input->GetKey(SDL_SCANCODE_F9) == KEY_REPEAT)
  		app->map->debugColliders = !app->map->debugColliders;
  
  	/*if (camera.y <= 0 || camera.h >= -720)
  	{
  		if(camera.y >= 0) camera.y = 0;
  		if (camera.y <= -2319) camera.y = -2319;
  	}
  
  	if (camera.x <= 0 || camera.x >= -1280)
  	{
  		if (camera.x >= 0) camera.x = 0;
  		if (camera.x <= -4476) camera.x = -4476;
  	}*/
  
  	//app->render->DrawTexture(img, 380, 100); // Placeholder not needed any more
  
  	// Draw map
  
  	// L03: DONE 7: Set the window title with map/tileset info
  	SString title("Map:%dx%d Tiles:%dx%d Tilesets:%d",
  				   app->map->mapData.width, app->map->mapData.height,
  				   app->map->mapData.tileWidth, app->map->mapData.tileHeight,
  				   app->map->mapData.tilesets.count());
  
  	app->win->SetTitle("Platform Game - LEVEL 1");
  
  	app->render->DrawTexture(background, -300, 1240, NULL, 0.75f);
  	app->map->Draw();
  
  	return true;
  }
  
  // Called each loop iteration
  bool Scene::PostUpdate()
  {
  	bool ret = true;
  
  	if(app->input->GetKey(SDL_SCANCODE_ESCAPE) == KEY_DOWN)
  		ret = false;
  
  	return ret;
  }
  
  // Called before quitting
  bool Scene::CleanUp()
  {
  	LOG("Freeing scene");
  
  	app->tex->UnLoad(background);
  	app->map->Disable();
  
  	return true;
  }
  
  void Scene::SaveGameRequest()
  {
  	pugi::xml_document saveFile;
  	pugi::xml_parse_result result = saveFile.load_file("save_game.xml");
  
  	pugi::xml_node save_node;
  	pugi::xml_node level;
  	pugi::xml_node player;
  	pugi::xml_node camera;
  
  	if (result == NULL)
  	{
  		pugi::xml_node declaration = saveFile.append_child(pugi::node_declaration);
  		declaration.append_attribute("version") = "1.0";
  
  		save_node = saveFile.append_child("game_state");
  
  		level = save_node.append_child("level");
  		level.append_attribute("value");
  
  		player = save_node.append_child("player");
  		player.append_attribute("x");
  		player.append_attribute("y");
  		player.append_attribute("state");
  
  		camera = save_node.append_child("camera");
  		camera.append_attribute("x");
  		camera.append_attribute("y");
  	}
  	else {
  		save_node = saveFile.child("game_state");
  		level = save_node.child("level");
  		player = save_node.child("player");
  		camera = save_node.child("camera");
  	}
  
  	level.attribute("value") = level;
  
  	app->player->SavePlayer(player);
  
  	camera.attribute("x") = app->render->camera.x;
  	camera.attribute("y") = app->render->camera.y;
  
  	saveFile.save_file("save_game.xml");
  
  	/*app->fadeToBlack->FadeToBlack(this, this);*/
  }
  
  void Scene::LoadGameRequest()
  {
  	pugi::xml_document saveFile;
  	pugi::xml_parse_result result = saveFile.load_file("save_game.xml");
  
  	if (result != NULL)
  	{
  		pugi::xml_node save_node = saveFile.child("game_state");
  
  		pugi::xml_node level = save_node.child("level");
  
  		pugi::xml_node player = save_node.child("player");
  		playerPosition.x = player.attribute("x").as_int();
  		playerPosition.y = player.attribute("y").as_int();
  		
  		pugi::xml_node camera = save_node.child("camera");
  		cameraPosition.x = camera.attribute("x").as_int();
  		cameraPosition.y = camera.attribute("y").as_int();
  
  		fromGameSaved = true;
  	}
  }
  