From 1eda1cf5d7b2bc8fb3ded04612e756f0a31a73ad Mon Sep 17 00:00:00 2001
From: Mark DiBarry <markdibarry@protonmail.com>
Date: Wed, 28 Aug 2024 16:27:56 -0400
Subject: [PATCH] Prevent Parallax2D autoscroll reset

---
 scene/2d/parallax_2d.cpp | 18 +++++++++++++-----
 scene/2d/parallax_2d.h   |  1 -
 2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/scene/2d/parallax_2d.cpp b/scene/2d/parallax_2d.cpp
index 9dd9d4a376d..fdb2d2cdd02 100644
--- a/scene/2d/parallax_2d.cpp
+++ b/scene/2d/parallax_2d.cpp
@@ -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();
diff --git a/scene/2d/parallax_2d.h b/scene/2d/parallax_2d.h
index 5fbc3a20c84..f15e3fa9ffa 100644
--- a/scene/2d/parallax_2d.h
+++ b/scene/2d/parallax_2d.h
@@ -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;