PlaneRun/Assets/Scripts/shopManager.cs

281 lines
7.0 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System;
using GoogleMobileAds.Sample;
public class shopManager : MonoBehaviour
{
public score scoreScript;
public soundManager soundmanager;
public bool[] owned;
public bool[] show;
public GameObject[] skins;
public int[] cost;
public Color[] buttonTextColor;
public int selectedSkin;
public int equippedSkin;
public TMP_Text buttontext;
public TMP_Text coinsText;
public TMP_Text costText;
public TMP_Text CoinText;
public int coins;
public bool coinsGiven;
public int gamecoins;
public Transform buyButton;
public GameObject noBuyText;
public string dateNow;
public GameObject nextButton, prevButton;
public Transform Canvas;
public GameObject adsLeft;
public GameObject RewardedAdButton;
public int rewardedAdsLeft;
[SerializeField] private RewardedAdController rewardedAdController;
[SerializeField] private PlayGamesManager PGM;
void Start()
{
dateNow = DateTime.Now.ToString("yyyyMMdd");
//PlayerPrefs.DeleteAll();
if (PlayerPrefs.GetString("lastDate") == "" || PlayerPrefs.GetString("lastDate") != DateTime.Now.ToString("yyyyMMdd"))
{
PlayerPrefs.SetInt("rewardedAdsLeft", 3);
PlayerPrefs.SetString("lastDate", DateTime.Now.ToString("yyyyMMdd"));
}
PlayerPrefs.SetInt("skin1owned", 1);
selectedSkin = PlayerPrefs.GetInt("equippedSkin");
EquipSkin(PlayerPrefs.GetInt("equippedSkin"));
ShowSkin(PlayerPrefs.GetInt("equippedSkin"));
coinsText.text = coins.ToString();
rewardedAdsLeft = PlayerPrefs.GetInt("rewardedAdsLeft");
UpdateButton();
}
void Update()
{
if (GameObject.Find("player").GetComponent<PLayerLife>().life == false)
{
gamecoins = (int)scoreScript.scoreAmount;
if (!coinsGiven)
{
coins += gamecoins;
// Save data
PGM.OpenSave(true);
coinsGiven = true;
}
}
CoinText.text = gamecoins.ToString();
}
public void ShowSkin(int index)
{
for (int i = 0; i < skins.Length; i++)
{
if (i == index)
skins[i].SetActive(true);
else
skins[i].SetActive(false);
}
}
public void EquipSkin(int index)
{
if (owned[selectedSkin])
{
equippedSkin = selectedSkin;
PlayerPrefs.SetInt("equippedSkin", equippedSkin);
ShowSkin(selectedSkin);
soundmanager.PlayClick1();
}
}
public void NextSkin()
{
if (selectedSkin < owned.Length - 1)
{
soundmanager.PlayClick1();
selectedSkin++;
}
ShowSkin(selectedSkin);
UpdateButton();
}
public void PrevSkin()
{
if (selectedSkin >0)
{
selectedSkin--;
soundmanager.PlayClick1();
}
ShowSkin(selectedSkin);
UpdateButton();
}
public void UpdateButton()
{
if (equippedSkin == selectedSkin)
{
costText.text = null;
buttontext.color = buttonTextColor[0];
buttontext.text = "equipped";
}
else if (owned[selectedSkin])
{
costText.text = null;
buttontext.color = buttonTextColor[0];
buttontext.text = "equip";
}
else if (!owned[selectedSkin] && coins >= cost[selectedSkin])
{
costText.text = cost[selectedSkin].ToString();
buttontext.color = buttonTextColor[0];
buttontext.text = "buy";
}
else if (!owned[selectedSkin] && coins < cost[selectedSkin])
{
costText.text = cost[selectedSkin].ToString();
buttontext.color = buttonTextColor[1];
buttontext.text = "buy";
}
if (selectedSkin == owned.Length - 1)
nextButton.SetActive(false);
else
nextButton.SetActive(true);
if(selectedSkin == 0)
prevButton.SetActive(false);
else
prevButton.SetActive(true);
}
public void UpdateUI()
{
UpdateButton();
coinsText.text = coins.ToString();
}
public void Buy()
{
if (!owned[selectedSkin] && coins > cost[selectedSkin] )
{
owned[selectedSkin] = true;
coins -= cost[selectedSkin];
coinsText.text = coins.ToString();
owned[selectedSkin+1] = true;
soundmanager.PlayBuy();
// Save data
PGM.OpenSave(true);
}
else if (owned[selectedSkin])
{
equippedSkin = selectedSkin;
EquipSkin( equippedSkin );
}
else if( coins < cost[selectedSkin])
{
Transform noMonneyText = Instantiate(noBuyText, buyButton.position, buyButton.rotation).transform;
noMonneyText.SetParent(buyButton);
noMonneyText.transform.localPosition = Vector3.zero;
soundmanager.PlayError();
}
UpdateUI();
}
public void ShowRewardedAd()
{
if (PlayerPrefs.GetInt("rewardedAdsLeft") > 0)
{
// SHOW REWARDED AD
rewardedAdController.ShowAd();
}
else
{
adsLeftText adsleft = Instantiate(adsLeft, Canvas.position, Canvas.rotation).GetComponent<adsLeftText>();
adsleft.transform.SetParent(Canvas);
soundmanager.PlayError();
adsleft.txt = "COME BACK TOMORROW";
}
}
public void ShowRewardedAdsLeft()
{
PlayerPrefs.SetInt("rewardedAdsLeft", PlayerPrefs.GetInt("rewardedAdsLeft") - 1);
rewardedAdsLeft = PlayerPrefs.GetInt("rewardedAdsLeft");
adsLeftText adsleft = Instantiate(adsLeft, Canvas.position, Canvas.rotation).GetComponent<adsLeftText>();
adsleft.transform.SetParent(Canvas);
adsleft.txt = (rewardedAdsLeft.ToString() + (rewardedAdsLeft == 1 ? " AD " : " ADs ") + "LEFT");
}
public void RewardUser()
{
coins += 100;
coinsText.text = coins.ToString();
ShowRewardedAdsLeft();
// Save data
PGM.OpenSave(true);
}
public void EnableRewardedAdButton(bool enable)
{
RewardedAdButton.SetActive(enable);
}
public String GetOwnedPlanesString()
{
String ownedPlanes = owned[0]?"1":"0";
for (int i = 1; i < owned.Length; i++)
{
ownedPlanes += ";" + (owned[i] ? "1" : "0");
}
return ownedPlanes;
}
public void ConvertOwnedPlanes(String ownedPlanesString)
{
String[] ownedPlanes = ownedPlanesString.Split(';');
for (int i = 0;i < ownedPlanes.Length; i++)
{
int planeOwned = int.Parse(ownedPlanes[i]);
owned[i] = (planeOwned == 1 ? true:false);
}
}
}