test_path_follow_2d.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 Ref<Path2D> &path = memnew(Path2D());
  44. path->set_curve(curve);
  45. const Ref<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. }
  66. TEST_CASE("[PathFollow2D] Sampling with offset") {
  67. const Ref<Curve2D> &curve = memnew(Curve2D());
  68. curve->add_point(Vector2(0, 0));
  69. curve->add_point(Vector2(100, 0));
  70. curve->add_point(Vector2(100, 100));
  71. curve->add_point(Vector2(0, 100));
  72. curve->add_point(Vector2(0, 0));
  73. const Ref<Path2D> &path = memnew(Path2D());
  74. path->set_curve(curve);
  75. const Ref<PathFollow2D> &path_follow_2d = memnew(PathFollow2D());
  76. path->add_child(path_follow_2d);
  77. path_follow_2d->set_offset(0);
  78. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(0, 0)));
  79. path_follow_2d->set_offset(50);
  80. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(50, 0)));
  81. path_follow_2d->set_offset(100);
  82. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(100, 0)));
  83. path_follow_2d->set_offset(150);
  84. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(100, 50)));
  85. path_follow_2d->set_offset(200);
  86. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(100, 100)));
  87. path_follow_2d->set_offset(250);
  88. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(50, 100)));
  89. path_follow_2d->set_offset(300);
  90. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(0, 100)));
  91. path_follow_2d->set_offset(350);
  92. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(0, 50)));
  93. path_follow_2d->set_offset(400);
  94. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(0, 0)));
  95. }
  96. TEST_CASE("[PathFollow2D] Removal of a point in curve") {
  97. const Ref<Curve2D> &curve = memnew(Curve2D());
  98. curve->add_point(Vector2(0, 0));
  99. curve->add_point(Vector2(100, 0));
  100. curve->add_point(Vector2(100, 100));
  101. const Ref<Path2D> &path = memnew(Path2D());
  102. path->set_curve(curve);
  103. const Ref<PathFollow2D> &path_follow_2d = memnew(PathFollow2D());
  104. path->add_child(path_follow_2d);
  105. path_follow_2d->set_unit_offset(0.5);
  106. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(100, 0)));
  107. curve->remove_point(1);
  108. CHECK_MESSAGE(
  109. path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(50, 50)),
  110. "Path follow's position should be updated after removing a point from the curve");
  111. }
  112. TEST_CASE("[PathFollow2D] Setting h_offset and v_offset") {
  113. const Ref<Curve2D> &curve = memnew(Curve2D());
  114. curve->add_point(Vector2(0, 0));
  115. curve->add_point(Vector2(100, 0));
  116. const Ref<Path2D> &path = memnew(Path2D());
  117. path->set_curve(curve);
  118. const Ref<PathFollow2D> &path_follow_2d = memnew(PathFollow2D());
  119. path->add_child(path_follow_2d);
  120. path_follow_2d->set_unit_offset(0.5);
  121. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(50, 0)));
  122. path_follow_2d->set_h_offset(25);
  123. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(75, 0)));
  124. path_follow_2d->set_v_offset(25);
  125. CHECK(path_follow_2d->get_transform().get_origin().is_equal_approx(Vector2(75, 25)));
  126. }
  127. TEST_CASE("[PathFollow2D] Unit offset out of range") {
  128. const Ref<Curve2D> &curve = memnew(Curve2D());
  129. curve->add_point(Vector2(0, 0));
  130. curve->add_point(Vector2(100, 0));
  131. const Ref<Path2D> &path = memnew(Path2D());
  132. path->set_curve(curve);
  133. const Ref<PathFollow2D> &path_follow_2d = memnew(PathFollow2D());
  134. path->add_child(path_follow_2d);
  135. path_follow_2d->set_loop(true);
  136. path_follow_2d->set_unit_offset(-0.3);
  137. CHECK_MESSAGE(
  138. path_follow_2d->get_unit_offset() == 0.7,
  139. "Unit Offset should loop back from the end in the opposite direction");
  140. path_follow_2d->set_unit_offset(1.3);
  141. CHECK_MESSAGE(
  142. path_follow_2d->get_unit_offset() == 0.3,
  143. "Unit Offset should loop back from the end in the opposite direction");
  144. path_follow_2d->set_loop(false);
  145. path_follow_2d->set_unit_offset(-0.3);
  146. CHECK_MESSAGE(
  147. path_follow_2d->get_unit_offset() == 0,
  148. "Unit Offset should be clamped at 0");
  149. path_follow_2d->set_unit_offset(1.3);
  150. CHECK_MESSAGE(
  151. path_follow_2d->get_unit_offset() == 1,
  152. "Unit Offset should be clamped at 1");
  153. }
  154. TEST_CASE("[PathFollow2D] Offset out of range") {
  155. const Ref<Curve2D> &curve = memnew(Curve2D());
  156. curve->add_point(Vector2(0, 0));
  157. curve->add_point(Vector2(100, 0));
  158. const Ref<Path2D> &path = memnew(Path2D());
  159. path->set_curve(curve);
  160. const Ref<PathFollow2D> &path_follow_2d = memnew(PathFollow2D());
  161. path->add_child(path_follow_2d);
  162. path_follow_2d->set_loop(true);
  163. path_follow_2d->set_offset(-50);
  164. CHECK_MESSAGE(
  165. path_follow_2d->get_offset() == 50,
  166. "Offset should loop back from the end in the opposite direction");
  167. path_follow_2d->set_offset(150);
  168. CHECK_MESSAGE(
  169. path_follow_2d->get_offset() == 50,
  170. "Offset should loop back from the end in the opposite direction");
  171. path_follow_2d->set_loop(false);
  172. path_follow_2d->set_offset(-50);
  173. CHECK_MESSAGE(
  174. path_follow_2d->get_offset() == 0,
  175. "Offset should be clamped at 0");
  176. path_follow_2d->set_offset(150);
  177. CHECK_MESSAGE(
  178. path_follow_2d->get_offset() == 100,
  179. "Offset should be clamped at 1");
  180. }
  181. } // namespace TestPathFollow2D
  182. #endif // TEST_PATH_FOLLOW_2D_H