1 // SuperTux 2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de> 3 // 2018 Ingo Ruhnke <grumbel@gmail.com> 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_COLLISION_COLLISION_SYSTEM_HPP 19 #define HEADER_SUPERTUX_COLLISION_COLLISION_SYSTEM_HPP 20 21 #include <vector> 22 #include <memory> 23 #include <stdint.h> 24 25 #include "collision/collision.hpp" 26 #include "supertux/tile.hpp" 27 #include "math/fwd.hpp" 28 29 class CollisionObject; 30 class CollisionGroundMovementManager; 31 class DrawingContext; 32 class Rectf; 33 class Sector; 34 35 class CollisionSystem final 36 { 37 public: 38 CollisionSystem(Sector& sector); 39 40 void add(CollisionObject* object); 41 void remove(CollisionObject* object); 42 43 /** Draw collision shapes for debugging */ 44 void draw(DrawingContext& context); 45 46 /** Checks for all possible collisions. And calls the 47 collision_handlers, which the collision_objects provide for this 48 case (or not). */ 49 void update(); 50 get_ground_movement_manager()51 const std::shared_ptr<CollisionGroundMovementManager>& get_ground_movement_manager() 52 { 53 return m_ground_movement_manager; 54 } 55 56 bool is_free_of_tiles(const Rectf& rect, const bool ignoreUnisolid = false, uint32_t tiletype = Tile::SOLID) const; 57 bool is_free_of_statics(const Rectf& rect, const CollisionObject* ignore_object, const bool ignoreUnisolid) const; 58 bool is_free_of_movingstatics(const Rectf& rect, const CollisionObject* ignore_object) const; 59 bool free_line_of_sight(const Vector& line_start, const Vector& line_end, bool ignore_objects, const CollisionObject* ignore_object) const; 60 61 std::vector<CollisionObject*> get_nearby_objects(const Vector& center, float max_distance) const; 62 63 private: 64 /** Does collision detection of an object against all other static 65 objects (and the tilemap) in the level. Collision response is 66 done for the first hit in time. (other hits get ignored, the 67 function should be called repeatedly to resolve those) 68 69 returns true if the collision detection should be aborted for 70 this object (because of ABORT_MOVE in the collision response or 71 no collisions) */ 72 void collision_static(collision::Constraints* constraints, 73 const Vector& movement, const Rectf& dest, 74 CollisionObject& object); 75 76 void collision_tilemap(collision::Constraints* constraints, 77 const Vector& movement, const Rectf& dest, 78 CollisionObject& object) const; 79 80 uint32_t collision_tile_attributes(const Rectf& dest, const Vector& mov) const; 81 82 void collision_object(CollisionObject* object1, CollisionObject* object2) const; 83 84 void collision_static_constrains(CollisionObject& object); 85 86 private: 87 Sector& m_sector; 88 89 std::vector<CollisionObject*> m_objects; 90 91 std::shared_ptr<CollisionGroundMovementManager> m_ground_movement_manager; 92 93 private: 94 CollisionSystem(const CollisionSystem&) = delete; 95 CollisionSystem& operator=(const CollisionSystem&) = delete; 96 }; 97 98 #endif 99 100 /* EOF */ 101