yyl
2025-07-21 5bc2cc9a3e007b96a0de96e70e87f25bc5a254a2
Main/System/Battle/BattleObject/BattleObject.cs
@@ -30,7 +30,9 @@
{
    public BattleField battleField;
    public int BattleObjectId { get; set; }
    public BattleObjectBuffMgr buffMgr;
    public int ObjID { get; set; }
    public BattleCamp Camp { get; protected set; }
@@ -38,13 +40,19 @@
    // public BuffMgr buffMgr;
    protected MotionBase motionBase;
    public MotionBase motionBase;
    public GameObject heroGo
    {
        get;
        private set;
    }
    protected Action onDeathAnimationComplete;
    protected Renderer[] renderers;
    public Transform effectNode;
    public BattleObject(BattleField _battleField)
    {
@@ -58,6 +66,11 @@
        Camp = _camp;
        motionBase = new MotionBase();
        motionBase.Init(heroGo.GetComponentInChildren<SkeletonGraphic>(true));
        motionBase.onAnimationComplete += OnAnimationComplete;
        buffMgr.Init(this);
        renderers = heroGo.GetComponentsInChildren<Renderer>(true);
    }
@@ -88,7 +101,27 @@
        motionBase.Release();
        motionBase = null;
        teamHero = null;
        BattleObjectId = 0;
        ObjID = 0;
        motionBase.onAnimationComplete -= OnAnimationComplete;
    }
    public void OnObjInfoRefresh(H0418_tagObjInfoRefresh _refreshInfo)
    {
        switch ((PlayerDataType)_refreshInfo.RefreshType)
        {
            case PlayerDataType.HP:
                teamHero.curHp = GeneralDefine.GetFactValue(_refreshInfo.Value, _refreshInfo.ValueEx);
                break;
            case PlayerDataType.MaxHP:
                teamHero.maxHp = GeneralDefine.GetFactValue(_refreshInfo.Value, _refreshInfo.ValueEx);
                break;
            case PlayerDataType.XP:
                teamHero.rage = (int)GeneralDefine.GetFactValue(_refreshInfo.Value, _refreshInfo.ValueEx);
                break;
            default:
                Debug.LogError("BattleObject.ObjInfoRefresh 出现意外类型 " + _refreshInfo.RefreshType.ToString());
                break;
        }
    }
    //  眩晕
@@ -108,12 +141,6 @@
    {
        return teamHero.isStoned;
    }
    // //   禁锢
    // public bool IsConfined()
    // {
    //     return false;
    // }
    //  被沉默
    public bool IsSlient()
@@ -180,32 +207,21 @@
        return true;
    }
    public virtual void TakeDamage(List<int> damageValues)
    public virtual void Hurt(List<long> damageValues, int attackType)
    {
        if (IsDead())
            return;
        PopDamage(damageValues);
        PopDamage(teamHero.curHp, damageValues, attackType);
        motionBase.PlayAnimation(MotionName.hit, false);
        //  计算伤害
        int totalDamage = 0;
        long totalDamage = 0;
        foreach (var damage in damageValues)
        {
            totalDamage += damage;
        }
        //  扣血
        teamHero.curHp -= totalDamage;
        //  其实这里应该是等服务器发death的action
        // if (IsDead())
        // {
        //     OnDeath();
        // }
    }
    //  闪避开始
@@ -213,8 +229,10 @@
    {
        float pingpongTime = 0.2f;
        RectTransform rectTrans = heroGo.GetComponent<RectTransform>();
        rectTrans.DOAnchorPos(new Vector3(-50, 50, 0), pingpongTime)
        var tween = rectTrans.DOAnchorPos(new Vector3(-50, 50, 0), pingpongTime)
            .SetEase(Ease.OutCubic);
        battleField.battleTweenMgr.OnPlayTween(tween);
    }
    //  闪避结束
@@ -222,21 +240,26 @@
    {
        float pingpongTime = 0.2f;
        RectTransform rectTrans = heroGo.GetComponent<RectTransform>();
        rectTrans.DOAnchorPos(Vector3.zero, pingpongTime)
        var tween = rectTrans.DOAnchorPos(Vector3.zero, pingpongTime)
                            .SetEase(Ease.OutCubic);
        battleField.battleTweenMgr.OnPlayTween(tween);
    }
    protected virtual void OnDeath()
    public virtual void OnDeath(Action _onDeathAnimationComplete)
    {
        motionBase.OnOtherAnimationComplete = OnOtherAnimationComplete;
        onDeathAnimationComplete = _onDeathAnimationComplete;
        motionBase.PlayAnimation(MotionName.dead, false);
    }
    protected virtual void OnOtherAnimationComplete(MotionName motionName)
    protected virtual void OnAnimationComplete(MotionName motionName)
    {
        if (motionName == MotionName.dead)
        {
            OnDeadAnimationComplete();
            onDeathAnimationComplete?.Invoke();
            onDeathAnimationComplete = null;
        }
    }
