Path: blob/master/tests/scene/test_path_follow_2d.cpp
45987 views
/**************************************************************************/1/* test_path_follow_2d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "tests/test_macros.h"3132TEST_FORCE_LINK(test_path_follow_2d)3334#include "scene/2d/path_2d.h"35#include "scene/main/scene_tree.h"36#include "scene/main/window.h"3738namespace TestPathFollow2D {3940bool is_equal_approx(const Vector2 &p_a, const Vector2 &p_b) {41const real_t tolerance = 0.001;42return Math::is_equal_approx(p_a.x, p_b.x, tolerance) &&43Math::is_equal_approx(p_a.y, p_b.y, tolerance);44}4546TEST_CASE("[SceneTree][PathFollow2D] Sampling with progress ratio") {47Ref<Curve2D> curve = memnew(Curve2D);48curve->set_bake_interval(1);49curve->add_point(Vector2(0, 0));50curve->add_point(Vector2(100, 0));51curve->add_point(Vector2(100, 100));52curve->add_point(Vector2(0, 100));53curve->add_point(Vector2(0, 0));54Path2D *path = memnew(Path2D);55path->set_curve(curve);56PathFollow2D *path_follow_2d = memnew(PathFollow2D);57path_follow_2d->set_loop(false);58path->add_child(path_follow_2d);59SceneTree::get_singleton()->get_root()->add_child(path);6061path_follow_2d->set_progress_ratio(0);62CHECK(is_equal_approx(Vector2(0, 0), path_follow_2d->get_transform().get_origin()));6364path_follow_2d->set_progress_ratio(0.125);65CHECK(is_equal_approx(Vector2(50, 0), path_follow_2d->get_transform().get_origin()));6667path_follow_2d->set_progress_ratio(0.25);68CHECK(is_equal_approx(Vector2(100, 0), path_follow_2d->get_transform().get_origin()));6970path_follow_2d->set_progress_ratio(0.375);71CHECK(is_equal_approx(Vector2(100, 50), path_follow_2d->get_transform().get_origin()));7273path_follow_2d->set_progress_ratio(0.5);74CHECK(is_equal_approx(Vector2(100, 100), path_follow_2d->get_transform().get_origin()));7576path_follow_2d->set_progress_ratio(0.625);77CHECK(is_equal_approx(Vector2(50, 100), path_follow_2d->get_transform().get_origin()));7879path_follow_2d->set_progress_ratio(0.75);80CHECK(is_equal_approx(Vector2(0, 100), path_follow_2d->get_transform().get_origin()));8182path_follow_2d->set_progress_ratio(0.875);83CHECK(is_equal_approx(Vector2(0, 50), path_follow_2d->get_transform().get_origin()));8485path_follow_2d->set_progress_ratio(1);86CHECK(is_equal_approx(Vector2(0, 0), path_follow_2d->get_transform().get_origin()));8788memdelete(path);89}9091TEST_CASE("[SceneTree][PathFollow2D] Sampling with progress") {92Ref<Curve2D> curve = memnew(Curve2D);93curve->set_bake_interval(1);94curve->add_point(Vector2(0, 0));95curve->add_point(Vector2(100, 0));96curve->add_point(Vector2(100, 100));97curve->add_point(Vector2(0, 100));98curve->add_point(Vector2(0, 0));99Path2D *path = memnew(Path2D);100path->set_curve(curve);101PathFollow2D *path_follow_2d = memnew(PathFollow2D);102path_follow_2d->set_loop(false);103path->add_child(path_follow_2d);104SceneTree::get_singleton()->get_root()->add_child(path);105106path_follow_2d->set_progress(0);107CHECK(is_equal_approx(Vector2(0, 0), path_follow_2d->get_transform().get_origin()));108109path_follow_2d->set_progress(50);110CHECK(is_equal_approx(Vector2(50, 0), path_follow_2d->get_transform().get_origin()));111112path_follow_2d->set_progress(100);113CHECK(is_equal_approx(Vector2(100, 0), path_follow_2d->get_transform().get_origin()));114115path_follow_2d->set_progress(150);116CHECK(is_equal_approx(Vector2(100, 50), path_follow_2d->get_transform().get_origin()));117118path_follow_2d->set_progress(200);119CHECK(is_equal_approx(Vector2(100, 100), path_follow_2d->get_transform().get_origin()));120121path_follow_2d->set_progress(250);122CHECK(is_equal_approx(Vector2(50, 100), path_follow_2d->get_transform().get_origin()));123124path_follow_2d->set_progress(300);125CHECK(is_equal_approx(Vector2(0, 100), path_follow_2d->get_transform().get_origin()));126127path_follow_2d->set_progress(350);128CHECK(is_equal_approx(Vector2(0, 50), path_follow_2d->get_transform().get_origin()));129130path_follow_2d->set_progress(400);131CHECK(is_equal_approx(Vector2(0, 0), path_follow_2d->get_transform().get_origin()));132133memdelete(path);134}135136TEST_CASE("[SceneTree][PathFollow2D] Removal of a point in curve") {137Ref<Curve2D> curve = memnew(Curve2D);138curve->add_point(Vector2(0, 0));139curve->add_point(Vector2(100, 0));140curve->add_point(Vector2(100, 100));141Path2D *path = memnew(Path2D);142path->set_curve(curve);143PathFollow2D *path_follow_2d = memnew(PathFollow2D);144path->add_child(path_follow_2d);145SceneTree::get_singleton()->get_root()->add_child(path);146147path_follow_2d->set_progress_ratio(0.5);148CHECK(is_equal_approx(Vector2(100, 0), path_follow_2d->get_transform().get_origin()));149150curve->remove_point(1);151152path_follow_2d->set_progress_ratio(0.5);153CHECK_MESSAGE(154is_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");156157memdelete(path);158}159160TEST_CASE("[SceneTree][PathFollow2D] Setting h_offset and v_offset") {161Ref<Curve2D> curve = memnew(Curve2D);162curve->add_point(Vector2(0, 0));163curve->add_point(Vector2(100, 0));164Path2D *path = memnew(Path2D);165path->set_curve(curve);166PathFollow2D *path_follow_2d = memnew(PathFollow2D);167path->add_child(path_follow_2d);168SceneTree::get_singleton()->get_root()->add_child(path);169170path_follow_2d->set_progress_ratio(0.5);171CHECK(is_equal_approx(Vector2(50, 0), path_follow_2d->get_transform().get_origin()));172173path_follow_2d->set_h_offset(25);174CHECK(is_equal_approx(Vector2(75, 0), path_follow_2d->get_transform().get_origin()));175176path_follow_2d->set_v_offset(25);177CHECK(is_equal_approx(Vector2(75, 25), path_follow_2d->get_transform().get_origin()));178179memdelete(path);180}181182TEST_CASE("[SceneTree][PathFollow2D] Progress ratio out of range") {183Ref<Curve2D> curve = memnew(Curve2D);184curve->add_point(Vector2(0, 0));185curve->add_point(Vector2(100, 0));186Path2D *path = memnew(Path2D);187path->set_curve(curve);188PathFollow2D *path_follow_2d = memnew(PathFollow2D);189path->add_child(path_follow_2d);190SceneTree::get_singleton()->get_root()->add_child(path);191192path_follow_2d->set_loop(true);193194path_follow_2d->set_progress_ratio(-0.3);195CHECK_MESSAGE(196Math::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");198199path_follow_2d->set_progress_ratio(1.3);200CHECK_MESSAGE(201Math::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");203204path_follow_2d->set_loop(false);205206path_follow_2d->set_progress_ratio(-0.3);207CHECK_MESSAGE(208Math::is_equal_approx(path_follow_2d->get_progress_ratio(), 0),209"Progress Ratio should be clamped at 0");210211path_follow_2d->set_progress_ratio(1.3);212CHECK_MESSAGE(213Math::is_equal_approx(path_follow_2d->get_progress_ratio(), 1),214"Progress Ratio should be clamped at 1");215216memdelete(path);217}218219TEST_CASE("[SceneTree][PathFollow2D] Progress out of range") {220Ref<Curve2D> curve = memnew(Curve2D);221curve->add_point(Vector2(0, 0));222curve->add_point(Vector2(100, 0));223Path2D *path = memnew(Path2D);224path->set_curve(curve);225PathFollow2D *path_follow_2d = memnew(PathFollow2D);226path->add_child(path_follow_2d);227SceneTree::get_singleton()->get_root()->add_child(path);228229path_follow_2d->set_loop(true);230231path_follow_2d->set_progress(-50);232CHECK_MESSAGE(233Math::is_equal_approx(path_follow_2d->get_progress(), 50),234"Progress should loop back from the end in the opposite direction");235236path_follow_2d->set_progress(150);237CHECK_MESSAGE(238Math::is_equal_approx(path_follow_2d->get_progress(), 50),239"Progress should loop back from the end in the opposite direction");240241path_follow_2d->set_loop(false);242243path_follow_2d->set_progress(-50);244CHECK_MESSAGE(245Math::is_equal_approx(path_follow_2d->get_progress(), 0),246"Progress should be clamped at 0");247248path_follow_2d->set_progress(150);249CHECK_MESSAGE(250Math::is_equal_approx(path_follow_2d->get_progress(), 100),251"Progress should be clamped at 1");252253memdelete(path);254}255256} // namespace TestPathFollow2D257258259