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