369 lines
11 KiB
C#
369 lines
11 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 RocketSkinStats rocketStats;
|
|
public RocketSkinStats rocketPrices;
|
|
public static RocketSkinStats RocketStats;
|
|
// public static bool availableToUpgrade;
|
|
public NewSkinDisplay newSkinDisplay;
|
|
public Building skinShopBuilding;
|
|
public SkinsData skinsData;
|
|
public static SkinsData SkinsData;
|
|
public GameObject popup;
|
|
|
|
public GameObject listItemPrefab;
|
|
public Transform listItemsParent;
|
|
public Button btn_Equip;
|
|
public Button btn_Buy;
|
|
public TMP_Text txt_buy_gold,txt_buy_metal;
|
|
public Button btn_Upgrade;
|
|
public RocketUpgradePanel upgradePanel;
|
|
|
|
public static SkinShopItemData selectedSkin;
|
|
public int[] prices;
|
|
public static int[] Prices => instance.prices;
|
|
List<SkinShopItem> skinShopItems=new List<SkinShopItem>();
|
|
|
|
void Awake() {
|
|
instance = this;
|
|
RocketStats = rocketStats;
|
|
SkinsData = skinsData;
|
|
}
|
|
void Start()
|
|
{
|
|
Populate();
|
|
|
|
btn_Equip.onClick.AddListener(onEquip);
|
|
btn_Buy.onClick.AddListener(onBuy);
|
|
btn_Upgrade.onClick.AddListener(onUpgrade);
|
|
}
|
|
|
|
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_metal==0 && skin.price_gold==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.GetSkinIdByName(skinsData.skins[i].name) >= 0;
|
|
|
|
// if(skinShopBuilding.curLevel == rarity){
|
|
// flagshipsCount++;
|
|
// if(isOwned){
|
|
// purchasedFlagships++;
|
|
// }
|
|
// }
|
|
|
|
if(!isOwned){
|
|
// skinsAvailableToPurchase.Add(skinsData.skins[i]);
|
|
}else{
|
|
if(!DBmanager.GetSkinByName(skinsData.skins[i].name).built){
|
|
continue;
|
|
}
|
|
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=3;}
|
|
if(skin.skinType == SkinType.Epic){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.GetSkinIdByName(data.name)>=0){ // <-- purchased
|
|
btn_Equip.gameObject.SetActive(true);
|
|
btn_Upgrade.gameObject.SetActive(getRarityInt(data) <= skinShopBuilding.curLevel);
|
|
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_Upgrade.gameObject.SetActive(false);
|
|
bool affordable = DBmanager.Metal >= data.price_metal && DBmanager.Coins >= data.price_gold;
|
|
btn_Buy.interactable = affordable;
|
|
txt_buy_gold.text = data.price_gold.ToString();
|
|
txt_buy_metal.text = data.price_metal.ToString();
|
|
txt_buy_gold.gameObject.SetActive(data.price_gold > 0);
|
|
txt_buy_metal.gameObject.SetActive(data.price_metal > 0);
|
|
}
|
|
|
|
foreach(SkinShopItem item in skinShopItems){
|
|
item.OnSelectionChanged(data.name);
|
|
}
|
|
|
|
AudioManager.instnace.UIClick();
|
|
}
|
|
|
|
void onEquip(){
|
|
EquipSkin(selectedSkin.name);
|
|
Populate();
|
|
}
|
|
|
|
public void EquipSkin(SkinShopItemData skin){
|
|
EquipSkin(skin.name);
|
|
Populate();
|
|
}
|
|
|
|
void onUpgrade(){
|
|
upgradePanel.Show(selectedSkin.name);
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
|
|
//Helpers
|
|
public static string GetEquipedSkin(){
|
|
if(PlayerPrefs.HasKey("shipSkin")){
|
|
return PlayerPrefs.GetString("shipSkin");
|
|
}
|
|
|
|
return "Default";
|
|
}
|
|
|
|
public static int GetRarityIndexFromName(string name){
|
|
int rarityIndex = 0;
|
|
|
|
foreach(SkinShopItemData item in SkinsData.skins){
|
|
if(item.name == name){
|
|
//this is it
|
|
if(item.price_gold <= 0 && item.price_metal <=0){
|
|
rarityIndex=0;
|
|
}else if(item.skinType == SkinType.Base){
|
|
rarityIndex=1;
|
|
}else if(item.skinType == SkinType.Rare){
|
|
rarityIndex = 2;
|
|
}else if(item.skinType == SkinType.Epic){
|
|
rarityIndex = 3;
|
|
}else{
|
|
rarityIndex=4;
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
return rarityIndex;
|
|
}
|
|
public static SkinShopItemData GetSkinDataByName(string name){
|
|
int rarityIndex = 0;
|
|
|
|
foreach(SkinShopItemData item in SkinsData.skins){
|
|
if(item.name == name){
|
|
//this is it
|
|
return item;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static SkinType GetRarityFromName(string name){
|
|
return GetRarityFromIndex(GetRarityIndexFromName(name));
|
|
}
|
|
|
|
public static SkinType GetRarityFromIndex(int id){
|
|
if(id==0){
|
|
return SkinType.Default;
|
|
}else if(id==1){
|
|
return SkinType.Base;
|
|
}else if(id==2){
|
|
return SkinType.Rare;
|
|
}else if(id ==3){
|
|
return SkinType.Epic;
|
|
}else{
|
|
return SkinType.Legendary;
|
|
}
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
public static RocketLevel[] GetStatsForRarity(SkinShopItemData data){
|
|
if(data.price_gold == 0 && data.price_metal ==0){
|
|
return RocketStats.baseLevels;
|
|
}else if(data.skinType == SkinType.Base){
|
|
return RocketStats.commonLevels;
|
|
}else if(data.skinType == SkinType.Rare){
|
|
return RocketStats.rareLevels;
|
|
}else if(data.skinType == SkinType.Epic){
|
|
return RocketStats.epicLevels;
|
|
}else{
|
|
return RocketStats.legendaryLevels;
|
|
}
|
|
}
|
|
|
|
public static RocketLevel[] GetStatsForRarity(SkinType data){
|
|
if(data == SkinType.Default){
|
|
return RocketStats.baseLevels;
|
|
}else if(data == SkinType.Base){
|
|
return RocketStats.commonLevels;
|
|
}else if(data == SkinType.Rare){
|
|
return RocketStats.rareLevels;
|
|
}else if(data == SkinType.Epic){
|
|
return RocketStats.epicLevels;
|
|
}else{
|
|
return RocketStats.legendaryLevels;
|
|
}
|
|
}
|
|
public static RocketLevel[] GetPriceForRarity(SkinType data){
|
|
if(data == SkinType.Default){
|
|
return instance.rocketPrices.baseLevels;
|
|
}else if(data == SkinType.Base){
|
|
return instance.rocketPrices.commonLevels;
|
|
}else if(data == SkinType.Rare){
|
|
return instance.rocketPrices.rareLevels;
|
|
}else if(data == SkinType.Epic){
|
|
return instance.rocketPrices.epicLevels;
|
|
}else{
|
|
return instance.rocketPrices.legendaryLevels;
|
|
}
|
|
}
|
|
|
|
public static string GetRarityName(SkinType data){
|
|
if(data == SkinType.Default){
|
|
return "Basic";
|
|
}else if(data == SkinType.Base){
|
|
return "Common";
|
|
}else if(data == SkinType.Rare){
|
|
return "Rare";
|
|
}else if(data == SkinType.Epic){
|
|
return "Epic";
|
|
}else{
|
|
return "Legendary";
|
|
}
|
|
}
|
|
|
|
public static Color GetRarityColor(SkinType data){
|
|
if(data == SkinType.Default){
|
|
return Color.grey;
|
|
}else if(data == SkinType.Base){
|
|
return Color.green;
|
|
|
|
}else if(data == SkinType.Rare){
|
|
return Color.blue;
|
|
|
|
}else if(data == SkinType.Epic){
|
|
return Color.magenta;
|
|
|
|
}else{
|
|
return Color.red;
|
|
}
|
|
}
|
|
}
|
|
|
|
[Serializable]public class SkinShopItemData{
|
|
public string name;
|
|
public Sprite image;
|
|
public int price_gold,price_metal;
|
|
public SkinType skinType;
|
|
}
|
|
|
|
|
|
public enum SkinType{
|
|
Default,
|
|
Base,
|
|
Rare,
|
|
Legendary,
|
|
Epic
|
|
}
|
|
|
|
[Serializable]
|
|
public class RocketLevel{
|
|
public int Level;
|
|
public int goldCost;
|
|
public int metalCost;
|
|
public float moonMultiplier;
|
|
public float speedMultiplier;
|
|
public float boostConsumption;
|
|
} |