yyl
4 天以前 168e039cef99df9f0ee91d8ef975c2e7f2a9fb95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using DG.Tweening.Core;
using System.Linq;
 
public static class BattleUtility
{
    // 其他通用的战斗工具方法可以放在这里
 
 
    public static void MarkStartAndEnd(RectTransform startNode, RectTransform endNode)
    {
        // 运行时才执行
        if (!Application.isPlaying)
        {
            Debug.LogWarning("请在运行时使用该功能!");
            return;
        }
 
        var battleField = BattleManager.Instance.storyBattleField;
        if (battleField == null)
        {
            Debug.LogError("BattleManager.storyBattleField 未初始化!");
            return;
        }
 
 
        BattleWin battleWin = UIManager.Instance.GetUI<BattleWin>();
 
        RectTransform canvasRect = battleWin.transform as RectTransform;
 
        CreateMarker(canvasRect, startNode, "StartMarker");
        CreateMarker(canvasRect, endNode, "EndMarker");
    }
 
    private static void CreateMarker(RectTransform canvasRect, RectTransform targetNode, string markerName)
    {
        // 获取目标节点的世界坐标(中心点)
        Vector3 worldPos = targetNode.TransformPoint(targetNode.rect.center);
 
        // 转换到Canvas本地坐标
        Vector2 localPoint;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(
            canvasRect,
            RectTransformUtility.WorldToScreenPoint(null, worldPos),
            null,
            out localPoint);
 
        // 创建RawImage
        GameObject marker = new GameObject(markerName, typeof(RawImage));
        GameObject.Destroy(marker, 5f);
        marker.transform.SetParent(canvasRect, false);
        var rawImage = marker.GetComponent<RawImage>();
        rawImage.color = Color.white;
        rawImage.rectTransform.sizeDelta = new Vector2(100, 100);
        rawImage.rectTransform.anchoredPosition = localPoint;
    }
 
 
    public static TweenerCore<Vector2, Vector2, DG.Tweening.Plugins.Options.VectorOptions> MoveToTarget(
        RectTransform transform, RectTransform target, Vector2 offset, Action onComplete = null, float speed = 500f)
    {
        // 获取目标节点的世界坐标(锚点位置)
        Vector3 worldPos = target.position;
 
        // 如果需要加 offset,需考虑 scale
        Vector3 offsetWorld = target.TransformVector(offset);
        worldPos += offsetWorld;
 
        RectTransform canvasRect = transform.parent as RectTransform;
 
        // 转换到Canvas本地坐标
        Vector2 localPoint;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(
            canvasRect,
            RectTransformUtility.WorldToScreenPoint(null, worldPos),
            null,
            out localPoint);
 
        float distance = Vector2.Distance(transform.anchoredPosition, localPoint);
        float duration = distance / speed;
 
        var tween = transform.DOAnchorPos(localPoint, duration).SetEase(Ease.Linear);
        tween.onComplete += () =>
        {
            onComplete?.Invoke();
        };
 
        return tween;
    }
 
    public static string DisplayDamageNum(long num, int attackType)
    {
        var config = DamageNumConfig.Get(attackType);
        var basePowerStr = UIHelper.ReplaceLargeArtNum(num);
        var result = string.Empty;
        for (int i = 0; i < basePowerStr.Length; i++)
        {
            var numChar = (char)GetDamageNumKey(config, basePowerStr[i]);
            if (numChar > 0)
            {
                result += numChar;
            }
        }
        return result;
    }
 
    public static string DisplayDamageNum(BattleDmg damage)
    {
        var config = DamageNumConfig.Get(damage.attackType);
 
        string result = string.Empty;
 
        //  如果是闪避 则只显示闪避两个字
        if (damage.IsType(DamageType.Dodge))
        {
            result += (char)config.prefix;
        }
        else
        {
            result = ConvertToArtFont(config, damage.damage);
        }
 
        return result;
    }
 
    public static string ConvertToArtFont(DamageNumConfig config, float _num)
    {
        var stringBuild = new System.Text.StringBuilder();
 
        if (0 != config.plus)
            stringBuild.Append((char)config.plus);
        if (0 != config.prefix)
            stringBuild.Append((char)config.prefix);
 
        var chars = UIHelper.ReplaceLargeArtNum(_num);
        for (var i = 0; i < chars.Length; i++)
        {
            int numChar = GetDamageNumKey(config, (int)chars[i]);
 
            if (numChar > 0)
            {
                stringBuild.Append((char)numChar);
            }
        }
 
        return stringBuild.ToString();
    }
 