@@ -247,7 +270,7 @@
    }
     // 伤害还要看 是否闪避 暴击 and so on 需要有一个DamageType 服务器应该会给
    protected virtual void PopDamage(List<int> damageValues)
    protected virtual void PopDamage(long curHp, List<long> damageValues, int attackType)
    {
        //  其实应该通知出去给UI界面解耦 让UI界面自己来显示的 YYL TODO
        //  播放伤害数字
@@ -258,121 +281,20 @@
            Debug.Log($"Damage: {damage}");
        }
        EventBroadcast.Instance.Broadcast<BattleObject, List<int>>(EventName.BATTLE_DAMAGE_TAKEN, this, damageValues);
        EventBroadcast.Instance.Broadcast(EventName.BATTLE_DAMAGE_TAKEN, battleField.guid, this, damageValues);
    }
    public void PlaySkill(SkillConfig skillConfig, List<Dictionary<int, List<int>>> damageList, Action _onComplete)
    public RectTransform GetAliasTeamNode()
    {
        bool moveToTarget = true;
        if (moveToTarget)
        {
            int targetId = damageList[0].First().Key;
            BattleObject _targetObj = battleField.battleObjMgr.GetBattleObject(targetId);
            RectTransform selfRect = heroGo.GetComponent<RectTransform>();
            RectTransform targetRect = _targetObj.heroGo.GetComponent<RectTransform>();
            Vector2 curAnchoredPos = selfRect.anchoredPosition;
            MoveToTargetUI(selfRect, targetRect, new Vector2(100f, 0f), () =>
            {
                PlaySkillAnimation(skillConfig, damageList, () =>
                {
                    // 回到原位置
                    selfRect.DOAnchorPos(curAnchoredPos, 0.2f)
                        .SetEase(Ease.Linear)
                        .OnComplete(() => {
                            _onComplete?.Invoke();
                        });
                });
            });
        }
        else
        {
            PlaySkillAnimation(skillConfig, damageList, _onComplete);
        }
        return battleField.GetTeamNode(Camp);
    }
    protected void MoveToTargetUI(RectTransform selfRect, RectTransform targetRect, Vector2 offset, Action _onComplete)
    public RectTransform GetEnemyTeamNode()
    {
        // 1. 目标的本地坐标转为世界坐标
        Vector3 targetWorldPos = targetRect.TransformPoint(targetRect.anchoredPosition + offset);
        // 2. 世界坐标转为自己父节点下的本地坐标
        RectTransform parentRect = selfRect.parent as RectTransform;
        Vector2 targetAnchoredPos;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(
            parentRect,
            RectTransformUtility.WorldToScreenPoint(null, targetWorldPos),
            null,
            out targetAnchoredPos);
        // 3. DOTween 移动
        selfRect.DOAnchorPos(targetAnchoredPos, 0.2f)
            .SetEase(Ease.Linear)
            .OnComplete(() => _onComplete?.Invoke());
        return battleField.GetTeamNode(Camp == BattleCamp.Red ? BattleCamp.Blue : BattleCamp.Red);
    }
    protected void PlaySkillAnimation(SkillConfig skillConfig, List<Dictionary<int, List<int>>> damageList, Action _onComplete)
    {
        //  关键帧列表
        List<int> keyFrameList = new List<int>() { 15 };
        motionBase.OnAttackHitEvent = (int _frame) =>
        {
            Dictionary<int, List<int>> oneRoundDamage = damageList[keyFrameList.IndexOf(_frame)];
            foreach (var kvp in oneRoundDamage)
            {
                int targetId = kvp.Key;
                List<int> damageValues = kvp.Value;
                BattleObject targetObj = battleField.battleObjMgr.GetBattleObject(targetId);
                if (targetObj != null && !targetObj.IsDead())
                {
                    targetObj.TakeDamage(damageValues);
                }
            }
        };
        motionBase.OnAttackAnimationComplete = () =>
        {
            _onComplete?.Invoke();
            motionBase.OnAttackHitEvent = null;
            motionBase.OnAttackAnimationComplete = null;
            //  死亡确定其实不应该在这里进行触发 应该由服务器下发 YYL TODO
#if UNITY_EDITOR
            //  暂时的处理
            HashSet<int> hitTargets = new HashSet<int>();
            foreach (var dmgDict in damageList)
            {
                foreach (var kvp in dmgDict)
                {
                    int targetId = kvp.Key;
                    hitTargets.Add(targetId);
                }
            }
            foreach (int targetId in hitTargets)
            {
                BattleObject targetObj = battleField.battleObjMgr.GetBattleObject(targetId);
                if (targetObj != null && targetObj.IsDead())
                {
                    targetObj.OnDeath();
                }
            }
#endif
        };
        motionBase.PlayAnimationEx(MotionName.attack, false, keyFrameList);
    }
#if UNITY_EDITOR
#if UNITY_EDITOR_STOP_USING
    public void EditorRevive()
    {
        teamHero.curHp = 100;