PJ/Assets/scripts/ArmorShop.cs

280 lines
8.4 KiB
C#
Executable file
Raw Blame History

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class ArmorShop : MonoBehaviour
{
public GameObject PlayerOnS;
public Sprite[] BodyIcon;
public Sprite[] HeadIcon;
public Sprite[] SwordIcon;
public List<TMP_Text> TxtS;
public List<TMP_Text> TxtH;
public GameObject ArmorData;
public GameObject Profile;
public List<GameObject> Profiles;
public GameObject ProfileH;
public List<GameObject> ProfilesH;
public TMP_Text MettalCount;
int Lan;
private List<int> HaveHead;
private List<int> HaveSword;
public TMP_Text MC;
public int HeadID;
public int SwordID;
bool CanBuy;
void SaveS()
{
string json = JsonUtility.ToJson(new Serialization<int>(HaveSword));
PlayerPrefs.SetString("HaveSw", json);
PlayerPrefs.Save();
Debug.Log(HaveSword.Count);
}
void LoadS()
{
if (PlayerPrefs.HasKey("HaveSw"))
{
string json = PlayerPrefs.GetString("HaveSw");
HaveSword = JsonUtility.FromJson<Serialization<int>>(json).List;
}
else
{
PlayerPrefs.SetString("HaveSW", "");
Debug.Log(HaveSword);
}
}
void SaveH()
{
string json = JsonUtility.ToJson(new Serialization<int>(HaveHead));
PlayerPrefs.SetString("HaveHe", json);
PlayerPrefs.Save();
Debug.Log(HaveHead.Count);
}
void LoadH()
{
if (PlayerPrefs.HasKey("HaveHe"))
{
string json = PlayerPrefs.GetString("HaveHe");
HaveHead = JsonUtility.FromJson<Serialization<int>>(json).List;
}
else
{
Debug.Log(HaveHead);
}
}
[System.Serializable]
private class Serialization<T>
{
public List<T> List;
public Serialization(List<T> list)
{
this.List = list;
}
public List<T> ToList()
{
return List;
}
}
// Start is called before the first frame update
void Start()
{
LoadH();
LoadS();
RebornDataScript DataF = ArmorData.GetComponent<RebornDataScript>();
Lan = PlayerPrefs.GetInt("LanguageID");
if (HaveSword == null)
{
Debug.Log("AdS");
HaveSword = new List<int>(0);
SaveS();
}
if (HaveHead == null)
{
HaveHead = new List<int>(0);
SaveH();
Debug.Log("AdH");
}
for (int i = 0; i < 7; i++)
{
int num = i;
Profiles.Add(Instantiate(Profile, Profile.transform.parent.parent));
Profiles[i].GetComponent<RectTransform>().position -= new Vector3(-(i)%2*6, i * 1.5f +((i+1)%2*1.5f), 0);
Profiles[i].transform.GetComponentInChildren<Image>().gameObject.transform.GetChild(0).GetComponentInChildren<Image>().sprite = DataF.Sword[i];
TxtS.Add(Profiles[i].transform.GetChild(0).transform.GetChild(1).transform.GetComponentInChildren<TMP_Text>());
switch(Lan)
{
case 0:
Profiles[i].transform.GetChild(1).GetComponent<TMP_Text>().text = DataF.SwordLan0[i];
break;
case 1:
Profiles[i].transform.GetChild(1).GetComponent<TMP_Text>().text = DataF.SwordLan1[i];
break;
}
Profiles[i].transform.GetChild(2).GetComponent<TMP_Text>().text = DataF.SwordAtack[i].ToString();
//print(starts.IndexOf(starts[i]));
//Open.Add(new Button.ButtonClickedEvent { });
//Open[i].AddListener(() => PlayOpen(num));
//print(i);
Profiles[i].transform.GetChild(0).GetComponentInChildren<Button>().onClick.AddListener(() => BuySword(num));
}
for (int i = 0; i < 7 ; i++)
{
int num = i;
ProfilesH.Add(Instantiate(ProfileH, ProfileH.transform.parent.parent));
ProfilesH[i].GetComponent<RectTransform>().position -= new Vector3(-(i)%2*6, i * 1.5f +((i+1)%2*1.5f), 0);
ProfilesH[i].transform.GetComponentInChildren<Image>().gameObject.transform.GetChild(0).GetComponentInChildren<Image>().sprite = DataF.Head[i];
TxtH.Add(ProfilesH[i].transform.GetChild(0).transform.GetChild(1).transform.GetComponentInChildren<TMP_Text>());
switch(Lan)
{
case 0:
ProfilesH[i].transform.GetChild(1).GetComponent<TMP_Text>().text = DataF.HeadLan0[i];
break;
case 1:
ProfilesH[i].transform.GetChild(1).GetComponent<TMP_Text>().text = DataF.HeadLan1[i];
break;
}
ProfilesH[i].transform.GetChild(2).GetComponent<TMP_Text>().text = DataF.HeadSafe[i].ToString();
//print(starts.IndexOf(starts[i]));
//Open.Add(new Button.ButtonClickedEvent { });
//Open[i].AddListener(() => PlayOpen(num));
//print(i);
ProfilesH[i].transform.GetChild(0).GetComponentInChildren<Button>().onClick.AddListener(() => BuyHead(num));
}
}
// Update is called once per frame
private void FixedUpdate()
{
MettalCount.text = PlayerPrefs.GetInt("Metall").ToString();
LoadS();
for (int i = 0; i < TxtS.Count; i++)
{
if (TxtS[i] != null)
{
TMP_Text Have = TxtS[i].GetComponent<TMP_Text>();
if (HaveSword.Contains(i))
{
switch (Lan)
{
case 0:
Have.text = "Use";
break;
case 1:
Have.text = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
break;
}
}
else{try{Have.text = ArmorData.GetComponent<RebornDataScript>().SwordCost[i].ToString();} catch{print(i);}}
}
}
int M = PlayerPrefs.GetInt("Metall");
//MC.text = M.ToString();
LoadH();
for (int i = 0; i < TxtH.Count; i++)
{
if (TxtH[i] != null)
{
TMP_Text Have = TxtH[i].GetComponent<TMP_Text>();
if (HaveHead.Contains(i))
{
switch (Lan)
{
case 0:
Have.text = "Use";
break;
case 1:
Have.text = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
break;
}
}
else{Have.text = ArmorData.GetComponent<RebornDataScript>().HeadCost[i].ToString();}
}
}
}
public void BuyHead(int ID)
{
print($"Try{ID}");
RebornDataScript DataF = ArmorData.GetComponent<RebornDataScript>();
LoadH();
if (HaveHead.Contains(ID))
{
PlayerPrefs.SetInt("HeadID", ID);
}
else
{
BuyH(DataF.HeadCost[ID], ID);
}
}
public void BuySword(int ID)
{
print($"Try{ID}");
RebornDataScript DataF = ArmorData.GetComponent<RebornDataScript>();
LoadS();
print(HaveSword);
if (HaveSword.Contains(ID))
{
PlayerPrefs.SetInt("SwordID", ID);
}
else
{
BuyS(DataF.SwordCost[ID], ID);
}
}
private void BuyS(int cost, int ID)
{
int Money = PlayerPrefs.GetInt("Metall");
if (Money > cost)
{
PlayerPrefs.SetInt("Metall", Money - cost);
HaveSword.Add(ID);
SaveS();
}
else
{
CanBuy = false;
}
}
private void BuyH(int cost, int ID)
{
int Money = PlayerPrefs.GetInt("Metall");
if (Money > cost)
{
PlayerPrefs.SetInt("Metall", Money - cost);
HaveHead.Add(ID);
SaveH();
}
else
{
CanBuy = false;
}
}
}