sector.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. /** Represents one of (potentially) multiple, separate parts of a Level.
  46. Sectors contain GameObjects, e.g. Badguys and Players. */
  47. class Sector final : public Base::Sector
  48. {
  49. public:
  50. friend class CollisionSystem;
  51. friend class EditorSectorMenu;
  52. private:
  53. static Sector* s_current;
  54. public:
  55. /** get currently activated sector. */
  56. static Sector& get() { assert(s_current != nullptr); return *s_current; }
  57. static Sector* current() { return s_current; }
  58. public:
  59. Sector(Level& parent);
  60. ~Sector() override;
  61. void finish_construction(bool editable) override;
  62. Level& get_level() const { return m_level; }
  63. TileSet* get_tileset() const override;
  64. bool in_worldmap() const override;
  65. /** activates this sector (change music, initialize player class, ...) */
  66. void activate(const std::string& spawnpoint);
  67. void activate(const Vector& player_pos);
  68. void deactivate();
  69. void draw(DrawingContext& context) override;
  70. void update(float dt_sec) override;
  71. void save(Writer &writer);
  72. /** stops all looping sounds in whole sector. */
  73. void stop_looping_sounds();
  74. /** continues the looping sounds in whole sector. */
  75. void play_looping_sounds();
  76. /** tests if a given rectangle is inside the sector
  77. (a rectangle that is on top of the sector is considered inside) */
  78. bool inside(const Rectf& rectangle) const;
  79. /** Checks if the specified rectangle is free of (solid) tiles.
  80. Note that this does not include static objects, e.g. bonus blocks. */
  81. bool is_free_of_tiles(const Rectf& rect, const bool ignoreUnisolid = false, uint32_t tiletype = Tile::SOLID) const;
  82. /** Checks if the specified rectangle is free of both
  83. 1.) solid tiles and
  84. 2.) MovingObjects in COLGROUP_STATIC.
  85. Note that this does not include badguys or players. */
  86. bool is_free_of_statics(const Rectf& rect, const MovingObject* ignore_object = nullptr, const bool ignoreUnisolid = false) const;
  87. /** Checks if the specified rectangle is free of both
  88. 1.) solid tiles and
  89. 2.) MovingObjects in COLGROUP_STATIC, COLGROUP_MOVINGSTATIC or COLGROUP_MOVING.
  90. This includes badguys and players. */
  91. bool is_free_of_movingstatics(const Rectf& rect, const MovingObject* ignore_object = nullptr) const;
  92. /** Checks if the specified rectangle is free of MovingObjects in COLGROUP_MOVINGSTATIC.
  93. Note that this does not include moving badguys, or players */
  94. bool is_free_of_specifically_movingstatics(const Rectf& rect, const MovingObject* ignore_object = nullptr) const;
  95. CollisionSystem::RaycastResult get_first_line_intersection(const Vector& line_start,
  96. const Vector& line_end,
  97. bool ignore_objects,
  98. const CollisionObject* ignore_object) const;
  99. bool free_line_of_sight(const Vector& line_start, const Vector& line_end, bool ignore_objects = false, const MovingObject* ignore_object = nullptr) const;
  100. bool can_see_player(const Vector& eye) const;
  101. Player* get_nearest_player (const Vector& pos) const;
  102. Player* get_nearest_player (const Rectf& pos) const {
  103. return (get_nearest_player (get_anchor_pos (pos, ANCHOR_MIDDLE)));
  104. }
  105. std::vector<MovingObject*> get_nearby_objects (const Vector& center, float max_distance) const;
  106. Rectf get_active_region() const;
  107. int get_foremost_opaque_layer() const;
  108. int get_foremost_layer() const;
  109. /** returns the editor size (in tiles) of a sector */
  110. Size get_editor_size() const;
  111. /** resize all tilemaps with given size */
  112. void resize_sector(const Size& old_size, const Size& new_size, const Size& resize_offset);
  113. /** globally changes solid tilemaps' tile ids */
  114. void change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id);
  115. /** set gravity throughout sector */
  116. void set_gravity(float gravity);
  117. float get_gravity() const { return m_gravity; }
  118. Camera& get_camera() const;
  119. std::vector<Player*> get_players() const;
  120. DisplayEffect& get_effect() const;
  121. TextObject& get_text_object() const { return m_text_object; }
  122. Vector get_spawn_point_position(const std::string& spawnpoint);
  123. private:
  124. uint32_t collision_tile_attributes(const Rectf& dest, const Vector& mov) const;
  125. virtual bool before_object_add(GameObject& object) override;
  126. virtual void before_object_remove(GameObject& object) override;
  127. int calculate_foremost_layer(bool including_transparent = true) const;
  128. /** Convert tiles into their corresponding GameObjects (e.g.
  129. bonusblocks, add light to lava tiles) */
  130. void convert_tiles2gameobject();
  131. SpawnPointMarker* get_spawn_point(const std::string& spawnpoint);
  132. private:
  133. Level& m_level; // Parent level
  134. bool m_fully_constructed;
  135. int m_foremost_layer;
  136. int m_foremost_opaque_layer;
  137. float m_gravity;
  138. std::unique_ptr<CollisionSystem> m_collision_system;
  139. TextObject& m_text_object;
  140. private:
  141. Sector(const Sector&) = delete;
  142. Sector& operator=(const Sector&) = delete;
  143. };
  144. #endif
  145. /* EOF */