ZoeRobotIngles/Assets/Scripts/Customize/CustomizeZoe_Screen.cs

279 lines
8.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CustomizeZoe_Screen : MonoBehaviour
{
[System.Serializable]
public class Resource
{
public string name, resource;
public int valueThis;
}
[System.Serializable]
public class ResourcePack
{
public string prephrase;
public Sprite imgModel;
public string chosen;
public List<Image> imgsToAffect;
public List<Resource> resources;
}
public List<ResourcePack> resources;
public GameObject objModel, objModelOption, objOption, ObjEdit;
public Transform options;
public Sprite sprChosen, sprNotChosen;
List<CustomizeOption> customizeOptions = new List<CustomizeOption>();
GameObject loadObj, blockObj;
Text txtCoins_01, txtCoins_02; public int coins;
GameObject confirmScreen;
public static CustomizeZoe_Screen instance;
private void Awake()
{
instance = this;
}
void Start()
{
confirmScreen = GameObject.Find("ConfirmBuy");
confirmScreen.SetActive(false);
blockObj = GameObject.Find("Block");
blockObj.SetActive(false);
Transform tr = GameObject.Find("Group_EditOptions").transform;
for (int i = 0; i < resources.Count; i++)
{
GameObject objCreate = Instantiate(objModel, tr);
CustoMizeItem custoMizeItem = objCreate.GetComponent<CustoMizeItem>();
custoMizeItem.SetInfo(resources[i].prephrase, i, resources[i].imgModel);
}
StartCoroutine(CheckItensWait());
}
public void SetListOfOptions(APICustomize.GetItensResponse[] itensResponse)
{
foreach (ResourcePack option in resources)
{
option.resources = new List<Resource>();
option.resources.Clear();
}
foreach (APICustomize.GetItensResponse item in itensResponse)
{
for (int i = 0; i < resources.Count; i++)
{
if (resources[i].prephrase == item.group)
{
Resource resource = new Resource();
resource.name = item.appearName;
resource.resource = item.resourceName;
resource.valueThis = item.cost;
resources[i].resources.Add(resource);
i = 999999;
}
}
}
}
IEnumerator CheckItensWait()
{
APICustomize aPICustomize = APICustomize.instance;
while (!aPICustomize.checkdItens)
{
yield return new WaitForSecondsRealtime(.25f);
}
string actualItens = aPICustomize.actualItens;
string[] itensSeparados = actualItens.Split(';');
for (int i = 0; i < resources.Count; i++)
{
string[] resourcesplit = itensSeparados[i].Split('_');
string thisIten = resourcesplit[resourcesplit.Length - 1];
resources[i].chosen = thisIten;
}
yield return StartCoroutine(DownloadChosens());
coins = aPICustomize.coins;
txtCoins_01 = GameObject.Find("txtCoinsBack").GetComponent<Text>();
txtCoins_02 = GameObject.Find("txtCoins").GetComponent<Text>();
UpdateCoins();
loadObj = GameObject.Find("Load");
loadObj.SetActive(false);
}
public void EndEditing()
{
loadObj.SetActive(true);
List<string> itensList = new List<string>();
foreach (ResourcePack item in resources)
{
itensList.Add(item.prephrase + "_" + item.chosen);
}
APICustomize.instance.UpdateActual(itensList);
}
IEnumerator DownloadChosens()
{
for (int i = 0; i < resources.Count; i++)
{
for (int i2 = 0; i2 < resources[i].resources.Count; i2++)
{
Debug.Log(resources[i].resources[i2].resource + ", " + resources[i].chosen);
if (resources[i].resources[i2].resource == resources[i].chosen)
{
yield return StartCoroutine(DownloadPhoto(i, i2));
Debug.Log("Downloading");
i2 = 99999;
}
}
}
yield break;
}
public void UpdateCoins()
{
txtCoins_01.text = coins.ToString();
txtCoins_02.text = coins.ToString();
}
public void ChoseThisOne(int group, int which)
{
foreach (var item in customizeOptions)
{
if (!item.CheckBlockStatus())
{
if (item.indexThis == which)
{
item.SetThisOn();
StartCoroutine(DownloadPhoto(group, which));
}
else
{
item.SetThisOff();
}
}
}
resources[group].chosen = resources[group].resources[which].resource;
}
IEnumerator DownloadPhoto(int group, int which)
{
blockObj.SetActive(true);
if (resources[group].resources[which].resource == "0")
{
foreach (Image image in resources[group].imgsToAffect)
{
image.sprite = null;
image.color = new Color(1, 1, 1, 0);
}
}
else
{
string url = "http://" + ReadingFile.iploc + "/zoeGamesResources/CustomizeZoe/" +
resources[group].prephrase + "_" + resources[group].resources[which].resource + ".png";
using (WWW www = new WWW(url))
{
yield return www;
Sprite spr = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height),
new Vector2(0, 0));
foreach (Image image in resources[group].imgsToAffect)
{
image.sprite = spr;
image.color = new Color(1, 1, 1, 1);
}
}
}
blockObj.SetActive(false);
}
public void OpenCustomization(int index)
{
objOption.SetActive(false);
ObjEdit.SetActive(true);
string boughts = APICustomize.instance.boughtItens;
for (int i = 0; i < resources[index].resources.Count; i++)
{
GameObject objCreate = Instantiate(objModelOption, options);
CustomizeOption custoMizeItem = objCreate.GetComponent<CustomizeOption>();
customizeOptions.Add(custoMizeItem);
custoMizeItem.SetInfo(resources[index].resources[i].name, false, index, i);
string thisName = resources[index].prephrase + "_" + resources[index].resources[i].resource;
if (!boughts.Contains(thisName))
{
custoMizeItem.SetThisBlocked();
}else if (resources[index].chosen == resources[index].resources[i].resource)
{
custoMizeItem.SetThisOn();
}
}
}
public void GoBackToMenu()
{
int sizeChilds = options.childCount;
for (int i = 0; i < sizeChilds; i++)
{
Destroy(options.GetChild(i).gameObject);
}
customizeOptions.Clear();
objOption.SetActive(true);
ObjEdit.SetActive(false);
}
public void LiberarItemComprado(string nameItem)
{
string group = resources[customizeOptions[0].indexgroup].prephrase + "_";
foreach (CustomizeOption item in customizeOptions)
{
string thisName = group + resources[item.indexgroup].resources[item.indexThis].resource;
Debug.Log(thisName + ", " + nameItem);
if (thisName == nameItem)
{
item.Unblock();
item.ChooseThis();
return;
}
}
}
public void ChooseToBuyItem(int group, int which) {
confirmScreen.SetActive(true);
CustomizeBuyScreen customizeBuyScreen = confirmScreen.GetComponent<CustomizeBuyScreen>();
customizeBuyScreen.SetInfo(resources[group].prephrase, resources[group].resources[which].name,
resources[group].resources[which].resource, resources[group].resources[which].valueThis);
}
}