    public static int GetMainTargetPositionNum(BattleObject caster, List<HB427_tagSCUseSkill.tagSCUseSkillHurt> targetList, SkillConfig skillConfig)
    {
        int returnIndex = 0;
        //  根据敌方血量阵营 存活人数来选择
        BattleCamp battleCamp = skillConfig.TagFriendly != 0 ? caster.Camp : caster.GetEnemyCamp();
        List<BattleObject> targetObjList = caster.battleField.battleObjMgr.GetBattleObjList(battleCamp);
 
 
        // 瞄准的目标范围,如果目标个数为0则为范围内全部
        // 0    全部范围
        // 1    对位,默认只选1个
        // 2    前排
        // 3    后排
        // 4    纵排,按对位规则选择纵排
        // 5    自己,默认只选自己
 
        switch (skillConfig.TagAim)
        {
            case 0:
                // 0   全部范围:
                // 若TagCount目标个数为0或6,根据TagFriendly敌我配置,代表作用于敌方全体或我方全体,此时主目标为敌我站位中的2号位置
                // 若TagCount目标个数为1~5个,根据TagFriendly敌我+TagAffect细分目标配置,代表随机作用于敌方或我方x个武将,第一个为主目标
                if (skillConfig.TagCount == 0 || skillConfig.TagCount == 6)
                {
                    returnIndex = 1;
                }
                else
                {
                    uint objId = targetList[0].ObjID;
                    BattleObject target = caster.battleField.battleObjMgr.GetBattleObject((int)objId);
                    return target.teamHero.positionNum;
                }
                break;
            case 1:
                // 1    对位:
                // 默认只选1个,对位规则为A1优先打B1,A2优先打B2,A3优先打B3,对位目标死亡时,优先前排,比如B2已经死亡,那么A2将优先打B
                if (targetList.Count > 0)
                {
                    BattleObject battleObject = caster.battleField.battleObjMgr.GetBattleObject((int)targetList[0].ObjID);
                    if (battleObject != null)
                    {
                        returnIndex = battleObject.teamHero.positionNum;
                    }
                    else
                    {
                        Debug.LogError("GetMainTargetPositionNum 找不到目标 ObjId : " + targetList[0].ObjID);
                        returnIndex = 0;
                    }
                }
                else
                {
                    Debug.LogError("targetList 目标列表为空");
                    returnIndex = 0;
                }
                break;
            case 2:
                //  1、2、3号位为前排,默认2号位置为主目标,当1、2、3号位置角色全部死亡,前排将替换成后排,5号位置变更为主目标,
                //  若配置TagAffect细分目标,且人数小于3,则所有被选择目标均为主目标(施法位置会用客户端配置)
                // (即前排默认2号位或5号位规则无效,实际作用多少人就是多少个主目标) (YL : TagAffect>0 && TagAffect != 3就是全体都是主目标 后排一样 )
                if (skillConfig.TagAffect != 0 || skillConfig.TagCount < 3)
                {
                    uint objId = targetList[0].ObjID;
                    BattleObject target = caster.battleField.battleObjMgr.GetBattleObject((int)objId);
                    returnIndex = target.teamHero.positionNum;
                }
                else
                {
                    //  看看对面前排是否都活着
                    List<BattleObject> front = new List<BattleObject>(from bo in targetObjList where !bo.IsDead() && bo.teamHero.positionNum < 3 select bo);
                    if (front.Count > 0)
                    {
                        returnIndex = 1;
                    }
                    else
                    {
                        returnIndex = 4;
                    }
                }
                break;
            case 3:
                if (skillConfig.TagAffect != 0 || skillConfig.TagCount < 3)
                {
                    uint objId = targetList[0].ObjID;
                    BattleObject target = caster.battleField.battleObjMgr.GetBattleObject((int)objId);
                    returnIndex = target.teamHero.positionNum;
                }
                else
                {
                    //  看看对面后排是否都活着
                    List<BattleObject> back = new List<BattleObject>(from bo in targetObjList where !bo.IsDead() && bo.teamHero.positionNum >= 3 select bo);
                    if (back.Count > 0)
                    {
                        returnIndex = 4;
                    }
                    else
                    {
                        returnIndex = 1;
                    }
                }
                break;
            // 4    纵排,按对位规则选择纵排
            case 4:
                returnIndex = int.MaxValue;
                for (int i = 0; i < targetList.Count; i++)
                {
                    var hurt = targetList[i];
                    BattleObject target = caster.battleField.battleObjMgr.GetBattleObject((int)hurt.ObjID);
                    if (target == null)
                    {
                        Debug.LogError("GetMainTargetPositionNum 找不到目标 ObjId : " + hurt.ObjID);
                        continue;
                    }
                    else
                    {
                        returnIndex = Mathf.Min(returnIndex, target.teamHero.positionNum);
                    }
                }
                break;
            // 5    自己,默认只选自己
            case 5:
                returnIndex = caster.teamHero.positionNum;
                break;
            default:
                Debug.LogError("暂时不支持其他的方式选择主目标 有需求请联系策划 技能id:" + skillConfig.SkillID + " TagAim " + skillConfig.TagAim);
                returnIndex = 0;
                break;
        }
 
        return returnIndex;
    }
 
