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 
32 class Controller;
33 class Level;
34 class PlayerStatus;
35 class Savegame;
36 class Sprite;
37 class SquirrelEnvironment;
38 class TileMap;
39 class TileSet;
40 
41 namespace worldmap {
42 
43 class Camera;
44 class LevelTile;
45 class SpecialTile;
46 class SpriteChange;
47 class Teleporter;
48 class Tux;
49 
50 class WorldMap final : public GameObjectManager,
51                        public Currenton<WorldMap>
52 {
53 public:
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 
61 public:
62   WorldMap(const std::string& filename, Savegame& savegame, const std::string& force_spawnpoint = "");
63   ~WorldMap() override;
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 
get_savegame() const94   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 */
get_spawnpoint_by_name(const std::string & spawnpoint_name) const98   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 
get_title() const123   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. */
set_initial_fade_tilemap(const std::string & tilemap_name,int direction)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 */
set_initial_spawnpoint(const std::string & spawnpoint_name)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 
get_camera() const159   Camera& get_camera() const { return *m_camera; }
160 
161   Vector get_tux_pos();
162 
163 protected:
164   virtual bool before_object_add(GameObject& object) override;
165   virtual void before_object_remove(GameObject& object) override;
166 
167 private:
168   void draw_status(DrawingContext& context);
169 
170   void load(const std::string& filename);
171   void on_escape_press();
172 
173 private:
174   std::unique_ptr<SquirrelEnvironment> m_squirrel_environment;
175   std::unique_ptr<Camera> m_camera;
176 
177   bool m_enter_level;
178 
179   Tux* m_tux;
180 
181   Savegame& m_savegame;
182 
183   TileSet* m_tileset;
184 
185   std::string m_name;
186   std::string m_init_script;
187 
188   /** Variables to deal with the passive map messages */
189   Timer m_passive_message_timer;
190   std::string m_passive_message;
191 
192   std::string m_map_filename;
193   std::string m_levels_path;
194 
195   std::vector<std::unique_ptr<SpawnPoint> > m_spawn_points;
196 
197   std::string m_force_spawnpoint; /**< if set, spawnpoint will be forced to this value */
198   bool m_main_is_default;
199   std::string m_initial_fade_tilemap;
200   int m_fade_direction;
201 
202   bool m_in_level;
203 
204 private:
205   WorldMap(const WorldMap&) = delete;
206   WorldMap& operator=(const WorldMap&) = delete;
207 };
208 
209 } // namespace worldmap
210 
211 #endif
212 
213 /* EOF */
214