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
17#include "supertux/game_manager.hpp"
18
19#include "supertux/levelset_screen.hpp"
20#include "supertux/player_status.hpp"
21#include "supertux/savegame.hpp"
22#include "supertux/screen.hpp"
23#include "supertux/screen_fade.hpp"
24#include "supertux/screen_manager.hpp"
25#include "supertux/world.hpp"
26#include "util/log.hpp"
27#include "util/reader.hpp"
28#include "util/reader_document.hpp"
29#include "util/reader_mapping.hpp"
30#include "worldmap/worldmap.hpp"
31#include "worldmap/worldmap_screen.hpp"
32
33GameManager::GameManager() :
34 m_savegame(),
35 m_next_worldmap(),
36 m_next_spawnpoint()
37{
38}
39
40void
41GameManager::start_level(const World& world, const std::string& level_filename)
42{
43 m_savegame = Savegame::from_file(world.get_savegame_filename());
44
45 auto screen = std::make_unique<LevelsetScreen>(world.get_basedir(),
46 level_filename,
47 *m_savegame);
48 ScreenManager::current()->push_screen(std::move(screen));
49}
50
51void
52GameManager::start_worldmap(const World& world, const std::string& spawnpoint, const std::string& worldmap_filename)
53{
54 try
55 {
56 m_savegame = Savegame::from_file(world.get_savegame_filename());
57
58 auto filename = m_savegame->get_player_status().last_worldmap;
59 // If we specified a worldmap filename manually,
60 // this overrides the default choice of "last worldmap"
61 if (!worldmap_filename.empty())
62 {
63 filename = worldmap_filename;
64 }
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
74 auto worldmap = std::make_unique<worldmap::WorldMap>(filename, *m_savegame, spawnpoint);
75 auto worldmap_screen = std::make_unique<worldmap::WorldMapScreen>(std::move(worldmap));
76 ScreenManager::current()->push_screen(std::move(worldmap_screen));
77 }
78 catch(std::exception& e)
79 {
80 log_fatal << "Couldn't start world: " << e.what() << std::endl;
81 }
82}
83
84bool
85GameManager::load_next_worldmap()
86{
87 if (m_next_worldmap.empty())
88 {
89 return false;
90 }
91 std::unique_ptr<World> world = World::from_directory(m_next_worldmap);
92 m_next_worldmap = "";
93 if (!world)
94 {
95 log_warning << "Can't load world '" << m_next_worldmap << "'" << std::endl;
96 return false;
97 }
98 start_worldmap(*world, m_next_spawnpoint); // New world, new savegame
99 return true;
100}
101
102void
103GameManager::set_next_worldmap(const std::string& worldmap, const std::string &spawnpoint)
104{
105 m_next_worldmap = worldmap;
106 m_next_spawnpoint = spawnpoint;
107}
108
109/* EOF */
110