MiniGames/Assets/Scripts/WhackAMole/WhackAMoleGameManager.cs

47 lines
1005 B
C#
Raw Normal View History

2025-07-11 15:42:48 +05:00
using UnityEngine;
using UnityEngine.UI;
using TMPro;
2025-07-11 18:05:08 +05:00
public class WhackAMoleGameManager : MonoBehaviour
2025-07-11 15:42:48 +05:00
{
public Mole[] moles;
public float spawnInterval = 1.5f;
public float gameDuration = 30f;
public TextMeshProUGUI scoreText;
public TextMeshProUGUI timerText;
private int score = 0;
private float timeLeft;
void Start()
{
timeLeft = gameDuration;
InvokeRepeating(nameof(SpawnMole), 1f, spawnInterval);
}
void Update()
{
timeLeft -= Time.deltaTime;
timerText.text = "Time: " + Mathf.CeilToInt(timeLeft);
if (timeLeft <= 0f)
{
CancelInvoke(nameof(SpawnMole));
timerText.text = "Time: 0";
}
}
void SpawnMole()
{
if (moles.Length == 0) return;
int index = Random.Range(0, moles.Length);
moles[index].Show();
}
public void AddScore(int points)
{
score += points;
scoreText.text = "Score: " + score;
}
}