58 lines
1.5 KiB
C#
58 lines
1.5 KiB
C#
|
|
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class ZibuSlotManager : MonoBehaviour {
|
|
public ScrollRect scrollTop;
|
|
public ScrollRect scrollMid;
|
|
public ScrollRect scrollBot;
|
|
|
|
public float matchDelay = 1f;
|
|
|
|
private Coroutine matchRoutine;
|
|
|
|
void Update() {
|
|
CheckMatch();
|
|
}
|
|
|
|
void CheckMatch() {
|
|
string top = GetCenteredZibuId(scrollTop);
|
|
string mid = GetCenteredZibuId(scrollMid);
|
|
string bot = GetCenteredZibuId(scrollBot);
|
|
|
|
if (top == mid && mid == bot && !string.IsNullOrEmpty(top)) {
|
|
if (matchRoutine == null) {
|
|
matchRoutine = StartCoroutine(WinAfterDelay(top));
|
|
}
|
|
} else {
|
|
if (matchRoutine != null) {
|
|
StopCoroutine(matchRoutine);
|
|
matchRoutine = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
string GetCenteredZibuId(ScrollRect scroll) {
|
|
Vector3 center = scroll.viewport.position;
|
|
float closestDist = float.MaxValue;
|
|
ZibuPiece closest = null;
|
|
|
|
foreach (ZibuPiece piece in scroll.content.GetComponentsInChildren<ZibuPiece>()) {
|
|
float dist = Vector3.Distance(piece.transform.position, center);
|
|
if (dist < closestDist) {
|
|
closestDist = dist;
|
|
closest = piece;
|
|
}
|
|
}
|
|
|
|
return closest?.zibuId;
|
|
}
|
|
|
|
IEnumerator WinAfterDelay(string id) {
|
|
yield return new WaitForSeconds(matchDelay);
|
|
Debug.Log("🎉 WIN! You matched: " + id);
|
|
// Add reward logic here
|
|
}
|
|
}
|