ZoeAppClass/Assets/Jogos/Modulo 02/Aula 4/Game 3/Scripts/UntangleDrop.cs

118 lines
2.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine;
using UnityEngine.UI;
public class UntangleDrop : MonoBehaviour, IDropHandler
{
public int currentIndex;
private Text thisTxt;
private string toAdd, current;
private UntangleQuestion thisQ;
private Coroutine running = null;
public static UntangleDrop instance;
private void Awake()
{
instance = this;
}
public void ReceiveQuestion(UntangleQuestion q)
{
if (!thisTxt)
{
thisTxt = GetComponent<Text>();
}
thisQ = q;
currentIndex = 0;
thisTxt.text = "";
}
public void SlowAddText()
{
if (running == null)
{
running = StartCoroutine(AddToText());
}
else
{
StopCoroutine(running);
thisTxt.text += current;
running = StartCoroutine(AddToText());
}
}
IEnumerator AddToText()
{
yield return new WaitForEndOfFrame();
current = toAdd;
int startN = current.Length;
thisTxt.text += " ";
for (int i = 0; i < startN; i++)
{
thisTxt.text += current[0];
current = current.Remove(0,1);
yield return new WaitForSeconds(.1f);
}
A10D1AudioSystem.instance.Tomare();
running = null;
yield return null;
}
public bool TestAnswer(UntangleDrag answer)
{
if (answer.thisAnswer.order == currentIndex)
{
A10D1AudioSystem.instance.Custom(true);
answer.isAnswered = true;
toAdd = answer.thisAnswer.answer;
CorrectAnswer();
return true;
}
else
{
A10D1AudioSystem.instance.Wrong();
return false;
}
}
public void OnDrop(PointerEventData data)
{
if (data.pointerDrag.GetComponent<UntangleDrag>())
{
UntangleDrag received = data.pointerDrag.GetComponent<UntangleDrag>();
if (received.thisAnswer.order == currentIndex)
{
A10D1AudioSystem.instance.Custom(true);
received.isAnswered = true;
toAdd = received.thisAnswer.answer;
CorrectAnswer();
}
else
{
A10D1AudioSystem.instance.Wrong();
}
}
}
public void CorrectAnswer()
{
currentIndex++;
SlowAddText();
if(currentIndex == thisQ.answers.Count)
{
StartCoroutine(WaitToRelayCA());
}
}
IEnumerator WaitToRelayCA()
{
yield return new WaitForSeconds(2);
UntangleManager.instance.NextQuestion();
}
}