yyl
7 天以前 10ca9daa8cbd54658924e3863752808b8251f0e7
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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
 
public class BulletCurve
{
    protected BattleObject caster;
    protected SkillConfig skillConfig;
    protected BattleEffectPlayer bulletEffect;
    protected RectTransform bulletTrans; // 子弹的RectTransform
    protected RectTransform target;
    protected Action<int, List<HB427_tagSCUseSkill.tagSCUseSkillHurt>> onHit;
 
    protected Vector2 bulletOffset = Vector2.zero;
 
    protected bool finished = false;
    protected float duration = 0f;
    protected float elapsed = 0f;
 
    protected int mBulletIndex;
 
    protected List<HB427_tagSCUseSkill.tagSCUseSkillHurt> hurts = new List<HB427_tagSCUseSkill.tagSCUseSkillHurt>();
 
    public BulletCurve(BattleObject caster, SkillConfig skillConfig, BattleEffectPlayer bulletEffect, RectTransform target,
        List<HB427_tagSCUseSkill.tagSCUseSkillHurt> hurtList, int bulletIndex, Action<int, List<HB427_tagSCUseSkill.tagSCUseSkillHurt>> onHit)
    {
        this.caster = caster;
        this.skillConfig = skillConfig;
        this.bulletEffect = bulletEffect;
        this.target = target;
        this.onHit = onHit;
        this.bulletTrans = bulletEffect.rectTrans;
        this.hurts = hurtList;
        this.mBulletIndex = bulletIndex;
 
        // 设置bulletTrans坐标为caster.heroRectTrans的世界坐标转换到bulletTrans父节点下的本地坐标
        if (bulletTrans != null && caster.heroRectTrans != null)
        {
            var parent = bulletTrans.parent as RectTransform;
            Vector2 localPoint;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(
                parent,
                RectTransformUtility.WorldToScreenPoint(null, caster.heroRectTrans.position),
                null,
                out localPoint);
            bulletTrans.anchoredPosition = localPoint;
        }
 
        if (bulletEffect.effectConfig.effectPos != null && bulletEffect.effectConfig.effectPos.Length >= 2)
        {
            bulletOffset = new Vector2(bulletEffect.effectConfig.effectPos[0], bulletEffect.effectConfig.effectPos[1]);
        }
    }
 
    public virtual void Reset()
    {
        finished = false;
        elapsed = 0f;
    }
 
    // 世界坐标转为bulletTrans父节点下的本地坐标
    protected Vector2 WorldToLocalAnchoredPosition(Vector3 worldPos)
    {
        var parent = bulletTrans.parent as RectTransform;
        Vector2 localPoint;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(parent, RectTransformUtility.WorldToScreenPoint(null, worldPos), null, out localPoint);
        return localPoint;
    }
 
    // Run就是Update,每帧调用
    public virtual void Run()
    {
        if (finished) return;
        Vector2 targetPos = WorldToLocalAnchoredPosition(target.position);
        bulletTrans.anchoredPosition = targetPos;
        ReachTarget();
    }
 
    protected virtual void ReachTarget()
    {
        finished = true;
        onHit?.Invoke(mBulletIndex, hurts);
        caster.battleField.battleEffectMgr.RemoveEffect(skillConfig.BulletEffectId, bulletEffect);
    }
 
    public virtual void ForceFinish()
    {
        finished = true;
    }
 
    public bool IsFinished => finished;
}