-
-
Notifications
You must be signed in to change notification settings - Fork 1
Implement Parallax Scrolling Background linked to Player Speed #574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ikostan
merged 37 commits into
main
from
implement-parallax-scrolling-background-linked-to-player-speed
Apr 19, 2026
Merged
Changes from 27 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
6c8427d
[FEATURE] Implement Parallax Scrolling Background linked to Player Sp…
ikostan 1246521
Update main_scene.gd
ikostan b137b63
Update scripts/main_scene.gd
ikostan e436db6
Update main_scene.gd
ikostan e3a9b70
Update main_scene.gd
ikostan 7040237
Here is a comprehensive GUT unit test suite for the new parallax_mana…
ikostan d6b27ed
Update parallax_manager.gd
ikostan 64b1bbc
completely eliminate the _process polling by using Dependency Inject…
ikostan 1dbb09d
Missing coverage for the fuel-depletion reset, and tests implicitly d…
ikostan 6af4227
Update test/gut/test_parallax_manager.gd
ikostan d5cc59a
Update main_scene.gd
ikostan 39bd963
Update parallax_manager.gd
ikostan ee92636
Update main_scene.gd
ikostan 43ed4c2
Update main_scene.gd
ikostan f6eeb1f
Update main_scene.gd
ikostan 19467a2
scroll_offset.y grows unboundedly — consider wrapping to preserve flo…
ikostan bc0702e
Update main_scene.gd
ikostan b101d71
Update parallax_manager.gd
ikostan 9abb76a
126-148: Nit: test name/assertion message is slightly misleading.
ikostan 2c99f7c
63-67: LGTM — refuel recovery path is correctly wired.
ikostan 27a0903
Update main_scene.gd
ikostan 0117f9f
Update parallax_manager.gd
ikostan 87efef1
Update main_scene.gd
ikostan 189ec21
Update main_scene.gd
ikostan 14084a0
This fully resolves the reviewer's concern by creating a proper, publ…
ikostan b7d5b25
let the ParallaxManager inspect its own children and calculate the ma…
ikostan ba0fb22
player's speed is now rigorously typed
ikostan cafaa32
Unit test updates
ikostan a8a50fe
Update test/gut/test_parallax_manager.gd
ikostan 539986c
Update GUT unit tests
ikostan e1c8628
Merge branch 'implement-parallax-scrolling-background-linked-to-playe…
ikostan de072dc
auto_calculate_wrap_period() silently disables wrap when no layers ar…
ikostan d08fb80
Update parallax_manager.gd
ikostan cac27df
Optional: max-of-periods is only safe when layer periods are commens…
ikostan 5fdc662
Update scripts/main_scene.gd
ikostan 4c3c8ec
Update main_scene.gd
ikostan e6cfa37
Update parallax_manager.gd
ikostan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| ## Copyright (C) 2026 Egor Kostan | ||
| ## SPDX-License-Identifier: GPL-3.0-or-later | ||
| ## parallax_manager.gd | ||
| ## Manages the scrolling speed of the parallax background based on player velocity. | ||
| ## Decoupled via Dependency Injection and the Observer Pattern. | ||
|
|
||
| class_name ParallaxManager | ||
| extends ParallaxBackground | ||
|
|
||
| ## Base multiplier applied to the final scroll math to scale the speed visually. | ||
| const SCROLL_MULTIPLIER: float = 0.8 | ||
|
|
||
| ## Optional wrap limit to prevent float32 precision degradation over long sessions. | ||
| ## Should be a common multiple of the layers' (motion_mirroring.y / motion_scale.y). | ||
| @export var wrap_period: float = 0.0 | ||
|
|
||
| var _current_speed: float = 0.0 | ||
| var _difficulty: float = 1.0 | ||
| var _out_of_fuel: bool = false | ||
|
|
||
|
|
||
| ## Injects the game settings resource and wires up observer signals. | ||
| ## Prevents tight coupling to global singletons in the process loop. | ||
| ## @param settings: GameSettingsResource - The configuration resource. | ||
| ## @return: void | ||
| func setup(settings: GameSettingsResource) -> void: | ||
| if not is_instance_valid(settings): | ||
| return | ||
|
|
||
| _difficulty = settings.difficulty | ||
| _out_of_fuel = (settings.current_fuel <= 0.0) | ||
|
|
||
| if not settings.setting_changed.is_connected(_on_setting_changed): | ||
| settings.setting_changed.connect(_on_setting_changed) | ||
| if not settings.fuel_depleted.is_connected(_on_fuel_depleted): | ||
| settings.fuel_depleted.connect(_on_fuel_depleted) | ||
|
|
||
|
|
||
| ## Public method to prime the background's initial speed. | ||
| ## Keeps private signal handlers properly encapsulated. | ||
| ## @param initial_speed: float - The starting forward speed. | ||
| ## @return: void | ||
| func prime_speed(initial_speed: float) -> void: | ||
| _current_speed = initial_speed | ||
|
|
||
|
|
||
| ## Public method to dynamically calculate the optimal wrap limit | ||
| ## based on the properties of its ParallaxLayer children. | ||
| ## Must be called after all layers have had their mirroring and scale set. | ||
| ## @return: void | ||
| func auto_calculate_wrap_period() -> void: | ||
| var max_period: float = 0.0 | ||
|
|
||
| # Iterate through all children to find the longest required wrap period | ||
| for child in get_children(): | ||
| if child is ParallaxLayer: | ||
| var layer_scale: float = child.motion_scale.y | ||
| var layer_mirror: float = child.motion_mirroring.y | ||
|
|
||
| if layer_scale > 0.0 and layer_mirror > 0.0: | ||
| var period: float = layer_mirror / layer_scale | ||
| if period > max_period: | ||
| max_period = period | ||
|
|
||
| wrap_period = max_period | ||
|
|
||
|
|
||
| ## Public method to update the scrolling speed. | ||
| ## Designed to be safely connected to external speed_changed signals. | ||
| ## @param new_speed: float - The new forward speed. | ||
| ## @param _max_speed: float - The maximum speed threshold (unused, defaults to 0.0). | ||
| ## @return: void | ||
| func update_speed(new_speed: float, _max_speed: float = 0.0) -> void: | ||
| _current_speed = new_speed | ||
|
|
||
|
|
||
| ## Observer callback for specific setting updates (difficulty and refueling). | ||
| ## @param setting_name: String - The name of the changed setting. | ||
| ## @param new_value: Variant - The updated value. | ||
| ## @return: void | ||
| func _on_setting_changed(setting_name: String, new_value: Variant) -> void: | ||
| if setting_name == "difficulty": | ||
| _difficulty = float(new_value) | ||
| elif setting_name == "current_fuel" and float(new_value) > 0.0: | ||
| _out_of_fuel = false | ||
|
|
||
|
|
||
| ## Observer callback to instantly snap the background when fuel runs out. | ||
| ## @return: void | ||
| func _on_fuel_depleted() -> void: | ||
| _out_of_fuel = true | ||
| scroll_offset = Vector2.ZERO | ||
|
|
||
|
|
||
| ## Called every physics/rendering frame. | ||
| ## Updates scroll offset based entirely on cached local variables | ||
| ## and wraps to preserve float precision. | ||
| ## @param delta: float - The elapsed time since the previous frame. | ||
| ## @return: void | ||
| func _process(delta: float) -> void: | ||
| if _out_of_fuel: | ||
| scroll_offset = Vector2.ZERO | ||
| else: | ||
| var scroll_amount: float = _current_speed * delta * _difficulty * SCROLL_MULTIPLIER | ||
| scroll_offset.y += scroll_amount | ||
|
|
||
| # Prevent float precision degradation by wrapping modulo the period | ||
| if wrap_period > 0.0: | ||
| scroll_offset.y = wrapf(scroll_offset.y, 0.0, wrap_period) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| uid://b5x2ehdthhrla |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.