Improvements and touchups to a more event-driven flow and fake physics

This commit is contained in:
Michal Pikulski
2025-09-18 14:14:01 +02:00
parent 44c1e48b82
commit 3f52de9e1e
5 changed files with 56 additions and 110 deletions

View File

@@ -4,8 +4,9 @@ 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.
/// Complete floating obstacle component that handles movement and pooling.
/// Obstacles move upward toward the surface. Collision detection is handled by the player.
/// Once an obstacle hits the player, its collider is disabled to prevent further collisions.
/// </summary>
public class FloatingObstacle : MonoBehaviour, IPoolable
{
@@ -13,9 +14,6 @@ namespace Minigames.DivingForPictures
[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;
@@ -34,23 +32,14 @@ namespace Minigames.DivingForPictures
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;
@@ -90,14 +79,15 @@ namespace Minigames.DivingForPictures
}
/// <summary>
/// Marks this obstacle as having dealt damage (called by PlayerDamageCollisionBehavior)
/// Disables the collider after hitting the player to prevent further collisions
/// This is more performant than tracking hit state
/// </summary>
public void MarkDamageDealt()
{
if (!_hasDealtDamage)
if (_collider != null && _collider.enabled)
{
_hasDealtDamage = true;
Debug.Log($"[FloatingObstacle] Obstacle {gameObject.name} dealt {damage} damage to player");
_collider.enabled = false;
Debug.Log($"[FloatingObstacle] Obstacle {gameObject.name} hit player - collider disabled");
}
}
@@ -152,9 +142,14 @@ namespace Minigames.DivingForPictures
/// </summary>
public void OnSpawn()
{
_hasDealtDamage = false;
_screenTop = 0f; // Reset cached screen bounds
// Re-enable the collider for reuse
if (_collider != null)
{
_collider.enabled = true;
}
// Ensure the obstacle is active and visible
gameObject.SetActive(true);
@@ -166,7 +161,11 @@ namespace Minigames.DivingForPictures
/// </summary>
public void OnDespawn()
{
_hasDealtDamage = false;
// Re-enable collider for next use (in case it was disabled)
if (_collider != null)
{
_collider.enabled = true;
}
Debug.Log($"[FloatingObstacle] Obstacle {gameObject.name} despawned");
}