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> onHit; protected Vector2 bulletOffset = Vector2.zero; protected bool finished = false; protected float duration = 0f; protected float elapsed = 0f; protected int mBulletIndex; protected List hurts = new List(); public BulletCurve(BattleObject caster, SkillConfig skillConfig, BattleEffectPlayer bulletEffect, RectTransform target, List hurtList, int bulletIndex, Action> 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; }