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