path_walker.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // SuperTux
  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_OBJECT_PATH_WALKER_HPP
  17. #define HEADER_SUPERTUX_OBJECT_PATH_WALKER_HPP
  18. #include <string.h>
  19. #include <memory>
  20. #include "math/sizef.hpp"
  21. #include "object/path.hpp"
  22. #include "util/uid.hpp"
  23. template<typename T>
  24. class ObjectOption;
  25. /** A walker that travels along a path */
  26. class PathWalker final
  27. {
  28. public:
  29. /** Helper class that allows to displace a handle on an object */
  30. class Handle
  31. {
  32. public:
  33. Handle() : m_scalar_pos(), m_pixel_offset() {}
  34. Vector get_pos(const Sizef& size, const Vector& pos) const;
  35. public:
  36. Vector m_scalar_pos; /**< The scale of the object the handle should be displaced to ((0,0) = top left, (1,1) = bottom right) */
  37. Vector m_pixel_offset; /**< The secondary displacement, in absolute size (pixels) */
  38. };
  39. public:
  40. PathWalker(UID path_uid, bool running = true);
  41. ~PathWalker();
  42. /** advances the path walker on the path and returns its new position */
  43. void update(float dt_sec);
  44. /** current position of path walker */
  45. Vector get_pos(const Sizef& object_size, const Handle& handle) const;
  46. /** advance until at given node, then stop */
  47. void goto_node(int node_idx);
  48. /** teleport instantly to given node */
  49. void jump_to_node(int node_idx, bool instantaneous = false);
  50. /** start advancing automatically */
  51. void start_moving();
  52. /** stop advancing automatically */
  53. void stop_moving();
  54. /** returns true if PathWalker is currently moving */
  55. bool is_running() const { return m_running; }
  56. private:
  57. void advance_node();
  58. void goback_node();
  59. Path* get_path() const;
  60. public:
  61. UID m_path_uid;
  62. /** set to false to immediately stop advancing */
  63. bool m_running;
  64. private:
  65. size_t m_current_node_nr;
  66. size_t m_next_node_nr;
  67. /** stop advancing automatically when this node is reached */
  68. int m_stop_at_node_nr;
  69. /** the position between the current node and the next node as
  70. fraction between 0 and 1 */
  71. float m_node_time;
  72. float m_node_mult;
  73. float m_walking_speed;
  74. private:
  75. PathWalker(const PathWalker&) = delete;
  76. PathWalker& operator=(const PathWalker&) = delete;
  77. };
  78. #endif
  79. /* EOF */