    public static int GetDamageNumKey(DamageNumConfig config, int _num)
    {
        if (_num == 46) return config.nums[10]; // '.'
        else if (_num == 107) return config.nums[11]; // 'k'
        else if (_num == 109) return config.nums[12]; // 'm'
        else if (_num == 98) return config.nums[13]; // 'b'
        else if (_num == 116) return config.nums[14]; // 't'
        int targetNum = _num - 48;
        if (targetNum >= config.nums.Length || targetNum < 0)
        {
            Debug.LogError("damage config " + config.TypeID + " _num is " + _num + " out of range");
            return _num;
        }
        return config.nums[_num - 48];
    }
    
    /// <summary>
    /// 将整个技能的总伤害按命中次数和分段配置分配
    /// </summary>
    /// <param name="damageDivideList">整个技能的所有命中分段配置</param>
    /// <param name="hitIndex">当前是第几击(从0开始)</param>
    /// <param name="totalDamage">整个技能的总伤害</param>
    /// <returns>这一击内每一段的伤害值列表</returns>
    public static List<long> DivideDamageToList(int[][] damageDivideList, int hitIndex, long totalDamage)
    {
        if (totalDamage <= 0)
        {
            return new List<long>{};
        }
 
        if (damageDivideList == null || damageDivideList.Length == 0)
        {
            Debug.LogError("damageDivideList 为空或长度为0");
            return new List<long> { totalDamage };
        }
 
        if (hitIndex < 0 || hitIndex >= damageDivideList.Length)
        {
            Debug.LogError($"hitIndex={hitIndex} 超出范围, damageDivideList.Length={damageDivideList.Length}");
            return new List<long> { totalDamage };
        }
 
        int[] currentHitDivide = damageDivideList[hitIndex];
        if (currentHitDivide == null || currentHitDivide.Length == 0)
        {
            Debug.LogError($"damageDivide[{hitIndex}] 为空或长度为0");
            return new List<long> { totalDamage };
        }
 
        // ============ 第一步: 计算每一击应该造成的伤害 ============
        // 先计算所有击的总权重
        int totalWeight = 0;
        for (int i = 0; i < damageDivideList.Length; i++)
        {
            if (damageDivideList[i] != null && damageDivideList[i].Length > 0)
            {
                // 每一击的权重是其所有分段之和
                for (int j = 0; j < damageDivideList[i].Length; j++)
                {
                    totalWeight += damageDivideList[i][j];
                }
            }
        }
 
        if (totalWeight == 0)
        {
            Debug.LogError("totalWeight 为 0");
            return new List<long> { totalDamage };
        }
 
        // 计算当前这一击的权重
        int currentHitWeight = 0;
        for (int i = 0; i < currentHitDivide.Length; i++)
        {
            currentHitWeight += currentHitDivide[i];
        }
 
        // 计算当前这一击应该造成的总伤害
        long currentHitTotalDamage;
        bool isLastHit = hitIndex >= damageDivideList.Length - 1;
        
        if (isLastHit)
        {
            // 最后一击: 计算前面所有击已经造成的伤害,剩余的全部给最后一击
            long previousHitsDamage = 0;
            for (int i = 0; i < hitIndex; i++)
            {
                if (damageDivideList[i] != null)
                {
                    int hitWeight = 0;
                    for (int j = 0; j < damageDivideList[i].Length; j++)
                    {
                        hitWeight += damageDivideList[i][j];
                    }
                    previousHitsDamage += (long)((float)totalDamage * (float)hitWeight / (float)totalWeight);
                }
            }
            currentHitTotalDamage = totalDamage - previousHitsDamage;
        }
        else
        {
            // 非最后一击: 按权重计算
            currentHitTotalDamage = (long)((float)totalDamage * (float)currentHitWeight / (float)totalWeight);
        }
 
        // ============ 第二步: 将当前这一击的伤害分配到各分段 ============
        List<long> fixedDamageList = new List<long>();
        long accumulatedDamage = 0;
 
        for (int i = 0; i < currentHitDivide.Length; i++)
        {
            long damage;
            
            // 当前击的最后一段进行误差补偿
            if (i == currentHitDivide.Length - 1)
            {
                damage = currentHitTotalDamage - accumulatedDamage;
            }
            else
            {
                // 按当前击的权重分配
                damage = (long)((float)currentHitTotalDamage * (float)currentHitDivide[i] / (float)currentHitWeight);
                accumulatedDamage += damage;
            }
            
            fixedDamageList.Add(damage);
        }
 
        return fixedDamageList;
    }
 
