You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
3.2 KiB
C#
133 lines
3.2 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
|
|
public class PeekabooGameManager : MonoBehaviour
|
|
{
|
|
public static PeekabooGameManager Instance;
|
|
|
|
public Transform mole;
|
|
public Transform[] behindPositions; // BehindTree, BehindBench, BehindDustbin
|
|
public Transform[] frontPositions; // FrontOfTree, FrontOfBench, FrontOfDustbin
|
|
|
|
public float showTime = 1.5f;
|
|
public float moveDuration = 0.3f;
|
|
public float interval = 0.5f;
|
|
public float gameDuration = 30f;
|
|
|
|
public TextMeshProUGUI scoreText;
|
|
public TextMeshProUGUI timerText;
|
|
|
|
private int score = 0;
|
|
private float timer;
|
|
private bool gameRunning = false;
|
|
private int currentIndex = -1;
|
|
private Coroutine peekCoroutine;
|
|
|
|
void Awake()
|
|
{
|
|
Instance = this;
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
StartCoroutine(GameLoop());
|
|
}
|
|
|
|
IEnumerator GameLoop()
|
|
{
|
|
timer = gameDuration;
|
|
score = 0;
|
|
UpdateScoreText();
|
|
gameRunning = true;
|
|
|
|
while (timer > 0)
|
|
{
|
|
peekCoroutine = StartCoroutine(PeekRoutine());
|
|
|
|
float roundTime = showTime + interval;
|
|
timer -= roundTime;
|
|
UpdateTimerText();
|
|
|
|
yield return new WaitForSeconds(roundTime);
|
|
}
|
|
|
|
gameRunning = false;
|
|
timerText.text = "Time: 0";
|
|
// Optionally: Trigger game over screen
|
|
}
|
|
|
|
IEnumerator PeekRoutine()
|
|
{
|
|
currentIndex = Random.Range(0, behindPositions.Length);
|
|
|
|
mole.position = behindPositions[currentIndex].position;
|
|
mole.rotation = behindPositions[currentIndex].rotation;
|
|
|
|
yield return MoveTo(mole, frontPositions[currentIndex].position, frontPositions[currentIndex].rotation, moveDuration);
|
|
|
|
float t = 0f;
|
|
while (t < showTime)
|
|
{
|
|
t += Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
|
|
yield return HideMole();
|
|
}
|
|
|
|
public void MoleClicked()
|
|
{
|
|
if (!gameRunning) return;
|
|
|
|
score++;
|
|
UpdateScoreText();
|
|
|
|
if (peekCoroutine != null)
|
|
{
|
|
StopCoroutine(peekCoroutine);
|
|
StartCoroutine(HideMole());
|
|
}
|
|
}
|
|
|
|
IEnumerator HideMole()
|
|
{
|
|
if (currentIndex >= 0)
|
|
{
|
|
yield return MoveTo(mole, behindPositions[currentIndex].position, behindPositions[currentIndex].rotation, moveDuration);
|
|
}
|
|
}
|
|
|
|
IEnumerator MoveTo(Transform obj, Vector3 targetPos, Quaternion targetRot, float duration)
|
|
{
|
|
Vector3 startPos = obj.position;
|
|
Quaternion startRot = obj.rotation;
|
|
float elapsed = 0f;
|
|
|
|
while (elapsed < duration)
|
|
{
|
|
float t = elapsed / duration;
|
|
obj.position = Vector3.Lerp(startPos, targetPos, t);
|
|
obj.rotation = Quaternion.Lerp(startRot, targetRot, t);
|
|
elapsed += Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
|
|
obj.position = targetPos;
|
|
obj.rotation = targetRot;
|
|
}
|
|
|
|
void UpdateScoreText()
|
|
{
|
|
if (scoreText)
|
|
scoreText.text = "Score: " + score;
|
|
}
|
|
|
|
void UpdateTimerText()
|
|
{
|
|
if (timerText)
|
|
timerText.text = "Time: " + Mathf.CeilToInt(timer);
|
|
}
|
|
}
|