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,3 @@
fileFormatVersion: 2
guid: 59dcc6f7af2c4e49a06f26fab743e664
timeCreated: 1757488376

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;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8adce29d10a54dafae7df1fcacc58da5
timeCreated: 1757015986

View File

@@ -0,0 +1,78 @@
using UnityEngine;
namespace Minigames.DivingForPictures
{
/// <summary>
/// Spawns bubbles at intervals, randomizing their properties and assigning a random sprite to each.
/// </summary>
public class BubbleSpawner : MonoBehaviour
{
public Bubble bubblePrefab;
public Sprite[] bubbleSprites; // Assign in inspector
public float spawnInterval = 0.3f;
public Vector2 speedRange = new Vector2(0.5f, 2f);
public Vector2 scaleRange = new Vector2(0.3f, 0.7f);
public Vector2 wobbleSpeedRange = new Vector2(1f, 3f);
public Vector2 wobbleAmountRange = new Vector2(0.05f, 0.15f);
public float spawnXMin = -3.5f;
public float spawnXMax = 3.5f;
public float spawnY = -5f;
public float wobbleMinScale = 0.2f;
public float wobbleMaxScale = 1.2f;
private float _timer;
private float _nextSpawnInterval;
void Start()
{
// Initialize the next spawn interval
_nextSpawnInterval = GetRandomizedInterval();
}
void Update()
{
_timer += Time.deltaTime;
if (_timer >= _nextSpawnInterval)
{
SpawnBubble();
_timer = 0f;
_nextSpawnInterval = GetRandomizedInterval();
}
}
/// <summary>
/// Returns a randomized interval for bubble spawning.
/// </summary>
/// <returns>Randomized interval in seconds.</returns>
float GetRandomizedInterval()
{
return spawnInterval * Random.Range(0.8f, 1.2f);
}
/// <summary>
/// Spawns a bubble with randomized properties and assigns a random sprite.
/// </summary>
void SpawnBubble()
{
float x = Random.Range(spawnXMin, spawnXMax);
Vector3 spawnPos = new Vector3(x, spawnY, 0f);
Bubble bubble = Instantiate(bubblePrefab, spawnPos, Quaternion.identity, transform);
// Randomize bubble properties
bubble.speed = Random.Range(speedRange.x, speedRange.y);
bubble.wobbleSpeed = Random.Range(wobbleSpeedRange.x, wobbleSpeedRange.y);
float scale = Random.Range(scaleRange.x, scaleRange.y);
bubble.transform.localScale = Vector3.one * scale;
// Assign random sprite to BottleSprite
if (bubbleSprites != null && bubbleSprites.Length > 0)
{
Sprite randomSprite = bubbleSprites[Random.Range(0, bubbleSprites.Length)];
bubble.SetBottleSprite(randomSprite);
}
// Random rotation
bubble.transform.rotation = Quaternion.Euler(0f, 0f, Random.Range(0f, 360f));
// Pass min/max scale for wobble clamping
bubble.SetWobbleScaleLimits(wobbleMinScale, wobbleMaxScale);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1f047af2457745158162344fe19c3477
timeCreated: 1757015997

View File

@@ -0,0 +1,94 @@
using UnityEngine;
namespace Minigames.DivingForPictures
{
/// <summary>
/// Handles endless descender movement in response to tap and hold input events.
/// Moves the character horizontally to follow the finger or tap position.
/// </summary>
public class PlayerController : MonoBehaviour, ITouchInputConsumer
{
private float _targetFingerX;
private bool _isTouchActive;
private float _originY;
void Awake()
{
_originY = transform.position.y;
}
void Start()
{
// Register as default consumer for input
InputManager.Instance?.SetDefaultConsumer(this);
// Initialize target to current position
_targetFingerX = transform.position.x;
_isTouchActive = false;
}
/// <summary>
/// Handles tap input. Moves to the tapped X position.
/// </summary>
public void OnTap(Vector2 worldPosition)
{
Debug.Log($"[EndlessDescenderController] OnTap at {worldPosition}");
_targetFingerX = Mathf.Clamp(worldPosition.x, GameManager.Instance.EndlessDescenderClampXMin, GameManager.Instance.EndlessDescenderClampXMax);
_isTouchActive = true;
}
/// <summary>
/// Handles the start of a hold input. Begins tracking the finger.
/// </summary>
public void OnHoldStart(Vector2 worldPosition)
{
Debug.Log($"[EndlessDescenderController] OnHoldStart at {worldPosition}");
_targetFingerX = Mathf.Clamp(worldPosition.x, GameManager.Instance.EndlessDescenderClampXMin, GameManager.Instance.EndlessDescenderClampXMax);
_isTouchActive = true;
}
/// <summary>
/// Handles hold move input. Updates the target X position as the finger moves.
/// </summary>
public void OnHoldMove(Vector2 worldPosition)
{
Debug.Log($"[EndlessDescenderController] OnHoldMove at {worldPosition}");
_targetFingerX = Mathf.Clamp(worldPosition.x, GameManager.Instance.EndlessDescenderClampXMin, GameManager.Instance.EndlessDescenderClampXMax);
}
/// <summary>
/// Handles the end of a hold input. Stops tracking.
/// </summary>
public void OnHoldEnd(Vector2 worldPosition)
{
Debug.Log($"[EndlessDescenderController] OnHoldEnd at {worldPosition}");
_isTouchActive = false;
}
void Update()
{
if (!_isTouchActive) return;
float currentX = transform.position.x;
float lerpSpeed = GameManager.Instance.EndlessDescenderLerpSpeed;
float maxOffset = GameManager.Instance.EndlessDescenderMaxOffset;
float exponent = GameManager.Instance.EndlessDescenderSpeedExponent;
float targetX = _targetFingerX;
float offset = targetX - currentX;
offset = Mathf.Clamp(offset, -maxOffset, maxOffset);
float absOffset = Mathf.Abs(offset);
float t = Mathf.Pow(absOffset / maxOffset, exponent); // Non-linear drop-off
float moveStep = Mathf.Sign(offset) * maxOffset * t * Time.deltaTime * lerpSpeed;
// Prevent overshooting
moveStep = Mathf.Clamp(moveStep, -absOffset, absOffset);
float newX = currentX + moveStep;
newX = Mathf.Clamp(newX, GameManager.Instance.EndlessDescenderClampXMin, GameManager.Instance.EndlessDescenderClampXMax);
float newY = _originY;
// Add vertical offset from WobbleBehavior if present
WobbleBehavior wobble = GetComponent<WobbleBehavior>();
if (wobble != null)
{
newY += wobble.VerticalOffset;
}
transform.position = new Vector3(newX, newY, transform.position.z);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d39dbaae819c4a128a11ca60fbbc98c9
timeCreated: 1757014906