Updates and reworks, code rewrites

This commit is contained in:
Michal Pikulski
2025-09-10 09:36:15 +02:00
parent 52bd7ef585
commit 2dfe6de144
14 changed files with 524 additions and 242 deletions

View File

@@ -0,0 +1,77 @@
using UnityEngine;
namespace Minigames.DivingForPictures
{
/// <summary>
/// Represents a single bubble, handling its movement, wobble effect, scaling, and sprite assignment.
/// </summary>
public class Bubble : MonoBehaviour
{
public float speed = 1f;
public float wobbleSpeed = 1f;
private SpriteRenderer spriteRenderer;
private SpriteRenderer bottleSpriteRenderer;
private float timeOffset;
private float minScale = 0.2f;
private float maxScale = 1.2f;
void Awake()
{
// Cache references and randomize time offset for wobble
spriteRenderer = GetComponent<SpriteRenderer>();
timeOffset = Random.value * 100f;
// Find the child named "BottleSprite" and get its SpriteRenderer
Transform bottleSpriteTransform = transform.Find("BubbleSprite");
if (bottleSpriteTransform != null)
{
bottleSpriteRenderer = bottleSpriteTransform.GetComponent<SpriteRenderer>();
}
}
void Update()
{
// Move bubble upward
transform.position += Vector3.up * speed * Time.deltaTime;
// Wobble effect (smooth oscillation between min and max scale)
float t = (Mathf.Sin((Time.time + timeOffset) * wobbleSpeed) + 1f) * 0.5f; // t in [0,1]
float newScale = Mathf.Lerp(minScale, maxScale, t);
transform.localScale = Vector3.one * newScale;
// Destroy when off screen
if (transform.position.y > Camera.main.orthographicSize + 2f)
{
Destroy(gameObject);
}
}
/// <summary>
/// Sets the main sprite for the bubble.
/// </summary>
/// <param name="sprite">Sprite to assign.</param>
public void SetSprite(Sprite sprite)
{
if (spriteRenderer != null)
spriteRenderer.sprite = sprite;
}
/// <summary>
/// Sets the sprite for the child "BottleSprite" renderer.
/// </summary>
/// <param name="sprite">Sprite to assign.</param>
public void SetBottleSprite(Sprite sprite)
{
if (bottleSpriteRenderer != null)
bottleSpriteRenderer.sprite = sprite;
}
/// <summary>
/// Sets the minimum and maximum scale for the wobble effect.
/// </summary>
/// <param name="min">Minimum scale.</param>
/// <param name="max">Maximum scale.</param>
public void SetWobbleScaleLimits(float min, float max)
{
minScale = min;
maxScale = max;
}
}
}