MiniGames/Assets/Scripts/CatchTheButterfly/ButterflyGameManager.cs
2025-07-15 22:16:23 +05:00

107 lines
2.4 KiB
C#

using UnityEngine;
using TMPro;
public class ButterflyGameManager : MonoBehaviour
{
public static ButterflyGameManager Instance;
public ButterflyZibu Mole;
public ButterflySpawner Spawner;
public TextMeshProUGUI scoreText;
public TextMeshProUGUI timerText;
public GameObject gameOverPanel;
public GameObject winPanel;
public int butterfliesToSpawn = 10;
public float levelTime = 60f;
private float timer;
private int score = 0;
private int butterfliesCaught = 0;
private bool gameEnded = false;
void Awake()
{
Instance = this;
timer = levelTime;
}
void Start()
{
Spawner.SpawnButterflies(butterfliesToSpawn);
//UpdateUI();
}
void Update()
{
if (gameEnded) return;
timer -= Time.deltaTime;
timerText.text = "Time: " + Mathf.Ceil(timer).ToString();
if (timer <= 0)
{
EndGame(false); // Time up
}
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (!Physics.Raycast(ray, out RaycastHit hit) || !hit.collider.CompareTag("Food"))
{
float moleZ = ButterflyGameManager.Instance.Mole.transform.position.z;
// Set target point at same Z-depth as the mole
Vector3 clickScreenPoint = new Vector3(
Input.mousePosition.x,
Input.mousePosition.y,
Camera.main.WorldToScreenPoint(ButterflyGameManager.Instance.Mole.transform.position).z
);
Vector3 worldPoint = Camera.main.ScreenToWorldPoint(clickScreenPoint);
Mole.JumpTo(worldPoint, null);
}
}
}
public void RegisterCatch()
{
butterfliesCaught++;
AddScore(10);
if (butterfliesCaught >= butterfliesToSpawn)
{
EndGame(true);
}
}
void AddScore(int amount)
{
score += amount;
scoreText.text = "Score: " + score;
}
public void EndGame(bool win)
{
gameEnded = true;
if (win)
{
if (winPanel != null)
winPanel.SetActive(true);
}
else
{
gameOverPanel?.SetActive(true);
}
Time.timeScale = 0;
}
public bool IsGameActive()
{
return !gameEnded;
}
}