collision_movement_manager.hpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SuperTux
  2. // Copyright (C) 2020 Maxim Bernard <mbernard2@videotron.ca>
  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_COLLISION_COLLISION_MOVEMENT_MANAGER_HPP
  17. #define HEADER_SUPERTUX_COLLISION_COLLISION_MOVEMENT_MANAGER_HPP
  18. #include "collision/collision_object.hpp"
  19. #include "object/tilemap.hpp"
  20. #include "math/vector.hpp"
  21. #include <unordered_map>
  22. /**
  23. * This class takes care of moving objects that have collided on top of other moving
  24. * objects or on top of moving solid tiles.
  25. *
  26. * This step is performed after the object updates and before the collision detection.
  27. */
  28. class CollisionGroundMovementManager final
  29. {
  30. private:
  31. /** Utility class used internally. */
  32. class TargetMovementData final
  33. {
  34. public:
  35. TargetMovementData() :
  36. m_moving_objects(),
  37. m_moving_tilemaps()
  38. {}
  39. void register_movement(CollisionObject& moving_object, const Vector& movement);
  40. void register_movement(TileMap& moving_tilemap, const Vector& movement);
  41. const std::unordered_map<CollisionObject*, Vector> get_objects_map() const
  42. {
  43. return m_moving_objects;
  44. }
  45. const std::unordered_map<TileMap*, Vector> get_tilemaps_map() const
  46. {
  47. return m_moving_tilemaps;
  48. }
  49. private:
  50. std::unordered_map<CollisionObject*, Vector> m_moving_objects;
  51. std::unordered_map<TileMap*, Vector> m_moving_tilemaps;
  52. };
  53. public:
  54. CollisionGroundMovementManager() :
  55. m_movements_per_target()
  56. {}
  57. void register_movement(CollisionObject& moving_object, CollisionObject& target_object, const Vector& movement);
  58. void register_movement(TileMap& moving_tilemap, CollisionObject& target_object, const Vector& movement);
  59. /** Moves all target objects according to their colliding object or tilemap whose movement
  60. vector has the lowest "y" coordinate.
  61. In other words: this makes sure objects always move up in case one of the colliding
  62. objects does. */
  63. void apply_all_ground_movement();
  64. private:
  65. /** Holds all movement operations performed by objects or tilemaps which had moving
  66. objects that collided on top of them. */
  67. std::unordered_map<CollisionObject*, TargetMovementData> m_movements_per_target;
  68. private:
  69. CollisionGroundMovementManager(const CollisionGroundMovementManager&) = delete;
  70. CollisionGroundMovementManager& operator=(const CollisionGroundMovementManager&) = delete;
  71. };
  72. #endif
  73. /* EOF */