Overhauled the ParallaxBackground system so that it works properly regardless of the zoom of the camera.

This commit is contained in:
Sofox
2017-10-26 20:08:40 +01:00
parent 2987e6ce4f
commit 43cbef219b
5 changed files with 27 additions and 16 deletions

View File

@ -40,7 +40,7 @@ void ParallaxLayer::set_motion_scale(const Size2 &p_scale) {
if (pb && is_inside_tree()) {
Vector2 ofs = pb->get_final_offset();
float scale = pb->get_scroll_scale();
set_base_offset_and_scale(ofs, scale);
set_base_offset_and_scale(ofs, scale, screen_offset);
}
}
@ -57,7 +57,7 @@ void ParallaxLayer::set_motion_offset(const Size2 &p_offset) {
if (pb && is_inside_tree()) {
Vector2 ofs = pb->get_final_offset();
float scale = pb->get_scroll_scale();
set_base_offset_and_scale(ofs, scale);
set_base_offset_and_scale(ofs, scale, screen_offset);
}
}
@ -106,26 +106,28 @@ void ParallaxLayer::_notification(int p_what) {
}
}
void ParallaxLayer::set_base_offset_and_scale(const Point2 &p_offset, float p_scale) {
void ParallaxLayer::set_base_offset_and_scale(const Point2 &p_offset, float p_scale, const Point2 &p_screen_offset) {
screen_offset = p_screen_offset;
if (!is_inside_tree())
return;
if (Engine::get_singleton()->is_editor_hint())
return;
Point2 new_ofs = ((orig_offset + p_offset) * motion_scale) * p_scale + motion_offset;
Point2 new_ofs = (screen_offset + (p_offset - screen_offset) * motion_scale) + motion_offset * p_scale + orig_offset * p_scale;
Vector2 mirror = Vector2(1, 1);
if (mirroring.x) {
double den = mirroring.x * p_scale;
new_ofs.x -= den * ceil(new_ofs.x / den);
mirror.x = -1;
}
if (mirroring.y) {
double den = mirroring.y * p_scale;
new_ofs.y -= den * ceil(new_ofs.y / den);
mirror.y = -1;
}
set_position(new_ofs);
set_scale(Vector2(1, 1) * p_scale);
set_scale(mirror * p_scale * orig_scale);
}
String ParallaxLayer::get_configuration_warning() const {