Finalize the parallax work

This commit is contained in:
Michal Pikulski
2025-12-17 22:54:24 +01:00
parent 867399c838
commit 7f5a229c3a
46 changed files with 3908 additions and 1246 deletions

View File

@@ -7,48 +7,27 @@ namespace Minigames.Airplane.Interactive
/// <summary>
/// Parallax element that adjusts position based on camera movement.
/// Creates depth illusion by moving at different speeds for different layers.
/// Continuously tracks active camera for seamless transitions.
/// Configuration is provided by ParallaxBackgroundSpawner at initialization.
/// IMPORTANT: Always calculates position relative to ORIGINAL spawn position and camera,
/// ensuring deterministic behavior during camera blends and switches.
/// </summary>
public class ParallaxElement : MonoBehaviour
{
[Header("Layer Configuration")]
[Tooltip("Which parallax layer this element belongs to")]
[SerializeField] private ParallaxLayer layer = ParallaxLayer.Background;
[Header("Parallax Settings")]
[Tooltip("Global parallax strength multiplier (0 = no parallax, 1 = full)")]
[SerializeField] private float globalStrength = 1f;
[Tooltip("Per-layer parallax factors (Background/Middle/Foreground)")]
[SerializeField] private float backgroundFactor = 0.3f;
[SerializeField] private float middleFactor = 0.6f;
[SerializeField] private float foregroundFactor = 0.9f;
[Header("Debug")]
[SerializeField] private bool showDebugLogs;
// Runtime state (set by spawner via Initialize)
private ParallaxLayer _layer;
private float _layerSpeed;
private float _globalStrength;
// Current camera being tracked
private Transform _cameraTransform;
private Vector3 _startPosition;
private float _startCameraX;
// ORIGINAL spawn state - NEVER reset after initialization
// This ensures objects return to spawn position when camera returns to spawn position
private Vector3 _originalSpawnPosition;
private float _originalCameraX;
private bool _isInitialized;
private void Awake()
{
// Ensure correct sort layer
EnsureSortLayer();
}
private void Start()
{
_startPosition = transform.position;
if (_cameraTransform != null)
{
_startCameraX = _cameraTransform.position.x;
_isInitialized = true;
}
}
private void Update()
{
if (!_isInitialized || _cameraTransform == null) return;
@@ -57,94 +36,69 @@ namespace Minigames.Airplane.Interactive
}
/// <summary>
/// Set the parallax layer for this element.
/// Initialize the parallax element with spawner-provided settings.
/// Called by ParallaxBackgroundSpawner when spawning.
/// Sets the ORIGINAL spawn position and camera position - these are never reset.
/// IMPORTANT: Uses Camera.main for tracking to ensure smooth updates during Cinemachine blends.
/// </summary>
public void SetLayer(ParallaxLayer newLayer)
public void Initialize(ParallaxLayer layer, float layerSpeed, float globalStrength, Transform cameraTransform)
{
layer = newLayer;
EnsureSortLayer();
_layer = layer;
_layerSpeed = layerSpeed;
_globalStrength = globalStrength;
if (showDebugLogs)
// ALWAYS use Camera.main for tracking - this ensures smooth updates during Cinemachine blends
// Virtual cameras don't move during blends, but Camera.main does
_cameraTransform = Camera.main != null ? Camera.main.transform : cameraTransform;
// Store ORIGINAL spawn position - never reset
_originalSpawnPosition = transform.position;
if (_cameraTransform != null)
{
Logging.Debug($"[ParallaxElement] Layer set to {layer}");
// Store ORIGINAL camera X - never reset
_originalCameraX = _cameraTransform.position.x;
_isInitialized = true;
}
}
/// <summary>
/// Set the camera transform to track.
/// Call this when camera changes.
/// Update the camera transform when camera changes.
/// Called by ParallaxBackgroundSpawner when camera switches or blends.
/// ONLY updates the camera reference - does NOT reset start position or camera X.
/// This ensures parallax remains deterministic across camera switches.
/// IMPORTANT: Always uses Camera.main to ensure smooth updates during blends.
/// </summary>
public void SetCameraTransform(Transform cameraTransform)
public void UpdateCamera(Transform newCameraTransform)
{
if (cameraTransform == null) return;
// ALWAYS use Camera.main for tracking - this ensures smooth updates during Cinemachine blends
// We ignore the passed transform and always use Camera.main
Transform mainCamera = Camera.main != null ? Camera.main.transform : newCameraTransform;
// If camera changed, recalculate base position
if (_cameraTransform != null && _cameraTransform != cameraTransform)
{
// Smooth transition: current world position becomes new start position
_startPosition = transform.position;
}
if (mainCamera == null) return;
_cameraTransform = cameraTransform;
_startCameraX = _cameraTransform.position.x;
// Simply update the camera reference - do NOT reset positions
// The parallax will continue to calculate relative to original spawn state
_cameraTransform = mainCamera;
_isInitialized = true;
if (showDebugLogs)
{
Logging.Debug($"[ParallaxElement] Camera set, starting camera X={_startCameraX:F2}");
}
}
private void ApplyParallax()
{
// Calculate camera displacement from start
float cameraDisplacement = _cameraTransform.position.x - _startCameraX;
// Calculate camera displacement from ORIGINAL camera position
// This ensures objects return to spawn position when camera returns to spawn position
float cameraDisplacement = _cameraTransform.position.x - _originalCameraX;
// Get layer-specific parallax factor
float layerFactor = GetLayerFactor();
// Calculate parallax offset - negative to move opposite direction
// Lower speed = appears further away (moves less)
// Camera moves right (+) → background moves left (-) at reduced speed
float parallaxOffset = -cameraDisplacement * _layerSpeed * _globalStrength;
// Calculate parallax offset (reduced displacement based on layer depth)
float parallaxOffset = cameraDisplacement * layerFactor * globalStrength;
// Apply offset to start position
Vector3 newPosition = _startPosition;
// Apply offset to ORIGINAL spawn position
Vector3 newPosition = _originalSpawnPosition;
newPosition.x += parallaxOffset;
transform.position = newPosition;
}
private float GetLayerFactor()
{
return layer switch
{
ParallaxLayer.Background => backgroundFactor,
ParallaxLayer.Middle => middleFactor,
ParallaxLayer.Foreground => foregroundFactor,
_ => 1f
};
}
private void EnsureSortLayer()
{
SpriteRenderer spriteRenderer = GetComponentInChildren<SpriteRenderer>();
if (spriteRenderer == null) return;
// Sort layer is set by spawner, this is just a validation/fallback
string expectedLayer = layer switch
{
ParallaxLayer.Background => "Background",
ParallaxLayer.Middle => "Midground",
ParallaxLayer.Foreground => "Foreground",
_ => "Default"
};
if (spriteRenderer.sortingLayerName != expectedLayer)
{
if (showDebugLogs)
{
Logging.Debug($"[ParallaxElement] Adjusting sort layer to '{expectedLayer}' for {layer}");
}
}
}
}
}