ZoeAppClass/Assets/Scripts/Class/Animation/AnimeControl.cs

372 lines
9.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;
using System;
using System.IO;
using System.Linq;
using static Conversation;
public class AnimeControl : MonoBehaviour
{
VideoPlayer video;
public Slider videoTime;
public Sprite pause, unpause;
Image buttonPause;
public RectTransform transformBase;
Animator thisAnime;
ApiClass.ResponseExerciseAnime[] exercises = null;
bool doneQuestion = false;
public List<TypesClass.Questions> questions = new List<TypesClass.Questions>(); public int actualType;
int actualExercise = 0;
public Transform effectTrans;
public void destroyEffect()
{
Destroy(effectTrans.gameObject);
effectTrans = null;
}
public IEnumerator secondStart(int idanimations, ApiClass.ResponseExerciseAnime[] getexercises)
{
exercises = getexercises;
getexercises = null;
thisAnime = GetComponent<Animator>();
//thisAnime.enabled = false;
video = GetComponent<VideoPlayer>();
video.targetCamera = Camera.main;
/*video.url = "http://18.228.91.135:3337/api/videos/backVideo/" + publishId;*/
bool foundVideo = false;
string path = Application.persistentDataPath + "/anime_" + idanimations + ".mp4";
#region teste
/*Text url = GameObject.Find("url").GetComponent<Text>();
url.text = path;*/
#endregion
Debug.Log(path);
while (!foundVideo)
{
if (File.Exists(path))
{
video.url = "file://" + path;
foundVideo = true;
}
else
{
yield return new WaitForSecondsRealtime(.5f);
}
}
video.Prepare();
StartCoroutine(setvideoTime());
buttonPause = GetComponentsInChildren<Image>()[1];
effectTrans = transform.GetChild(0).GetChild(2);
//txtcache = GetComponentInChildren<Text>();
yield break;
}
IEnumerator checkPause()
{
while (true)
{
if (video.isPlaying)
{
buttonPause.sprite = pause;
}
else
{
buttonPause.sprite = unpause;
}
yield return new WaitForSecondsRealtime(.25f);
}
}
IEnumerator setvideoTime()
{
while (!video.isPrepared)
{
yield return new WaitForFixedUpdate();
}
StartCoroutine(checkExercises());
int nbrChilds = transform.GetChild(0).childCount;
GameObject imgLoad = transform.GetChild(0).GetChild(nbrChilds - 1).gameObject;
Destroy(imgLoad);
video.Play();
StartCoroutine(checkEnd());
yield break;
/*StartCoroutine(checkPause());
Debug.Log("Prepared");
videoTime = GetComponentInChildren<Slider>();
videoTime.maxValue = Convert.ToSingle(video.length);
while (true)
{
videoTime.value = Convert.ToSingle(video.time);
yield return new WaitForSecondsRealtime(1);
}*/
}
IEnumerator checkExercises() {
List<ApiClass.ResponseExerciseAnime> ordered = new List<ApiClass.ResponseExerciseAnime>();
for (int i = 0; i < exercises.Length; i++)
{
ordered.Add(exercises[i]);
}
int nbrRepeat = ordered.Count();
for (int i = 0; i < nbrRepeat; i++)
{
while (video.time < ordered[i].seconds)
{
yield return new WaitForFixedUpdate();
}
instantiateexercise(ordered[i]);
video.Pause();
while (!doneQuestion)
{
yield return new WaitForEndOfFrame();
}
doneQuestion = false;
video.Play();
}
yield break;
}
void instantiateexercise(ApiClass.ResponseExerciseAnime exerciseAnime)
{
switch (exerciseAnime.type)
{
case "Conversação":
StartCoroutine(SpawnConversationFace(exerciseAnime.idconversationtree));
break;
case "Exercício":
GeneralScript general = GeneralScript.instance;
ApiClass api = general.api;
StartCoroutine(api.getExercise(exerciseAnime.idexercises, true, this));
break;
case "ChatTime":
StartCoroutine(SpawnConversationFace(exerciseAnime.idzoechattime));
break;
}
}
public void SpawnExercise() {
switch (actualType)
{
case 1:
GameObject spawnObj = Instantiate(Resources.Load<GameObject>("Class/Anime/QuestionAnime"), effectTrans);
QuestionsController thisQuest = spawnObj.GetComponent<QuestionsController>();
thisQuest.inAnime = true;
GeneralScript general = GeneralScript.instance;
int rand = UnityEngine.Random.Range(0, general.colorsGroups.Count);
thisQuest.create(general.colorsGroups[rand], questions[actualExercise], actualExercise, true);
AnimeObject(spawnObj);
break;
case 5:
GameObject spawnObjj = Instantiate(Resources.Load<GameObject>("Class/Anime/QuestionAnime"), effectTrans);
QuestionsController thisQuest2 = spawnObjj.GetComponent<QuestionsController>();
thisQuest2.inAnime = true;
GeneralScript general2 = GeneralScript.instance;
int rand2 = UnityEngine.Random.Range(0, general2.colorsGroups.Count);
thisQuest2.create(general2.colorsGroups[rand2], questions[actualExercise], actualExercise, true);
AnimeObject(spawnObjj);
break;
case 4:
GeneralScript general3 = GeneralScript.instance;
GameObject Face = Instantiate(Resources.Load<GameObject>("Class/FaceMitsuku"), transform);
Conversation cv = Face.GetComponent<Conversation>();
cv.CorpoRequisicao.ChatTimeID = 0;
cv.CorpoRequisicao.IDAula = general3.etapa[general3.minExercises].idclasses;
cv.CorpoRequisicao.IDExercicio = exercises[actualExercise].idexercises;
cv.exercisePhrase.Clear();
for (int i = 0; i < questions.Count; i++)
{
cv.exercisePhrase.Add(questions[i].enunciado);
}
cv.inAnime = true;
cv.fakeStart(true, true);
AnimeObject(Face, true);
actualExercise++;
break;
}
}
IEnumerator SpawnConversationFace(int conversationtree)
{
GeneralScript general = GeneralScript.instance;
GameObject Face = Instantiate(Resources.Load<GameObject>("Class/FaceMitsuku"), transform);
Conversation cv = Face.GetComponent<Conversation>();
cv.conversationtree = conversationtree;
cv.CorpoRequisicao.ChatTimeID = conversationtree;
cv.CorpoRequisicao.IDAula = general.etapa[general.minExercises].idclasses;
//cv.CorpoRequisicao.IDExercicio = general.etapa[general.minExercises].idexercises;
//cv.CorpoRequisicao.IDExercicio = exercises[actualExercise].idexercises;
//cv.fakeStart();
cv.inAnime = true;
cv.fakeStart(true);
AnimeObject(Face, true);
yield break;
}
void AnimeObject(GameObject obj, bool face = false)
{
obj.name = "actualQuestion";
thisAnime.Rebind();
thisAnime.SetBool("Face", face);
thisAnime.SetInteger("Question", 1);
}
public void endQuestion(int answerid)
{
GeneralScript general = GeneralScript.instance;
general.addanswerdb(answerid.ToString());
thisAnime.SetInteger("Question", 2);
}
public void endConversation()
{
thisAnime.SetInteger("Question", 2);
}
public void DestroyQuestion()
{
GameObject obj = GameObject.Find("actualQuestion");
Destroy(obj);
thisAnime.SetInteger("Question", 0);
doneQuestion = true;
}
IEnumerator SpawnConversation() {
yield break;
}
public bool endVideo = false;
IEnumerator checkEnd()
{
while (!endVideo)
{
video.loopPointReached += EndReached;
yield return new WaitForFixedUpdate();
if ((video.length - 1) == video.time)
{
endVideo = true;
}
yield return new WaitForFixedUpdate();
}
thisAnime.enabled = true;
Debug.Log("End");
thisAnime.SetTrigger("end");
GeneralScript general = GeneralScript.instance;
general.LiberarAvanco(true);
yield return new WaitForSecondsRealtime(1);
RectTransform rectVideo = thisAnime.gameObject.GetComponent<RectTransform>();
Vector2 rect = rectVideo.anchoredPosition;
thisAnime.enabled = false;
yield return new WaitForEndOfFrame();
rectVideo.anchoredPosition = rect;
yield return new WaitForEndOfFrame();
video.Stop();
yield break;
}
void EndReached(UnityEngine.Video.VideoPlayer vp)
{
endVideo = true;
}
public void pauseVideo()
{
if (video.isPlaying)
{
video.Pause();
}
else
{
video.Play();
}
}
public void changeValue()
{
if (Input.GetMouseButtonDown(0))
{
if (video.canSetTime)
{
video.time = videoTime.value;
}
}
}
public void ShowHide()
{
bool actualstate = thisAnime.GetBool("show");
thisAnime.SetBool("show", !actualstate);
}
}