Files
AppleHillsProduction/Assets/Scripts/Minigames/DivingForPictures/Monster/Monster.cs

177 lines
5.4 KiB
C#
Raw Normal View History

using UnityEngine;
using System;
using System.Collections;
namespace Minigames.DivingForPictures
{
public class Monster : MonoBehaviour
{
[Header("References")]
[SerializeField] private CircleCollider2D detectionCollider;
private bool pictureAlreadyTaken = false;
private bool photoSequenceInProgress = false;
private UnityEngine.Camera mainCamera;
// Track if player is in detection range
private bool playerInDetectionRange = false;
// Events
public event Action<Monster> OnMonsterDespawned;
public event Action<Monster> OnPlayerEnterDetectionRange;
public event Action<Monster> OnPlayerExitDetectionRange;
// Properties
public float PictureRadius => detectionCollider != null ? detectionCollider.radius : 0f;
public bool IsPhotoSequenceInProgress => photoSequenceInProgress;
public bool IsPlayerInDetectionRange => playerInDetectionRange;
public bool IsPictureTaken => pictureAlreadyTaken;
private void Awake()
{
if (detectionCollider == null)
detectionCollider = GetComponent<CircleCollider2D>();
mainCamera = UnityEngine.Camera.main;
// Start checking if monster is off-screen
StartCoroutine(CheckIfOffScreen());
}
private void OnEnable()
{
pictureAlreadyTaken = false;
photoSequenceInProgress = false;
}
private IEnumerator CheckIfOffScreen()
{
WaitForSeconds wait = new WaitForSeconds(0.5f);
while (true)
{
yield return wait;
if (!IsVisibleToCamera())
{
DespawnMonster();
yield break;
}
}
}
private bool IsVisibleToCamera()
{
if (mainCamera == null)
mainCamera = UnityEngine.Camera.main;
if (mainCamera == null)
return false;
2025-09-19 13:46:24 +02:00
// Get the world position (will account for parent movement)
Vector3 worldPosition = transform.position;
Vector3 viewportPoint = mainCamera.WorldToViewportPoint(worldPosition);
// Adjust buffer to be larger below the screen to account for downward movement
float bufferSides = 0.2f;
float bufferTop = 0.2f;
float bufferBottom = 0.5f; // Larger buffer below the screen
return viewportPoint.x > -bufferSides &&
viewportPoint.x < 1 + bufferSides &&
viewportPoint.y > -bufferBottom &&
viewportPoint.y < 1 + bufferTop;
}
private void OnTriggerEnter2D(Collider2D other)
{
// Check if it's the player
if (other.CompareTag("Player") && !pictureAlreadyTaken)
{
playerInDetectionRange = true;
// Fire the event so the game manager can display the viewfinder without pausing
OnPlayerEnterDetectionRange?.Invoke(this);
}
}
private void OnTriggerExit2D(Collider2D other)
{
// Check if it's the player
if (other.CompareTag("Player"))
{
playerInDetectionRange = false;
// Fire the event so the game manager can hide the viewfinder
OnPlayerExitDetectionRange?.Invoke(this);
}
}
/// <summary>
/// Mark this monster as having its photo sequence in progress
/// </summary>
public void SetPhotoSequenceInProgress(bool inProgress = true)
{
if (pictureAlreadyTaken) return;
photoSequenceInProgress = inProgress;
}
/// <summary>
/// Notify that a picture has been taken of this monster
/// This is now called by DivingGameManager instead of handling the logic internally
/// </summary>
public void NotifyPictureTaken()
{
if (pictureAlreadyTaken) return;
pictureAlreadyTaken = true;
photoSequenceInProgress = false;
}
/// <summary>
/// Despawn this monster (can be called externally)
/// </summary>
/// <param name="immediate">Whether to despawn immediately or after a delay</param>
public void Despawn(bool immediate = false)
{
if (immediate)
{
DespawnMonster();
}
else
{
// Add small delay before despawning
StartCoroutine(DelayedDespawn());
}
}
/// <summary>
/// Coroutine to despawn after a short delay
/// </summary>
private IEnumerator DelayedDespawn()
{
yield return new WaitForSeconds(1.0f); // 1-second delay before despawn
DespawnMonster();
}
// Public method to despawn this monster
public void DespawnMonster()
{
if (gameObject.activeSelf)
{
OnMonsterDespawned?.Invoke(this);
gameObject.SetActive(false);
Destroy(gameObject);
}
}
#if UNITY_EDITOR
// Update collider radius in editor
private void OnValidate()
{
if (detectionCollider == null)
detectionCollider = GetComponent<CircleCollider2D>();
}
#endif
}
}