Files
JJBB/Assets/Project/Script/GUI/SkillBar/SkillButton.cs
2024-08-23 15:49:34 +08:00

387 lines
13 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Games.Events;
using Games.LogicObj;
using Games.SkillModle;
using GCGame.Table;
using Module.Log;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class SkillButton : SkillButtonBase, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
// 技能cd数值精度 - 处理一个因为cd数值不精确导致的误判
public const float cooldownPercision = 0.001f;
private bool _allowInput;
private GameObject _cdEffectClone;
private Image _comboTimer;
private float _cooldownRatio; // 冷却时间
// 当前操作开始时技能连段数目
private int _currentComboId;
public GameObject cdEffectObj;
public GameObject comboTimerObj;
public Image LockImage; //技能未开启
public Image XPSKill;
public SkillSelectUi m_skillselect;
public Text NeedLevel;
//技能开启需要个提示
public int SkillBarIndex;
public Image skillCDImage; //技能CD蒙版
public Image skillIcon; //技能图标
public Text CdTime;
//连击目标按钮
public SkillButton ComboTarget { get; set; }
// 当前操作技能按钮的手指Id
public int? CurrentFingerId { get; private set; }
// 当前技能按钮的操作Id
public int SkillIndex { get; private set; }
public bool AllowInput
{
get { return _allowInput; }
set
{
if (_allowInput != value)
{
_allowInput = value;
CleanSkillSelect();
ShowLockIcon(!_allowInput);
}
}
}
public OwnSkillData SkillData
{
get
{
if (Singleton<ObjManager>.GetInstance().MainPlayer != null && SkillIndex >= 0)
return Singleton<ObjManager>.GetInstance().MainPlayer.OwnSkillInfo[SkillIndex];
return null;
}
}
public void Clear()
{
SkillIndex = -1;
comboTimerObj.SetActive(false);
CleanSkillSelect();
if (XPSKill != null)
XPSKill.gameObject.SetActive(false);
if (LockImage != null)
LockImage.gameObject.SetActive(true);
if (skillCDImage != null)
skillCDImage.gameObject.SetActive(false);
if (skillIcon != null)
skillIcon.gameObject.SetActive(false);
SetSkillButtonOpenLevel();
}
private void Awake()
{
SkillIndex = -1;
CurrentFingerId = null;
_comboTimer = comboTimerObj.transform.Find("FillImage").GetComponent<Image>();
comboTimerObj.SetActive(false);
}
private void Start()
{
EventDispatcher.Instance.Add(Games.Events.EventId.LockSkillInput, OnSkillInputUpdate);
EventDispatcher.Instance.Add(Games.Events.EventId.ProcessInputBlock, OnProcessInputBlock);
OnSkillInputUpdate(null);
}
private void OnDisable()
{
if (CurrentFingerId != null && m_skillselect != null)
{
m_skillselect.EndSelect((int)CurrentFingerId);
CurrentFingerId = null;
}
}
private void OnDestroy()
{
EventDispatcher.Instance.Remove(Games.Events.EventId.LockSkillInput, OnSkillInputUpdate);
EventDispatcher.Instance.Remove(Games.Events.EventId.ProcessInputBlock, OnProcessInputBlock);
}
private void OnSkillInputUpdate(object args)
{
AllowInput = GameManager.gameManager.PlayerDataPool.AllowSkillInput;
}
private void OnProcessInputBlock(object args)
{
var block = (bool)args;
if (block)
CleanSkillSelect();
}
private void Update()
{
if (SkillIndex >= 0 && AllowInput)
{
var mainPlayer = Singleton<ObjManager>.GetInstance().MainPlayer;
if (mainPlayer != null)
{
var ownSkill = mainPlayer.OwnSkillInfo[SkillIndex];
if (ownSkill.IsValid())
{
ownSkill.UpdateCombo();
var cdTime = ownSkill.CurrentCooldown;
var totalCd = ownSkill.Cooldown;
if (Mathf.Abs(cdTime) < cooldownPercision)
cdTime = 0f;
CdTime.gameObject.SetActive(cdTime > 0f);
if (cdTime > 0f)
{
if (totalCd > 0)
SetCdRatio(cdTime / totalCd);
else
SetCdRatio(0f);
CdTime.text = Mathf.CeilToInt(cdTime).ToString();
}
else
SetCdRatio(0f);
var comboSkill = ownSkill.GetComboSkill();
if (comboSkill != null && ComboTarget != null)
{
ComboTarget.gameObject.SetActive(comboSkill.ComboRemain > 0);
}
// 显示当前连段技能
if (ownSkill.ComboRemain > 0)
{
comboTimerObj.SetActive(true);
_comboTimer.fillAmount = ownSkill.ComboRemain / ownSkill.ComboTime;
}
else
{
comboTimerObj.SetActive(false);
}
//// 如果连段数目发生改变,则强制清空操作并且更新连段技能图标
//if (ownSkill.ComboExTable.SkillExID != _currentComboId)
//{
// _currentComboId = ownSkill.ComboExTable.SkillExID;
// UpdateImageByCombo();
// if (CurrentFingerId != null)
// CleanSkillSelect();
//}
}
}
}
}
public void SetSkillBarInfo(int skillIndex)
{
//if (SkillIndex != skillIndex)
//{
SkillIndex = skillIndex;
ShowLockIcon(!AllowInput);
//}
}
private void ShowLockIcon(bool isLock)
{
//if (isLock || SkillIndex < 0)
if (isLock)
{
skillIcon.gameObject.SetActive(false);
skillCDImage.gameObject.SetActive(false);
comboTimerObj.SetActive(false);
SetSkillButtonOpenLevel();
}
else if (SkillIndex < 0)
{
skillIcon.gameObject.SetActive(false);
skillCDImage.gameObject.SetActive(false);
comboTimerObj.SetActive(false);
SetSkillButtonOpenLevel();
}
else
{
LockImage.gameObject.SetActive(false);
NeedLevel.gameObject.SetActive(false);
skillIcon.gameObject.SetActive(true);
UpdateImageByCombo();
}
}
private void SetSkillButtonOpenLevel()
{
// 仅仅显示未达到等级的技能
var mainPlayer = ObjManager.Instance.MainPlayer;
if (mainPlayer == null)
return;
var skillActives = TableManager.GetSkillLearn().Values;
foreach (var skill in skillActives)
{
if (skill.Profession == mainPlayer.Profession
&& SkillBarIndex == skill.SkillBarIndex)
{
if (mainPlayer.BaseAttr.Level < skill.LearnLv)
{
NeedLevel.text = StrDictionary.GetClientDictionaryString("#{1166}", skill.LearnLv);
NeedLevel.gameObject.SetActive(true);
LockImage.gameObject.SetActive(true);
if (XPSKill != null)
XPSKill.gameObject.SetActive(false);
}
else
{
NeedLevel.gameObject.SetActive(false);
LockImage.gameObject.SetActive(false);
if (XPSKill != null)
XPSKill.gameObject.SetActive(SkillBarIndex == 4 || SkillBarIndex == 5);
}
return;
}
}
NeedLevel.gameObject.SetActive(false);
LockImage.gameObject.SetActive(false);
if (SkillBarIndex >= 4)
gameObject.SetActive(false);
}
private void SetCdRatio(float ratio)
{
// 仅在Ratio有所改变的状况下有响应
if (_cooldownRatio != ratio)
{
_cooldownRatio = ratio;
if (ratio > 0f)
{
skillCDImage.gameObject.SetActive(true);
skillCDImage.fillAmount = ratio;
}
else
{
skillCDImage.gameObject.SetActive(false);
if (_cdEffectClone == null)
_cdEffectClone = CloneEffectObj(cdEffectObj);
_cdEffectClone.SetActive(true);
}
}
}
#region
public void OnDrag(PointerEventData eventData)
{
if (CurrentFingerId == eventData.pointerId)
if (SkillIndex >= 0)
{
if (m_skillselect != null)
{
var mainPlayer = Singleton<ObjManager>.Instance.MainPlayer;
m_skillselect.MoveSelect(eventData, mainPlayer.OwnSkillInfo[SkillIndex].ComboExTableFinal);
}
}
else
{
CurrentFingerId = null;
}
}
public void OnPointerDown(PointerEventData eventData)
{
if (SkillIndex >= 0 && AllowInput && ProcessInput.Instance != null && !ProcessInput.Instance.isBlocked)
if (CurrentFingerId == null)
{
var mainPlayer = Singleton<ObjManager>.GetInstance().MainPlayer;
if (mainPlayer == null)
{
LogModule.ErrorLog("MainPlayer is Null");
}
else
{
var ownSkill = mainPlayer.OwnSkillInfo[SkillIndex];
var castable = mainPlayer.CheckSkillCastable(ownSkill.SkillBaseTable, ownSkill.SkillExTable, ownSkill.IsCooldownFinish());
switch (castable)
{
case SkillCastableCheckResult.Success:
case SkillCastableCheckResult.FailOtherSkill:
// 注严进宽出OnUp会直接用CurrentFingerId检查就释放事件
if (m_skillselect != null && m_skillselect.StartSelect(eventData,
(RectTransform)transform.parent, ownSkill))
CurrentFingerId = eventData.pointerId;
break;
case SkillCastableCheckResult.FailCooldown:
mainPlayer.SendNoticMsg(false, "#{1245}");
break;
case SkillCastableCheckResult.FailSelect:
mainPlayer.SendNoticMsg(false, "#{1250}");
break;
}
}
}
}
public void OnPointerUp(PointerEventData eventData)
{
if (LockImage.gameObject.activeSelf)
{
GUIData.AddNotifyData(StrDictionary.GetClientDictionaryString("#{4747}"));
}
if (CurrentFingerId == eventData.pointerId)
{
CurrentFingerId = null;
if (SkillIndex >= 0)
if (m_skillselect != null && m_skillselect.EndSelect(eventData.pointerId))
{
var mainPlayer = Singleton<ObjManager>.Instance.MainPlayer;
if (mainPlayer != null)
if (mainPlayer.IsDisableControl())
{
//组队跟随,不响应技能报错
if (GameManager.gameManager.PlayerDataPool.IsFollowTeam)
GUIData.AddNotifyData(StrDictionary.GetClientDictionaryString("#{5128}"));
// 暂时没有混乱中报错的机制
}
else
UseSkill(m_skillselect.AttackPoint, SkillIndex);
}
}
}
/// <summary>
/// 强制停止当前使用中的技能
/// </summary>
private void CleanSkillSelect()
{
if (CurrentFingerId != null)
{
if (m_skillselect != null)
m_skillselect.EndSelect(CurrentFingerId.Value);
CurrentFingerId = null;
}
}
/// <summary>
/// 根据当前连段数目配置技能图标
/// </summary>
private void UpdateImageByCombo()
{
var mainPlayer = Singleton<ObjManager>.Instance.MainPlayer;
if (mainPlayer != null)
LoadAssetBundle.Instance.SetImageSprite(skillIcon, mainPlayer.OwnSkillInfo[SkillIndex].ComboBaseTable.Icon);
}
#endregion
}