591 lines
18 KiB
C#
591 lines
18 KiB
C#
using CodeJay.Classes;
|
|
using CodeJay.Defines;
|
|
using System;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class ProductSlot : CardBase
|
|
{
|
|
[SerializeField] private GameObject[] gos_glow; // 0 파랑, 1 빨강
|
|
[SerializeField] private Image icon;
|
|
[SerializeField] private TMPro.TextMeshProUGUI NameTMP;
|
|
[SerializeField] private TMPro.TextMeshProUGUI ProductTMP;
|
|
[SerializeField] private TMPro.TextMeshProUGUI PriceTMP;
|
|
|
|
[Space(20)]
|
|
[SerializeField] private GameObject AdButton;
|
|
[SerializeField] private TMPro.TextMeshProUGUI AdNumberTMP;
|
|
|
|
[Space(20)]
|
|
[SerializeField] private GameObject PurchaseButton;
|
|
|
|
[Space(20)]
|
|
[SerializeField] private GameObject SaleGO;
|
|
[SerializeField] private TMPro.TextMeshProUGUI SaleTMP;
|
|
|
|
[Space(20)]
|
|
[SerializeField] private GameObject BuyWithCashBtn;
|
|
[SerializeField] private TMPro.TextMeshProUGUI t_buycash;
|
|
|
|
private ProductData _data;
|
|
|
|
private StringBuilder sb = new StringBuilder();
|
|
|
|
#if UNITY_EDITOR
|
|
private void OnApplicationFocus(bool focusStatus)
|
|
{
|
|
if (focusStatus == true)
|
|
{
|
|
DateTime loadTime = GameManager.Timer.LoadTime();
|
|
if (loadTime.Day != DateTime.Now.Day)
|
|
{
|
|
GameManager.DB.ResetADSRewardActionCount();
|
|
GameManager.DB.ResetFreeHeartCount();
|
|
|
|
GameManager.Timer.DeactivateTimer(name);
|
|
//buyButton.onClick.AddListener(OnBuyButtonClicked);
|
|
}
|
|
}
|
|
}
|
|
|
|
#else
|
|
private void OnApplicationPause(bool pauseStatus)
|
|
{
|
|
if(pauseStatus == false)
|
|
{
|
|
DateTime loadTime = GameManager.Timer.LoadTime();
|
|
if (loadTime.Day != DateTime.Now.Day)
|
|
{
|
|
GameManager.DB.ResetADSRewardActionCount();
|
|
GameManager.DB.ResetFreeHeartCount();
|
|
|
|
GameManager.Timer.DeactivateTimer(name);
|
|
//buyButton.onClick.AddListener(OnBuyButtonClicked);
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (GameManager.Instance != null && GameManager.ADS != null)
|
|
{
|
|
GameManager.ADS.OnCompletedRewardedAd -= OnCompletedRewardedAd;
|
|
}
|
|
|
|
if (GameManager.Instance != null && GameManager.DB != null)
|
|
{
|
|
GameManager.DB.OnResetFreeHeartCountEvent -= OnResetFreeHeartCountEvent;
|
|
GameManager.DB.OnResetADSRewardActionCountEvent -= OnResetADSRewardActionCountEvent;
|
|
}
|
|
}
|
|
|
|
public override void Set<T>(T _base, int iLoop = -1, int idata = -1)
|
|
{
|
|
base.Set(_base, iLoop, idata);
|
|
SetData(_base as ProductData);
|
|
}
|
|
|
|
public void SetData(ProductData data)
|
|
{
|
|
_data = data;
|
|
icon.sprite = _data.sprite_item;
|
|
NameTMP.text = _data.productName;
|
|
ProductTMP.text = _data.productDescription;
|
|
PriceTMP.text = _data.price.ToString("c", new System.Globalization.CultureInfo("ko-KR", false));
|
|
|
|
BuyWithCashBtn.SetActive(false);
|
|
DSUtil.InActivateGameObjects(gos_glow, data.rewardType == CodeJay.Enum.EProductReward.Key ? 0 : 1);
|
|
|
|
if (data.productID == "heart_free")
|
|
{
|
|
GameManager.DB.OnResetFreeHeartCountEvent += OnResetFreeHeartCountEvent;
|
|
|
|
SaleGO.SetActive(false);
|
|
AdButton.SetActive(false);
|
|
PriceTMP.transform.parent.gameObject.SetActive(true);
|
|
|
|
PriceTMP.text = "무료";
|
|
|
|
if (GameManager.DB.FreeHeartCount <= 0)
|
|
{
|
|
if (PurchaseButton != null)
|
|
{
|
|
Color c = PurchaseButton.GetComponent<Image>().color;
|
|
c.a = 0.5f;
|
|
PurchaseButton.GetComponent<Image>().color = c;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (PurchaseButton != null)
|
|
{
|
|
Color c = PurchaseButton.GetComponent<Image>().color;
|
|
c.a = 1.0f;
|
|
PurchaseButton.GetComponent<Image>().color = c;
|
|
}
|
|
}
|
|
}
|
|
else if (data.productID == "heart_ads")
|
|
{
|
|
GameManager.DB.OnResetADSRewardActionCountEvent += OnResetADSRewardActionCountEvent;
|
|
|
|
|
|
SaleGO.SetActive(false);
|
|
AdButton.SetActive(true);
|
|
PriceTMP.transform.parent.gameObject.SetActive(false);
|
|
|
|
AdNumberTMP.text = $"{GameManager.DB.ProductAdWatchedNumber} / 3";
|
|
|
|
SetADSButton();
|
|
|
|
if (GameManager.DB.ProductAdWatchedNumber <= 0)
|
|
{
|
|
if (AdButton != null)
|
|
{
|
|
Color c = AdButton.GetComponent<Image>().color;
|
|
c.a = 0.5f;
|
|
AdButton.GetComponent<Image>().color = c;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (AdButton != null)
|
|
{
|
|
Color c = AdButton.GetComponent<Image>().color;
|
|
c.a = 1.0f;
|
|
AdButton.GetComponent<Image>().color = c;
|
|
}
|
|
}
|
|
}
|
|
else if (data.productID == "com.fgb.adsremove")
|
|
{
|
|
SaleGO.SetActive(false);
|
|
AdButton.SetActive(false);
|
|
PriceTMP.transform.parent.gameObject.SetActive(true);
|
|
|
|
if (GameManager.DB.IsRemoveADS == true)
|
|
{
|
|
Color c = PurchaseButton.GetComponent<Image>().color;
|
|
c.a = 0.5f;
|
|
PurchaseButton.GetComponent<Image>().color = c;
|
|
}
|
|
}
|
|
else if (data.productID == "key_free")
|
|
{
|
|
GameManager.DB.OnResetFreeKeyCountEvent += OnResetFreeHeartCountEvent;
|
|
|
|
SaleGO.SetActive(false);
|
|
AdButton.SetActive(false);
|
|
PriceTMP.transform.parent.gameObject.SetActive(true);
|
|
|
|
PriceTMP.text = "무료";
|
|
|
|
if (GameManager.DB.FreeKeyCount <= 0)
|
|
{
|
|
if (PurchaseButton != null)
|
|
{
|
|
Color c = PurchaseButton.GetComponent<Image>().color;
|
|
c.a = 0.5f;
|
|
PurchaseButton.GetComponent<Image>().color = c;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (PurchaseButton != null)
|
|
{
|
|
Color c = PurchaseButton.GetComponent<Image>().color;
|
|
c.a = 1.0f;
|
|
PurchaseButton.GetComponent<Image>().color = c;
|
|
}
|
|
}
|
|
}
|
|
else if (data.productID == "heart_60")
|
|
{
|
|
SaleGO.SetActive(true);
|
|
AdButton.SetActive(false);
|
|
PriceTMP.transform.parent.gameObject.SetActive(true);
|
|
|
|
SaleTMP.text = "▼8%";
|
|
}
|
|
else if (data.productID == "heart_130")
|
|
{
|
|
SaleGO.SetActive(true);
|
|
AdButton.SetActive(false);
|
|
PriceTMP.transform.parent.gameObject.SetActive(true);
|
|
|
|
SaleTMP.text = "▼15%";
|
|
}
|
|
else if (data.productID == "key_45")
|
|
{
|
|
SaleGO.SetActive(true);
|
|
AdButton.SetActive(false);
|
|
PriceTMP.transform.parent.gameObject.SetActive(true);
|
|
|
|
SaleTMP.text = "▼11%";
|
|
}
|
|
else if (data.productID == "key_75")
|
|
{
|
|
SaleGO.SetActive(true);
|
|
AdButton.SetActive(false);
|
|
PriceTMP.transform.parent.gameObject.SetActive(true);
|
|
|
|
SaleTMP.text = "▼20%";
|
|
}
|
|
else if (data.productID == "heart_cash")
|
|
{
|
|
BuyWithCashBtn.SetActive(true);
|
|
t_buycash.text = _data.price.ToString();
|
|
}
|
|
else
|
|
{
|
|
SaleGO.SetActive(false);
|
|
AdButton.SetActive(false);
|
|
PriceTMP.transform.parent.gameObject.SetActive(true);
|
|
}
|
|
|
|
if (data.type == CodeJay.Enum.EProductType.IAP)
|
|
PriceTMP.text = $"\\{data.price}";
|
|
}
|
|
|
|
private void SetADSButton()
|
|
{
|
|
// 광고 이벤트 연결하기
|
|
GameManager.ADS.OnCompletedRewardedAd += OnCompletedRewardedAd;
|
|
|
|
DateTime loadTime = GameManager.Timer.LoadTime();
|
|
if (loadTime.Day != DateTime.Now.Day)
|
|
{
|
|
//GameManager.DB.ResetADSRewardActionCount();
|
|
//GameManager.DB.ResetFreeHeartCount();
|
|
}
|
|
else
|
|
{
|
|
GameManager.Timer.ActivateLoadSimpleTimer(name, Constants.ADS_TIMER_COUNT, OnStartTimer, OnPauseTimer, OnStopTimer, OnPassedTime);
|
|
}
|
|
}
|
|
|
|
public void ClickButton()
|
|
{
|
|
GameManager.Sound.PlaySFX(ESFXType.Button_Hit);
|
|
|
|
if (CheckConditionForState() == false)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (_data.productID == "heart_free")
|
|
{
|
|
if (GameManager.DB.FreeHeartCount > 0)
|
|
{
|
|
GameManager.DB.AddHeart(1, this.name);
|
|
GameManager.DB.MinFreeHeartCount(1);
|
|
|
|
if (GameManager.DB.FreeHeartCount <= 0 && PurchaseButton != null)
|
|
{
|
|
Color c = PurchaseButton.GetComponent<Image>().color;
|
|
c.a = 0.5f;
|
|
PurchaseButton.GetComponent<Image>().color = c;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// 무료 하트 횟수가 없다는 팝업 or 가만히 두기
|
|
GameManager.UI.ShowNStackPopup(EPopupType.NotVaildFreeHeartPopup);
|
|
}
|
|
}
|
|
else if (_data.productID == "key_free")
|
|
{
|
|
if (GameManager.DB.FreeKeyCount > 0)
|
|
{
|
|
GameManager.DB.AddKey(1, this.name);
|
|
GameManager.DB.MinFreeKeyCount(1);
|
|
|
|
if (GameManager.DB.FreeKeyCount <= 0 && PurchaseButton != null)
|
|
{
|
|
Color c = PurchaseButton.GetComponent<Image>().color;
|
|
c.a = 0.5f;
|
|
PurchaseButton.GetComponent<Image>().color = c;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// 무료 하트 횟수가 없다는 팝업 or 가만히 두기
|
|
//GameManager.UI.ShowNStackPopup(EPopupType.NotVaildFreeHeartPopup);
|
|
}
|
|
}
|
|
else if (_data.productID == "heart_ads")
|
|
{
|
|
// 광고 출력
|
|
// 광고 횟수 -
|
|
|
|
if (GameManager.DB.ProductAdWatchedNumber > 0)
|
|
{
|
|
GameManager.ADS.ShowHeartRewardedAd(this.name);
|
|
}
|
|
else
|
|
{
|
|
// 광고 횟수가 없다는 팝업 or 가만히 두기
|
|
GameManager.UI.ShowNStackPopup(EPopupType.NotVaildFreeHaert_ADS_Popup);
|
|
}
|
|
}
|
|
else if (_data.productID == "heart_cash")
|
|
{
|
|
if (GameManager.DB.Key >= _data.price)
|
|
{
|
|
GameManager.DB.SubKey(_data.price, this.name);
|
|
GameManager.DB.AddHeart(1, this.name);
|
|
GameManager.DB.AddBuyKeyCount(1);
|
|
|
|
GameManager.DB.SaveDatas();
|
|
}
|
|
else
|
|
{
|
|
// 팝업 출력
|
|
//GameManager.UI.ShowNStackPopup(EPopupType.NotEnoughGoldPopup);
|
|
}
|
|
}
|
|
else if (_data.productID == "com.fgb.adsremove")
|
|
{
|
|
if (GameManager.DB.IsRemoveADS == true) return;
|
|
GameManager.IAP.OnProcessPurchase += OnProcessPurchase;
|
|
GameManager.IAP.Purchase(_data.productID);
|
|
}
|
|
else
|
|
{
|
|
GameManager.IAP.OnProcessPurchase += OnProcessPurchase;
|
|
GameManager.IAP.Purchase(_data.productID);
|
|
}
|
|
|
|
//#####################################################################
|
|
}
|
|
|
|
private bool CheckConditionForState()
|
|
{
|
|
switch (_data.type)
|
|
{
|
|
case CodeJay.Enum.EProductType.ADS:
|
|
// 타이머 체크
|
|
if (IsRunningTimer() == false)
|
|
{
|
|
if (GameManager.Network.IsOnline == true)
|
|
{
|
|
// 광고 준비 체크
|
|
if (true)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
// 팝업 출력(광고 준비 X)
|
|
GameManager.UI.ShowNStackPopup(EPopupType.FailLoadADSPopup);
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// 팝업 출력
|
|
GameManager.UI.ShowNStackPopup(EPopupType.NotOnline_ClosePopup);
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
break;
|
|
|
|
case CodeJay.Enum.EProductType.IAP:
|
|
if (GameManager.Network.IsOnline == true)
|
|
{
|
|
// 구글 로그인에 관련된 처리는 필요없을듯 하다
|
|
// 결제 실패에 관한 처리가 있어어야 하는가?
|
|
// StorePanel에서 결제 실패에 관한 처리를 하면 되는가?
|
|
// 결제 실패시 이 팝업 로드 EPopupType.FailIAPPopup
|
|
|
|
// 70만원 이상이면 결제 금지(한달 기간)
|
|
if (GameManager.DB.BuyShopAccumulatedMoney <= 600000)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
GameManager.UI.ShowNStackPopup(EPopupType.MonthlyPurchaselimitPopup);
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// 팝업 출력
|
|
GameManager.UI.ShowNStackPopup(EPopupType.NotOnline_ClosePopup);
|
|
return false;
|
|
}
|
|
|
|
break;
|
|
case CodeJay.Enum.EProductType.Gold:
|
|
|
|
// 위에서 처리하기 때문에 비여있는 상태
|
|
return true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private void OnStartTimer(int timerCount)
|
|
{
|
|
if (timerCount <= 0)
|
|
{
|
|
GameManager.Timer.DeactivateTimer(name);
|
|
}
|
|
else
|
|
{
|
|
SetTimerText(timerCount);
|
|
|
|
// buyButton.onClick.RemoveListener(OnBuyButtonClicked);
|
|
}
|
|
}
|
|
|
|
private void OnPauseTimer(int timerCount)
|
|
{
|
|
if (timerCount <= 0)
|
|
{
|
|
SetADSRewardActionCountText(GameManager.DB.ProductAdWatchedNumber);
|
|
GameManager.Timer.DeactivateTimer(name);
|
|
//buyButton.onClick.AddListener(OnBuyButtonClicked);
|
|
}
|
|
else
|
|
{
|
|
SetTimerText(timerCount);
|
|
|
|
//buyButton.onClick.RemoveListener(OnBuyButtonClicked);
|
|
}
|
|
}
|
|
|
|
private void OnStopTimer(int timerCount)
|
|
{
|
|
if (timerCount <= 0)
|
|
{
|
|
SetADSRewardActionCountText(GameManager.DB.ProductAdWatchedNumber);
|
|
GameManager.Timer.DeactivateTimer(name);
|
|
//buyButton.onClick.AddListener(OnBuyButtonClicked);
|
|
}
|
|
}
|
|
|
|
private void OnPassedTime(int timerCount)
|
|
{
|
|
SetTimerText(timerCount);
|
|
|
|
if (timerCount <= 0)
|
|
{
|
|
SetADSRewardActionCountText(GameManager.DB.ProductAdWatchedNumber);
|
|
}
|
|
}
|
|
|
|
private bool IsRunningTimer()
|
|
{
|
|
return GameManager.Timer.IsVaildTimer(name);
|
|
}
|
|
|
|
private void SetADSRewardActionCountText(int count)
|
|
{
|
|
sb.Clear();
|
|
sb.Append(count.ToString()).Append("/").Append(Constants.ADS_REWARD_COUNT.ToString());
|
|
|
|
AdNumberTMP.text = sb.ToString();
|
|
}
|
|
|
|
private void SetTimerText(int time)
|
|
{
|
|
int minute = time / 60;
|
|
int second = time % 60;
|
|
|
|
sb.Clear();
|
|
sb.Append(GetDoubleCharNumber(minute)).Append(":").Append(GetDoubleCharNumber(second));
|
|
|
|
AdNumberTMP.text = sb.ToString();
|
|
}
|
|
|
|
private string GetDoubleCharNumber(int num)
|
|
{
|
|
string text;
|
|
if (num < 10)
|
|
{
|
|
text = "0" + num.ToString();
|
|
}
|
|
else
|
|
{
|
|
text = num.ToString();
|
|
}
|
|
|
|
return text;
|
|
}
|
|
|
|
private void OnCompletedRewardedAd(bool isCompleted, string name)
|
|
{
|
|
if (name == this.name)
|
|
{
|
|
if (isCompleted)
|
|
{
|
|
GameManager.DB.AddHeart(1, this.name);
|
|
GameManager.DB.MinProductAdWatchedNumber(1);
|
|
|
|
if (GameManager.DB.ProductAdWatchedNumber <= 0 && AdButton != null)
|
|
{
|
|
Color c = AdButton.GetComponent<Image>().color;
|
|
c.a = 0.5f;
|
|
AdButton.GetComponent<Image>().color = c;
|
|
}
|
|
|
|
if (GameManager.DB.ProductAdWatchedNumber > 0)
|
|
{
|
|
GameManager.Timer.ActivateSimpleTimer(name, Constants.ADS_TIMER_COUNT, OnStartTimer, OnPauseTimer, OnStopTimer, OnPassedTime);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnProcessPurchase(string id)
|
|
{
|
|
if (id == "FailPurchase")
|
|
{
|
|
// 구매 실패
|
|
GameManager.UI.ShowNStackPopup(EPopupType.FailIAPPopup);
|
|
GameManager.IAP.OnProcessPurchase -= OnProcessPurchase;
|
|
return;
|
|
}
|
|
|
|
if (_data.productID == id)
|
|
{
|
|
if (_data.productID == "com.fgb.adsremove")
|
|
{
|
|
Color c = PurchaseButton.GetComponent<Image>().color;
|
|
c.a = 0.5f;
|
|
PurchaseButton.GetComponent<Image>().color = c;
|
|
}
|
|
GameManager.DB.AddBuyShopAccumulatedMoney(_data.price);
|
|
}
|
|
|
|
GameManager.IAP.OnProcessPurchase -= OnProcessPurchase;
|
|
}
|
|
|
|
private void OnResetFreeHeartCountEvent()
|
|
{
|
|
Color c = PurchaseButton.GetComponent<Image>().color;
|
|
c.a = 1.0f;
|
|
PurchaseButton.GetComponent<Image>().color = c;
|
|
}
|
|
|
|
private void OnResetADSRewardActionCountEvent()
|
|
{
|
|
Color c = AdButton.GetComponent<Image>().color;
|
|
c.a = 1.0f;
|
|
AdButton.GetComponent<Image>().color = c;
|
|
|
|
AdNumberTMP.text = $"{GameManager.DB.ProductAdWatchedNumber} / 3";
|
|
}
|
|
}
|