183 lines
5.6 KiB
C#
183 lines
5.6 KiB
C#
using UnityEngine;
|
|
using Pooling;
|
|
|
|
namespace Minigames.DivingForPictures
|
|
{
|
|
/// <summary>
|
|
/// Complete floating obstacle component that handles data, movement, and pooling.
|
|
/// Obstacles move upward toward the surface. Collision detection is now handled by the player.
|
|
/// </summary>
|
|
public class FloatingObstacle : MonoBehaviour, IPoolable
|
|
{
|
|
[Header("Obstacle Properties")]
|
|
[Tooltip("Index of the prefab this obstacle was created from")]
|
|
[SerializeField] private int prefabIndex;
|
|
|
|
[Tooltip("Damage this obstacle deals to the player")]
|
|
[SerializeField] private float damage = 1f;
|
|
|
|
[Tooltip("Movement speed of this obstacle")]
|
|
[SerializeField] private float moveSpeed = 2f;
|
|
|
|
[Header("Movement")]
|
|
[Tooltip("Whether this obstacle moves (can be disabled for static obstacles)")]
|
|
[SerializeField] private bool enableMovement = true;
|
|
|
|
[Header("References")]
|
|
[Tooltip("Reference to the spawner that created this obstacle")]
|
|
[SerializeField] private ObstacleSpawner spawner;
|
|
|
|
// Public properties
|
|
public int PrefabIndex
|
|
{
|
|
get => prefabIndex;
|
|
set => prefabIndex = value;
|
|
}
|
|
|
|
public float Damage
|
|
{
|
|
get => damage;
|
|
set => damage = value;
|
|
}
|
|
|
|
public float MoveSpeed
|
|
{
|
|
get => moveSpeed;
|
|
set => moveSpeed = value;
|
|
}
|
|
|
|
public bool HasDealtDamage => _hasDealtDamage;
|
|
|
|
// Private fields
|
|
private Collider2D _collider;
|
|
private bool _hasDealtDamage;
|
|
private Camera _mainCamera;
|
|
private float _screenTop;
|
|
|
|
private void Awake()
|
|
{
|
|
_collider = GetComponent<Collider2D>();
|
|
|
|
if (_collider == null)
|
|
{
|
|
_collider = GetComponentInChildren<Collider2D>();
|
|
}
|
|
|
|
if (_collider == null)
|
|
{
|
|
Debug.LogError($"[FloatingObstacle] No Collider2D found on {gameObject.name}!");
|
|
}
|
|
|
|
_mainCamera = Camera.main;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (enableMovement)
|
|
{
|
|
HandleMovement();
|
|
}
|
|
|
|
CheckIfOffScreen();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Moves the obstacle upward based on its speed
|
|
/// </summary>
|
|
private void HandleMovement()
|
|
{
|
|
transform.position += Vector3.up * (moveSpeed * Time.deltaTime);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Marks this obstacle as having dealt damage (called by PlayerDamageCollisionBehavior)
|
|
/// </summary>
|
|
public void MarkDamageDealt()
|
|
{
|
|
if (!_hasDealtDamage)
|
|
{
|
|
_hasDealtDamage = true;
|
|
Debug.Log($"[FloatingObstacle] Obstacle {gameObject.name} dealt {damage} damage to player");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks if the obstacle has moved off-screen and should be despawned
|
|
/// </summary>
|
|
private void CheckIfOffScreen()
|
|
{
|
|
if (_mainCamera == null) return;
|
|
|
|
// Calculate screen top if not cached
|
|
if (_screenTop == 0f)
|
|
{
|
|
Vector3 topWorldPoint = _mainCamera.ViewportToWorldPoint(new Vector3(0.5f, 1f, _mainCamera.nearClipPlane));
|
|
_screenTop = topWorldPoint.y;
|
|
}
|
|
|
|
// Check if obstacle is above screen
|
|
if (transform.position.y > _screenTop + 2f) // Extra buffer for safety
|
|
{
|
|
ReturnToPool();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns this obstacle to the spawner's pool
|
|
/// </summary>
|
|
private void ReturnToPool()
|
|
{
|
|
if (spawner != null)
|
|
{
|
|
spawner.ReturnObstacleToPool(gameObject, prefabIndex);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"[FloatingObstacle] Cannot return {gameObject.name} to pool - missing spawner reference");
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the spawner reference for this obstacle
|
|
/// </summary>
|
|
/// <param name="obstacleSpawner">The spawner that created this obstacle</param>
|
|
public void SetSpawner(ObstacleSpawner obstacleSpawner)
|
|
{
|
|
spawner = obstacleSpawner;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called when the obstacle is retrieved from the pool
|
|
/// </summary>
|
|
public void OnSpawn()
|
|
{
|
|
_hasDealtDamage = false;
|
|
_screenTop = 0f; // Reset cached screen bounds
|
|
|
|
// Ensure the obstacle is active and visible
|
|
gameObject.SetActive(true);
|
|
|
|
Debug.Log($"[FloatingObstacle] Obstacle {gameObject.name} spawned");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called when the obstacle is returned to the pool
|
|
/// </summary>
|
|
public void OnDespawn()
|
|
{
|
|
_hasDealtDamage = false;
|
|
|
|
Debug.Log($"[FloatingObstacle] Obstacle {gameObject.name} despawned");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Public method to manually trigger return to pool (for external systems)
|
|
/// </summary>
|
|
public void ForceReturnToPool()
|
|
{
|
|
ReturnToPool();
|
|
}
|
|
}
|
|
}
|