mirror of
https://github.com/godotengine/godot.git
synced 2025-02-18 08:41:18 +00:00
Merge pull request #43399 from KoBeWi/path_stalking
Update PathFollow2D when curve is changed
This commit is contained in:
commit
5663c78bd2
@ -31,6 +31,7 @@
|
|||||||
#include "path_2d.h"
|
#include "path_2d.h"
|
||||||
|
|
||||||
#include "core/math/geometry_2d.h"
|
#include "core/math/geometry_2d.h"
|
||||||
|
#include "scene/main/timer.h"
|
||||||
|
|
||||||
#ifdef TOOLS_ENABLED
|
#ifdef TOOLS_ENABLED
|
||||||
#include "editor/editor_scale.h"
|
#include "editor/editor_scale.h"
|
||||||
@ -171,6 +172,12 @@ void Path2D::_curve_changed() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
queue_redraw();
|
queue_redraw();
|
||||||
|
for (int i = 0; i < get_child_count(); i++) {
|
||||||
|
PathFollow2D *follow = Object::cast_to<PathFollow2D>(get_child(i));
|
||||||
|
if (follow) {
|
||||||
|
follow->path_changed();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Path2D::set_curve(const Ref<Curve2D> &p_curve) {
|
void Path2D::set_curve(const Ref<Curve2D> &p_curve) {
|
||||||
@ -200,6 +207,14 @@ void Path2D::_bind_methods() {
|
|||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
void PathFollow2D::path_changed() {
|
||||||
|
if (update_timer && !update_timer->is_stopped()) {
|
||||||
|
update_timer->start();
|
||||||
|
} else {
|
||||||
|
_update_transform();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void PathFollow2D::_update_transform() {
|
void PathFollow2D::_update_transform() {
|
||||||
if (!path) {
|
if (!path) {
|
||||||
return;
|
return;
|
||||||
@ -230,6 +245,16 @@ void PathFollow2D::_update_transform() {
|
|||||||
|
|
||||||
void PathFollow2D::_notification(int p_what) {
|
void PathFollow2D::_notification(int p_what) {
|
||||||
switch (p_what) {
|
switch (p_what) {
|
||||||
|
case NOTIFICATION_READY: {
|
||||||
|
if (Engine::get_singleton()->is_editor_hint()) {
|
||||||
|
update_timer = memnew(Timer);
|
||||||
|
update_timer->set_wait_time(0.2);
|
||||||
|
update_timer->set_one_shot(true);
|
||||||
|
update_timer->connect("timeout", callable_mp(this, &PathFollow2D::_update_transform));
|
||||||
|
add_child(update_timer, false, Node::INTERNAL_MODE_BACK);
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
|
||||||
case NOTIFICATION_ENTER_TREE: {
|
case NOTIFICATION_ENTER_TREE: {
|
||||||
path = Object::cast_to<Path2D>(get_parent());
|
path = Object::cast_to<Path2D>(get_parent());
|
||||||
if (path) {
|
if (path) {
|
||||||
|
@ -34,6 +34,8 @@
|
|||||||
#include "scene/2d/node_2d.h"
|
#include "scene/2d/node_2d.h"
|
||||||
#include "scene/resources/curve.h"
|
#include "scene/resources/curve.h"
|
||||||
|
|
||||||
|
class Timer;
|
||||||
|
|
||||||
class Path2D : public Node2D {
|
class Path2D : public Node2D {
|
||||||
GDCLASS(Path2D, Node2D);
|
GDCLASS(Path2D, Node2D);
|
||||||
|
|
||||||
@ -65,6 +67,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
Path2D *path = nullptr;
|
Path2D *path = nullptr;
|
||||||
real_t progress = 0.0;
|
real_t progress = 0.0;
|
||||||
|
Timer *update_timer = nullptr;
|
||||||
real_t h_offset = 0.0;
|
real_t h_offset = 0.0;
|
||||||
real_t v_offset = 0.0;
|
real_t v_offset = 0.0;
|
||||||
real_t lookahead = 4.0;
|
real_t lookahead = 4.0;
|
||||||
@ -81,6 +84,8 @@ protected:
|
|||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
void path_changed();
|
||||||
|
|
||||||
void set_progress(real_t p_progress);
|
void set_progress(real_t p_progress);
|
||||||
real_t get_progress() const;
|
real_t get_progress() const;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user