| 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_OBJECT_HPP |
| 19 | #define |
| 20 | |
| 21 | #include <stdint.h> |
| 22 | |
| 23 | #include "collision/collision_group.hpp" |
| 24 | #include "collision/collision_hit.hpp" |
| 25 | #include "math/rectf.hpp" |
| 26 | |
| 27 | class CollisionListener; |
| 28 | class GameObject; |
| 29 | |
| 30 | class CollisionObject |
| 31 | { |
| 32 | friend class CollisionSystem; |
| 33 | |
| 34 | public: |
| 35 | CollisionObject(CollisionGroup group, CollisionListener& parent); |
| 36 | |
| 37 | /** this function is called when the object collided with something solid */ |
| 38 | void collision_solid(const CollisionHit& hit); |
| 39 | |
| 40 | /** when 2 objects collided, we will first call the |
| 41 | pre_collision_check functions of both objects that can decide on |
| 42 | how to react to the collision. */ |
| 43 | bool collides(CollisionObject& other, const CollisionHit& hit) const; |
| 44 | |
| 45 | /** this function is called when the object collided with any other object */ |
| 46 | HitResponse collision(CollisionObject& other, const CollisionHit& hit); |
| 47 | |
| 48 | /** called when tiles with special attributes have been touched */ |
| 49 | void collision_tile(uint32_t tile_attributes); |
| 50 | |
| 51 | /** returns the bounding box of the Object */ |
| 52 | const Rectf& get_bbox() const |
| 53 | { |
| 54 | return m_bbox; |
| 55 | } |
| 56 | |
| 57 | const Vector& get_movement() const |
| 58 | { |
| 59 | return m_movement; |
| 60 | } |
| 61 | |
| 62 | /** places the moving object at a specific position. Be careful when |
| 63 | using this function. There are no collision detection checks |
| 64 | performed here so bad things could happen. */ |
| 65 | void set_pos(const Vector& pos) |
| 66 | { |
| 67 | m_dest.move(pos - get_pos()); |
| 68 | m_bbox.set_pos(pos); |
| 69 | } |
| 70 | |
| 71 | Vector get_pos() const |
| 72 | { |
| 73 | return m_bbox.p1(); |
| 74 | } |
| 75 | |
| 76 | /** moves entire object to a specific position, including all |
| 77 | points those the object has, exactly like the object has |
| 78 | spawned in that given pos instead.*/ |
| 79 | void move_to(const Vector& pos) |
| 80 | { |
| 81 | set_pos(pos); |
| 82 | } |
| 83 | |
| 84 | /** sets the moving object's bbox to a specific width. Be careful |
| 85 | when using this function. There are no collision detection |
| 86 | checks performed here so bad things could happen. */ |
| 87 | void set_width(float w) |
| 88 | { |
| 89 | m_dest.set_width(w); |
| 90 | m_bbox.set_width(w); |
| 91 | } |
| 92 | |
| 93 | /** sets the moving object's bbox to a specific size. Be careful |
| 94 | when using this function. There are no collision detection |
| 95 | checks performed here so bad things could happen. */ |
| 96 | void set_size(float w, float h) |
| 97 | { |
| 98 | m_dest.set_size(w, h); |
| 99 | m_bbox.set_size(w, h); |
| 100 | } |
| 101 | |
| 102 | CollisionGroup get_group() const |
| 103 | { |
| 104 | return m_group; |
| 105 | } |
| 106 | |
| 107 | bool is_valid() const; |
| 108 | |
| 109 | CollisionListener& get_listener() |
| 110 | { |
| 111 | return m_listener; |
| 112 | } |
| 113 | |
| 114 | private: |
| 115 | CollisionListener& m_listener; |
| 116 | |
| 117 | public: |
| 118 | /** The bounding box of the object (as used for collision detection, |
| 119 | this isn't necessarily the bounding box for graphics) */ |
| 120 | Rectf m_bbox; |
| 121 | |
| 122 | /** The movement that will happen till next frame */ |
| 123 | Vector m_movement; |
| 124 | |
| 125 | /** The collision group */ |
| 126 | CollisionGroup m_group; |
| 127 | |
| 128 | private: |
| 129 | /** this is only here for internal collision detection use (don't touch this |
| 130 | from outside collision detection code) |
| 131 | |
| 132 | This field holds the currently anticipated destination of the object |
| 133 | during collision detection */ |
| 134 | Rectf m_dest; |
| 135 | |
| 136 | private: |
| 137 | CollisionObject(const CollisionObject&) = delete; |
| 138 | CollisionObject& operator=(const CollisionObject&) = delete; |
| 139 | }; |
| 140 | |
| 141 | #endif |
| 142 | |
| 143 | /* EOF */ |
| 144 | |