Bullet.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. //子弹效果
  5. public class Bullet : LogicTransform
  6. {
  7. Fighter mFighter;
  8. FighterSkillStateData mSkillState;
  9. SkillActionAttackInfo mAttackInfo;
  10. BulletData mData;
  11. BulletGoCtrl mCtrl;
  12. //float mLifeTime; //时长
  13. int mLeftFrame = 0; //总帧数
  14. Vector3 mStartOffset; //开始偏移位置
  15. Vector3 mTargetOffset; //目标偏移位置
  16. BulletMoveProcessor mMoveProcessor; //移动处理器
  17. List<Fighter> mAlreadyHitList; //被击对象
  18. GameObject effectGo;
  19. BattleBuff mBattleBuff = null;
  20. int mHurtFrame = 0;
  21. bool hideEffect = false;
  22. public Fighter Caster { get { return mFighter; } }
  23. public Fighter Target { get; private set; }
  24. public bool IsDisposed { get; private set; }
  25. public BulletData BulletData { get { return mData; } }
  26. public SkillActionAttackInfo AttackInfo { get { return mAttackInfo; } }
  27. public Vector3 TargetPosition { get { return Target != null ? (mData.targetOffset + Target.Ctrl.GetPosByLink(mData.targetLink)) : mTargetOffset; } }
  28. public BulletGoCtrl Ctrl { get { return mCtrl; } }
  29. public BulletMoveProcessor MoveProcessor { get { return mMoveProcessor; } }
  30. public Bullet (Fighter owner,Fighter target, SkillActionAttackInfo attackInfo,BattleBuff buff)
  31. {
  32. KeepHorizontalLook = false;
  33. IsDisposed = false;
  34. mFighter = owner;
  35. mAttackInfo = attackInfo;
  36. mSkillState = mFighter.StateData.SkillStateData;
  37. mBattleBuff = buff;
  38. Target = target;
  39. mData = BattlePrepareManager.Instance.PopBulletData (attackInfo.bulletID);
  40. Init ();
  41. //if (BattleMgr.Instance.Battle.IsPlayRecord || BattleMgr.Instance.Battle.IsKillingBoss)
  42. //{
  43. // DebugHelper.LogError(string.Format("<color=#ff0000>bulletID = {0}-------stateFrame = {1} start mHurtFrame = {2}</color>", mData.id, mFighter.Battle.CurBattleField.CurrentStateFrame,mHurtFrame));
  44. //}
  45. }
  46. void Init ()
  47. {
  48. //mLifeTime = mData.lifeTime;
  49. mLeftFrame = (int)(mData.lifeTime * Constants.frame_to_time);
  50. Vector3 startForward = Target != null ? (Target.Position - Caster.Position).normalized : Vector3.forward;
  51. SetStartTransform (startForward);
  52. float moveSpeedPerFrame = (Vector3.Distance(Target.Position, Caster.Position) / mData.lifeTime) /Constants.frame_to_time;
  53. mMoveProcessor = new BulletMoveProcessor (this, moveSpeedPerFrame);
  54. if(mData.HurtDist != null)
  55. {
  56. float dist = Vector3.Distance(Caster.Position, Target.Position);
  57. for (int idx = mData.HurtDist.Length-1; idx >= 0; idx--)
  58. {
  59. if(dist > mData.HurtDist[idx])
  60. {
  61. mHurtFrame = mData.HurtFrames[idx];
  62. break;
  63. }
  64. }
  65. }
  66. InitGo ();
  67. hideEffect = false;
  68. }
  69. void InitGo ()
  70. {
  71. if (mFighter.Ctrl == null)
  72. return;
  73. effectGo = BattlePrepareManager.Instance.PopAssetGo (Constants.EffectPath,mData.effect);
  74. if (effectGo != null)
  75. {
  76. mCtrl = effectGo.GetComponent<BulletGoCtrl> ();
  77. if (mCtrl == null)
  78. mCtrl = effectGo.AddComponent<BulletGoCtrl> ();
  79. mCtrl.Bullet = this;
  80. ForceSync (mCtrl.transform);
  81. LayerName = BattleCamera.SceneEffectLayerName;
  82. effectGo.SetActive (true);
  83. effectGo.transform.SetParent(null);
  84. CommonUtil.SetGameObjectLayer(effectGo, LayerName);
  85. }
  86. }
  87. public void Update (float deltaTime)
  88. {
  89. if (IsDisposed)
  90. return;
  91. if (Parent != null)
  92. UpdateFromParent ();
  93. mHurtFrame--;
  94. if (mHurtFrame <= 0)
  95. {
  96. //if (BattleMgr.Instance.Battle.IsPlayRecord || BattleMgr.Instance.Battle.IsKillingBoss)
  97. //{
  98. // DebugHelper.LogError(string.Format("<color=#ff0000>bulletID = {0}-------stateFrame = {1} hit</color>", mData.id, mFighter.Battle.CurBattleField.CurrentStateFrame));
  99. //}
  100. ProcessHitFighter();
  101. Dispose();
  102. }
  103. if (!IsDisposed)
  104. {
  105. mLeftFrame--;
  106. if (mLeftFrame <= 0)
  107. Dispose();
  108. }
  109. }
  110. public void FrameUpdate(float deltaTime)
  111. {
  112. if(mMoveProcessor!=null)
  113. {
  114. mMoveProcessor.Update(deltaTime);
  115. }
  116. CheckFlyBulletHit();
  117. }
  118. public void Dispose ()
  119. {
  120. if (IsDisposed)
  121. return;
  122. if(mCtrl!=null)
  123. {
  124. mCtrl.Bullet = null;
  125. }
  126. mBattleBuff = null;
  127. mAlreadyHitList = null;
  128. ResourceMgr.Instance.RecycleGO(Constants.EffectPath, mData.effect, effectGo);
  129. GameObject.Destroy(mCtrl);
  130. mCtrl = null;
  131. IsDisposed = true;
  132. }
  133. public override void SetPosition(Vector3 position)
  134. {
  135. Position = position;
  136. }
  137. void SetStartTransform (Vector3 startForward)
  138. {
  139. mStartOffset = mData.offset + Caster.Ctrl.GetPosByLink(mData.startLink);
  140. mTargetOffset = mData.targetOffset + Target.Ctrl.GetPosByLink(mData.targetLink);
  141. if (mData.followType == EffectFollowType.Effect_Follow_Self ||
  142. mData.followType == EffectFollowType.Effect_Follow_Self_Bloodbar)
  143. {
  144. SetParent (Caster);
  145. LocalPosition = mStartOffset;
  146. }
  147. else if (mData.followType == EffectFollowType.Effect_Follow_Target ||
  148. mData.followType == EffectFollowType.Effect_Follow_Target_Bloodbar)
  149. {
  150. if (Target != null)
  151. {
  152. SetParent (Target);
  153. LocalPosition = mStartOffset;
  154. }
  155. }
  156. else if (mData.targetType == EffectTargetType.Effect_Target_Self)
  157. {
  158. SetPosition (mStartOffset);
  159. }
  160. else if (mData.targetType == EffectTargetType.Effect_Target_Target)
  161. {
  162. if (Target != null)
  163. {
  164. SetPosition (Target.TransformPoint (mStartOffset));
  165. }
  166. }
  167. else
  168. {
  169. SetPosition (Caster.TransformPoint (mStartOffset));
  170. }
  171. LookForward (startForward);
  172. }
  173. void CheckFlyBulletHit ()
  174. {
  175. if (mData.actorType != EffectType.EffectType_Fly)
  176. return;
  177. if (!hideEffect && CheckHitTarget())
  178. {
  179. if (effectGo != null)
  180. CommonUtil.SetGameObjectLayer(effectGo, "Hide");
  181. hideEffect = true;
  182. }
  183. }
  184. void ProcessHitFighter()
  185. {
  186. if (mBattleBuff == null) return;
  187. mBattleBuff.PerformBullteAttackInfo(mAttackInfo,Target);
  188. }
  189. List<Fighter> GetBeHitFighters ()
  190. {
  191. mSkillState.HitRange.Set (Caster, Target, AttackInfo, this);
  192. return mSkillState.HitRange.GetSkillHitFighters (Caster, mAttackInfo.skill, Caster.FighterMgr.AllFighters, true, mAlreadyHitList);
  193. }
  194. bool CheckHitTarget ()
  195. {
  196. if (Target != null)
  197. {
  198. AttackInfo.rangeType = SkillRangeType.RangeType_Bullet;
  199. mSkillState.HitRange.Set(Caster, Target, AttackInfo, this);
  200. return mSkillState.HitRange.CheckHitFighter(Target, Position, mData.targetLink);
  201. }
  202. return false;
  203. }
  204. }