1// SuperTux
2// Copyright (C) 2004 Ingo Ruhnke <grumbel@gmail.com>
3// Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18#ifndef HEADER_SUPERTUX_WORLDMAP_WORLDMAP_HPP
19#define HEADER_SUPERTUX_WORLDMAP_WORLDMAP_HPP
20
21#include <vector>
22
23#include "math/vector.hpp"
24#include "supertux/game_object_manager.hpp"
25#include "squirrel/squirrel_environment.hpp"
26#include "supertux/statistics.hpp"
27#include "supertux/timer.hpp"
28#include "util/currenton.hpp"
29#include "worldmap/direction.hpp"
30#include "worldmap/spawn_point.hpp"
31
32class Controller;
33class Level;
34class PlayerStatus;
35class Savegame;
36class Sprite;
37class SquirrelEnvironment;
38class TileMap;
39class TileSet;
40
41namespace worldmap {
42
43class Camera;
44class LevelTile;
45class SpecialTile;
46class SpriteChange;
47class Teleporter;
48class Tux;
49
50class WorldMap final : public GameObjectManager,
51 public Currenton<WorldMap>
52{
53public:
54 friend class WorldMapParser;
55 friend class WorldMapState;
56
57 static Color level_title_color;
58 static Color message_color;
59 static Color teleporter_message_color;
60
61public:
62 WorldMap(const std::string& filename, Savegame& savegame, const std::string& force_spawnpoint = "");
63 ~WorldMap();
64
65 void finish_construction();
66
67 void setup();
68 void leave();
69
70 void draw(DrawingContext& context);
71 void update(float dt_sec);
72
73 void process_input(const Controller& controller);
74
75 Vector get_next_tile(const Vector& pos, const Direction& direction) const;
76
77 /** gets a bitfield of Tile::WORLDMAP_NORTH | Tile::WORLDMAP_WEST |
78 ... values, which indicates the directions Tux can move to when
79 at the given position. */
80 int available_directions_at(const Vector& pos) const;
81
82 /** returns a bitfield representing the union of all
83 Tile::WORLDMAP_XXX values of all solid tiles at the given
84 position */
85 int tile_data_at(const Vector& pos) const;
86
87 size_t level_count() const;
88 size_t solved_level_count() const;
89
90 /** gets called from the GameSession when a level has been successfully
91 finished */
92 void finished_level(Level* level);
93
94 Savegame& get_savegame() const { return m_savegame; }
95
96 /** Get a spawnpoint by its name @param name The name of the
97 spawnpoint @return spawnpoint corresponding to that name */
98 SpawnPoint* get_spawnpoint_by_name(const std::string& spawnpoint_name) const
99 {
100 for (const auto& sp : m_spawn_points) {
101 if (sp->get_name() == spawnpoint_name) {
102 return sp.get();
103 }
104 }
105 return nullptr;
106 }
107
108 LevelTile* at_level() const;
109 SpecialTile* at_special_tile() const;
110 SpriteChange* at_sprite_change(const Vector& pos) const;
111 Teleporter* at_teleporter(const Vector& pos) const;
112
113 /** Check if it is possible to walk from \a pos into \a direction,
114 if possible, write the new position to \a new_pos */
115 bool path_ok(const Direction& direction, const Vector& pos, Vector* new_pos) const;
116
117 /** Save worldmap state to squirrel state table */
118 void save_state();
119
120 /** Load worldmap state from squirrel state table */
121 void load_state();
122
123 const std::string& get_title() const { return m_name; }
124
125 /** switch to another worldmap.
126 filename is relative to data root path */
127 void change(const std::string& filename, const std::string& force_spawnpoint="");
128
129 /** Moves Tux to the given spawnpoint
130 @param spawnpoint Name of the spawnpoint to move to
131 @param pan Pan the camera during to new spawnpoint
132 @param main_as_default Move Tux to main spawnpoint if specified spawnpoint can't be found */
133 void move_to_spawnpoint(const std::string& spawnpoint, bool pan = false, bool main_as_default = true);
134
135 /** Mark all levels as solved or unsolved */
136 void set_levels_solved(bool solved, bool perfect);
137
138 /** Sets the name of the tilemap that should fade when worldmap is set up. */
139 void set_initial_fade_tilemap(const std::string& tilemap_name, int direction)
140 {
141 m_initial_fade_tilemap = tilemap_name;
142 m_fade_direction = direction;
143 }
144
145 /** Sets the initial spawnpoint on worldmap setup */
146 void set_initial_spawnpoint(const std::string& spawnpoint_name)
147 {
148 m_force_spawnpoint = spawnpoint_name;
149
150 // If spawnpoint we specified can not be found,
151 // don't bother moving to the main spawnpoint.
152 m_main_is_default = false;
153 }
154
155 void run_script(const std::string& script, const std::string& sourcename);
156
157 void set_passive_message(const std::string& message, float time);
158
159 Camera& get_camera() const { return *m_camera; }
160
161protected:
162 virtual bool before_object_add(GameObject& object) override;
163 virtual void before_object_remove(GameObject& object) override;
164
165private:
166 void draw_status(DrawingContext& context);
167
168 void load(const std::string& filename);
169 void on_escape_press();
170
171private:
172 std::unique_ptr<SquirrelEnvironment> m_squirrel_environment;
173 std::unique_ptr<Camera> m_camera;
174
175 bool m_enter_level;
176
177 Tux* m_tux;
178
179 Savegame& m_savegame;
180
181 TileSet* m_tileset;
182
183 std::string m_name;
184 std::string m_init_script;
185
186 /** Variables to deal with the passive map messages */
187 Timer m_passive_message_timer;
188 std::string m_passive_message;
189
190 std::string m_map_filename;
191 std::string m_levels_path;
192
193 std::vector<std::unique_ptr<SpawnPoint> > m_spawn_points;
194
195 std::string m_force_spawnpoint; /**< if set, spawnpoint will be forced to this value */
196 bool m_main_is_default;
197 std::string m_initial_fade_tilemap;
198 int m_fade_direction;
199
200 bool m_in_level;
201
202private:
203 WorldMap(const WorldMap&) = delete;
204 WorldMap& operator=(const WorldMap&) = delete;
205};
206
207} // namespace worldmap
208
209#endif
210
211/* EOF */
212