sector.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // SuperTux - A Jump'n Run
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  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. #ifndef HEADER_SUPERTUX_SUPERTUX_SECTOR_HPP
  17. #define HEADER_SUPERTUX_SUPERTUX_SECTOR_HPP
  18. #include "supertux/sector_base.hpp"
  19. #include <vector>
  20. #include <stdint.h>
  21. #include "collision/collision_system.hpp"
  22. #include "math/anchor_point.hpp"
  23. #include "math/easing.hpp"
  24. #include "math/fwd.hpp"
  25. #include "supertux/d_scope.hpp"
  26. #include "supertux/tile.hpp"
  27. #include "video/color.hpp"
  28. namespace collision {
  29. class Constraints;
  30. }
  31. class Camera;
  32. class CollisionGroundMovementManager;
  33. class DisplayEffect;
  34. class DrawingContext;
  35. class Level;
  36. class MovingObject;
  37. class Player;
  38. class ReaderMapping;
  39. class Rectf;
  40. class Size;
  41. class SpawnPointMarker;
  42. class TextObject;
  43. class TileMap;
  44. class Writer;
  45. /**
  46. * Represents one of (potentially) multiple, separate parts of a Level.
  47. Sectors contain GameObjects, e.g. Badguys and Players.
  48. */
  49. /**
  50. * @scripting
  51. * @summary This class provides additional controlling functions for a sector, other than the ones listed at ${SRG_REF_GameObjectManager}.
  52. * @instances An instance under ""sector.settings"" is available from scripts and the console.
  53. */
  54. class Sector final : public Base::Sector
  55. {
  56. friend class CollisionSystem;
  57. friend class EditorSectorMenu;
  58. public:
  59. static void register_class(ssq::VM& vm);
  60. private:
  61. static Sector* s_current;
  62. public:
  63. /** get currently activated sector. */
  64. static Sector& get() { assert(s_current != nullptr); return *s_current; }
  65. static Sector* current() { return s_current; }
  66. public:
  67. Sector(Level& parent);
  68. ~Sector() override;
  69. void finish_construction(bool editable) override;
  70. std::string get_exposed_class_name() const override { return "Sector"; }
  71. Level& get_level() const { return m_level; }
  72. TileSet* get_tileset() const override;
  73. bool in_worldmap() const override;
  74. /** activates this sector (change music, initialize player class, ...) */
  75. void activate(const std::string& spawnpoint);
  76. void activate(const Vector& player_pos);
  77. void deactivate();
  78. void draw(DrawingContext& context) override;
  79. void update(float dt_sec) override;
  80. void save(Writer &writer);
  81. /** stops all looping sounds in whole sector. */
  82. void stop_looping_sounds();
  83. /** Freeze camera position for this frame, preventing camera interpolation jumps and loops */
  84. void pause_camera_interpolation();
  85. /** continues the looping sounds in whole sector. */
  86. void play_looping_sounds();
  87. /** tests if a given rectangle is inside the sector
  88. (a rectangle that is on top of the sector is considered inside) */
  89. bool inside(const Rectf& rectangle) const;
  90. /** Checks if the specified rectangle is free of (solid) tiles.
  91. Note that this does not include static objects, e.g. bonus blocks. */
  92. bool is_free_of_tiles(const Rectf& rect, const bool ignoreUnisolid = false, uint32_t tiletype = Tile::SOLID) const;
  93. /**
  94. * @scripting
  95. * @description Checks if the specified sector-relative rectangle is free of solid tiles.
  96. * @param float $left
  97. * @param float $top
  98. * @param float $right
  99. * @param float $bottom
  100. * @param bool $ignore_unisolid If ""true"", unisolid tiles will be ignored.
  101. */
  102. bool is_free_of_solid_tiles(float left, float top, float right, float bottom,
  103. bool ignore_unisolid) const;
  104. /** Checks if the specified rectangle is free of both
  105. 1.) solid tiles and
  106. 2.) MovingObjects in COLGROUP_STATIC.
  107. Note that this does not include badguys or players. */
  108. bool is_free_of_statics(const Rectf& rect, const MovingObject* ignore_object = nullptr, const bool ignoreUnisolid = false) const;
  109. /**
  110. * @scripting
  111. * @description Checks if the specified sector-relative rectangle is free of both:
  112. 1) Solid tiles.
  113. 2) ""MovingObject""s in ""COLGROUP_STATIC"".
  114. Note: This does not include badguys or players.
  115. * @param float $left
  116. * @param float $top
  117. * @param float $right
  118. * @param float $bottom
  119. * @param bool $ignore_unisolid If ""true"", unisolid tiles will be ignored.
  120. */
  121. bool is_free_of_statics(float left, float top, float right, float bottom,
  122. bool ignore_unisolid) const;
  123. /** Checks if the specified rectangle is free of both
  124. 1.) solid tiles and
  125. 2.) MovingObjects in COLGROUP_STATIC, COLGROUP_MOVINGSTATIC or COLGROUP_MOVING.
  126. This includes badguys and players. */
  127. bool is_free_of_movingstatics(const Rectf& rect, const MovingObject* ignore_object = nullptr) const;
  128. /**
  129. * @scripting
  130. * @description Checks if the specified sector-relative rectangle is free of both:
  131. 1) Solid tiles.
  132. 2) ""MovingObject""s in ""COLGROUP_STATIC"", ""COLGROUP_MOVINGSTATIC"" or ""COLGROUP_MOVING"".
  133. This includes badguys and players.
  134. * @param float $left
  135. * @param float $top
  136. * @param float $right
  137. * @param float $bottom
  138. */
  139. bool is_free_of_movingstatics(float left, float top, float right, float bottom) const;
  140. /** Checks if the specified rectangle is free of MovingObjects in COLGROUP_MOVINGSTATIC.
  141. Note that this does not include moving badguys, or players */
  142. bool is_free_of_specifically_movingstatics(const Rectf& rect, const MovingObject* ignore_object = nullptr) const;
  143. /**
  144. * @scripting
  145. * @description Checks if the specified sector-relative rectangle is free of ""MovingObject""s in ""COLGROUP_MOVINGSTATIC"".
  146. Note: This does not include moving badguys or players.
  147. * @param float $left
  148. * @param float $top
  149. * @param float $right
  150. * @param float $bottom
  151. */
  152. bool is_free_of_specifically_movingstatics(float left, float top, float right, float bottom) const;
  153. CollisionSystem::RaycastResult get_first_line_intersection(const Vector& line_start,
  154. const Vector& line_end,
  155. bool ignore_objects,
  156. const CollisionObject* ignore_object) const;
  157. bool free_line_of_sight(const Vector& line_start, const Vector& line_end, bool ignore_objects = false, const MovingObject* ignore_object = nullptr) const;
  158. bool can_see_player(const Vector& eye) const;
  159. Player* get_nearest_player(const Vector& pos) const;
  160. Player* get_nearest_player(const Rectf& pos) const
  161. {
  162. return get_nearest_player(get_anchor_pos(pos, ANCHOR_MIDDLE));
  163. }
  164. std::vector<MovingObject*> get_nearby_objects (const Vector& center, float max_distance) const;
  165. Rectf get_active_region() const;
  166. int get_foremost_opaque_layer() const;
  167. int get_foremost_layer() const;
  168. /** returns the editor size (in tiles) of a sector */
  169. Size get_editor_size() const;
  170. /** resize all tilemaps with given size */
  171. void resize_sector(const Size& old_size, const Size& new_size, const Size& resize_offset);
  172. /** globally changes solid tilemaps' tile ids */
  173. void change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id);
  174. /**
  175. * @scripting
  176. * Sets the sector's gravity.
  177. * @param float $gravity
  178. */
  179. void set_gravity(float gravity);
  180. /**
  181. * @scripting
  182. * Returns the sector's gravity.
  183. * @param float $gravity
  184. */
  185. float get_gravity() const;
  186. Camera& get_camera() const;
  187. std::vector<Player*> get_players() const;
  188. DisplayEffect& get_effect() const;
  189. TextObject& get_text_object() const { return m_text_object; }
  190. Vector get_spawn_point_position(const std::string& spawnpoint);
  191. private:
  192. uint32_t collision_tile_attributes(const Rectf& dest, const Vector& mov) const;
  193. virtual bool before_object_add(GameObject& object) override;
  194. virtual void before_object_remove(GameObject& object) override;
  195. int calculate_foremost_layer(bool including_transparent = true) const;
  196. /** Convert tiles into their corresponding GameObjects (e.g.
  197. bonusblocks, add light to lava tiles) */
  198. void convert_tiles2gameobject();
  199. SpawnPointMarker* get_spawn_point(const std::string& spawnpoint);
  200. private:
  201. Level& m_level; // Parent level
  202. bool m_fully_constructed;
  203. int m_foremost_layer;
  204. int m_foremost_opaque_layer;
  205. /**
  206. * @scripting
  207. * @description The sector's gravity.
  208. */
  209. float m_gravity;
  210. std::unique_ptr<CollisionSystem> m_collision_system;
  211. TextObject& m_text_object;
  212. Vector m_last_translation; // For camera interpolation at high frame rates
  213. float m_last_scale;
  214. float m_last_dt;
  215. private:
  216. Sector(const Sector&) = delete;
  217. Sector& operator=(const Sector&) = delete;
  218. };
  219. #endif
  220. /* EOF */