203 lines
6.1 KiB
C#
203 lines
6.1 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 static bool availableToUpgrade;
|
|
public NewSkinDisplay newSkinDisplay;
|
|
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;
|
|
public int[] prices;
|
|
public static int[] Prices => instance.prices;
|
|
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);
|
|
}
|
|
List<SkinShopItemData> skinsAvailableToPurchase= new List<SkinShopItemData>();
|
|
public void Populate(){
|
|
int purchasedFlagships=0;
|
|
int flagshipsCount=0;
|
|
|
|
//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);
|
|
}
|
|
skinsAvailableToPurchase = new List<SkinShopItemData>();
|
|
skinShopItems = new List<SkinShopItem>();
|
|
for(int i=0; i<skinsData.skins.Length;i++){
|
|
int rarity = getRarityInt(skinsData.skins[i]);
|
|
bool isAvailable = skinShopBuilding.curLevel >= rarity;
|
|
if(!isAvailable){continue;}
|
|
|
|
bool isOwned = DBmanager.SkinsPurchased.Contains(skinsData.skins[i].name);
|
|
|
|
|
|
if(skinShopBuilding.curLevel == rarity){
|
|
flagshipsCount++;
|
|
if(isOwned){
|
|
purchasedFlagships++;
|
|
}
|
|
}
|
|
|
|
if(!isOwned){
|
|
skinsAvailableToPurchase.Add(skinsData.skins[i]);
|
|
}else{
|
|
SkinShopItem newItem = Instantiate(listItemPrefab, listItemsParent).GetComponent<SkinShopItem>();
|
|
|
|
newItem.Set(skinsData.skins[i],isOwned);
|
|
skinShopItems.Add(newItem);
|
|
}
|
|
|
|
|
|
// newItem.backgroundImg.color = (skinsData.skins[i].skinType==SkinType.Base) ? basicColor : (skinsData.skins[i].skinType==SkinType.Rare ? rareColor : legendaryColor);
|
|
}
|
|
Debug.Log("Question ships: " +skinsAvailableToPurchase.Count);
|
|
foreach(SkinShopItemData skin in skinsAvailableToPurchase){
|
|
int rarity = getRarityInt(skin);
|
|
SkinShopItem newItem = Instantiate(listItemPrefab, listItemsParent).GetComponent<SkinShopItem>();
|
|
newItem.Set(skin,false);
|
|
skinShopItems.Add(newItem);
|
|
}
|
|
|
|
availableToUpgrade = purchasedFlagships >= ((float)flagshipsCount/2f);
|
|
|
|
SelectItem(selectedSkin,true);
|
|
}
|
|
|
|
int getRarityInt(SkinShopItemData skin){
|
|
int rarity = 0;
|
|
if(skin.skinType == SkinType.Rare){rarity=1;}
|
|
if(skin.skinType == SkinType.Legendary){rarity=2;}
|
|
|
|
return rarity;
|
|
}
|
|
|
|
public void SelectItem(int index){
|
|
skinShopItems[index].OnClick();
|
|
}
|
|
|
|
public void SelectItem(SkinShopItemData data, bool Available){
|
|
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();
|
|
}
|
|
|
|
public void EquipSkin(SkinShopItemData skin){
|
|
EquipSkin(skin.name);
|
|
Populate();
|
|
}
|
|
|
|
public void onBuy(){
|
|
List<SkinShopItemData> skinsInSameRarity = new List<SkinShopItemData>();
|
|
foreach(SkinShopItemData skin in skinsAvailableToPurchase){
|
|
if(skin.skinType == selectedSkin.skinType){
|
|
skinsInSameRarity.Add(skin);
|
|
}
|
|
}
|
|
|
|
SkinShopItemData randomSkin = skinsInSameRarity[UnityEngine.Random.Range(0,skinsInSameRarity.Count)];
|
|
|
|
DBmanager.PurchaseSkin(randomSkin);
|
|
SkinShopManager.instance.Populate();
|
|
newSkinDisplay.Show(randomSkin);
|
|
}
|
|
|
|
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";
|
|
}
|
|
|
|
public static Color getRarityColor(SkinShopItemData skin){
|
|
Color color = new Color(0,1,0);
|
|
if(skin.skinType == SkinType.Rare){
|
|
color = new Color(0,1,1);
|
|
}else if(skin.skinType == SkinType.Legendary){
|
|
color = new Color(1,0,0);
|
|
}
|
|
|
|
return color;
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class SkinShopItemData{
|
|
public string name;
|
|
public Sprite image;
|
|
public int price;
|
|
public SkinType skinType;
|
|
}
|
|
|
|
|
|
public enum SkinType{
|
|
Base,
|
|
Rare,
|
|
Legendary
|
|
} |