135 lines
3.8 KiB
C#
135 lines
3.8 KiB
C#
using TMPro;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class SkinShopManager : MonoBehaviour
|
|
{
|
|
public static SkinShopManager instance;
|
|
public Building skinShopBuilding;
|
|
public SkinsData skinsData;
|
|
public GameObject popup;
|
|
|
|
public GameObject listItemPrefab;
|
|
public Transform listItemsParent;
|
|
public Button btn_Equip;
|
|
public Button btn_Buy;
|
|
public static SkinShopItemData selectedSkin;
|
|
List<SkinShopItem> skinShopItems=new List<SkinShopItem>();
|
|
|
|
void Awake() {
|
|
instance = this;
|
|
}
|
|
void Start()
|
|
{
|
|
Populate();
|
|
|
|
btn_Equip.onClick.AddListener(onEquip);
|
|
btn_Buy.onClick.AddListener(onBuy);
|
|
}
|
|
|
|
public void Show(){
|
|
Populate();
|
|
popup.SetActive(true);
|
|
}
|
|
|
|
public void Populate(){
|
|
//Validate skins list
|
|
if(DBmanager.SkinsPurchased.Count <=0){
|
|
foreach(SkinShopItemData skin in skinsData.skins){
|
|
if(skin.price==0){ //Defaults
|
|
DBmanager.PurchaseSkin(skin);
|
|
if(GetEquipedSkin().Length <=0){
|
|
EquipSkin(skin.name);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//PURGE
|
|
for(int i=0;i < listItemsParent.childCount;i++){
|
|
Destroy(listItemsParent.GetChild(i).gameObject);
|
|
}
|
|
skinShopItems = new List<SkinShopItem>();
|
|
for(int i=0; i<skinsData.skins.Length;i++){
|
|
int rarity = 0;
|
|
if(skinsData.skins[i].skinType == SkinType.Rare){rarity=1;}
|
|
if(skinsData.skins[i].skinType == SkinType.Legendary){rarity=2;}
|
|
|
|
if(skinShopBuilding.curLevel < rarity){
|
|
continue;
|
|
}
|
|
SkinShopItem newItem = Instantiate(listItemPrefab, listItemsParent).GetComponent<SkinShopItem>();
|
|
newItem.Set(skinsData.skins[i]);
|
|
skinShopItems.Add(newItem);
|
|
// newItem.backgroundImg.color = (skinsData.skins[i].skinType==SkinType.Base) ? basicColor : (skinsData.skins[i].skinType==SkinType.Rare ? rareColor : legendaryColor);
|
|
}
|
|
|
|
SelectItem(selectedSkin);
|
|
}
|
|
|
|
public void SelectItem(SkinShopItemData data){
|
|
selectedSkin = data;
|
|
if(data==null){
|
|
btn_Equip.gameObject.SetActive(false); btn_Buy.gameObject.SetActive(false);
|
|
return;
|
|
}
|
|
if(DBmanager.SkinsPurchased.Contains(data.name)){ // <-- purchased
|
|
btn_Equip.gameObject.SetActive(true);
|
|
btn_Buy.gameObject.SetActive(false);
|
|
btn_Equip.interactable = GetEquipedSkin() != data.name; // <-- disable equip button if already equipped
|
|
}else{
|
|
btn_Buy.gameObject.SetActive(true);
|
|
btn_Equip.gameObject.SetActive(false);
|
|
|
|
btn_Buy.interactable = DBmanager.Metal >= data.price;
|
|
btn_Buy.GetComponentInChildren<TMP_Text>().text = data.price.ToString();
|
|
}
|
|
|
|
foreach(SkinShopItem item in skinShopItems){
|
|
item.OnSelectionChanged(data.name);
|
|
}
|
|
|
|
}
|
|
|
|
void onEquip(){
|
|
EquipSkin(selectedSkin.name);
|
|
Populate();
|
|
}
|
|
|
|
void onBuy(){
|
|
DBmanager.PurchaseSkin(selectedSkin);
|
|
|
|
SkinShopManager.instance.Populate();
|
|
}
|
|
|
|
public static void EquipSkin(string skin){
|
|
PlayerPrefs.SetString("shipSkin", skin);
|
|
PlayerPrefs.Save();
|
|
}
|
|
|
|
public static string GetEquipedSkin(){
|
|
if(PlayerPrefs.HasKey("shipSkin")){
|
|
return PlayerPrefs.GetString("shipSkin");
|
|
}
|
|
|
|
return "Default";
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class SkinShopItemData{
|
|
public string name;
|
|
public Sprite image;
|
|
public int price;
|
|
public SkinType skinType;
|
|
}
|
|
|
|
|
|
public enum SkinType{
|
|
Base,
|
|
Rare,
|
|
Legendary
|
|
} |