test_path_follow_2d.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifndef TEST_PATH_FOLLOW_2D_H
  31. #define TEST_PATH_FOLLOW_2D_H
  32. #include "scene/2d/path_2d.h"
  33. #include "scene/resources/curve.h"
  34. #include "tests/test_macros.h"
  35. namespace TestPathFollow2D {
  36. TEST_CASE("[PathFollow2D] Sampling with unit offset") {
  37. const Ref<Curve2D> &curve = memnew(Curve2D());
  38. curve->add_point(Vector2(0, 0));
  39. curve->add_point(Vector2(100, 0));
  40. curve->add_point(Vector2(100, 100));
  41. curve->add_point(Vector2(0, 100));
  42. curve->add_point(Vector2(0, 0));
  43. const Path2D *path = memnew(Path2D);
  44. path->set_curve(curve);
  45. const PathFollow2D *path_follow_2d = memnew(PathFollow2D);
  46. path->add_child(path_follow_2d);
  47. path_follow_2d->set_unit_offset(0);
  48. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(0, 0)));
  49. path_follow_2d->set_unit_offset(0.125);
  50. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(50, 0)));
  51. path_follow_2d->set_unit_offset(0.25);
  52. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(100, 0)));
  53. path_follow_2d->set_unit_offset(0.375);
  54. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(100, 50)));
  55. path_follow_2d->set_unit_offset(0.5);
  56. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(100, 100)));
  57. path_follow_2d->set_unit_offset(0.625);
  58. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(50, 100)));
  59. path_follow_2d->set_unit_offset(0.75);
  60. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(0, 100)));
  61. path_follow_2d->set_unit_offset(0.875);
  62. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(0, 50)));
  63. path_follow_2d->set_unit_offset(1);
  64. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(0, 0)));
  65. memdelete(path);
  66. }
  67. TEST_CASE("[PathFollow2D] Sampling with offset") {
  68. const Ref<Curve2D> &curve = memnew(Curve2D());
  69. curve->add_point(Vector2(0, 0));
  70. curve->add_point(Vector2(100, 0));
  71. curve->add_point(Vector2(100, 100));
  72. curve->add_point(Vector2(0, 100));
  73. curve->add_point(Vector2(0, 0));
  74. const Path2D *path = memnew(Path2D);
  75. path->set_curve(curve);
  76. const PathFollow2D *path_follow_2d = memnew(PathFollow2D);
  77. path->add_child(path_follow_2d);
  78. path_follow_2d->set_offset(0);
  79. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(0, 0)));
  80. path_follow_2d->set_offset(50);
  81. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(50, 0)));
  82. path_follow_2d->set_offset(100);
  83. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(100, 0)));
  84. path_follow_2d->set_offset(150);
  85. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(100, 50)));
  86. path_follow_2d->set_offset(200);
  87. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(100, 100)));
  88. path_follow_2d->set_offset(250);
  89. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(50, 100)));
  90. path_follow_2d->set_offset(300);
  91. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(0, 100)));
  92. path_follow_2d->set_offset(350);
  93. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(0, 50)));
  94. path_follow_2d->set_offset(400);
  95. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(0, 0)));
  96. memdelete(path);
  97. }
  98. TEST_CASE("[PathFollow2D] Removal of a point in curve") {
  99. const Ref<Curve2D> &curve = memnew(Curve2D());
  100. curve->add_point(Vector2(0, 0));
  101. curve->add_point(Vector2(100, 0));
  102. curve->add_point(Vector2(100, 100));
  103. const Path2D *path = memnew(Path2D);
  104. path->set_curve(curve);
  105. const PathFollow2D *path_follow_2d = memnew(PathFollow2D);
  106. path->add_child(path_follow_2d);
  107. path_follow_2d->set_unit_offset(0.5);
  108. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(100, 0)));
  109. curve->remove_point(1);
  110. CHECK_MESSAGE(
  111. path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(50, 50)),
  112. "Path follow's position should be updated after removing a point from the curve");
  113. memdelete(path);
  114. }
  115. TEST_CASE("[PathFollow2D] Setting h_offset and v_offset") {
  116. const Ref<Curve2D> &curve = memnew(Curve2D());
  117. curve->add_point(Vector2(0, 0));
  118. curve->add_point(Vector2(100, 0));
  119. const Path2D *path = memnew(Path2D);
  120. path->set_curve(curve);
  121. const PathFollow2D *path_follow_2d = memnew(PathFollow2D);
  122. path->add_child(path_follow_2d);
  123. path_follow_2d->set_unit_offset(0.5);
  124. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(50, 0)));
  125. path_follow_2d->set_h_offset(25);
  126. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(75, 0)));
  127. path_follow_2d->set_v_offset(25);
  128. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(75, 25)));
  129. memdelete(path);
  130. }
  131. TEST_CASE("[PathFollow2D] Unit offset out of range") {
  132. const Ref<Curve2D> &curve = memnew(Curve2D());
  133. curve->add_point(Vector2(0, 0));
  134. curve->add_point(Vector2(100, 0));
  135. const Path2D *path = memnew(Path2D);
  136. path->set_curve(curve);
  137. const PathFollow2D *path_follow_2d = memnew(PathFollow2D);
  138. path->add_child(path_follow_2d);
  139. path_follow_2d->set_loop(true);
  140. path_follow_2d->set_unit_offset(-0.3);
  141. CHECK_MESSAGE(
  142. path_follow_2d->get_unit_offset() == 0.7,
  143. "Unit Offset should loop back from the end in the opposite direction");
  144. path_follow_2d->set_unit_offset(1.3);
  145. CHECK_MESSAGE(
  146. path_follow_2d->get_unit_offset() == 0.3,
  147. "Unit Offset should loop back from the end in the opposite direction");
  148. path_follow_2d->set_loop(false);
  149. path_follow_2d->set_unit_offset(-0.3);
  150. CHECK_MESSAGE(
  151. path_follow_2d->get_unit_offset() == 0,
  152. "Unit Offset should be clamped at 0");
  153. path_follow_2d->set_unit_offset(1.3);
  154. CHECK_MESSAGE(
  155. path_follow_2d->get_unit_offset() == 1,
  156. "Unit Offset should be clamped at 1");
  157. memdelete(path);
  158. }
  159. TEST_CASE("[PathFollow2D] Offset out of range") {
  160. const Ref<Curve2D> &curve = memnew(Curve2D());
  161. curve->add_point(Vector2(0, 0));
  162. curve->add_point(Vector2(100, 0));
  163. const Path2D *path = memnew(Path2D);
  164. path->set_curve(curve);
  165. const PathFollow2D *path_follow_2d = memnew(PathFollow2D);
  166. path->add_child(path_follow_2d);
  167. path_follow_2d->set_loop(true);
  168. path_follow_2d->set_offset(-50);
  169. CHECK_MESSAGE(
  170. path_follow_2d->get_offset() == 50,
  171. "Offset should loop back from the end in the opposite direction");
  172. path_follow_2d->set_offset(150);
  173. CHECK_MESSAGE(
  174. path_follow_2d->get_offset() == 50,
  175. "Offset should loop back from the end in the opposite direction");
  176. path_follow_2d->set_loop(false);
  177. path_follow_2d->set_offset(-50);
  178. CHECK_MESSAGE(
  179. path_follow_2d->get_offset() == 0,
  180. "Offset should be clamped at 0");
  181. path_follow_2d->set_offset(150);
  182. CHECK_MESSAGE(
  183. path_follow_2d->get_offset() == 100,
  184. "Offset should be clamped at 1");
  185. memdelete(path);
  186. }
  187. } // namespace TestPathFollow2D
  188. #endif // TEST_PATH_FOLLOW_2D_H