supertux
worldmap.hpp
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;
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();
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 
80  int available_directions_at(const Vector& pos) const;
81 
85  int tile_data_at(const Vector& pos) const;
86 
87  size_t level_count() const;
88  size_t solved_level_count() const;
89 
92  void finished_level(Level* level);
93 
94  Savegame& get_savegame() const { return m_savegame; }
95 
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 
115  bool path_ok(const Direction& direction, const Vector& pos, Vector* new_pos) const;
116 
118  void save_state();
119 
121  void load_state();
122 
123  const std::string& get_title() const { return m_name; }
124 
127  void change(const std::string& filename, const std::string& force_spawnpoint="");
128 
133  void move_to_spawnpoint(const std::string& spawnpoint, bool pan = false, bool main_as_default = true);
134 
136  void set_levels_solved(bool solved, bool perfect);
137 
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 
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 
161 protected:
162  virtual bool before_object_add(GameObject& object) override;
163  virtual void before_object_remove(GameObject& object) override;
164 
165 private:
166  void draw_status(DrawingContext& context);
167 
168  void load(const std::string& filename);
169  void on_escape_press();
170 
171 private:
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 
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;
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 
202 private:
203  WorldMap(const WorldMap&) = delete;
204  WorldMap& operator=(const WorldMap&) = delete;
205 };
206 
207 } // namespace worldmap
208 
209 #endif
210 
211 /* EOF */
This class keeps player status between different game sessions (for example when switching maps in th...
Definition: player_status.hpp:38
Definition: controller.hpp:56
Definition: spawn_point.hpp:29
int tile_data_at(const Vector &pos) const
returns a bitfield representing the union of all Tile::WORLDMAP_XXX values of all solid tiles at the ...
Definition: worldmap.cpp:418
Simple two dimensional vector.
Definition: vector.hpp:24
Definition: sprite.hpp:25
Definition: worldmap_state.hpp:25
SpawnPoint * get_spawnpoint_by_name(const std::string &spawnpoint_name) const
Get a spawnpoint by its name.
Definition: worldmap.hpp:98
Definition: game_object_manager.hpp:34
void set_initial_fade_tilemap(const std::string &tilemap_name, int direction)
Sets the name of the tilemap that should fade when worldmap is set up.
Definition: worldmap.hpp:139
void change(const std::string &filename, const std::string &force_spawnpoint="")
switch to another worldmap.
Definition: worldmap.cpp:160
The SquirrelEnvironment contains the environment in which a script is executed, meaning a root table ...
Definition: squirrel_environment.hpp:35
virtual bool before_object_add(GameObject &object) override
Hook that is called before an object is added to the vector.
Definition: worldmap.cpp:127
Definition: camera.hpp:25
Definition: object_settings.hpp:28
int available_directions_at(const Vector &pos) const
gets a bitfield of Tile::WORLDMAP_NORTH | Tile::WORLDMAP_WEST | ...
Definition: worldmap.cpp:432
Definition: level_tile.hpp:30
A &#39;Currenton&#39; allows access to the currently active instance of a class via the static current() func...
Definition: currenton.hpp:30
void finished_level(Level *level)
gets called from the GameSession when a level has been successfully finished
Definition: worldmap.cpp:244
void save_state()
Save worldmap state to squirrel state table.
Definition: worldmap.cpp:697
void set_initial_spawnpoint(const std::string &spawnpoint_name)
Sets the initial spawnpoint on worldmap setup.
Definition: worldmap.hpp:146
Represents a collection of Sectors running in a single GameSession.
Definition: level.hpp:29
Base class for all the things that make up Levels&#39; Sectors.
Definition: game_object.hpp:46
void load_state()
Load worldmap state from squirrel state table.
Definition: worldmap.cpp:690
Definition: savegame.hpp:66
void move_to_spawnpoint(const std::string &spawnpoint, bool pan=false, bool main_as_default=true)
Moves Tux to the given spawnpoint.
Definition: worldmap.cpp:140
Definition: worldmap_parser.hpp:28
Definition: tux.hpp:34
virtual void before_object_remove(GameObject &object) override
Hook that is called before an object is removed from the vector.
Definition: worldmap.cpp:134
Definition: special_tile.hpp:31
Definition: sprite_change.hpp:32
Definition: color.hpp:25
Definition: teleporter.hpp:30
void set_levels_solved(bool solved, bool perfect)
Mark all levels as solved or unsolved.
Definition: worldmap.cpp:661
Simple timer designed to be used in the update functions of objects.
Definition: timer.hpp:24
This class is responsible for drawing the level tiles.
Definition: tilemap.hpp:39
Definition: camera.hpp:36
Definition: tile_set.hpp:42
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42
bool path_ok(const Direction &direction, const Vector &pos, Vector *new_pos) const
Check if it is possible to walk from pos into direction, if possible, write the new position to new_p...
Definition: worldmap.cpp:204
Definition: worldmap.hpp:50