Debian Codegraph @

tests/scene/test_path_follow_2d.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_path_follow_2d.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "scene/2d/path_2d.h"
34 #include "scene/main/window.h"
35
36 #include "tests/test_macros.h"
37
38 namespace TestPathFollow2D {
39
40 bool is_equal_approx(const Vector2 &p_a, const Vector2 &p_b) {
41 const real_t tolerance = 0.001;
42 return Math::is_equal_approx(p_a.x, p_b.x, tolerance) &&
43 Math::is_equal_approx(p_a.y, p_b.y, tolerance);
44 }
45
46 TEST_CASE("[SceneTree][PathFollow2D] Sampling with progress ratio") {
47 Ref<Curve2D> curve = memnew(Curve2D);
48 curve->set_bake_interval(1);
49 curve->add_point(Vector2(0, 0));
50 curve->add_point(Vector2(100, 0));
51 curve->add_point(Vector2(100, 100));
52 curve->add_point(Vector2(0, 100));
53 curve->add_point(Vector2(0, 0));
54 Path2D *path = memnew(Path2D);
55 path->set_curve(curve);
56 PathFollow2D *path_follow_2d = memnew(PathFollow2D);
57 path_follow_2d->set_loop(false);
58 path->add_child(path_follow_2d);
59 SceneTree::get_singleton()->get_root()->add_child(path);
60
61 path_follow_2d->set_progress_ratio(0);
62 CHECK(is_equal_approx(Vector2(0, 0), path_follow_2d->get_transform().get_origin()));
63
64 path_follow_2d->set_progress_ratio(0.125);
65 CHECK(is_equal_approx(Vector2(50, 0), path_follow_2d->get_transform().get_origin()));
66
67 path_follow_2d->set_progress_ratio(0.25);
68 CHECK(is_equal_approx(Vector2(100, 0), path_follow_2d->get_transform().get_origin()));
69
70 path_follow_2d->set_progress_ratio(0.375);
71 CHECK(is_equal_approx(Vector2(100, 50), path_follow_2d->get_transform().get_origin()));
72
73 path_follow_2d->set_progress_ratio(0.5);
74 CHECK(is_equal_approx(Vector2(100, 100), path_follow_2d->get_transform().get_origin()));
75
76 path_follow_2d->set_progress_ratio(0.625);
77 CHECK(is_equal_approx(Vector2(50, 100), path_follow_2d->get_transform().get_origin()));
78
79 path_follow_2d->set_progress_ratio(0.75);
80 CHECK(is_equal_approx(Vector2(0, 100), path_follow_2d->get_transform().get_origin()));
81
82 path_follow_2d->set_progress_ratio(0.875);
83 CHECK(is_equal_approx(Vector2(0, 50), path_follow_2d->get_transform().get_origin()));
84
85 path_follow_2d->set_progress_ratio(1);
86 CHECK(is_equal_approx(Vector2(0, 0), path_follow_2d->get_transform().get_origin()));
87
88 memdelete(path);
89 }
90
91 TEST_CASE("[SceneTree][PathFollow2D] Sampling with progress") {
92 Ref<Curve2D> curve = memnew(Curve2D);
93 curve->set_bake_interval(1);
94 curve->add_point(Vector2(0, 0));
95 curve->add_point(Vector2(100, 0));
96 curve->add_point(Vector2(100, 100));
97 curve->add_point(Vector2(0, 100));
98 curve->add_point(Vector2(0, 0));
99 Path2D *path = memnew(Path2D);
100 path->set_curve(curve);
101 PathFollow2D *path_follow_2d = memnew(PathFollow2D);
102 path_follow_2d->set_loop(false);
103 path->add_child(path_follow_2d);
104 SceneTree::get_singleton()->get_root()->add_child(path);
105
106 path_follow_2d->set_progress(0);
107 CHECK(is_equal_approx(Vector2(0, 0), path_follow_2d->get_transform().get_origin()));
108
109 path_follow_2d->set_progress(50);
110 CHECK(is_equal_approx(Vector2(50, 0), path_follow_2d->get_transform().get_origin()));
111
112 path_follow_2d->set_progress(100);
113 CHECK(is_equal_approx(Vector2(100, 0), path_follow_2d->get_transform().get_origin()));
114
115 path_follow_2d->set_progress(150);
116 CHECK(is_equal_approx(Vector2(100, 50), path_follow_2d->get_transform().get_origin()));
117
118 path_follow_2d->set_progress(200);
119 CHECK(is_equal_approx(Vector2(100, 100), path_follow_2d->get_transform().get_origin()));
120
121 path_follow_2d->set_progress(250);
122 CHECK(is_equal_approx(Vector2(50, 100), path_follow_2d->get_transform().get_origin()));
123
124 path_follow_2d->set_progress(300);
125 CHECK(is_equal_approx(Vector2(0, 100), path_follow_2d->get_transform().get_origin()));
126
127 path_follow_2d->set_progress(350);
128 CHECK(is_equal_approx(Vector2(0, 50), path_follow_2d->get_transform().get_origin()));
129
130 path_follow_2d->set_progress(400);
131 CHECK(is_equal_approx(Vector2(0, 0), path_follow_2d->get_transform().get_origin()));
132
133 memdelete(path);
134 }
135
136 TEST_CASE("[SceneTree][PathFollow2D] Removal of a point in curve") {
137 Ref<Curve2D> curve = memnew(Curve2D);
138 curve->add_point(Vector2(0, 0));
139 curve->add_point(Vector2(100, 0));
140 curve->add_point(Vector2(100, 100));
141 Path2D *path = memnew(Path2D);
142 path->set_curve(curve);
143 PathFollow2D *path_follow_2d = memnew(PathFollow2D);
144 path->add_child(path_follow_2d);
145 SceneTree::get_singleton()->get_root()->add_child(path);
146
147 path_follow_2d->set_progress_ratio(0.5);
148 CHECK(is_equal_approx(Vector2(100, 0), path_follow_2d->get_transform().get_origin()));
149
150 curve->remove_point(1);
151
152 path_follow_2d->set_progress_ratio(0.5);
153 CHECK_MESSAGE(
154 is_equal_approx(Vector2(50, 50), path_follow_2d->get_transform().get_origin()),
155 "Path follow's position should be updated after removing a point from the curve");
156
157 memdelete(path);
158 }
159
160 TEST_CASE("[SceneTree][PathFollow2D] Setting h_offset and v_offset") {
161 Ref<Curve2D> curve = memnew(Curve2D);
162 curve->add_point(Vector2(0, 0));
163 curve->add_point(Vector2(100, 0));
164 Path2D *path = memnew(Path2D);
165 path->set_curve(curve);
166 PathFollow2D *path_follow_2d = memnew(PathFollow2D);
167 path->add_child(path_follow_2d);
168 SceneTree::get_singleton()->get_root()->add_child(path);
169
170 path_follow_2d->set_progress_ratio(0.5);
171 CHECK(is_equal_approx(Vector2(50, 0), path_follow_2d->get_transform().get_origin()));
172
173 path_follow_2d->set_h_offset(25);
174 CHECK(is_equal_approx(Vector2(75, 0), path_follow_2d->get_transform().get_origin()));
175
176 path_follow_2d->set_v_offset(25);
177 CHECK(is_equal_approx(Vector2(75, 25), path_follow_2d->get_transform().get_origin()));
178
179 memdelete(path);
180 }
181
182 TEST_CASE("[SceneTree][PathFollow2D] Progress ratio out of range") {
183 Ref<Curve2D> curve = memnew(Curve2D);
184 curve->add_point(Vector2(0, 0));
185 curve->add_point(Vector2(100, 0));
186 Path2D *path = memnew(Path2D);
187 path->set_curve(curve);
188 PathFollow2D *path_follow_2d = memnew(PathFollow2D);
189 path->add_child(path_follow_2d);
190 SceneTree::get_singleton()->get_root()->add_child(path);
191
192 path_follow_2d->set_loop(true);
193
194 path_follow_2d->set_progress_ratio(-0.3);
195 CHECK_MESSAGE(
196 Math::is_equal_approx(path_follow_2d->get_progress_ratio(), (real_t)0.7),
197 "Progress Ratio should loop back from the end in the opposite direction");
198
199 path_follow_2d->set_progress_ratio(1.3);
200 CHECK_MESSAGE(
201 Math::is_equal_approx(path_follow_2d->get_progress_ratio(), (real_t)0.3),
202 "Progress Ratio should loop back from the end in the opposite direction");
203
204 path_follow_2d->set_loop(false);
205
206 path_follow_2d->set_progress_ratio(-0.3);
207 CHECK_MESSAGE(
208 Math::is_equal_approx(path_follow_2d->get_progress_ratio(), 0),
209 "Progress Ratio should be clamped at 0");
210
211 path_follow_2d->set_progress_ratio(1.3);
212 CHECK_MESSAGE(
213 Math::is_equal_approx(path_follow_2d->get_progress_ratio(), 1),
214 "Progress Ratio should be clamped at 1");
215
216 memdelete(path);
217 }
218
219 TEST_CASE("[SceneTree][PathFollow2D] Progress out of range") {
220 Ref<Curve2D> curve = memnew(Curve2D);
221 curve->add_point(Vector2(0, 0));
222 curve->add_point(Vector2(100, 0));
223 Path2D *path = memnew(Path2D);
224 path->set_curve(curve);
225 PathFollow2D *path_follow_2d = memnew(PathFollow2D);
226 path->add_child(path_follow_2d);
227 SceneTree::get_singleton()->get_root()->add_child(path);
228
229 path_follow_2d->set_loop(true);
230
231 path_follow_2d->set_progress(-50);
232 CHECK_MESSAGE(
233 Math::is_equal_approx(path_follow_2d->get_progress(), 50),
234 "Progress should loop back from the end in the opposite direction");
235
236 path_follow_2d->set_progress(150);
237 CHECK_MESSAGE(
238 Math::is_equal_approx(path_follow_2d->get_progress(), 50),
239 "Progress should loop back from the end in the opposite direction");
240
241 path_follow_2d->set_loop(false);
242
243 path_follow_2d->set_progress(-50);
244 CHECK_MESSAGE(
245 Math::is_equal_approx(path_follow_2d->get_progress(), 0),
246 "Progress should be clamped at 0");
247
248 path_follow_2d->set_progress(150);
249 CHECK_MESSAGE(
250 Math::is_equal_approx(path_follow_2d->get_progress(), 100),
251 "Progress should be clamped at 1");
252
253 memdelete(path);
254 }
255 } // namespace TestPathFollow2D