ZoeAppClass/Assets/Scripts/Games/GamesResourcesFinder.cs

266 lines
6.6 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
public class GamesResourcesFinder : MonoBehaviour
{
public static GamesResourcesFinder instance;
public string resourcePath;
public bool gameReady = false;
[System.Serializable]
public class ArchiveDownload
{
public string resource;
public bool isImg;
}
[System.Serializable]
public class ImageArchive
{
public string resource;
public Sprite img;
}
[System.Serializable]
public class VideoArchive
{
public string resource, fullPath;
}
[System.Serializable]
public class SongArchive
{
public string resource;
public AudioClip clip;
}
public List<ArchiveDownload> downloads;
public List<ImageArchive> images;
public List<VideoArchive> videos;
public List<SongArchive> audios;
public bool waitToCallDownload = false;
int extraDownloads = 0;
private void Awake()
{
instance = this;
if (!waitToCallDownload)
{
Debug.Log("Por aqui em cima");
StartCoroutine(DownloadEverything());
}
}
public void AddObjectsOnFirstDownload(List<ArchiveDownload> archives)
{
downloads.AddRange(archives);
}
public void CallFirstDownload()
{
Debug.Log("Por aqui em baixo");
StartCoroutine(DownloadEverything());
}
public Sprite AskForImage(string name)
{
foreach (ImageArchive item in images)
{
if (item.resource == name)
{
return item.img;
}
}
return null;
}
public string AskForVideo(string name)
{
foreach (VideoArchive item in videos)
{
if (item.resource == name)
{
return item.fullPath;
}
}
return "";
}
public AudioClip AskForAudio(string name)
{
Debug.Log("Qntdade de audios = " + audios.Count);
foreach (SongArchive item in audios)
{
Debug.Log(item.resource + ", " + name);
if (item.resource == name)
{
return item.clip;
}
}
return null;
}
public void AddObjNDownload(List<ArchiveDownload> res)
{
downloads.AddRange(res);
StartCoroutine(DownloadEverything(false));
}
IEnumerator DownloadEverything(bool isFirstDownload = true)
{
Debug.Log("Baixar recursos do jogo, isFirstDownload = " + isFirstDownload);
GameObject objLoad = Instantiate(Resources.Load<GameObject>("Load Game"));
if (!isFirstDownload)
{
Destroy(objLoad);
}
foreach (ArchiveDownload item in downloads)
{
if (item.isImg)
{
yield return StartCoroutine(DownloadPhoto(item.resource));
}
else
{
if (item.resource.EndsWith(".wav") || item.resource.EndsWith(".mp3"))
{
yield return StartCoroutine(DownloadAudioFile(item.resource));
}
else
{
yield return StartCoroutine(DownloadVideoFile(item.resource));
}
}
yield return new WaitForEndOfFrame();
}
if (isFirstDownload)
{
Destroy(objLoad);
gameReady = true;
Debug.LogWarning("Downloaded Everything");
}
else
{
extraDownloads++;
}
}
IEnumerator DownloadAudioFile(string fileName)
{
string path = "http://" + ReadingFile.iploc + "/zoeGamesResources/" + fileName;
Debug.Log("Baixando audio de: " + path);
var www = new WWW(path);
Debug.Log("Downloading " + fileName + "!");
yield return www.GetAudioClip();
string pathToArchive = Application.persistentDataPath + "/" + fileName;
SongArchive songArchive = new SongArchive();
songArchive.resource = fileName;
songArchive.clip = www.GetAudioClip();
audios.Add(songArchive);
yield break;
}
IEnumerator DownloadVideoFile(string fileName)
{
string path = "http://" + ReadingFile.iploc + "/zoeGamesResources/" + fileName;
Debug.Log("Baixando video de: " + path);
var www = new WWW(path);
Debug.Log("Downloading " + fileName + "!");
yield return www;
string pathToArchive = Application.persistentDataPath + "/" + fileName;
if (File.Exists(pathToArchive))
{
File.Delete(pathToArchive);
}
File.WriteAllBytes(pathToArchive, www.bytes);
VideoArchive videoArchive = new VideoArchive();
videoArchive.fullPath = pathToArchive;
videoArchive.resource = fileName;
videos.Add(videoArchive);
yield break;
}
IEnumerator DownloadPhoto(string photoPath)
{
string url = "http://" + ReadingFile.iploc + "/zoeGamesResources/" + photoPath;
Debug.Log("Baixando imagem de: " + url);
using (WWW www = new WWW(url))
{
yield return www;
ImageArchive img = new ImageArchive();
img.resource = photoPath;
img.img = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height),
new Vector2(0, 0));
images.Add(img);
}
}
public void CallEndGame()
{
StartCoroutine(EndGame(true));
}
public IEnumerator EndGame(bool won, bool jusClosing = false)
{
yield return new WaitForSecondsRealtime(1);
GameObject obj = Instantiate(Resources.Load<GameObject>("End Game"));
foreach (ImageArchive item in images)
{
item.img = null;
yield return new WaitForEndOfFrame();
}
foreach (VideoArchive item in videos)
{
File.Delete(item.fullPath);
yield return new WaitForEndOfFrame();
}
Resources.UnloadUnusedAssets();
GameControl jooj = GameObject.Find("GameControl").GetComponent<GameControl>();
if (!jusClosing)
{
yield return new WaitForSecondsRealtime(1);
Destroy(obj);
jooj.ended = false;
if (won)
{
jooj.finishedGame = true;
}
else
{
jooj.failedGame = true;
}
}
}
}