supertux
path.hpp
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 
19 #ifndef HEADER_SUPERTUX_OBJECT_PATH_HPP
20 #define HEADER_SUPERTUX_OBJECT_PATH_HPP
21 
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 #include "math/vector.hpp"
27 #include "math/easing.hpp"
28 #include "util/gettext.hpp"
29 
30 template<typename T>
31 class ObjectOption;
32 class PathGameObject;
33 class ReaderMapping;
34 class Writer;
35 
36 enum class WalkMode {
37  // moves from first to last path node and stops
38  ONE_SHOT,
39  // moves from first to last node then in reverse order back to first
40  PING_PONG,
41  // moves from last node back to the first node
42  CIRCULAR
43 };
44 
45 WalkMode string_to_walk_mode(const std::string& mode_string);
46 std::string walk_mode_to_string(WalkMode walk_mode);
47 
48 class Path final
49 {
50 public:
52  class Node
53  {
54  private:
55  Path* parent;
57  public:
58  Vector position;
59  Vector bezier_before;
60  Vector bezier_after;
61  float time;
62  float speed;
63  EasingMode easing;
66  public:
67  Node(Path* parent_) :
68  parent(parent_),
69  position(0.0f, 0.0f),
70  bezier_before(0.0f, 0.0f),
71  bezier_after(0.0f, 0.0f),
72  time(),
73  speed(),
74  easing()
75  {}
76 
77  Path& get_parent() const { return *parent; }
78  };
79 
80 public:
81  Path(PathGameObject& parent);
82  Path(const Vector& pos, PathGameObject& parent);
83 
84  void read(const ReaderMapping& reader);
85  void save(Writer& writer);
86 
87  Vector get_base() const;
88 
90  int get_nearest_node_idx(const Vector& reference_point) const;
91 
93  int get_farthest_node_idx(const Vector& reference_point) const;
94 
96  void move_by(const Vector& shift);
97 
99  void edit_path();
100 
102  bool is_valid() const;
103 
104  const std::vector<Node>& get_nodes() const { return m_nodes; }
105 
106  PathGameObject& get_gameobject() const { return m_parent_gameobject; }
107 
108 private:
109  PathGameObject& m_parent_gameobject;
110 
111 public:
112  std::vector<Node> m_nodes;
113 
114  WalkMode m_mode;
115 
120  void on_flip(float height);
121 
122 private:
123  Path(const Path&) = delete;
124  Path& operator=(const Path&) = delete;
125 };
126 
127 #endif
128 
129 /* EOF */
Vector bezier_after
the position of the bezier handle towards the following node
Definition: path.hpp:60
Definition: object_option.hpp:77
float speed
speed (in px/seconds); editor use only
Definition: path.hpp:62
Definition: path.hpp:48
Vector bezier_before
the position of the bezier handle towards the preceding node
Definition: path.hpp:59
EasingMode easing
speed variations during travel (constant speed, start slow and go progressively quicker, etc.)
Definition: path.hpp:63
Helper class that stores an individual node of a Path.
Definition: path.hpp:52
bool m_adapt_speed
Whether or not to adapt the speed to bezier curves, cancelling the code that forces traveling bezier ...
Definition: path.hpp:116
Definition: reader_mapping.hpp:32
Vector position
the position of this node
Definition: path.hpp:58
Definition: path_gameobject.hpp:32
Definition: writer.cpp:23
float time
time (in seconds) to get from this node to next node
Definition: path.hpp:61