path.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SuperTux Path
  2. // Copyright (C) 2005 Philipp <balinor@pnxs.de>
  3. // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
  4. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  5. //
  6. // This program is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #ifndef HEADER_SUPERTUX_OBJECT_PATH_HPP
  19. #define HEADER_SUPERTUX_OBJECT_PATH_HPP
  20. #include <memory>
  21. #include <string>
  22. #include <vector>
  23. #include "math/vector.hpp"
  24. #include "math/easing.hpp"
  25. #include "util/gettext.hpp"
  26. template<typename T>
  27. class ObjectOption;
  28. class PathGameObject;
  29. class ReaderMapping;
  30. class Writer;
  31. enum class WalkMode {
  32. // moves from first to last path node and stops
  33. ONE_SHOT,
  34. // moves from first to last node then in reverse order back to first
  35. PING_PONG,
  36. // moves from last node back to the first node
  37. CIRCULAR
  38. };
  39. WalkMode string_to_walk_mode(const std::string& mode_string);
  40. std::string walk_mode_to_string(WalkMode walk_mode);
  41. class Path final
  42. {
  43. public:
  44. /** Helper class that stores an individual node of a Path */
  45. class Node
  46. {
  47. private:
  48. Path* parent; /**< the parent path of this node */
  49. public:
  50. Vector position; /**< the position of this node */
  51. Vector bezier_before; /**< the position of the bezier handle towards the preceding node */
  52. Vector bezier_after; /**< the position of the bezier handle towards the following node */
  53. float time; /**< time (in seconds) to get from this node to next node */
  54. float speed; /**< speed (in px/seconds); editor use only */
  55. EasingMode easing; /**< speed variations during travel
  56. (constant speed, start slow and go progressively quicker, etc.) */
  57. public:
  58. Node(Path* parent_) :
  59. parent(parent_),
  60. position(0.0f, 0.0f),
  61. bezier_before(0.0f, 0.0f),
  62. bezier_after(0.0f, 0.0f),
  63. time(),
  64. speed(),
  65. easing()
  66. {}
  67. Path& get_parent() const { return *parent; }
  68. };
  69. public:
  70. Path(PathGameObject& parent);
  71. Path(const Vector& pos, PathGameObject& parent);
  72. void read(const ReaderMapping& reader);
  73. void save(Writer& writer);
  74. Vector get_base() const;
  75. /** returns Node index nearest to reference_point or -1 if not applicable */
  76. int get_nearest_node_idx(const Vector& reference_point) const;
  77. /** returns Node index farthest from reference_point or -1 if not applicable */
  78. int get_farthest_node_idx(const Vector& reference_point) const;
  79. /** Moves all nodes by given shift. */
  80. void move_by(const Vector& shift);
  81. /** Puts node markers to the nodes to edit them. */
  82. void edit_path();
  83. /** Returns false when has no nodes */
  84. bool is_valid() const;
  85. const std::vector<Node>& get_nodes() const { return m_nodes; }
  86. PathGameObject& get_gameobject() const { return m_parent_gameobject; }
  87. private:
  88. PathGameObject& m_parent_gameobject;
  89. public:
  90. std::vector<Node> m_nodes;
  91. WalkMode m_mode;
  92. bool m_adapt_speed; /**< Whether or not to adapt the speed to bezier curves,
  93. cancelling the code that forces traveling bezier
  94. curves at constant speed */
  95. void on_flip(float height);
  96. private:
  97. Path(const Path&) = delete;
  98. Path& operator=(const Path&) = delete;
  99. };
  100. #endif
  101. /* EOF */