Prevent Parallax2D autoscroll reset

This commit is contained in:
Mark DiBarry 2024-08-28 16:27:56 -04:00
parent 40b378e9e2
commit 1eda1cf5d7
2 changed files with 13 additions and 6 deletions

View file

@ -47,9 +47,18 @@ void Parallax2D::_notification(int p_what) {
} break;
case NOTIFICATION_INTERNAL_PROCESS: {
autoscroll_offset += autoscroll * get_process_delta_time();
autoscroll_offset = autoscroll_offset.posmodv(repeat_size);
Point2 offset = scroll_offset;
offset += autoscroll * get_process_delta_time();
if (repeat_size.x) {
offset.x = Math::fposmod(offset.x, repeat_size.x);
}
if (repeat_size.y) {
offset.y = Math::fposmod(offset.y, repeat_size.y);
}
scroll_offset = offset;
_update_scroll();
} break;
@ -106,14 +115,14 @@ void Parallax2D::_update_scroll() {
scroll_ofs *= scroll_scale;
if (repeat_size.x) {
real_t mod = Math::fposmod(scroll_ofs.x - scroll_offset.x - autoscroll_offset.x, repeat_size.x * get_scale().x);
real_t mod = Math::fposmod(scroll_ofs.x - scroll_offset.x, repeat_size.x * get_scale().x);
scroll_ofs.x = screen_offset.x - mod;
} else {
scroll_ofs.x = screen_offset.x + scroll_offset.x - scroll_ofs.x;
}
if (repeat_size.y) {
real_t mod = Math::fposmod(scroll_ofs.y - scroll_offset.y - autoscroll_offset.y, repeat_size.y * get_scale().y);
real_t mod = Math::fposmod(scroll_ofs.y - scroll_offset.y, repeat_size.y * get_scale().y);
scroll_ofs.y = screen_offset.y - mod;
} else {
scroll_ofs.y = screen_offset.y + scroll_offset.y - scroll_ofs.y;
@ -193,7 +202,6 @@ void Parallax2D::set_autoscroll(const Point2 &p_autoscroll) {
}
autoscroll = p_autoscroll;
autoscroll_offset = Point2();
_update_process();
_update_scroll();

View file

@ -47,7 +47,6 @@ class Parallax2D : public Node2D {
Point2 limit_begin = Point2(-DEFAULT_LIMIT, -DEFAULT_LIMIT);
Point2 limit_end = Point2(DEFAULT_LIMIT, DEFAULT_LIMIT);
Point2 autoscroll;
Point2 autoscroll_offset;
bool follow_viewport = true;
bool ignore_camera_scroll = false;