    /// <summary>
    /// 保证所有分配项加起来等于totalDamage,避免因整除导致的误差
    /// </summary>
    public static List<long> DivideDamageToList(int[] damageDivide, long totalDamage)
    {
        if (damageDivide == null || damageDivide.Length == 0)
        {
            Debug.LogError("damageDivide 为空或长度为0");
            return new List<long> { totalDamage };
        }
 
        List<long> fixedDamageList = new List<long>();
        long accumulatedDamage = 0; // 累计已分配的伤害
 
        for (int i = 0; i < damageDivide.Length; i++)
        {
            long damage;
            
            // 最后一次分配:用总伤害减去已分配的伤害,确保总和精确
            if (i == damageDivide.Length - 1)
            {
                damage = totalDamage - accumulatedDamage;
            }
            else
            {
                // 计算当前分段伤害(向下取整)
                damage = (long)((float)totalDamage * (float)damageDivide[i] / 10000f);
                accumulatedDamage += damage;
            }
            
            fixedDamageList.Add(damage);
        }
 
        return fixedDamageList;
    }
    
    public static HB419_tagSCObjHPRefresh FindObjHPRefreshPack(List<GameNetPackBasic> packList)
    {
        for (int i = 0; i < packList.Count; i++)
        {
            var pack = packList[i];
            if (pack is HB419_tagSCObjHPRefresh hpRefreshPack)
            {
                return hpRefreshPack;
            }
            else if (pack is CustomHB426CombinePack)
            {
                break;
            }
        }
        return null;
    }
 
    public static List<HB422_tagMCTurnFightObjDead> FindDeadPack(List<GameNetPackBasic> packList)
    {
        List<HB422_tagMCTurnFightObjDead> deadPacks = new List<HB422_tagMCTurnFightObjDead>();
        for (int i = 0; i < packList.Count; i++)
        {
            var pack = packList[i];
            //    寻找死亡包 找到死亡包之后要找掉落包 不能超过技能包
            if (pack is HB422_tagMCTurnFightObjDead deadPack)
            {
                deadPacks.Add(deadPack);
            }
            else if (pack is CustomHB426CombinePack)
            {
                break;
            }
        }
        // Debug.LogError("find dead pack " + deadPacks.Count);
        return deadPacks;
    }
 
 
    public static List<HB423_tagMCTurnFightObjReborn> FindRebornPack(List<GameNetPackBasic> packList)
    {
        List<HB423_tagMCTurnFightObjReborn> rebornPackList = new List<HB423_tagMCTurnFightObjReborn>();
        for (int i = 0; i < packList.Count; i++)
        {
            var pack = packList[i];
            //    寻找死亡包 找到死亡包之后要找掉落包 不能超过技能包
            if (pack is HB423_tagMCTurnFightObjReborn rebornPack)
            {
                rebornPackList.Add(rebornPack);
            }
            else if (pack is CustomHB426CombinePack)
            {
                break;
            }
        }
        return rebornPackList;
    }
}