sector.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. inline 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,
  109. const bool ignoreUnisolid = false, uint32_t tiletype = Tile::SOLID) const;
  110. /**
  111. * @scripting
  112. * @description Checks if the specified sector-relative rectangle is free of both:
  113. 1) Solid tiles.
  114. 2) ""MovingObject""s in ""COLGROUP_STATIC"".
  115. Note: This does not include badguys or players.
  116. * @param float $left
  117. * @param float $top
  118. * @param float $right
  119. * @param float $bottom
  120. * @param bool $ignore_unisolid If ""true"", unisolid tiles will be ignored.
  121. */
  122. bool is_free_of_statics(float left, float top, float right, float bottom,
  123. bool ignore_unisolid) const;
  124. /** Checks if the specified rectangle is free of both
  125. 1.) solid tiles and
  126. 2.) MovingObjects in COLGROUP_STATIC, COLGROUP_MOVINGSTATIC or COLGROUP_MOVING.
  127. This includes badguys and players. */
  128. bool is_free_of_movingstatics(const Rectf& rect, const MovingObject* ignore_object = nullptr,
  129. bool ignore_unisolid = false) const;
  130. /**
  131. * @scripting
  132. * @description Checks if the specified sector-relative rectangle is free of both:
  133. 1) Solid tiles.
  134. 2) ""MovingObject""s in ""COLGROUP_STATIC"", ""COLGROUP_MOVINGSTATIC"" or ""COLGROUP_MOVING"".
  135. This includes badguys and players.
  136. * @param float $left
  137. * @param float $top
  138. * @param float $right
  139. * @param float $bottom
  140. */
  141. bool is_free_of_movingstatics(float left, float top, float right, float bottom) const;
  142. /** Checks if the specified rectangle is free of MovingObjects in COLGROUP_MOVINGSTATIC.
  143. Note that this does not include moving badguys, or players */
  144. bool is_free_of_specifically_movingstatics(const Rectf& rect, const MovingObject* ignore_object = nullptr) const;
  145. /**
  146. * @scripting
  147. * @description Checks if the specified sector-relative rectangle is free of ""MovingObject""s in ""COLGROUP_MOVINGSTATIC"".
  148. Note: This does not include moving badguys or players.
  149. * @param float $left
  150. * @param float $top
  151. * @param float $right
  152. * @param float $bottom
  153. */
  154. bool is_free_of_specifically_movingstatics(float left, float top, float right, float bottom) const;
  155. CollisionSystem::RaycastResult get_first_line_intersection(const Vector& line_start,
  156. const Vector& line_end,
  157. CollisionSystem::RaycastIgnore ignore,
  158. const CollisionObject* ignore_object) const;
  159. CollisionSystem::RaycastResult get_first_line_intersection(const Vector& line_start,
  160. const Vector& line_end,
  161. bool ignore_objects,
  162. const CollisionObject* ignore_object) const;
  163. bool free_line_of_sight(const Vector& line_start, const Vector& line_end, bool ignore_objects = false, const MovingObject* ignore_object = nullptr) const;
  164. bool can_see_player(const Vector& eye) const;
  165. Player* get_nearest_player(const Vector& pos) const;
  166. Player* get_nearest_player(const Rectf& pos) const
  167. {
  168. return get_nearest_player(get_anchor_pos(pos, ANCHOR_MIDDLE));
  169. }
  170. std::vector<MovingObject*> get_nearby_objects (const Vector& center, float max_distance) const;
  171. Rectf get_active_region() const;
  172. inline int get_foremost_opaque_layer() const { return m_foremost_opaque_layer; }
  173. inline int get_foremost_layer() const { return m_foremost_layer; }
  174. /** returns the editor size (in tiles) of a sector */
  175. Size get_editor_size() const;
  176. /** resize all tilemaps with given size */
  177. void resize(const Size& old_size, const Size& new_size, const Size& resize_offset);
  178. /** globally changes solid tilemaps' tile ids */
  179. void change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id);
  180. /**
  181. * @scripting
  182. * Sets the sector's gravity.
  183. * @param float $gravity
  184. */
  185. inline void set_gravity(float gravity) { m_gravity = gravity; }
  186. /**
  187. * @scripting
  188. * Returns the sector's gravity.
  189. * @param float $gravity
  190. */
  191. inline float get_gravity() const { return m_gravity; }
  192. Camera& get_camera() const;
  193. DisplayEffect& get_effect() const;
  194. inline TextObject& get_text_object() const { return m_text_object; }
  195. std::vector<Player*> get_players() const;
  196. Vector get_spawn_point_position(const std::string& spawnpoint);
  197. private:
  198. uint32_t collision_tile_attributes(const Rectf& dest, const Vector& mov) const;
  199. virtual bool before_object_add(GameObject& object) override;
  200. virtual void before_object_remove(GameObject& object) override;
  201. int calculate_foremost_layer(bool including_transparent = true) const;
  202. /** Convert tiles into their corresponding GameObjects (e.g.
  203. bonusblocks, add light to lava tiles) */
  204. void convert_tiles2gameobject();
  205. SpawnPointMarker* get_spawn_point(const std::string& spawnpoint);
  206. private:
  207. Level& m_level; // Parent level
  208. int m_foremost_layer;
  209. int m_foremost_opaque_layer;
  210. /**
  211. * @scripting
  212. * @description The sector's gravity.
  213. */
  214. float m_gravity;
  215. std::unique_ptr<CollisionSystem> m_collision_system;
  216. TextObject& m_text_object;
  217. Vector m_last_translation; // For camera interpolation at high frame rates
  218. float m_last_scale;
  219. float m_last_dt;
  220. private:
  221. Sector(const Sector&) = delete;
  222. Sector& operator=(const Sector&) = delete;
  223. };
  224. #endif
  225. /* EOF */