NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/UI/ShowAllCharacter/GenericItem.cs

475 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using cfg.ActorCfg;
using Framework;
using Gameplay;
using PhxhSDK;
using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 通用道具
/// </summary>
public class GenericItem : UIGameObjectWrapper
{
/// <summary>
/// 置灰的灰度
/// </summary>
private const float GREY = 0.5f;
/// <summary>
/// 正常颜色
/// </summary>
private const float NORMAL = 1f;
#region UI
/// <summary>
/// 道具品质_蓝
/// </summary>
private Image imageQuality_blue;
/// <summary>
/// 道具品质_黄
/// </summary>
private Image imageQuality_yellow;
/// <summary>
/// 道具品质_红
/// </summary>
private Image imageQuality_red;
/// <summary>
/// 道具图标
/// </summary>
private Image imageIcon;
/// <summary>
/// 道具数量背景
/// </summary>
private Image imageCount;
/// <summary>
/// 道具数量文本
/// </summary>
private TMP_Text textCount;
/// <summary>
/// 道具按钮
/// </summary>
private Button button;
#endregion
/// <summary>
/// 道具配置
/// </summary>
private DataItem dataItem;
/// <summary>
/// 回调
/// </summary>
private Action callbackAction;
#region 属性
/// <summary>
/// 是否显示道具数量
/// </summary>
public bool IsShowCount
{
set { imageCount.gameObject.IsScaleShow(value); }
}
/// <summary>
/// 是否置灰
/// </summary>
public bool IsGrey
{
set
{
if (value)
{
//imageQuality.color = new Color(GREY, GREY, GREY, imageQuality.color.a);
imageIcon.color = new Color(GREY, GREY, GREY, imageIcon.color.a);
imageCount.color = new Color(GREY, GREY, GREY, imageCount.color.a);
textCount.color = new Color(GREY, GREY, GREY, textCount.color.a);
}
else
{
//imageQuality.color = new Color(NORMAL, NORMAL, NORMAL, imageQuality.color.a);
imageIcon.color = new Color(NORMAL, NORMAL, NORMAL, imageIcon.color.a);
imageCount.color = new Color(NORMAL, NORMAL, NORMAL, imageCount.color.a);
textCount.color = new Color(NORMAL, NORMAL, NORMAL, textCount.color.a);
}
}
}
/// <summary>
/// 是否开启按钮
/// </summary>
public bool IsEnableButton
{
set { button.enabled = value; }
}
#endregion
#region 构造
/// <summary>
/// 根据GameObject构造
/// </summary>
/// <param name="root"></param>
public GenericItem(GameObject root, Action callback = null) : base(root, false)
{
Construct(callback);
}
/// <summary>
/// 根据GameObject克隆并构造
/// </summary>
/// <param name="root"></param>
/// <param name="i"></param>
public GenericItem(GameObject root, int i, Action callback = null) : base(root, i, false)
{
Construct(callback);
}
private void Construct(Action callback)
{
imageQuality_blue = GetComponent<Image>("BaseItem/Rarity/ItemQuality_blue");
imageQuality_yellow = GetComponent<Image>("BaseItem/Rarity/ItemQuality_yellow");
imageQuality_red = GetComponent<Image>("BaseItem/Rarity/ItemQuality_red");
imageIcon = GetComponent<Image>("BaseItem/ItemIcon");
imageCount = GetComponent<Image>("BaseItem/ImageCount");
textCount = GetComponent<TMP_Text>("BaseItem/ImageCount/TextCount");
button = GetComponent<Button>("BaseItem");
IsShowCount = false;
IsGrey = false;
SetCallback(callback);
CommonUtils.BindButton(button, OnClick);
}
#endregion
#region 入口
/// <summary>
/// 设置道具数据
/// </summary>
/// <param name="data">道具配置</param>
/// <param name="count">道具数量道具数量小于等于0时不显示道具数量</param>
public virtual async void SetInfo(DataItem data, int count = 0, Action callback = null)
{
dataItem = data;
if (dataItem == null)
{
DebugUtil.LogError("空数据");
return;
}
SetCount(count);
SetCallback(callback);
imageIcon.sprite = await LoadAssetAsync<Sprite>(dataItem.IconPath);
imageQuality_red.gameObject.IsScaleShow(dataItem.Rarity == ItemRarity.Red);
imageQuality_yellow.gameObject.IsScaleShow(dataItem.Rarity == ItemRarity.Yellow);
imageQuality_blue.gameObject.IsScaleShow((int)dataItem.Rarity <= (int)ItemRarity.Blue);
IsScaleShow = true;
}
/// <summary>
/// 设置道具数据
/// </summary>
/// <param name="iconPath">图标路径</param>
/// <param name="count">道具数量道具数量小于等于0时不显示道具数量</param>
/// <param name="callback">点击回调,回调是空时禁用点击</param>
public virtual async void SetInfo(string iconPath, int count = 0, Action callback = null)
{
SetCount(count);
SetCallback(callback);
imageIcon.sprite = await LoadAssetAsync<Sprite>(iconPath);
IsScaleShow = true;
}
/// <summary>
/// 根据道具id设置道具数据
/// </summary>
/// <param name="itemID">道具配置id</param>
/// <param name="count">道具数量道具数量小于等于0时不显示道具数量</param>
public virtual void SetInfo(int itemID, int count = 0, Action callback = null)
{
DataItem data = TableManager.Instance.Tables.Item.Get(itemID);
if (data == null)
{
DebugUtil.LogError($"道具id错误,错误道具id{itemID}");
return;
}
SetInfo(data, count, callback);
}
#endregion
/// <summary>
/// 设置回调
/// </summary>
/// <param name="callback"></param>
private void SetCallback(Action callback)
{
if (callback != null)
callbackAction = callback;
IsEnableButton = callbackAction != null;
}
/// <summary>
/// 设置道具数量
/// </summary>
private void SetCount(int count)
{
if (count <= 0)
{
IsShowCount = false;
return;
}
textCount.text = count.ToString();
IsShowCount = true;
}
private void OnClick()
{
callbackAction?.Invoke();
}
/// <summary>
/// 含道具信息的点击事件
/// 该函数调用会清空之前的点击事件
/// </summary>
/// <param name="action"></param>
public void BindDataItemClick(Action<DataItem> action)
{
callbackAction = () =>
{
if (dataItem == null)
{
DebugUtil.LogError("道具数据为空");
return;
}
action?.Invoke(dataItem);
};
}
}
/// <summary>
/// 通用道具,通关奖励
/// </summary>
public class GenericItem_PassReward : GenericItem
{
#region UI
/// <summary>
/// 已获取状态
/// </summary>
private GameObject goGet;
/// <summary>
/// 首次通关状态
/// </summary>
private GameObject goFirstPass;
/// <summary>
/// 重复通关状态
/// </summary>
private GameObject goNormalPass;
#endregion
#region 属性
/// <summary>
/// 是否已经获取
/// </summary>
public bool IsGet
{
set
{
goGet.IsScaleShow(value);
IsGrey = value;
}
}
/// <summary>
/// 是否首次通关
/// </summary>
public bool IsFirstPass
{
set { goFirstPass.IsScaleShow(value); }
}
/// <summary>
/// 是否重复通关
/// </summary>
public bool IsNormalPass
{
set { goNormalPass.IsScaleShow(value); }
}
#endregion
#region 构造
public GenericItem_PassReward(GameObject root, Action callback = null) : base(root, callback)
{
Construct();
}
public GenericItem_PassReward(GameObject root, int i, Action callback = null) : base(root, i, callback)
{
Construct();
}
private void Construct()
{
goGet = FindObj("BaseItem_PassReward/TextAlreadyGet");
goNormalPass = FindObj("BaseItem_PassReward/LevelNormalPass");
goFirstPass = FindObj("BaseItem_PassReward/LevelFirstPass");
IsGet = false;
IsFirstPass = false;
IsNormalPass = false;
}
#endregion
/// <summary>
/// 设置奖励道具数据
/// </summary>
/// <param name="rewardItem"></param>
/// <param name="isGet"></param>
/// <param name="rewardType"></param>
public void SetInfo(RewardItem rewardItem, bool isGet = false, ERewardType rewardType = ERewardType.None, Action callback = null)
{
base.SetInfo(rewardItem.Data, rewardItem.Count, callback);
IsGet = isGet;
IsFirstPass = rewardType == ERewardType.LevelFirstPassReward;
IsNormalPass = rewardType == ERewardType.LevelNormalPassReward;
}
}
/// <summary>
/// 通用道具,背包
/// </summary>
public class GenericItem_Package : GenericItem
{
/// <summary>
/// 倒计时状态分钟,显示白色文本
/// </summary>
private readonly Color TIME_MIN_COLOR = Color.white;
/// <summary>
/// 倒计时状态小时和天,显示黑色文本
/// </summary>
private readonly Color TIME_HOUR_DAY_COLOR = Color.black;
#region UI
/// <summary>
/// 到期时间状态-分钟
/// </summary>
private GameObject goTimeMin;
/// <summary>
/// 到期时间状态-小时
/// </summary>
private GameObject goTimeHour;
/// <summary>
/// 到期时间状态-天
/// </summary>
private GameObject goTimeDay;
/// <summary>
/// 到期时间文本
/// </summary>
private TMP_Text textTime;
#endregion
/// <summary>
/// 是否限时道具
/// </summary>
public bool IsLimiteTime
{
set
{
if (value)
textTime.gameObject.IsScaleShow(true);
else
{
goTimeMin.IsScaleShow(false);
goTimeHour.IsScaleShow(false);
goTimeDay.IsScaleShow(false);
textTime.gameObject.IsScaleShow(false);
}
}
}
#region 构造
public GenericItem_Package(GameObject root, Action callback = null) : base(root, callback)
{
Construct();
}
public GenericItem_Package(GameObject root, int i, Action callback = null) : base(root, i, callback)
{
Construct();
}
private void Construct()
{
goTimeMin = FindObj("BaseItem_Package/Time/ImageMin");
goTimeHour = FindObj("BaseItem_Package/Time/ImageHour");
goTimeDay = FindObj("BaseItem_Package/Time/ImageDay");
textTime = GetComponent<TMP_Text>("BaseItem_Package/Time/TextTime");
}
#endregion
/// <summary>
/// 设置背包道具数据
/// </summary>
/// <param name="rewardItem"></param>
/// <param name="isGet"></param>
/// <param name="rewardType"></param>
public void SetInfo()
{
//base.SetInfo(rewardItem.Data, rewardItem.Count); // 传入指定类型数据
/*
if (是否限时)
{
IsLimiteTime = true;
// TODO
}
else
IsLimiteTime = false;
*/
}
}
/// <summary>
/// 通用道具,关卡星级奖励
/// </summary>
public class GenericItem_LevelStarReward : UIGameObjectWrapper
{
/// <summary>
/// 已领取状态
/// </summary>
private GameObject goGet;
/// <summary>
/// 通用道具
/// </summary>
private GenericItem genericItem;
/// <summary>
/// 是否领取
/// </summary>
public bool IsGet
{
set { goGet.IsScaleShow(value); }
}
public GenericItem_LevelStarReward(GameObject root) : base(root)
{
goGet = FindObj("BaseItem_LevelStarReward/TextAlreadyGet");
genericItem = new(root);
}
public void SetInfo(RewardItem rewardItem, bool isGet = false, Action callback = null)
{
genericItem.SetInfo(rewardItem.Data, rewardItem.Count, callback);
IsGet = isGet;
}
}