game_manager.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SuperTux
  2. // Copyright (C) 2013 Ingo Ruhnke <grumbel@gmail.com>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "supertux/game_manager.hpp"
  17. #include "editor/editor.hpp"
  18. #include "sdk/integration.hpp"
  19. #include "supertux/levelset_screen.hpp"
  20. #include "supertux/player_status.hpp"
  21. #include "supertux/profile.hpp"
  22. #include "supertux/savegame.hpp"
  23. #include "supertux/screen.hpp"
  24. #include "supertux/screen_fade.hpp"
  25. #include "supertux/screen_manager.hpp"
  26. #include "supertux/world.hpp"
  27. #include "util/log.hpp"
  28. #include "util/reader.hpp"
  29. #include "util/reader_document.hpp"
  30. #include "util/reader_mapping.hpp"
  31. #include "worldmap/tux.hpp"
  32. #include "worldmap/worldmap.hpp"
  33. #include "worldmap/worldmap_screen.hpp"
  34. GameManager::GameManager() :
  35. m_savegame(),
  36. m_next_worldmap()
  37. {
  38. }
  39. void
  40. GameManager::start_level(const World& world, const std::string& level_filename,
  41. const std::optional<std::pair<std::string, Vector>>& start_pos)
  42. {
  43. m_savegame = Savegame::from_current_profile(world.get_basename());
  44. auto screen = std::make_unique<LevelsetScreen>(world.get_basedir(),
  45. level_filename,
  46. *m_savegame,
  47. start_pos);
  48. ScreenManager::current()->push_screen(std::move(screen));
  49. if (!Editor::current())
  50. m_savegame->get_profile().set_last_world(world.get_basename());
  51. }
  52. void
  53. GameManager::start_worldmap(const World& world, const std::string& worldmap_filename,
  54. const std::string& sector, const std::string& spawnpoint)
  55. {
  56. try
  57. {
  58. m_savegame = Savegame::from_current_profile(world.get_basename());
  59. auto filename = m_savegame->get_player_status().last_worldmap;
  60. // If we specified a worldmap filename manually,
  61. // this overrides the default choice of "last worldmap".
  62. if (!worldmap_filename.empty())
  63. {
  64. filename = worldmap_filename;
  65. }
  66. // No "last worldmap" found and no worldmap_filename
  67. // specified. Let's go ahead and use the worldmap
  68. // filename specified in the world.
  69. if (filename.empty())
  70. {
  71. filename = world.get_worldmap_filename();
  72. }
  73. auto worldmap = std::make_unique<worldmap::WorldMap>(filename, *m_savegame, sector, spawnpoint);
  74. auto worldmap_screen = std::make_unique<worldmap::WorldMapScreen>(std::move(worldmap));
  75. ScreenManager::current()->push_screen(std::move(worldmap_screen));
  76. if (!Editor::current())
  77. m_savegame->get_profile().set_last_world(world.get_basename());
  78. }
  79. catch(std::exception& e)
  80. {
  81. log_fatal << "Couldn't start world: " << e.what() << std::endl;
  82. }
  83. }
  84. void
  85. GameManager::start_worldmap(const World& world, const std::string& worldmap_filename,
  86. const std::optional<std::pair<std::string, Vector>>& start_pos)
  87. {
  88. start_worldmap(world, worldmap_filename, start_pos ? start_pos->first : "");
  89. if (start_pos)
  90. worldmap::WorldMapSector::current()->get_tux().set_initial_pos(start_pos->second);
  91. }
  92. bool
  93. GameManager::load_next_worldmap()
  94. {
  95. if (!m_next_worldmap)
  96. return false;
  97. const auto next_worldmap = std::move(*m_next_worldmap);
  98. m_next_worldmap.reset();
  99. std::unique_ptr<World> world = World::from_directory(next_worldmap.world);
  100. if (!world)
  101. {
  102. log_warning << "Cannot load world '" << next_worldmap.world << "'" << std::endl;
  103. return false;
  104. }
  105. start_worldmap(*world, "", next_worldmap.sector, next_worldmap.spawnpoint); // New world, new savegame.
  106. return true;
  107. }
  108. void
  109. GameManager::set_next_worldmap(const std::string& world, const std::string& sector,
  110. const std::string& spawnpoint)
  111. {
  112. m_next_worldmap.emplace(world, sector, spawnpoint);
  113. }
  114. /* EOF */