Chargement dynamique de la liste des niveaux dans le menu
This commit is contained in:
		
							parent
							
								
									2aafd24037
								
							
						
					
					
						commit
						f8c87e1f76
					
				|  | @ -24,7 +24,7 @@ private: | |||
|     GravityDirection gravity_direction; | ||||
| 
 | ||||
|     sf::String name; | ||||
|     std::string current_file; | ||||
|     std::string current_path; | ||||
|     int total_time; | ||||
| 
 | ||||
|     sf::Sprite background_sprite; | ||||
|  | @ -69,9 +69,10 @@ public: | |||
|     void load(); | ||||
| 
 | ||||
|     /**
 | ||||
|      * Charge le niveau de jeu donné | ||||
|      * Charge le niveau de jeu donné depuis le fichier | ||||
|      * dont le chemin complet absolu est en paramètre | ||||
|      */ | ||||
|     void load(std::string filename); | ||||
|     void load(std::string path); | ||||
| 
 | ||||
|     /**
 | ||||
|      * Écrase le niveau chargé avec les données en mémoire | ||||
|  | @ -80,8 +81,9 @@ public: | |||
| 
 | ||||
|     /**
 | ||||
|      * Sauvegarde la configuration actuelle dans le fichier donné | ||||
|      * dont le chemin complet absolu est donné en paramètre | ||||
|      */ | ||||
|     void save(std::string filename); | ||||
|     void save(std::string path); | ||||
| 
 | ||||
|     /**
 | ||||
|      * Appelé par le manager lorsque l'état est utilisé | ||||
|  |  | |||
|  | @ -32,9 +32,10 @@ protected: | |||
|     void launchEditor(); | ||||
| 
 | ||||
|     /**
 | ||||
|      * Démarre le jeu avec le niveau donné | ||||
|      * Démarre le jeu avec le niveau dont le chemin | ||||
|      * complet absolu est donné en paramètre | ||||
|      */ | ||||
|     void launchGame(std::string name); | ||||
|     void launchGame(std::string path); | ||||
| 
 | ||||
|     /**
 | ||||
|      * Quitte le jeu et le menu | ||||
|  |  | |||
|  | @ -54,6 +54,11 @@ public: | |||
|      */ | ||||
|     std::string getLevelPath(std::string name); | ||||
| 
 | ||||
|     /**
 | ||||
|      * Récupère la liste de tous les niveaux | ||||
|      */ | ||||
|     std::vector<std::string> getLevelList(); | ||||
| 
 | ||||
|     /**
 | ||||
|      * Démarre la musique de fond donnée | ||||
|      */ | ||||
|  |  | |||
							
								
								
									
										21
									
								
								src/menu.cpp
								
								
								
								
							
							
						
						
									
										21
									
								
								src/menu.cpp
								
								
								
								
							|  | @ -162,14 +162,13 @@ void Menu::loadLevelMenu() { | |||
|     actions.clear(); | ||||
|     selection = 0; | ||||
| 
 | ||||
|     choices.push_back(sf::String(L"Tutoriel")); | ||||
|     actions.push_back(std::bind(&Menu::launchGame, this, "level1.dat")); | ||||
|     std::vector<std::string> path_list = getResourceManager().getLevelList(); | ||||
|     std::vector<std::string> name_list; | ||||
| 
 | ||||
|     choices.push_back(sf::String(L"Niveau 1")); | ||||
|     actions.push_back(std::bind(&Menu::launchGame, this, "level2.dat")); | ||||
| 
 | ||||
|     choices.push_back(sf::String(L"Niveau 2")); | ||||
|     actions.push_back(std::bind(&Menu::launchGame, this, "level3.dat")); | ||||
|     for (auto it = path_list.begin(); it != path_list.end(); it++) { | ||||
|         choices.push_back(Level::getLevelName(*it)); | ||||
|         actions.push_back(std::bind(&Menu::launchGame, this, *it)); | ||||
|     } | ||||
| 
 | ||||
|     choices.push_back(sf::String(L"Retour")); | ||||
|     actions.push_back(std::bind(&Menu::loadMainMenu, this)); | ||||
|  | @ -182,14 +181,14 @@ void Menu::loadRules() { | |||
| void Menu::launchEditor() { | ||||
|     std::shared_ptr<Editor> editor = std::shared_ptr<Editor>(new Editor(getManager())); | ||||
| 
 | ||||
|     // TODO: charger dynamiquement le niveau
 | ||||
|     editor->load(); | ||||
|     // TODO: charger dynamiquement le niveau dans l'éditeur
 | ||||
|     editor->load(getResourceManager().getLevelPath("editor_result.dat")); | ||||
|     getManager().setState(editor); | ||||
| } | ||||
| 
 | ||||
| void Menu::launchGame(std::string name) { | ||||
| void Menu::launchGame(std::string path) { | ||||
|     std::shared_ptr<Game> game = std::shared_ptr<Game>(new Game(getManager())); | ||||
|     game->load(name); | ||||
|     game->load(path); | ||||
|     getManager().setState(game); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -1,6 +1,6 @@ | |||
| #include "resource_manager.hpp" | ||||
| #include <iostream> | ||||
| #include <cstring> | ||||
| #include <algorithm> | ||||
| 
 | ||||
| using dir_iter = boost::filesystem::directory_iterator; | ||||
| using fs_path = boost::filesystem::path; | ||||
|  | @ -88,7 +88,29 @@ sf::Font& ResourceManager::getFont(std::string name) { | |||
| } | ||||
| 
 | ||||
| std::string ResourceManager::getLevelPath(std::string name) { | ||||
|     return boost::filesystem::canonical(levels_path / name).string(); | ||||
|     return (levels_path / name).string(); | ||||
| } | ||||
| 
 | ||||
| std::vector<std::string> ResourceManager::getLevelList() { | ||||
|     boost::filesystem::directory_iterator iter(levels_path); | ||||
|     std::vector<boost::filesystem::path> list; | ||||
|     std::vector<std::string> path_list; | ||||
| 
 | ||||
|     // récupération de la liste de tous les niveaux
 | ||||
|     std::copy( | ||||
|         iter, boost::filesystem::directory_iterator(), | ||||
|         std::back_inserter(list) | ||||
|     ); | ||||
| 
 | ||||
|     // tri par ordre alphabétique
 | ||||
|     std::sort(list.begin(), list.end()); | ||||
| 
 | ||||
|     // conversion en chemins absolus
 | ||||
|     for (auto it = list.begin(); it != list.end(); it++) { | ||||
|         path_list.push_back((*it).string()); | ||||
|     } | ||||
| 
 | ||||
|     return path_list; | ||||
| } | ||||
| 
 | ||||
| void ResourceManager::playMusic(std::string name) { | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue