Files
JJBB/Assets/Project/Script/GUI/Shop/SysShopPage.cs

456 lines
18 KiB
C#
Raw Normal View History

2024-08-23 15:49:34 +08:00

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using GCGame.Table;
using GCGame;
using Module.Log;
public class SysShopPage : MonoBehaviour
{
public UIContainerSelect _ShopItemContainer;
public UINumBoardInput _NumInput; // 数字输入组件
public UICurrencyItem _ItemPrice; // 物品单价*数量
public UICurrencyItem _TotalMoney; // 拥有货币数量
public GameObject Tip_ReplaceCanBuy; // 代替可满足Tips
public GameObject Tip_CantReplaceCantBuy; // 代替后不满足Tips
public UIDynamicText _Desc;
public Image _SelectedItemIcon;
public Image _SelectedItemQuality;
public Text _SelectedItemName;
public GameObject addMoneyBtn; // 货币兑换加号图标
public Text m_BuyLimitType; // 限购类型:每日/每周/每月 0-日限购 1-周限购
public Text m_BuyLimitNum; // 限购数目 (当前剩下可买数)
private MONEYTYPE _MoneyType; // 主类型货币
private MONEYTYPE _ReplaceType; // 代替类型货币
private SysShopPageItem.ShopItemInfo _SelectedShopItem;
private int _Price;
public delegate void BuyItemCallBack(SysShopPageItem.ShopItemInfo itemInfo, int itemCount);
private BuyItemCallBack _BuyItemCallBack;
public void InitShop(MONEYTYPE moneyType, BuyItemCallBack buyItemCallBack)
{
_NumInput.Init(0, 1, 999);
InitMoney(moneyType);
_BuyItemCallBack = buyItemCallBack;
if (moneyType == MONEYTYPE.MONEYTYPE_USERCHALLENGE
|| moneyType == MONEYTYPE.MONEYTYPE_SNATCHSCORE
|| (int)moneyType == (int)PropID.PropertyID.GUANNING
|| (int)moneyType == (int)PropID.PropertyID.CROSSSERVERSCORE)
{
if (addMoneyBtn != null)
{
addMoneyBtn.SetActive(false);
}
}
else
{
if (addMoneyBtn != null)
{
addMoneyBtn.SetActive(true);
}
}
}
void OnEnable()
{
// 监听货币数量更新事件
GUIData.delMoneyChanged += UpdateTotalMoney;
}
void OnDisable()
{
GUIData.delMoneyChanged -= UpdateTotalMoney;
}
void InitMoney(MONEYTYPE moneyType)
{
_MoneyType = moneyType;
UpdateTotalMoney();
_ItemPrice.ShowCurrency(moneyType, 0);
}
public void SetShopItems(List<SysShopPageItem.ShopItemInfo> shopItemList, List<SysShopPageItem.ShopItemInfo> shopSelectItem = null)
{
if (shopSelectItem == null || shopSelectItem.Count == 0)
{
SysShopPageItem.ShopItemInfo[] selectItems = null;
if (shopItemList.Count > 0)
selectItems = new SysShopPageItem.ShopItemInfo[] { shopItemList[0] };
_ShopItemContainer.InitSelectContent(shopItemList, selectItems, OnShopItemSelect);
}
else
{
_ShopItemContainer.InitSelectContent(shopItemList, shopSelectItem, OnShopItemSelect);
}
}
// 监听货币数量变化
private void UpdateMoney()
{
// 控制提示的状态,
// 当为1是表示替代可满足
// 当为2是表示代替不可满足
int state = -1;
int cConsumeType = -1;
int cConsumeId = -1;
int consumeType = -1;
if (_SelectedShopItem != null)
{
_Price = _NumInput.Value * _SelectedShopItem.Price;
long own = GetMoneyCount(_MoneyType);
if (_MoneyType < MONEYTYPE.MONEYTYPE_SNATCHSCORE)
{
consumeType = (int)CONSUM_TYPE.MONEY;
}
if (own >= _Price)
{
_ItemPrice.ShowCurrency(_MoneyType, _Price);
_TotalMoney.ShowCurrency(_MoneyType, own);
}
else
{
_ItemPrice.ShowCurrency(_MoneyType, _Price);
_TotalMoney.SetColor("<color=red>");
_TotalMoney.ShowCurrency(_MoneyType, own);
}
// 存在可替换货币时
//if(Utils.ConsumeTypeExchange(consumeType, (int)_MoneyType, out cConsumeType, out cConsumeId))
//{
// long ownC = GameManager.gameManager.PlayerDataPool.GetIntPropty(cConsumeType, cConsumeId);
// // 足够
// if(own >= _Price)
// {
// _ItemPrice.ShowCurrency(_MoneyType, _Price);
// _TotalMoney.ShowCurrency(_MoneyType, own);
// }
// // 代替后足够
// else if(own + ownC >= _Price)
// {
// state = 1;
// if (own > 0)
// {
// _ItemPrice.ShowCurrency(_MoneyType, _Price);
// _TotalMoney.SetColor("<color=red>");
// _TotalMoney.ShowCurrency(_MoneyType, own);
// }
// else
// {
// _ItemPrice.ShowCurrency((MONEYTYPE)cConsumeId, _Price);
// _TotalMoney.ShowCurrency((MONEYTYPE)cConsumeId, ownC);
// }
// }
// // 当不足够时,原类型货币足够时,显示原货币;仅当原货币为 0 时显示代替货币
// else if(own + ownC < _Price)
// {
// state = 2;
// if(own > 0 || ownC == 0)
// {
// _ItemPrice.ShowCurrency(_MoneyType, _Price);
// _TotalMoney.SetColor("<color=red>");
// _TotalMoney.ShowCurrency(_MoneyType, own);
// }
// else
// {
// _ItemPrice.ShowCurrency((MONEYTYPE)cConsumeId, _Price);
// _TotalMoney.SetColor("<color=red>");
// _TotalMoney.ShowCurrency((MONEYTYPE)cConsumeId, ownC);
// }
// }
//}
//// 不存在可替换货币
//else
//{
// state = -1;
// if(own >= _Price)
// {
// _ItemPrice.ShowCurrency(_MoneyType, _Price);
// _TotalMoney.ShowCurrency(_MoneyType, own);
// }
// else
// {
// _ItemPrice.ShowCurrency(_MoneyType, _Price);
// _TotalMoney.SetColor("<color=red>");
// _TotalMoney.ShowCurrency(_MoneyType, own);
// }
//}
}
else
{
_Price = 0;
_ItemPrice.ShowCurrency(_Price);
}
if(Tip_ReplaceCanBuy != null)
{
Tip_ReplaceCanBuy.SetActive(state == 1);
if(cConsumeType==4)
{
Image MoneyType = Tip_ReplaceCanBuy.GetComponentInChildren<Image>();
if(MoneyType!=null)
{
LoadAssetBundle.Instance.SetImageSprite(MoneyType, UICurrencyItem.GetCurrencySprite((MONEYTYPE)cConsumeId));
}
}
}
if(Tip_CantReplaceCantBuy != null)
{
Tip_CantReplaceCantBuy.SetActive(state == 2);
if (cConsumeType == 4)
{
Image MoneyType = Tip_CantReplaceCantBuy.GetComponentInChildren<Image>();
if (MoneyType != null)
{
LoadAssetBundle.Instance.SetImageSprite(MoneyType, UICurrencyItem.GetCurrencySprite((MONEYTYPE)cConsumeId));
}
}
}
}
private void UpdateTotalMoney()
{
long num = GetMoneyCount(_MoneyType);
_TotalMoney.ShowCurrency(_MoneyType, num);
}
private long GetMoneyCount(MONEYTYPE t)
{
switch (t)
{
case MONEYTYPE.MONEYTYPE_COIN:
return GameManager.gameManager.PlayerDataPool.Money.GetMoney_Coin();
case MONEYTYPE.MONEYTYPE_YUANBAO:
return GameManager.gameManager.PlayerDataPool.Money.GetMoney_YuanBao();
case MONEYTYPE.MONEYTYPE_YUANBAO_BIND:
return GameManager.gameManager.PlayerDataPool.Money.GetMoney_YuanBaoBind();
case MONEYTYPE.MONEYTYPE_COIN_BIND:
return GameManager.gameManager.PlayerDataPool.Money.GetMoney_CoinBind();
case MONEYTYPE.MONEYTYPE_USERCHALLENGE:
return GameManager.gameManager.PlayerDataPool.Money.GetMoney_ChallengeScore();
case MONEYTYPE.MONEYTYPE_SNATCHSCORE:
return GameManager.gameManager.PlayerDataPool.Money.GetMoney_SnatchScore();
default:
return GameManager.gameManager.PlayerDataPool.GetPropInt((PropID.PropertyID)_MoneyType);
}
}
private void OnShopItemSelect(object obj)
{
_SelectedShopItem = obj as SysShopPageItem.ShopItemInfo;
if (m_BuyLimitType != null)
{
m_BuyLimitType.gameObject.SetActive(false);
}
// 设置限购显示
Tab_ShopItemLimit shopLimit = TableManager.GetShopItemLimitByID(_SelectedShopItem.LimitID, 0);
int limitCount = -1;
if (shopLimit != null && m_BuyLimitType != null)
{
var _curVipLevel = Singleton<ObjManager>.Instance.MainPlayer.VipCost;
if (_curVipLevel <= 0)
{
limitCount = shopLimit.GetVipLvbyIndex(0);
}
else
{
limitCount = shopLimit.GetVipLvbyIndex(_curVipLevel);
}
// 日限购
var remainLimitCount = limitCount - _SelectedShopItem.BuyCnt + (_SelectedShopItem.MoneyType == MONEYTYPE.MONEYTYPE_YUANBAO ? GlobalData.GetVipLimitBuyCount() : 0);
if (shopLimit.LimitType == 0)
{
m_BuyLimitType.text = StrDictionary.GetClientDictionaryString("#{1306}");//"每日限购:";
m_BuyLimitNum.text = (remainLimitCount > 0 ? remainLimitCount : 0).ToString();
m_BuyLimitType.gameObject.SetActive(true);
}
// 周限购
else if (shopLimit.LimitType == 1)
{
m_BuyLimitType.text = StrDictionary.GetClientDictionaryString("#{1307}");//"每周限购:";
m_BuyLimitNum.text = (remainLimitCount > 0 ? remainLimitCount : 0).ToString();
m_BuyLimitType.gameObject.SetActive(true);
}
// 月限购
else if(shopLimit.LimitType == 2)
{
m_BuyLimitType.text = StrDictionary.GetClientDictionaryString("#{63000}");//"每月限购:";
m_BuyLimitNum.text = (remainLimitCount > 0 ? remainLimitCount : 0).ToString();
m_BuyLimitType.gameObject.SetActive(true);
}
}
string strItemDesc = StrDictionary.GetClientString_WithNameSex(_SelectedShopItem.TabItem.Tips);
_Desc.SetText(StrDictionary.GetClientString_WithNameSex(_SelectedShopItem.TabItem.Tips));
var usableItemTab = TableManager.GetUsableItemByID(_SelectedShopItem.TabItem.Id, 0);
if(usableItemTab != null)
{
if(usableItemTab.LimitId != -1)
{
_Desc.SetText(strItemDesc + StrDictionary.GetClientDictionaryString("#{41439}",
GameManager.gameManager.PlayerDataPool.ItemLimitInfo.GetItemRemainCanUseTimes(_SelectedShopItem.TabItem.Id) == -1 ?
0 : GameManager.gameManager.PlayerDataPool.ItemLimitInfo.GetItemRemainCanUseTimes(_SelectedShopItem.TabItem.Id)));
}
}
#region
int _PlayerProfession = GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Profession; //获取玩家职业
if (_PlayerProfession == 0 || _PlayerProfession == 2)
{
LoadAssetBundle.Instance.SetImageSprite(_SelectedItemIcon, _SelectedShopItem.TabItem.Icon);//男
}
else if (_PlayerProfession == 1 || _PlayerProfession == 3)
{
LoadAssetBundle.Instance.SetImageSprite(_SelectedItemIcon, _SelectedShopItem.TabItem.Iconnv); //女
}
#endregion
//LoadAssetBundle.Instance.SetImageSprite(_SelectedItemIcon, _SelectedShopItem.TabItem.Icon);
_SelectedItemIcon.gameObject.SetActive(true);
LoadAssetBundle.Instance.SetImageSprite(_SelectedItemQuality, GCGame.Utils.GetItemQualityFrame(_SelectedShopItem.TabItem));
_SelectedItemQuality.gameObject.SetActive(true);
if (_SelectedShopItem.TabItem.QualityEffect > 0)
{
CommonItemContainerItem.ShowQualityEffect(true, _SelectedShopItem.TabItem.QualityEffect, _SelectedItemIcon.transform);
}
else
{
CommonItemContainerItem.ShowQualityEffect(false, _SelectedShopItem.TabItem.QualityEffect, _SelectedItemIcon.transform);
}
_SelectedItemName.text = _SelectedShopItem.TabItem.Name;
// 目前 MONEYTYPE 索引最大的是 MONEYTYPE.MONEYTYPE_SNATCHSCORE
// PropID.PropertyID 中也可以获得同样货币
// 两种同样货币在不同枚举中,索引差异大,所以用大小判断用什么类型获取。
long Ownvalue = 0;
if (_MoneyType == MONEYTYPE.MONEYTYPE_COIN_BIND)
{//特殊的,使用银票的地方把银两加进来
Ownvalue = GetMoneyCount(MONEYTYPE.MONEYTYPE_COIN_BIND) + GetMoneyCount(MONEYTYPE.MONEYTYPE_COIN);
}
else if (_MoneyType > MONEYTYPE.MONEYTYPE_SNATCHSCORE)
{
Ownvalue = GameManager.gameManager.PlayerDataPool.GetPropInt((PropID.PropertyID)_MoneyType);
}
else
{
Ownvalue = GameManager.gameManager.PlayerDataPool.GetIntPropty((int)CONSUM_TYPE.MONEY, (int)_MoneyType);
int cConsumeType;
int cConsumeId;
// 现求获得数量最大值只允许用一种货币
if(Ownvalue <= 0)
{
if (Utils.ConsumeTypeExchange((int)CONSUM_TYPE.MONEY, (int)_MoneyType, out cConsumeType, out cConsumeId))
{
long ownC = GameManager.gameManager.PlayerDataPool.GetIntPropty((int)cConsumeType, (int)cConsumeId);
Ownvalue = ownC;
}
}
}
// 计算货币方面对数量的限制
long count = 0;
if(_SelectedShopItem.Price == 0)
{
count = Ownvalue;
}
else
{
count = Ownvalue / _SelectedShopItem.Price;
}
// 特权vip权利对限购数量的提升
int privilegeVipRight = _SelectedShopItem.MoneyType == MONEYTYPE.MONEYTYPE_YUANBAO ? GlobalData.GetVipLimitBuyCount() : 0;
// 计算系统对物品购买数目的限制
if (limitCount != -1)
{
count = (count < (limitCount + privilegeVipRight - _SelectedShopItem.BuyCnt)) ? count : (limitCount + privilegeVipRight - _SelectedShopItem.BuyCnt);
}
count = count > 0 ? count : 1;
int maxStack = _SelectedShopItem.TabItem.MaxStackSize;
count = count < maxStack ? count : maxStack;
// 设置输入面板最大最小值
var _itemId = _SelectedShopItem.TabItem.Id;
if (GameManager.gameManager.PlayerDataPool._MissionLootItemNeedCountDic.ContainsKey(_itemId))
{
var _BackPack = GameManager.gameManager.PlayerDataPool.BackPack;
var _CurOwnItemCount = _BackPack.GetItemCountByDataId(_itemId);
var _LackItemCount = GameManager.gameManager.PlayerDataPool._MissionLootItemNeedCountDic[_itemId] - _CurOwnItemCount;
if (_LackItemCount <= 0) //道具足够
{
_NumInput.Init(0, 1, (int)count);
}
else //需要的道具不足
{
_NumInput.Init(
_LackItemCount < (int)count ? _LackItemCount : (int)count,
1, (int)count);
}
}
else
{
_NumInput.Init(_SelectedShopItem.isNeed, 1, (int)count);
}
UpdateMoney();
}
public void RefreshSelected()
{
if (_SelectedShopItem != null)
{
OnShopItemSelect(_SelectedShopItem);
}
}
public void OnItemNumModify()
{
UpdateMoney();
}
public void OnBtnBuy()
{
if (_BuyItemCallBack != null && _SelectedShopItem != null)
{
if (_MoneyType == MONEYTYPE.MONEYTYPE_COIN_BIND)
{
var coin = GameManager.gameManager.PlayerDataPool.Money.GetMoney_CoinBind();
if (coin < _ItemPrice.CurrencyIntValue)
{
MessageBoxLogic.OpenOKCancelBox(StrDictionary.GetClientDictionaryString("#{71004}"), "", () => { _BuyItemCallBack(_SelectedShopItem, _NumInput.Value); }, null);
}
else
{
_BuyItemCallBack(_SelectedShopItem, _NumInput.Value);
}
}
else if((int)_MoneyType == (int)PropID.PropertyID.BOSSCORE)
{
int BossCore = GameManager.gameManager.PlayerDataPool.GetPropInt(PropID.PropertyID.BOSSCORE);
if (BossCore < _ItemPrice.CurrencyIntValue)
{
GUIData.AddNotifyData(StrDictionary.GetClientDictionaryString("{#49151}"));
return;
}
_BuyItemCallBack(_SelectedShopItem, _NumInput.Value);
}
else
{
_BuyItemCallBack(_SelectedShopItem, _NumInput.Value);
}
}
}
}